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

gov.sandia.cognition.learning.function.cost.NegativeLogLikelihood Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
/*
 * 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>
{

    /**
     * Default constructor
     */
    public NegativeLogLikelihood()
    {
        this(null);
    }

    /**
     * Creates a new instance of NegativeLogLikelihood
     * @param costParameters
     * Data generated by the target distribution
     */
    public NegativeLogLikelihood(
        Collection 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 data)
    {
        double logSum = 0.0;
        final int N = data.size();
        for (DataType observation : data)
        {
            logSum += f.logEvaluate(observation) / N;
        }
        return -logSum;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy