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

gov.sandia.cognition.learning.function.scalar.KolmogorovSmirnovEvaluator Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
/*
 * File:                KolmogorovSmirnovEvaluator.java
 * Authors:             jdmorr
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 *
 * Copyright Apr 30, 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.scalar;

import gov.sandia.cognition.collection.FiniteCapacityBuffer;
import gov.sandia.cognition.evaluator.AbstractStatefulEvaluator;
import gov.sandia.cognition.statistics.CumulativeDistributionFunction;
import gov.sandia.cognition.statistics.distribution.ChiSquareDistribution;
import gov.sandia.cognition.statistics.method.KolmogorovSmirnovConfidence;
import gov.sandia.cognition.util.ObjectUtil;


/**
 * You can specify a particular CDF.
 * It accepts a double sample value which is buffered inside this class in
 * a finite capacity buffer.  The evaluation uses the entire contents of that
 * buffer to compute the null hypothesis probability.
 * 
 * @author jdmorr
 */
public class KolmogorovSmirnovEvaluator
    extends AbstractStatefulEvaluator>
{

    /**
     * Default capacity, {@value}.
     */
    public static final int DEFAULT_CAPACITY = 100;

    /**
     * Default CDF, a 3-DOF Chi-Square.
     */
    public static final CumulativeDistributionFunction DEFAULT_CDF =
        new ChiSquareDistribution.CDF( 3.0 );

    /**
     * The cumulative distribution function to base the evaluator on.
     */
    protected CumulativeDistributionFunction cdf;

    /**
     * The capacity of the state.
     */
    private int capacity;

    /**
     * Creates a new {@code KolmogorovSmirnovEvaluator}.
     */
    public KolmogorovSmirnovEvaluator()
    {
        this( ObjectUtil.cloneSafe(DEFAULT_CDF), DEFAULT_CAPACITY );
    }

    /**
     * Creates a new {@code KolmogorovSmirnovEvaluator}.
     *
     * @param cdf
     * The cumulative distribution function to base the evaluator on.
     * @param capacity
     * The capacity of the state.
     */
    public KolmogorovSmirnovEvaluator(
        CumulativeDistributionFunction cdf,
        int capacity)
    {
        this.setCDF(cdf);
        this.setCapacity(capacity);
    }

    @Override
    public KolmogorovSmirnovEvaluator clone()
    {
        KolmogorovSmirnovEvaluator clone =
            (KolmogorovSmirnovEvaluator) super.clone();
        clone.setCDF( ObjectUtil.cloneSafe( this.getCDF() ) );
        return clone;
    }

    /**
     * takes in the double value and adds it to finite capacity buffer
     * then computes the KS null hypothesis probability on the samples
     * in the buffer against the particular CDF specified.
     *
     * @param value a sample value
     * @return null hypothesis probability
     */
    public Double evaluate(
        Double value)
    {
       Double result=0.0;

       if( value != null )
       {
           this.getState().addLast(value);

           result = KolmogorovSmirnovConfidence.evaluateNullHypothesis(
                   this.getState(), this.cdf).getNullHypothesisProbability();
       }

       return result;
    }

    /**
     * Getter for cdf
     * @return
     * The cumulative distribution function to base the evaluator on.
     */
    public CumulativeDistributionFunction getCDF()
    {
        return this.cdf;
    }

    /**
     * Setter for cdf
     * @param cdf
     * The cumulative distribution function to base the evaluator on.
     */
    public void setCDF(
        CumulativeDistributionFunction cdf)
    {
        this.cdf = cdf;
    }

    public FiniteCapacityBuffer createDefaultState()
    {
        return new FiniteCapacityBuffer(this.getCapacity());
    }

    /**
     * Getter for capacity
     * @return
     * The capacity of the state.
     */
    public int getCapacity()
    {
        return this.capacity;
    }

    /**
     * Setter for capacity
     * @param capacity
     * The capacity of the state.
     */
    public void setCapacity(
        int capacity)
    {
        this.capacity = capacity;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy