gov.sandia.cognition.learning.function.cost.NegativeLogLikelihood Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cognitive-foundry Show documentation
Show all versions of cognitive-foundry Show documentation
A single jar with all the Cognitive Foundry components.
/*
* 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
* Type of data generated by the Distribution
*/
public class NegativeLogLikelihood
extends AbstractCostFunction, 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 target)
{
ProbabilityFunction 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
* 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 double evaluate(
ProbabilityFunction 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;
}
}