package mltk.predictor.evaluation;

import mltk.core.Instances;
import mltk.util.OptimUtils;

/**
 * Class for evaluating log loss (cross entropy).
 * 
 * @author Yin Lou
 *
 */
public class LogLoss extends SimpleMetric {
	
	protected boolean isRawScore;

	/**
	 * Constructor.
	 */
	public LogLoss() {
		this(false);
	}
	
	public LogLoss(boolean isRawScore) {
		super(false);
		this.isRawScore = isRawScore;
	}

	@Override
	public double eval(double[] preds, double[] targets) {
		return OptimUtils.computeLogLoss(preds, targets, isRawScore);
	}

	@Override
	public double eval(double[] preds, Instances instances) {
		double logLoss = 0;
		for (int i = 0; i < preds.length; i++) {
			logLoss += OptimUtils.computeLogLoss(preds[i], instances.get(i).getTarget(), isRawScore);
		}
		return logLoss;
	}
	
	/**
	 * Returns <code>true</code> if raw score is expected as input.
	 * 
	 * @return <code>true</code> if raw score is expected as input.
	 */
	public boolean isRawScore() {
		return isRawScore;
	}

}

@@ -316,16 +316,123 @@
 );%0A%09%7D%0A%09%0A
+%09/**%0A%09 * Constructor.%0A%09 * %0A%09 * @param isRawScore %3Ccode%3Etrue%3C/code%3E if raw score is expected as input.%0A%09 */%0A
 %09public 
@@ -880,16 +880,43 @@
 e);%0A%09%09%7D%0A
+%09%09logLoss /= preds.length;%0A
 %09%09return

Normalize log loss