The following changes were made to the Univariate implementation. The public

interface of Univariate was extracted in an interface of the same name.
Univariate, an interface, is now implemented by UnivariateImpl which contains
all code originally present in the original Univariate implementation.

* StoredUnivariate is an interface which extends Univariate and adds
measures not available in the superinterface such as mode, kurtosis, and skew

* StoredUnivariateImpl provides an implementation which uses the
ExpandableDoubleArray for internal storage.  Calculations are performed
on demand *each* time a particular measure is required no state is
maintained by this implementation.

* Univariate provided methods addValue(int), addValue(float), addValue(long).
There functions were removed as no cast is required - all of these
assignments are widening conversions - no cast required

* Removed the name property from Univariate - property not relevant to
univariate statistics


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140828 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tim O'Brien 2003-05-15 05:39:01 +00:00
parent f039b677b8
commit 97568dc06f
8 changed files with 836 additions and 197 deletions

View File

@ -0,0 +1,118 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math;
/**
* StoreUnivariate implements the Univariate interface but maintains the set of values
* which contribute to the values being returned. This implementation of Univariate
* provides additional functionality such as skewness, kurtosis, and mode. This additional
* functionality comes with a price of increased storage costs.
*
* @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
*/
public interface StoreUnivariate extends Univariate {
/**
* A LEPTOKURTIC set has a positive kurtosis (a high peak)
*/
public static int LEPTOKURTIC = 1;
/**
* A MESOKURTIC set has a kurtosis of 0 - it is a normal distribution
*/
public static int MESOKURTIC = 0;
/**
* A PLATYKURTIC set has a negative kurtosis (a flat "peak")
*/
public static int PLATYKURTIC = -1;
/**
* Returns the mode of the values that have been added. The mode is
* the element which occurs with the most frequency
* @return the mode
*/
public abstract double getMode();
/**
* Returns the skewness of a given distribution. Skewness is a measure of the
* assymetry of a given distribution.
*
* @return The skewness of this distribution
*/
public abstract double getSkewness();
/**
* Kurtosis is a measure of the "peakedness" of a distribution
*
* @return the mode
*/
public abstract double getKurtosis();
/**
* Returns the Kurtosis "classification" a distribution can be leptokurtic (high peak), platykurtic (flat peak),
* or mesokurtic (zero kurtosis).
*
* @return A static constant defined in this interface, StoredDeviation.LEPTOKURITC,
* StoredDeviation.PLATYKURTIC, or StoredDeviation.MESOKURTIC
*/
public abstract int getKurtosisClass();
/**
* Returns the current set of values in an array of double primitives. The order of addition is preserved
*
* @return returns the current set of numbers in the order in which they were added to this set
*/
}

View File

