diff --git a/src/java/org/apache/commons/math/EmpiricalDistribution.java b/src/java/org/apache/commons/math/EmpiricalDistribution.java index b51aa547c..62d3a6b5c 100644 --- a/src/java/org/apache/commons/math/EmpiricalDistribution.java +++ b/src/java/org/apache/commons/math/EmpiricalDistribution.java @@ -58,6 +58,8 @@ import java.io.IOException; import java.io.File; import java.util.ArrayList; +import org.apache.commons.math.stat.Univariate; + /** * Represents an * empirical probability distribution -- a probability distribution derived @@ -79,7 +81,7 @@ import java.util.ArrayList; * generate random values "like" those in the input file -- i.e., the values * generated will follow the distribution of the values in the file. * @author Phil Steitz - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ */ public interface EmpiricalDistribution { diff --git a/src/java/org/apache/commons/math/EmpiricalDistributionImpl.java b/src/java/org/apache/commons/math/EmpiricalDistributionImpl.java index 5a088b8e6..95870089a 100644 --- a/src/java/org/apache/commons/math/EmpiricalDistributionImpl.java +++ b/src/java/org/apache/commons/math/EmpiricalDistributionImpl.java @@ -61,6 +61,9 @@ import java.io.FileReader; import java.io.File; import java.io.IOException; +import org.apache.commons.math.stat.Univariate; +import org.apache.commons.math.stat.UnivariateImpl; + /** * Implements EmpiricalDistribution interface using * what amounts to the @@ -87,7 +90,7 @@ import java.io.IOException; *

* * @author Phil Steitz - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ */ public class EmpiricalDistributionImpl implements Serializable,EmpiricalDistribution { diff --git a/src/java/org/apache/commons/math/TestStatisticImpl.java b/src/java/org/apache/commons/math/TestStatisticImpl.java index 1b099b18b..a0898d06e 100644 --- a/src/java/org/apache/commons/math/TestStatisticImpl.java +++ b/src/java/org/apache/commons/math/TestStatisticImpl.java @@ -54,6 +54,9 @@ package org.apache.commons.math; +import org.apache.commons.math.stat.Univariate; +import org.apache.commons.math.stat.UnivariateImpl; + /** * Implements the following test statistics * @author Phil Steitz - * @version $Revision: 1.2 $ $Date: 2003/05/26 17:29:36 $ + * @version $Revision: 1.3 $ $Date: 2003/05/29 20:35:44 $ * */ public class TestStatisticImpl implements TestStatistic { diff --git a/src/java/org/apache/commons/math/AbstractStoreUnivariate.java b/src/java/org/apache/commons/math/stat/AbstractStoreUnivariate.java similarity index 99% rename from src/java/org/apache/commons/math/AbstractStoreUnivariate.java rename to src/java/org/apache/commons/math/stat/AbstractStoreUnivariate.java index 4ec611df1..dfed617cf 100644 --- a/src/java/org/apache/commons/math/AbstractStoreUnivariate.java +++ b/src/java/org/apache/commons/math/stat/AbstractStoreUnivariate.java @@ -51,7 +51,7 @@ * information on the Apache Software Foundation, please see * . */ -package org.apache.commons.math; +package org.apache.commons.math.stat; /** * Provides univariate measures for an array of doubles. diff --git a/src/java/org/apache/commons/math/BeanListUnivariateImpl.java b/src/java/org/apache/commons/math/stat/BeanListUnivariateImpl.java similarity index 99% rename from src/java/org/apache/commons/math/BeanListUnivariateImpl.java rename to src/java/org/apache/commons/math/stat/BeanListUnivariateImpl.java index 32166a26f..799437daa 100644 --- a/src/java/org/apache/commons/math/BeanListUnivariateImpl.java +++ b/src/java/org/apache/commons/math/stat/BeanListUnivariateImpl.java @@ -51,7 +51,7 @@ * information on the Apache Software Foundation, please see * . */ -package org.apache.commons.math; +package org.apache.commons.math.stat; import java.util.Iterator; import java.util.List; diff --git a/src/java/org/apache/commons/math/BivariateRegression.java b/src/java/org/apache/commons/math/stat/BivariateRegression.java similarity index 99% rename from src/java/org/apache/commons/math/BivariateRegression.java rename to src/java/org/apache/commons/math/stat/BivariateRegression.java index 5ceb1b9c6..508428c43 100644 --- a/src/java/org/apache/commons/math/BivariateRegression.java +++ b/src/java/org/apache/commons/math/stat/BivariateRegression.java @@ -53,7 +53,7 @@ * */ -package org.apache.commons.math; +package org.apache.commons.math.stat; /** * Estimates an ordinary least squares regression model @@ -82,7 +82,7 @@ package org.apache.commons.math; * * * @author Phil Steitz - * @version $Revision: 1.1 $ $Date: 2003/05/26 02:11:50 $ + * @version $Revision: 1.1 $ $Date: 2003/05/29 20:35:45 $ */ public class BivariateRegression { diff --git a/src/java/org/apache/commons/math/ListUnivariateImpl.java b/src/java/org/apache/commons/math/stat/ListUnivariateImpl.java similarity index 99% rename from src/java/org/apache/commons/math/ListUnivariateImpl.java rename to src/java/org/apache/commons/math/stat/ListUnivariateImpl.java index 5a6b4ac63..ac23f80fb 100644 --- a/src/java/org/apache/commons/math/ListUnivariateImpl.java +++ b/src/java/org/apache/commons/math/stat/ListUnivariateImpl.java @@ -51,7 +51,7 @@ * information on the Apache Software Foundation, please see * . */ -package org.apache.commons.math; +package org.apache.commons.math.stat; import java.util.Iterator; import java.util.List; diff --git a/src/java/org/apache/commons/math/StoreUnivariate.java b/src/java/org/apache/commons/math/stat/StoreUnivariate.java similarity index 99% rename from src/java/org/apache/commons/math/StoreUnivariate.java rename to src/java/org/apache/commons/math/stat/StoreUnivariate.java index df80de0d3..e11fea9f2 100644 --- a/src/java/org/apache/commons/math/StoreUnivariate.java +++ b/src/java/org/apache/commons/math/stat/StoreUnivariate.java @@ -51,7 +51,7 @@ * information on the Apache Software Foundation, please see * . */ -package org.apache.commons.math; +package org.apache.commons.math.stat; /** * StoreUnivariate implements the Univariate interface but maintains the set of values diff --git a/src/java/org/apache/commons/math/StoreUnivariateImpl.java b/src/java/org/apache/commons/math/stat/StoreUnivariateImpl.java similarity index 97% rename from src/java/org/apache/commons/math/StoreUnivariateImpl.java rename to src/java/org/apache/commons/math/stat/StoreUnivariateImpl.java index 49a5878a1..d7766a326 100644 --- a/src/java/org/apache/commons/math/StoreUnivariateImpl.java +++ b/src/java/org/apache/commons/math/stat/StoreUnivariateImpl.java @@ -51,7 +51,10 @@ * information on the Apache Software Foundation, please see * . */ -package org.apache.commons.math; +package org.apache.commons.math.stat; + +import org.apache.commons.math.DoubleArray; +import org.apache.commons.math.ContractableDoubleArray; /** * @author Tim O'Brien diff --git a/src/java/org/apache/commons/math/Univariate.java b/src/java/org/apache/commons/math/stat/Univariate.java similarity index 98% rename from src/java/org/apache/commons/math/Univariate.java rename to src/java/org/apache/commons/math/stat/Univariate.java index 0dae4210e..7738ac815 100644 --- a/src/java/org/apache/commons/math/Univariate.java +++ b/src/java/org/apache/commons/math/stat/Univariate.java @@ -51,7 +51,7 @@ * information on the Apache Software Foundation, please see * . */ - package org.apache.commons.math; + package org.apache.commons.math.stat; /** * @@ -73,7 +73,7 @@ * @author Phil Steitz * @author Tim O'Brien * @author Mark Diggory - * @version $Revision: 1.8 $ $Date: 2003/05/29 19:49:18 $ + * @version $Revision: 1.1 $ $Date: 2003/05/29 20:35:45 $ * */ public interface Univariate { diff --git a/src/java/org/apache/commons/math/UnivariateImpl.java b/src/java/org/apache/commons/math/stat/UnivariateImpl.java similarity index 98% rename from src/java/org/apache/commons/math/UnivariateImpl.java rename to src/java/org/apache/commons/math/stat/UnivariateImpl.java index 935c683df..3f3d668bd 100644 --- a/src/java/org/apache/commons/math/UnivariateImpl.java +++ b/src/java/org/apache/commons/math/stat/UnivariateImpl.java @@ -51,10 +51,13 @@ * information on the Apache Software Foundation, please see * . */ -package org.apache.commons.math; +package org.apache.commons.math.stat; import java.io.Serializable; +import org.apache.commons.math.DoubleArray; +import org.apache.commons.math.FixedDoubleArray; + /** * * Accumulates univariate statistics for values fed in @@ -67,7 +70,7 @@ import java.io.Serializable; * @author Tim O'Brien * @author Mark Diggory * @author Brent Worden - * @version $Revision: 1.9 $ $Date: 2003/05/29 19:49:18 $ + * @version $Revision: 1.1 $ $Date: 2003/05/29 20:35:45 $ * */ public class UnivariateImpl implements Univariate, Serializable { diff --git a/src/test/org/apache/commons/math/EmpiricalDistributionTest.java b/src/test/org/apache/commons/math/EmpiricalDistributionTest.java index 5cb6035e6..a0d0beaf0 100644 --- a/src/test/org/apache/commons/math/EmpiricalDistributionTest.java +++ b/src/test/org/apache/commons/math/EmpiricalDistributionTest.java @@ -60,11 +60,14 @@ import junit.framework.AssertionFailedError; import java.io.File; import java.net.URL; +import org.apache.commons.math.stat.Univariate; +import org.apache.commons.math.stat.UnivariateImpl; + /** * Test cases for the EmpiricalDistribution class * * @author Phil Steitz - * @version $Revision: 1.1 $ $Date: 2003/05/21 14:21:15 $ + * @version $Revision: 1.2 $ $Date: 2003/05/29 20:35:45 $ */ public final class EmpiricalDistributionTest extends TestCase { diff --git a/src/test/org/apache/commons/math/MathTestSuite.java b/src/test/org/apache/commons/math/MathTestSuite.java deleted file mode 100644 index 200c04778..000000000 --- a/src/test/org/apache/commons/math/MathTestSuite.java +++ /dev/null @@ -1,95 +0,0 @@ -/* ==================================================================== - * 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 - * . - */ -package org.apache.commons.math; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; -import junit.textui.TestRunner; -/** - * Test suite for the Math package. - * - * @author Phil Steitz - * @version $Id: MathTestSuite.java,v 1.3 2003/05/18 00:58:52 tobrien Exp $ - */ -public class MathTestSuite extends TestCase { - - /** - * Construct a new instance. - */ - public MathTestSuite(String name) { - super(name); - } - - /** - * Command-line interface. - */ - public static void main(String[] args) { - TestRunner.run(suite()); - } - - /** - * Get the suite of tests - */ - public static Test suite() { - TestSuite suite = new TestSuite(); - suite.setName("Commons Math Tests"); - suite.addTest(RealMatrixImplTest.suite()); - suite.addTest(FreqTest.suite()); - suite.addTest(UnivariateImplTest.suite()); - suite.addTest(TestStatisticTest.suite()); - suite.addTest(RandomDataTest.suite()); - return suite; - } -} diff --git a/src/test/org/apache/commons/math/RandomDataTest.java b/src/test/org/apache/commons/math/RandomDataTest.java index 6796517c3..3caa2a45d 100644 --- a/src/test/org/apache/commons/math/RandomDataTest.java +++ b/src/test/org/apache/commons/math/RandomDataTest.java @@ -61,11 +61,15 @@ import java.security.NoSuchProviderException; import java.security.NoSuchAlgorithmException; import java.util.Collection; import java.util.HashSet; + +import org.apache.commons.math.stat.Univariate; +import org.apache.commons.math.stat.UnivariateImpl; + /** * Test cases for the RandomData class. * * @author Phil Steitz - * @version $Revision: 1.3 $ $Date: 2003/05/29 19:45:35 $ + * @version $Revision: 1.4 $ $Date: 2003/05/29 20:35:45 $ */ public final class RandomDataTest extends TestCase { diff --git a/src/test/org/apache/commons/math/ValueServerTest.java b/src/test/org/apache/commons/math/ValueServerTest.java index c1d9d4a30..5abd114c4 100644 --- a/src/test/org/apache/commons/math/ValueServerTest.java +++ b/src/test/org/apache/commons/math/ValueServerTest.java @@ -58,12 +58,15 @@ import junit.framework.TestCase; import junit.framework.TestSuite; import junit.framework.AssertionFailedError; import java.net.URL; + +import org.apache.commons.math.stat.Univariate; +import org.apache.commons.math.stat.UnivariateImpl; /** * Test cases for the ValueServer class. * * @author Phil Steitz - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ */ public final class ValueServerTest extends TestCase { diff --git a/src/test/org/apache/commons/math/stat/BeanListUnivariateImplTest.java b/src/test/org/apache/commons/math/stat/BeanListUnivariateImplTest.java new file mode 100644 index 000000000..6a109f810 --- /dev/null +++ b/src/test/org/apache/commons/math/stat/BeanListUnivariateImplTest.java @@ -0,0 +1,199 @@ +/* ==================================================================== + * 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 + * . + */ +package org.apache.commons.math.stat; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.apache.commons.math.beans.*; + +/** + * Test cases for the {@link BeanListUnivariateImpl} class. + * + * @author Tim O'Brien + * @version $Revision: 1.1 $ $Date: 2003/05/29 20:35:46 $ + */ + +public final class BeanListUnivariateImplTest extends TestCase { + + private List patientList = null; + private double tolerance = Double.MIN_VALUE; + + public BeanListUnivariateImplTest(String name) { + super(name); + } + + public void setUp() { + patientList = new ArrayList(); + + // Create and add patient bean 1 + VitalStats vs1 = new VitalStats( new Double(120.0), + new Double(96.4) ); + Patient p1 = new Patient( vs1, new Integer( 35 ) ); + patientList.add( p1 ); + + // Create and add patient bean 2 + VitalStats vs2 = new VitalStats( new Double(70.0), + new Double(97.4) ); + Patient p2 = new Patient( vs2, new Integer( 23 ) ); + patientList.add( p2 ); + + // Create and add patient bean 3 + VitalStats vs3 = new VitalStats( new Double(90.0), + new Double(98.6) ); + Patient p3 = new Patient( vs3, new Integer( 42 ) ); + patientList.add( p3 ); + } + + public static Test suite() { + TestSuite suite = new TestSuite(BeanListUnivariateImplTest.class); + suite.setName("Freq Tests"); + return suite; + } + + /** test stats */ + public void testStats() { + + StoreUnivariate u = new BeanListUnivariateImpl( patientList ); + + assertEquals("total count",3,u.getN(),tolerance); + + u.clear(); + assertEquals("total count",0,u.getN(),tolerance); + } + + public void testPropStats() { + + StoreUnivariate heartU = new BeanListUnivariateImpl( patientList, + "vitalStats.heartRate" ); + + + + assertEquals( "Mean heart rate unexpected", 93.333, + heartU.getMean(), 0.001 ); + assertEquals( "Max heart rate unexpected", 120.0, + heartU.getMax(), 0.001 ); + + StoreUnivariate ageU = new BeanListUnivariateImpl( patientList, + "age" ); + + assertEquals( "Mean age unexpected", 33.333, + ageU.getMean(), 0.001 ); + assertEquals( "Max age unexpected", 42.0, + ageU.getMax(), 0.001 ); + + } + + /* public void testN0andN1Conditions() throws Exception { + List list = new ArrayList(); + + StoreUnivariate u = new ListUnivariateImpl( list ); + + 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() ) ); + + list.add( new Double(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); + } + + public void testProductAndGeometricMean() throws Exception { + ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList()); + u.setWindowSize(10); + + u.addValue( 1.0 ); + u.addValue( 2.0 ); + u.addValue( 3.0 ); + u.addValue( 4.0 ); + + assertEquals( "Product not expected", 24.0, u.getProduct(), Double.MIN_VALUE ); + assertEquals( "Geometric mean not expected", 2.213364, u.getGeometricMean(), 0.00001 ); + + // Now test rolling - UnivariateImpl should discount the contribution + // of a discarded element + for( int i = 0; i < 10; i++ ) { + u.addValue( i + 2 ); + } + // Values should be (2,3,4,5,6,7,8,9,10,11) + + assertEquals( "Product not expected", 39916800.0, u.getProduct(), 0.00001 ); + assertEquals( "Geometric mean not expected", 5.755931, u.getGeometricMean(), 0.00001 ); + + + } */ + +} + diff --git a/src/test/org/apache/commons/math/stat/BivariateRegressionTest.java b/src/test/org/apache/commons/math/stat/BivariateRegressionTest.java new file mode 100644 index 000000000..618854d36 --- /dev/null +++ b/src/test/org/apache/commons/math/stat/BivariateRegressionTest.java @@ -0,0 +1,226 @@ +/* ==================================================================== + * 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 + * . + */ +package org.apache.commons.math.stat; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +/** + * Test cases for the TestStatistic class. + * + * @author Phil Steitz + * @version $Revision: 1.1 $ $Date: 2003/05/29 20:35:46 $ + */ + +public final class BivariateRegressionTest extends TestCase { + + /* + * NIST "Norris" refernce data set from + * http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Norris.dat + * Strangely, order is {y,x} + */ + private double[][] data = {{0.1,0.2},{338.8,337.4},{118.1,118.2}, + {888.0,884.6},{9.2,10.1},{228.1,226.5},{668.5,666.3},{998.5,996.3}, + {449.1,448.6},{778.9,777.0},{559.2,558.2},{0.3,0.4},{0.1,0.6}, + {778.1,775.5},{668.8,666.9},{339.3,338.0},{448.9,447.5},{10.8,11.6}, + {557.7,556.0},{228.3,228.1},{998.0,995.8},{888.8,887.6},{119.6,120.2}, + {0.3,0.3},{0.6,0.3},{557.6,556.8},{339.3,339.1},{888.0,887.2}, + {998.5,999.0},{778.9,779.0},{10.2,11.1},{117.6,118.3},{228.9,229.2}, + {668.4,669.1},{449.2,448.9},{0.2,0.5}}; + + /* + * Correlation example from + * http://www.xycoon.com/correlation.htm + */ + private double[][] corrData = {{101.0,99.2},{100.1,99.0},{100.0,100.0}, + {90.6,111.6},{86.5,122.2},{89.7,117.6},{90.6,121.1},{82.8,136.0}, + {70.1,154.2},{65.4,153.6},{61.3,158.5},{62.5,140.6},{63.6,136.2}, + {52.6,168.0},{59.7,154.3},{59.5,149.0},{61.3,165.5}}; + + public BivariateRegressionTest(String name) { + super(name); + } + + public void setUp() { + } + + public static Test suite() { + TestSuite suite = new TestSuite(BivariateRegressionTest.class); + suite.setName("BivariateRegression Tests"); + return suite; + } + + public void testNorris() { + BivariateRegression regression = new BivariateRegression(); + for (int i = 0; i < data.length; i++) { + regression.addData(data[i][1],data[i][0]); + } + assertEquals("slope",1.00211681802045, + regression.getSlope(),10E-12); + assertEquals("slope std err",0.429796848199937E-03, + regression.getSlopeStdErr(),10E-12); + assertEquals("number of observations",36,regression.getN()); + assertEquals("intercept", -0.262323073774029, + regression.getIntercept(),10E-12); + assertEquals("std err intercept", 0.232818234301152, + regression.getInterceptStdErr(),10E-12); + assertEquals("r-square",0.999993745883712, + regression.getRSquare(),10E-12); + assertEquals("SSR",4255954.13232369, + regression.getRegressionSumSquares(),10E-8); + assertEquals("MSE",0.782864662630069, + regression.getMeanSquareError(),10E-8); + assertEquals("SSE",26.6173985294224, + regression.getSumSquaredErrors(),10E-8); + assertEquals("predict(0)",-0.262323073774029, + regression.predict(0),10E-12); + assertEquals("predict(1)",1.00211681802045-0.262323073774029, + regression.predict(1),10E-11); + } + + public void testCorr() { + BivariateRegression regression = new BivariateRegression(); + regression.addData(corrData); + assertEquals("number of observations",17,regression.getN()); + assertEquals("r-square",.896123, + regression.getRSquare(),10E-6); + assertEquals("r",-.946638, + regression.getR(),10E-6); + } + + public void testNaNs() { + + BivariateRegression regression = new BivariateRegression(); + + assertTrue("intercept not NaN",Double.isNaN(regression.getIntercept())); + assertTrue("slope not NaN",Double.isNaN(regression.getSlope())); + assertTrue("slope std err not NaN", + Double.isNaN(regression.getSlopeStdErr())); + assertTrue("intercept std err not NaN", + Double.isNaN(regression.getInterceptStdErr())); + assertTrue("MSE not NaN",Double.isNaN(regression.getMeanSquareError())); + assertTrue("e not NaN",Double.isNaN(regression.getR())); + assertTrue("r-square not NaN",Double.isNaN(regression.getRSquare())); + assertTrue("RSS not NaN", + Double.isNaN(regression.getRegressionSumSquares())); + assertTrue("SSE not NaN",Double.isNaN(regression.getSumSquaredErrors())); + assertTrue("SSTO not NaN",Double.isNaN(regression.getTotalSumSquares())); + assertTrue("predict not NaN",Double.isNaN(regression.predict(0))); + + regression.addData(1,2); + regression.addData(1,3); + + // No x variation, so these should still blow... + assertTrue("intercept not NaN",Double.isNaN(regression.getIntercept())); + assertTrue("slope not NaN",Double.isNaN(regression.getSlope())); + assertTrue("slope std err not NaN", + Double.isNaN(regression.getSlopeStdErr())); + assertTrue("intercept std err not NaN", + Double.isNaN(regression.getInterceptStdErr())); + assertTrue("MSE not NaN",Double.isNaN(regression.getMeanSquareError())); + assertTrue("e not NaN",Double.isNaN(regression.getR())); + assertTrue("r-square not NaN",Double.isNaN(regression.getRSquare())); + assertTrue("RSS not NaN", + Double.isNaN(regression.getRegressionSumSquares())); + assertTrue("SSE not NaN",Double.isNaN(regression.getSumSquaredErrors())); + assertTrue("predict not NaN",Double.isNaN(regression.predict(0))); + + // but SSTO should be OK + assertTrue("SSTO NaN",!Double.isNaN(regression.getTotalSumSquares())); + + regression = new BivariateRegression(); + + regression.addData(1,2); + regression.addData(3,3); + + // All should be OK except MSE, s(b0), s(b1) which need one more df + assertTrue("interceptNaN",!Double.isNaN(regression.getIntercept())); + assertTrue("slope NaN",!Double.isNaN(regression.getSlope())); + assertTrue("slope std err not NaN", + Double.isNaN(regression.getSlopeStdErr())); + assertTrue("intercept std err not NaN", + Double.isNaN(regression.getInterceptStdErr())); + assertTrue("MSE not NaN",Double.isNaN(regression.getMeanSquareError())); + assertTrue("r NaN",!Double.isNaN(regression.getR())); + assertTrue("r-square NaN",!Double.isNaN(regression.getRSquare())); + assertTrue("RSS NaN", + !Double.isNaN(regression.getRegressionSumSquares())); + assertTrue("SSE NaN",!Double.isNaN(regression.getSumSquaredErrors())); + assertTrue("SSTO NaN",!Double.isNaN(regression.getTotalSumSquares())); + assertTrue("predict NaN",!Double.isNaN(regression.predict(0))); + + regression.addData(1,4); + + // MSE, MSE, s(b0), s(b1) should all be OK now + assertTrue("MSE NaN",!Double.isNaN(regression.getMeanSquareError())); + assertTrue("slope std err NaN", + !Double.isNaN(regression.getSlopeStdErr())); + assertTrue("intercept std err NaN", + !Double.isNaN(regression.getInterceptStdErr())); + } + + public void testClear() { + BivariateRegression regression = new BivariateRegression(); + regression.addData(corrData); + assertEquals("number of observations",17,regression.getN()); + regression.clear(); + assertEquals("number of observations",0,regression.getN()); + regression.addData(corrData); + assertEquals("r-square",.896123,regression.getRSquare(),10E-6); + regression.addData(data); + assertEquals("number of observations",53,regression.getN()); + } + +} + diff --git a/src/test/org/apache/commons/math/stat/ListUnivariateImplTest.java b/src/test/org/apache/commons/math/stat/ListUnivariateImplTest.java new file mode 100644 index 000000000..ce1ccc1fe --- /dev/null +++ b/src/test/org/apache/commons/math/stat/ListUnivariateImplTest.java @@ -0,0 +1,181 @@ +/* ==================================================================== + * 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 + * . + */ +package org.apache.commons.math.stat; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Test cases for the {@link Univariate} class. + * + * @author Phil Steitz + * @version $Revision: 1.1 $ $Date: 2003/05/29 20:35:46 $ + */ + +public final class ListUnivariateImplTest 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 ListUnivariateImplTest(String name) { + super(name); + } + + public void setUp() { + } + + public static Test suite() { + TestSuite suite = new TestSuite(ListUnivariateImplTest.class); + suite.setName("Freq Tests"); + return suite; + } + + /** test stats */ + public void testStats() { + List externalList = new ArrayList(); + + StoreUnivariate u = new ListUnivariateImpl( externalList ); + + 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 { + List list = new ArrayList(); + + StoreUnivariate u = new ListUnivariateImpl( list ); + + 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() ) ); + + list.add( new Double(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); + } + + public void testProductAndGeometricMean() throws Exception { + ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList()); + u.setWindowSize(10); + + u.addValue( 1.0 ); + u.addValue( 2.0 ); + u.addValue( 3.0 ); + u.addValue( 4.0 ); + + assertEquals( "Product not expected", 24.0, u.getProduct(), Double.MIN_VALUE ); + assertEquals( "Geometric mean not expected", 2.213364, u.getGeometricMean(), 0.00001 ); + + // Now test rolling - UnivariateImpl should discount the contribution + // of a discarded element + for( int i = 0; i < 10; i++ ) { + u.addValue( i + 2 ); + } + // Values should be (2,3,4,5,6,7,8,9,10,11) + + assertEquals( "Product not expected", 39916800.0, u.getProduct(), 0.00001 ); + assertEquals( "Geometric mean not expected", 5.755931, u.getGeometricMean(), 0.00001 ); + + + } + +} + diff --git a/src/test/org/apache/commons/math/stat/StoreUnivariateImplTest.java b/src/test/org/apache/commons/math/stat/StoreUnivariateImplTest.java new file mode 100644 index 000000000..b1bcc3e87 --- /dev/null +++ b/src/test/org/apache/commons/math/stat/StoreUnivariateImplTest.java @@ -0,0 +1,172 @@ +/* ==================================================================== + * 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 + * . + */ +package org.apache.commons.math.stat; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Test cases for the {@link Univariate} class. + * + * @author Phil Steitz + * @version $Revision: 1.1 $ $Date: 2003/05/29 20:35:46 $ + */ + +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); + } + + public void testProductAndGeometricMean() throws Exception { + StoreUnivariateImpl u = new StoreUnivariateImpl(); + u.setWindowSize(10); + + u.addValue( 1.0 ); + u.addValue( 2.0 ); + u.addValue( 3.0 ); + u.addValue( 4.0 ); + + assertEquals( "Product not expected", 24.0, u.getProduct(), Double.MIN_VALUE ); + assertEquals( "Geometric mean not expected", 2.213364, u.getGeometricMean(), 0.00001 ); + + // Now test rolling - UnivariateImpl should discount the contribution + // of a discarded element + for( int i = 0; i < 10; i++ ) { + u.addValue( i + 2 ); + } + // Values should be (2,3,4,5,6,7,8,9,10,11) + + assertEquals( "Product not expected", 39916800.0, u.getProduct(), 0.00001 ); + assertEquals( "Geometric mean not expected", 5.755931, u.getGeometricMean(), 0.00001 ); + + + } + +} + diff --git a/src/test/org/apache/commons/math/stat/UnivariateImplTest.java b/src/test/org/apache/commons/math/stat/UnivariateImplTest.java new file mode 100644 index 000000000..b62a5a34a --- /dev/null +++ b/src/test/org/apache/commons/math/stat/UnivariateImplTest.java @@ -0,0 +1,238 @@ +/* ==================================================================== + * 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 + * . + */ +package org.apache.commons.math.stat; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Test cases for the {@link Univariate} class. + * + * @author Phil Steitz + * @author Tim Obrien + * @version $Revision: 1.1 $ $Date: 2003/05/29 20:35:46 $ + */ + +public final class UnivariateImplTest extends TestCase { + private double one = 1; + private float twoF = 2; + private long twoL = 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 tolerance = 10E-15; + + public UnivariateImplTest(String name) { + super(name); + } + + public void setUp() { + } + + public static Test suite() { + TestSuite suite = new TestSuite(UnivariateImplTest.class); + suite.setName("Freq Tests"); + return suite; + } + + /** test stats */ + public void testStats() { + UnivariateImpl u = new UnivariateImpl(); + assertEquals("total count",0,u.getN(),tolerance); + u.addValue(one); + u.addValue(twoF); + u.addValue(twoL); + 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 { + 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() ) ); + assertTrue("skew of n = 0 set should be NaN", + Double.isNaN(u.getSkewness() ) ); + assertTrue("kurtosis of n = 0 set should be NaN", + Double.isNaN(u.getKurtosis() ) ); + + + /* n=1 */ + u.addValue(one); + assertTrue("mean should be one (n = 1)", + u.getMean() == one); + assertTrue("geometric should be one (n = 1)", + u.getGeometricMean() == one); + assertTrue("Std should be zero (n = 1)", + u.getStandardDeviation() == 0.0); + assertTrue("variance should be zero (n = 1)", + u.getVariance() == 0.0); + assertTrue("skew should be zero (n = 1)", + u.getSkewness() == 0.0); + assertTrue("kurtosis should be zero (n = 1)", + u.getKurtosis() == 0.0); + + /* n=2 */ + u.addValue(twoF); + assertTrue("Std should not be zero (n = 2)", + u.getStandardDeviation() != 0.0); + assertTrue("variance should not be zero (n = 2)", + u.getVariance() != 0.0); + assertTrue("skew should not be zero (n = 2)", + u.getSkewness() == 0.0); + assertTrue("kurtosis should be zero (n = 2)", + u.getKurtosis() == 0.0); + + /* n=3 */ + u.addValue(twoL); + assertTrue("skew should not be zero (n = 3)", + u.getSkewness() != 0.0); + assertTrue("kurtosis should be zero (n = 3)", + u.getKurtosis() == 0.0); + + /* n=4 */ + u.addValue(three); + assertTrue("kurtosis should not be zero (n = 4)", + u.getKurtosis() != 0.0); + + } + + public void testProductAndGeometricMean() throws Exception { + UnivariateImpl u = new UnivariateImpl(10); + + u.addValue( 1.0 ); + u.addValue( 2.0 ); + u.addValue( 3.0 ); + u.addValue( 4.0 ); + + assertEquals( "Product not expected", 24.0, u.getProduct(), + Double.MIN_VALUE ); + assertEquals( "Geometric mean not expected", 2.213364, + u.getGeometricMean(), 0.00001 ); + + // Now test rolling - UnivariateImpl should discount the contribution + // of a discarded element + for( int i = 0; i < 10; i++ ) { + u.addValue( i + 2 ); + } + // Values should be (2,3,4,5,6,7,8,9,10,11) + + assertEquals( "Product not expected", 39916800.0, + u.getProduct(), 0.00001 ); + assertEquals( "Geometric mean not expected", 5.755931, + u.getGeometricMean(), 0.00001 ); + } + + public void testRollingMinMax() { + UnivariateImpl u = new UnivariateImpl(3); + u.addValue( 1.0 ); + u.addValue( 5.0 ); + u.addValue( 3.0 ); + u.addValue( 4.0 ); // discarding min + assertEquals( "min not expected", 3.0, + u.getMin(), Double.MIN_VALUE); + u.addValue(1.0); // discarding max + assertEquals( "max not expected", 4.0, + u.getMax(), Double.MIN_VALUE); + } + + public void testNaNContracts() { + UnivariateImpl u = new UnivariateImpl(); + double nan = Double.NaN; + assertTrue("mean not NaN",Double.isNaN(u.getMean())); + assertTrue("min not NaN",Double.isNaN(u.getMin())); + assertTrue("std dev not NaN",Double.isNaN(u.getStandardDeviation())); + assertTrue("var not NaN",Double.isNaN(u.getVariance())); + assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean())); + + u.addValue(1.0); + + assertEquals( "mean not expected", 1.0, + u.getMean(), Double.MIN_VALUE); + assertEquals( "variance not expected", 0.0, + u.getVariance(), Double.MIN_VALUE); + assertEquals( "geometric mean not expected", 1.0, + u.getGeometricMean(), Double.MIN_VALUE); + + u.addValue(-1.0); + + assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean())); + + u.addValue(0.0); + + assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean())); + + //FiXME: test all other NaN contract specs + } + +}