ciir.umass.edu.metric.DCGScorer Maven / Gradle / Ivy
/*===============================================================================
* Copyright (c) 2010-2012 University of Massachusetts. All Rights Reserved.
*
* Use of the RankLib package is subject to the terms of the software license set
* forth in the LICENSE file included with this software, and also available at
* http://people.cs.umass.edu/~vdang/ranklib_license.html
*===============================================================================
*/
package ciir.umass.edu.metric;
import ciir.umass.edu.learning.RankList;
import ciir.umass.edu.utilities.SimpleMath;
public class DCGScorer extends MetricScorer {
protected static double[] discount = null;//cache
protected static double[] gain = null;//cache
public DCGScorer() {
this.k = 10;
//init cache if we haven't already done so
if (discount == null) {
discount = new double[5000];
for (int i = 0; i < discount.length; i++) {
discount[i] = 1.0 / SimpleMath.logBase2(i + 2);
}
gain = new double[6];
for (int i = 0; i < 6; i++) {
gain[i] = (1 << i) - 1;//2^i-1
}
}
}
public DCGScorer(final int k) {
this.k = k;
//init cache if we haven't already done so
if (discount == null) {
discount = new double[5000];
for (int i = 0; i < discount.length; i++) {
discount[i] = 1.0 / SimpleMath.logBase2(i + 2);
}
gain = new double[6];
for (int i = 0; i < 6; i++) {
gain[i] = (1 << i) - 1;//2^i - 1
}
}
}
@Override
public MetricScorer copy() {
return new DCGScorer();
}
/**
* Compute DCG at k.
*/
@Override
public double score(final RankList rl) {
if (rl.size() == 0) {
return 0;
}
int size = k;
if (k > rl.size() || k <= 0) {
size = rl.size();
}
final int[] rel = getRelevanceLabels(rl);
return getDCG(rel, size);
}
@Override
public double[][] swapChange(final RankList rl) {
final int[] rel = getRelevanceLabels(rl);
final int size = (rl.size() > k) ? k : rl.size();
final double[][] changes = new double[rl.size()][];
for (int i = 0; i < rl.size(); i++) {
changes[i] = new double[rl.size()];
}
//for(int i=0;i