@ -0,0 +1,279 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math;
/**
* Provides univariate measures for an array of doubles.
*
* @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
*/
public class StoreUnivariateImpl implements StoreUnivariate {
ExpandableDoubleArray eDA;
public StoreUnivariateImpl() {
eDA = new ExpandableDoubleArray();
}
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getMode()
*/
public double getMode() {
// Mode depends on a refactor Freq class
throw new UnsupportedOperationException("getMode() is not yet implemented");
}
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getSkewness()
*/
public double getSkewness() {
// Initialize the skewness
double skewness = Double.NaN;
// Get the mean and the standard deviation
double mean = getMean();
double stdDev = getStandardDeviation();
// Sum the cubes of the distance from the mean divided by the standard deviation
double accum = 0.0;
for( int i = 0; i < eDA.getNumElements(); i++ ) {
accum += Math.pow( (eDA.getElement(i) - mean) / stdDev, 3.0);
}
// Get N
double n = getN();
// Calculate skewness
skewness = ( n / ( (n-1) * (n-2) ) ) * accum;
return skewness;
}
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getKurtosis()
*/
public double getKurtosis() {
// Initialize the kurtosis
double kurtosis = Double.NaN;
// Get the mean and the standard deviation
double mean = getMean();
double stdDev = getStandardDeviation();
// Sum the ^4 of the distance from the mean divided by the standard deviation
double accum = 0.0;
for( int i = 0; i < eDA.getNumElements(); i++ ) {
accum += Math.pow( (eDA.getElement(i) - mean) / stdDev, 4.0);
}
// Get N
double n = getN();
double coefficientOne = ( n * (n+1)) / ( (n-1) * (n-2) * (n-3) );
double termTwo = ( ( 3 * Math.pow( n - 1, 2.0)) / ( (n-2) * (n-3) ) );
// Calculate kurtosis
kurtosis = ( coefficientOne * accum ) - termTwo;
return kurtosis;
}
/* (non-Javadoc)
* @see org.apache.commons.math.StoreUnivariate#getKurtosisClass()
*/
public int getKurtosisClass() {
int kClass = StoreUnivariate.MESOKURTIC;
double kurtosis = getKurtosis();
if( kurtosis > 0 ) {
kClass = StoreUnivariate.LEPTOKURTIC;
} else if( kurtosis < 0 ) {
kClass = StoreUnivariate.PLATYKURTIC;
}
return( kClass );
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#addValue(double)
*/
public void addValue(double v) {
eDA.addElement( v );
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getMean()
*/
public double getMean() {
double arithMean = getSum() / getN();
return arithMean;
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getVariance()
*/
public double getVariance() {
// Initialize variance
double variance = Double.NaN;
if( getN() == 1 ) {
// If this is a single value
variance = 0;
} else if( getN() > 1 ) {
// Get the mean
double mean = getMean();
// Calculate the sum of the squares of the distance between each value and the mean
double accum = 0.0;
for( int i = 0; i < eDA.getNumElements(); i++ ){
accum += Math.pow( (eDA.getElement(i) - mean), 2.0 );
}
// Divide the accumulator by N - Hmmm... unbiased or biased?
variance = accum / (getN() - 1);
}
return variance;
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getStandardDeviation()
*/
public double getStandardDeviation() {
double stdDev = Double.NaN;
if( getN() != 0 ) {
stdDev = Math.sqrt( getVariance() );
}
return( stdDev );
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getMax()
*/
public double getMax() {
// Initialize maximum to NaN
double max = Double.NaN;
for( int i = 0; i < eDA.getNumElements(); i++) {
if( i == 0 ) {
max = eDA.getElement(i);
} else {
if( eDA.getElement(i) > max ) {
max = eDA.getElement(i);
}
}
}
return max;
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getMin()
*/
public double getMin() {
// Initialize minimum to NaN
double min = Double.NaN;
for( int i = 0; i < eDA.getNumElements(); i++) {
if( i == 0 ) {
min = eDA.getElement(i);
} else {
if( eDA.getElement(i) < min ) {
min = eDA.getElement(i);
}
}
}
return min;
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getN()
*/
public double getN() {
return eDA.getNumElements();
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getSum()
*/
public double getSum() {
double accum = 0.0;
for( int i = 0; i < eDA.getNumElements(); i++) {
accum += eDA.getElement(i);
}
return accum;
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getSumsq()
*/
public double getSumsq() {
double accum = 0.0;
for( int i = 0; i < eDA.getNumElements(); i++) {
accum += Math.pow(eDA.getElement(i), 2.0);
}
return accum;
}
/* (non-Javadoc)
* @see org.apache.commons.math.Univariate#clear()
*/
public void clear() {
eDA.clear();
}
}

View File

@ -51,209 +51,70 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math;
package org.apache.commons.math;
/**
*
* Accumulates univariate statistics for values fed in
* through the addValue() method. Does not store raw data values.
* All data (including n) are represented internally as doubles.
* Integers, floats and longs can be added, but will be converted
* to doubles by addValue().
* through the addValue() method. This interface defines the LCD interface
* which all Univariate implementations must implement.
*
* @author Phil Steitz
* @version $Revision: 1.1 $ $Date: 2003/05/12 19:04:10 $
* @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
* @version $Revision: 1.2 $ $Date: 2003/05/15 05:39:00 $
*
*/
public class Univariate {
public interface Univariate {
/**
* Adds the value to the set of numbers
* @param v the value to be added
*/
public abstract void addValue(double v);
/**
* Returns the mean of the values that have been added
* @return mean value
*/
public abstract double getMean();
/** running sum of values that have been added */
private double sum = 0.0;
/**
* Returns the variance of the values that have been added
* @return variance value
*/
public abstract double getVariance();
/** running sum of squares that have been added */
private double sumsq = 0.0;
/**
* Returns the standard deviation of the values that have been added
* @return standard deviation value
*/
public abstract double getStandardDeviation();
/** count of values that have been added */
private double n = 0.0;
/** Getter for property max.
* @return Value of property max.
*/
public abstract double getMax();
/** min of values that have been added */
private double min = Double.MAX_VALUE;
/** Getter for property min.
* @return Value of property min.
*/
public abstract double getMin();
/** max of values that have been added */
private double max = Double.MIN_VALUE;
/** Getter for property n.
* @return Value of property n.
*/
public abstract double getN();
/** display name */
private String name = "";
/** Getter for property sum.
* @return Value of property sum.
*/
public abstract double getSum();
/** Creates new univariate */
public Univariate() {
clear();
}
/** Getter for property sumsq.
* @return Value of property sumsq.
*/
public abstract double getSumsq();
/** Creates a new univariate with the given name */
public Univariate(java.lang.String name) {
this.name = name;
clear();
}
/**
* Adds the value, updating running sums.<br>
* Converts value to a double before adding.
* @param v the value to be added
*/
public void addValue(int v) {
double f = (new Double(v)).doubleValue();
insertValue(f);
}
/**
* Adds the value, updating running sums.<br>
* Converts value to a double before adding.
* @param v the value to be added
*/
public void addValue(long v) {
double f = (new Double(v)).doubleValue();
insertValue(f);
}
/**
* Adds the value, updating running sums.<br>
* Converts value to a double before adding.
* @param v the value to be added
*/
public void addValue(float v) {
insertValue(v);
}
/**
* Adds the value, updating running sums.
* @param v the value to be added
*/
public void addValue(double v) {
insertValue(v);
}
/**
* Returns the mean of the values that have been added
* @return mean value
*/
public double getMean() {
// FIXME: throw something meaningful if n = 0
return sum/n;
}
/**
* Returns the variance of the values that have been added
* @return variance value
*/
public double getVariance() {
double xbar = getMean();
// FIXME: throw something meaningful if n = 0
return (sumsq - xbar*xbar*n)/(n-1);
}
/**
* Returns the standard deviation of the values that have been added
* @return standard deviation value
*/
public double getStandardDeviation() {
// FIXME: throw something meaningful if n = 0
return (new Double(Math.sqrt
((new Double(getVariance())).doubleValue()))).doubleValue();
}
/**
* Adds the value, updating running sums.
* @param v the value to be added
*/
private void insertValue(double v) {
n += 1.0;
if (v < min) min = v;
if (v > max) max = v;
sum += v;
sumsq += v*v;
}
/** Getter for property max.
* @return Value of property max.
*/
public double getMax() {
return max;
}
/** Setter for property max.
* @param max New value of property max.
*/
public void setMax(double max) {
this.max = max;
}
/** Getter for property min.
* @return Value of property min.
*/
public double getMin() {
return min;
}
/** Getter for property n.
* @return Value of property n.
*/
public double getN() {
return n;
}
/** Getter for property sum.
* @return Value of property sum.
*/
public double getSum() {
return sum;
}
/** Getter for property sumsq.
* @return Value of property sumsq.
*/
public double getSumsq() {
return sumsq;
}
/** Getter for property name.
* @return Value of property name.
*/
public java.lang.String getName() {
return name;
}
/** Setter for property name.
* @param name New value of property name.
*/
public void setName(java.lang.String name) {
this.name = name;
}
/**
* Generates a text report displaying
* univariate statistics from values that
* have been added.
* @return String with line feeds displaying statistics
*/
public String toString() {
StringBuffer outBuffer = new StringBuffer();
outBuffer.append(name + "\n");
outBuffer.append("n: " + n + "\n");
outBuffer.append("min: " + min + "\n");
outBuffer.append("max: " + max + "\n");
outBuffer.append("mean: " + getMean() + "\n");
outBuffer.append("std dev: " + getStandardDeviation() + "\n");
return outBuffer.toString();
}
/** Resets all sums to 0, resets min and max */
public void clear() {
this.sum = 0.0;
this.sumsq = 0.0;
this.n = 0.0;
this.min = Double.MAX_VALUE;
this.max = Double.MIN_VALUE;
}
}
/** Resets all sums to 0, resets min and max */
public abstract void clear();
}

View File

@ -0,0 +1,216 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math;
/**
*
* Accumulates univariate statistics for values fed in
* through the addValue() method. Does not store raw data values.
* All data (including n) are represented internally as doubles.
* Integers, floats and longs can be added, but will be converted
* to doubles by addValue().
*
* @author Phil Steitz
* @version $Revision: 1.1 $ $Date: 2003/05/15 05:39:00 $
*
*/
public class UnivariateImpl implements Univariate {
/** running sum of values that have been added */
private double sum = 0.0;
/** running sum of squares that have been added */
private double sumsq = 0.0;
/** count of values that have been added */
private double n = 0.0;
/** min of values that have been added */
private double min = Double.MAX_VALUE;
/** max of values that have been added */
private double max = Double.MIN_VALUE;
/** Creates new univariate */
public UnivariateImpl() {
clear();
}
/**
* Adds the value, updating running sums.
* @param v the value to be added
*/
public void addValue(double v) {
insertValue(v);
}
/**
* Returns the mean of the values that have been added
* @return mean value
*/
public double getMean() {
// FIXME: throw something meaningful if n = 0
return sum/n;
}
/**
* Returns the variance of the values that have been added.
* @return The variance of a set of values. Double.NaN is returned for
* an empty set of values and 0.0 is returned for a single value set.
*/
public double getVariance() {
double variance = Double.NaN;
if( n == 1 ) {
variance = 0.0;
} else if( n > 1 ) {
double xbar = getMean();
variance = (sumsq - xbar*xbar*n)/(n-1);
}
return variance;
}
/**
* Returns the standard deviation of the values that have been added
* @return The standard deviation of a set of values. Double.NaN is returned for
* an empty set of values and 0.0 is returned for a single value set.
*/
public double getStandardDeviation() {
return (new Double(Math.sqrt
((new Double(getVariance())).doubleValue()))).doubleValue();
}
/**
* Adds the value, updating running sums.
* @param v the value to be added
*/
private void insertValue(double v) {
n += 1.0;
if (v < min) min = v;
if (v > max) max = v;
sum += v;
sumsq += v*v;
}
/** Getter for property max.
* @return Value of property max.
*/
public double getMax() {
return max;
}
/** Setter for property max.
* @param max New value of property max.
*/
public void setMax(double max) {
this.max = max;
}
/** Getter for property min.
* @return Value of property min.
*/
public double getMin() {
return min;
}
/** Getter for property n.
* @return Value of property n.
*/
public double getN() {
return n;
}
/** Getter for property sum.
* @return Value of property sum.
*/
public double getSum() {
return sum;
}
/** Getter for property sumsq.
* @return Value of property sumsq.
*/
public double getSumsq() {
return sumsq;
}
/**
* Generates a text report displaying
* univariate statistics from values that
* have been added.
* @return String with line feeds displaying statistics
*/
public String toString() {
StringBuffer outBuffer = new StringBuffer();
outBuffer.append("UnivariateImpl:\n");
outBuffer.append("n: " + n + "\n");
outBuffer.append("min: " + min + "\n");
outBuffer.append("max: " + max + "\n");
outBuffer.append("mean: " + getMean() + "\n");
outBuffer.append("std dev: " + getStandardDeviation() + "\n");
return outBuffer.toString();
}
/** Resets all sums to 0, resets min and max */
public void clear() {
this.sum = 0.0;
this.sumsq = 0.0;
this.n = 0.0;
this.min = Double.MAX_VALUE;
this.max = Double.MIN_VALUE;
}
}

View File

@ -0,0 +1,5 @@
<html>
<body>
Provides common math utilities such as statistics and a matrix class.
</body>
</html>

View File

@ -61,7 +61,7 @@ import junit.textui.TestRunner;
* Test suite for the Math package.
*
* @author Phil Steitz
* @version $Id: MathTestSuite.java,v 1.1 2003/05/12 19:04:38 rdonkin Exp $
* @version $Id: MathTestSuite.java,v 1.2 2003/05/15 05:39:01 tobrien Exp $
*/
public class MathTestSuite extends TestCase {
@ -87,7 +87,7 @@ public class MathTestSuite extends TestCase {
suite.setName("Commons Math Tests");
suite.addTest(RealMatrixImplTest.suite());
suite.addTest(FreqTest.suite());
suite.addTest(UnivariateTest.suite());
suite.addTest(UnivariateImplTest.suite());
return suite;
}
}

View File

@ -0,0 +1,146 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test cases for the {@link Univariate} class.
*
* @author <a href="mailto:phil@steitz.com">Phil Steitz</a>
* @version $Revision: 1.1 $ $Date: 2003/05/15 05:39:01 $
*/
public final class StoreUnivariateImplTest extends TestCase {
private double one = 1;
private float two = 2;
private int three = 3;
private double mean = 2;
private double sumSq = 18;
private double sum = 8;
private double var = 0.666666666666666666667;
private double std = Math.sqrt(var);
private double n = 4;
private double min = 1;
private double max = 3;
private double skewness = 0;
private double kurtosis = 0.5;
private int kClass = StoreUnivariate.LEPTOKURTIC;
private double tolerance = 10E-15;
public StoreUnivariateImplTest(String name) {
super(name);
}
public void setUp() {
}
public static Test suite() {
TestSuite suite = new TestSuite(StoreUnivariateImplTest.class);
suite.setName("Freq Tests");
return suite;
}
/** test stats */
public void testStats() {
StoreUnivariate u = new StoreUnivariateImpl();
assertEquals("total count",0,u.getN(),tolerance);
u.addValue(one);
u.addValue(two);
u.addValue(two);
u.addValue(three);
assertEquals("N",n,u.getN(),tolerance);
assertEquals("sum",sum,u.getSum(),tolerance);
assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
assertEquals("var",var,u.getVariance(),tolerance);
assertEquals("std",std,u.getStandardDeviation(),tolerance);
assertEquals("mean",mean,u.getMean(),tolerance);
assertEquals("min",min,u.getMin(),tolerance);
assertEquals("max",max,u.getMax(),tolerance);
u.clear();
assertEquals("total count",0,u.getN(),tolerance);
}
public void testN0andN1Conditions() throws Exception {
StoreUnivariate u = new StoreUnivariateImpl();
assertTrue("Mean of n = 0 set should be NaN", Double.isNaN( u.getMean() ) );
assertTrue("Standard Deviation of n = 0 set should be NaN", Double.isNaN( u.getStandardDeviation() ) );
assertTrue("Variance of n = 0 set should be NaN", Double.isNaN(u.getVariance() ) );
u.addValue(one);
assertTrue( "Mean of n = 1 set should be value of single item n1", u.getMean() == one);
assertTrue( "StdDev of n = 1 set should be zero, instead it is: " + u.getStandardDeviation(), u.getStandardDeviation() == 0);
assertTrue( "Variance of n = 1 set should be zero", u.getVariance() == 0);
}
public void testSkewAndKurtosis() {
StoreUnivariate u = new StoreUnivariateImpl();
double[] testArray = { 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21, 8.2, 10.3, 11.3, 14.1,
9.9, 12.2, 12, 12.1, 11, 19.8, 11, 10, 8.8, 9, 12.3 };
for( int i = 0; i < testArray.length; i++) {
u.addValue( testArray[i]);
}
assertEquals("mean", 12.40455, u.getMean(), 0.0001);
assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
}
}

View File

@ -61,10 +61,10 @@ import junit.framework.TestSuite;
* Test cases for the {@link Univariate} class.
*
* @author <a href="mailto:phil@steitz.com">Phil Steitz</a>
* @version $Revision: 1.1 $ $Date: 2003/05/12 19:02:53 $
* @version $Revision: 1.1 $ $Date: 2003/05/15 05:39:01 $
*/
public final class UnivariateTest extends TestCase {
public final class UnivariateImplTest extends TestCase {
private double one = 1;
private float twoF = 2;
private long twoL = 2;
@ -79,7 +79,7 @@ public final class UnivariateTest extends TestCase {
private double max = 3;
private double tolerance = 10E-15;
public UnivariateTest(String name) {
public UnivariateImplTest(String name) {
super(name);
}
@ -87,14 +87,14 @@ public final class UnivariateTest extends TestCase {
}
public static Test suite() {
TestSuite suite = new TestSuite(UnivariateTest.class);
TestSuite suite = new TestSuite(UnivariateImplTest.class);
suite.setName("Freq Tests");
return suite;
}
/** test stats */
public void testStats() {
Univariate u = new Univariate("test univariate");
UnivariateImpl u = new UnivariateImpl();
assertEquals("total count",0,u.getN(),tolerance);
u.addValue(one);
u.addValue(twoF);
@ -111,5 +111,19 @@ public final class UnivariateTest extends TestCase {
u.clear();
assertEquals("total count",0,u.getN(),tolerance);
}
public void testN0andN1Conditions() throws Exception {
UnivariateImpl u = new UnivariateImpl();
assertTrue("Mean of n = 0 set should be NaN", Double.isNaN( u.getMean() ) );
assertTrue("Standard Deviation of n = 0 set should be NaN", Double.isNaN( u.getStandardDeviation() ) );
assertTrue("Variance of n = 0 set should be NaN", Double.isNaN(u.getVariance() ) );
u.addValue(one);
assertTrue( "Mean of n = 1 set should be value of single item n1", u.getMean() == one);
assertTrue( "Mean of n = 1 set should be zero", u.getStandardDeviation() == 0);
assertTrue( "Variance of n = 1 set should be zero", u.getVariance() == 0);
}
}