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

gov.sandia.cognition.learning.function.vector.EntropyEvaluator Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
/*
 * File:                EntropyEvaluator.java
 * Authors:             Kevin R. Dixon
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 * 
 * Copyright Jun 24, 2009, 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.vector;

import gov.sandia.cognition.evaluator.Evaluator;
import gov.sandia.cognition.math.UnivariateStatisticsUtil;
import gov.sandia.cognition.math.matrix.Vector;
import gov.sandia.cognition.util.AbstractCloneableSerializable;
import java.util.ArrayList;

/**
 * Takes a vector of inputs and computes the log base 2 entropy of the input.
 * The values of the input must sum to 1.0 (L1 norm).
 * 
 * @author Kevin R. Dixon
 * @since 3.0
 */
public class EntropyEvaluator 
    extends AbstractCloneableSerializable
    implements Evaluator
{

    /**
     * Tolerance for asserting equal to 1.0, {@value}.
     */
    public final static double TOLERANCE = 1e-5;

    /** 
     * Creates a new instance of EntropyEvaluator 
     */
    public EntropyEvaluator()
    {
    }

    public Double evaluate(
        Vector input)
    {
        double sum = 0.0;
        final int num = input.getDimensionality();
        ArrayList values = new ArrayList( num );
        for( int i = 0; i < num; i++ )
        {
            final double v = input.getElement(i);
            sum += v;
            values.add( v );
        }

        if( Math.abs(sum-1.0) > TOLERANCE )
        {
            throw new IllegalArgumentException( "input elements must sum to 1.0" );
        }

        return UnivariateStatisticsUtil.computeEntropy(values);

    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy