org.lemurproject.ireval.SetRetrievalComparator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of IREval Show documentation
Show all versions of IREval Show documentation
A modified version of the IREval module (version 4.12) from the lemur project with
extensions to better integrate with OpenIMAJ. See http://www.lemurproject.org
/**
* Copyright (c) 2011, The University of Southampton and the individual contributors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the University of Southampton nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* SetRetrievalComparator.java
*
* Created on November 30, 2006, 4:56 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package org.lemurproject.ireval;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.BinomialDistributionImpl;
import org.apache.commons.math.distribution.TDistributionImpl;
/**
*
* @author Trevor Strohman
* @author Jonathon Hare ([email protected])
*/
public class SetRetrievalComparator {
double[] baseline;
double[] treatment;
/**
* Creates a new instance of SetRetrievalComparator
* @param baseline
* @param treatment
*/
public SetRetrievalComparator( Map baseline, Map treatment ) {
Set commonQueries = new TreeSet( baseline.keySet() );
commonQueries.retainAll( treatment.keySet() );
this.baseline = new double[commonQueries.size()];
this.treatment = new double[commonQueries.size()];
int i = 0;
for( String key : commonQueries ) {
this.baseline[i] = baseline.get(key);
this.treatment[i] = treatment.get(key);
i++;
}
}
private double mean( double[] numbers ) {
double sum = 0;
for( int i=0; i treatment[i] )
better++;
}
return better;
}
/**
* @return number of times baseline and treatment are equal
*/
public int countEqual() {
int same = 0;
for( int i=0; i baseline[i] )
treatmentIsBetter++;
if( treatment[i] != baseline[i] )
different++;
}
double pvalue;
try {
pvalue = 1 - new BinomialDistributionImpl(different, 0.5).cumulativeProbability(treatmentIsBetter - 1);
} catch (MathException e) {
throw new RuntimeException(e);
}
return pvalue;
}
/**
* @return result of randomized test between baseline and treatment
*/
public double randomizedTest() {
double baseMean = mean( baseline );
double treatmentMean = mean( treatment );
double difference = treatmentMean - baseMean;
int batch = 10000;
final int maxIterationsWithoutMatch = 1000000;
long iterations = 0;
long matches = 0;
double[] leftSample = new double[baseline.length];
double[] rightSample = new double[baseline.length];
Random random = new Random();
double pValue = 0.0;
while(true) {
for( int i=0; i iterations )
if( iterations > estimatedIterations )
break;
}
return pValue;
}
}