/* * File: NegativeLogLikelihood.java * Authors: Kevin R. Dixon * Company: Sandia National Laboratories * Project: Cognitive Foundry * * Copyright Jul 12, 2010, Sandia Corporation. * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive * license for use of this work by or on behalf of the U.S. Government. * Export of this program may require a license from the United States * Government. See CopyrightHistory.txt for complete details. * */ package gov.sandia.cognition.learning.function.cost; import gov.sandia.cognition.statistics.ComputableDistribution; import gov.sandia.cognition.statistics.ProbabilityFunction; import java.util.Collection; /** * CostFunction for computing the maximum likelihood * (because we are minimizing the negative of the log likelihood) * @param <DataType> * Type of data generated by the Distribution */ public class NegativeLogLikelihood<DataType> extends AbstractCostFunction<ComputableDistribution<DataType>, Collection<? extends DataType>> { /** * Default constructor */ public NegativeLogLikelihood() { this(null); } /** * Creates a new instance of NegativeLogLikelihood * @param costParameters * Data generated by the target distribution */ public NegativeLogLikelihood( Collection<? extends DataType> costParameters) { super(costParameters); } public Double evaluate( ComputableDistribution<DataType> target) { ProbabilityFunction<DataType> f = target.getProbabilityFunction(); return evaluate(f, this.getCostParameters()); } /** * Evaluates the negative log-likelihood of the given collection of data * according to the given probability function. * * @param <DataType> * The type of data generated by the distribution. * @param f * The function to compute the log-likelihood. * @param data * The data to compute the log-likelihood of. * @return * The total negative log-likelihood of the data according to the * function. */ public static <DataType> double evaluate( ProbabilityFunction<DataType> f, Collection<? extends DataType> data) { double logSum = 0.0; final int N = data.size(); for (DataType observation : data) { logSum += f.logEvaluate(observation) / N; } return -logSum; } }