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

org.grouplens.lenskit.eval.metrics.predict.RMSEPredictMetric Maven / Gradle / Ivy

There is a newer version: 3.0-T5
Show newest version
/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2014 LensKit Contributors.  See CONTRIBUTORS.md.
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.lenskit.eval.metrics.predict;

import org.grouplens.lenskit.Recommender;
import org.grouplens.lenskit.eval.Attributed;
import org.grouplens.lenskit.eval.data.traintest.TTDataSet;
import org.grouplens.lenskit.eval.metrics.AbstractMetric;
import org.grouplens.lenskit.eval.metrics.ResultColumn;
import org.grouplens.lenskit.eval.traintest.TestUser;
import org.grouplens.lenskit.vectors.SparseVector;
import org.grouplens.lenskit.vectors.VectorEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.lang.Math.sqrt;

/**
 * Evaluate a recommender's prediction accuracy with RMSE.
 *
 * @author GroupLens Research
 */
public class RMSEPredictMetric extends AbstractMetric {
    private static final Logger logger = LoggerFactory.getLogger(RMSEPredictMetric.class);

    public RMSEPredictMetric() {
        super(AggregateResult.class, UserResult.class);
    }

    @Override
    public Context createContext(Attributed algo, TTDataSet ds, Recommender rec) {
        return new Context();
    }

    @Override
    public UserResult doMeasureUser(TestUser user, Context context) {
        SparseVector ratings = user.getTestRatings();
        SparseVector predictions = user.getPredictions();
        if (predictions == null) {
            return null;
        }
        double sse = 0;
        int n = 0;
        for (VectorEntry e : predictions) {
            if (Double.isNaN(e.getValue())) {
                continue;
            }

            double err = e.getValue() - ratings.get(e.getKey());
            sse += err * err;
            n++;
        }
        if (n > 0) {
            double rmse = sqrt(sse / n);
            context.addUser(n, sse, rmse);
            return new UserResult(rmse);
        } else {
            return null;
        }
    }

    @Override
    protected AggregateResult getTypedResults(Context context) {
        return context.finish();
    }

    public static class UserResult {
        @ResultColumn("RMSE")
        public final double mae;

        public UserResult(double err) {
            mae = err;
        }
    }

    public static class AggregateResult {
        @ResultColumn("RMSE.ByUser")
        public final double userRMSE;
        @ResultColumn("RMSE.ByRating")
        public final double globalRMSE;

        public AggregateResult(double uerr, double gerr) {
            userRMSE = uerr;
            globalRMSE = gerr;
        }
    }

    public class Context {
        private double totalSSE = 0;
        private double totalRMSE = 0;
        private int nratings = 0;
        private int nusers = 0;

        private void addUser(int n, double sse, double rmse) {
            totalSSE += sse;
            totalRMSE += rmse;
            nratings += n;
            nusers += 1;
        }

        public AggregateResult finish() {
            if (nratings > 0) {
                double v = sqrt(totalSSE / nratings);
                logger.info("RMSE: {}", v);
                return new AggregateResult(totalRMSE / nusers, v);
            } else {
                return null;
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy