All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ciir.umass.edu.metric.ERRScorer Maven / Gradle / Ivy

There is a newer version: 2.10.1
Show newest version
/*===============================================================================
 * 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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import ciir.umass.edu.learning.RankList;

/**
 *
 * @author Van Dang
 * Expected Reciprocal Rank
 */
public class ERRScorer extends MetricScorer {

    public static double MAX = 16;//by default, we assume the relevance scale of {0, 1, 2, 3, 4} => g_max = 4 => 2^g_max = 16

    public ERRScorer() {
        this.k = 10;
    }

    public ERRScorer(final int k) {
        this.k = k;
    }

    @Override
    public ERRScorer copy() {
        return new ERRScorer();
    }

    /**
     * Compute ERR at k. NDCG(k) = DCG(k) / DCG_{perfect}(k). Note that the "perfect ranking" must be computed based on the whole list,
     * not just top-k portion of the list.
     */
    @Override
    public double score(final RankList rl) {
        int size = k;
        if (k > rl.size() || k <= 0) {
            size = rl.size();
        }

        final List rel = new ArrayList<>();
        for (int i = 0; i < rl.size(); i++) {
            rel.add((int) rl.get(i).getLabel());
        }

        double s = 0.0;
        double p = 1.0;
        for (int i = 1; i <= size; i++) {
            final double R = R(rel.get(i - 1));
            s += p * R / i;
            p *= (1.0 - R);
        }
        return s;
    }

    @Override
    public String name() {
        return "ERR@" + k;
    }

    private double R(final int rel) {
        return ((1 << rel) - 1) / MAX;// (2^rel - 1)/MAX;
    }

    @Override
    public double[][] swapChange(final RankList rl) {
        final int size = (rl.size() > k) ? k : rl.size();
        final int[] labels = new int[rl.size()];
        final double[] R = new double[rl.size()];
        final double[] np = new double[rl.size()];//p[i] = (1 - p[0])(1 - p[1])...(1-p[i-1])
        double p = 1.0;
        //for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy