Moved all Univariate and Bivariate stats interfaces and classes to the math.stat package
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140867 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
be5a9fb219
commit
a286c243eb
|
@ -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 <a href=http://random.mat.sbg.ac.at/~ste/dipl/node11.html>
|
||||
* empirical probability distribution</a> -- 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 {
|
||||
|
||||
|
|
|
@ -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 <code>EmpiricalDistribution</code> interface using
|
||||
* what amounts to the
|
||||
|
@ -87,7 +90,7 @@ import java.io.IOException;
|
|||
* </ol></p>
|
||||
*
|
||||
* @author Phil Steitz
|
||||
* @version $Revision: 1.2 $
|
||||
* @version $Revision: 1.3 $
|
||||
*/
|
||||
public class EmpiricalDistributionImpl implements Serializable,EmpiricalDistribution {
|
||||
|
||||
|
|
|
@ -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 <ul>
|
||||
* <li>
|
||||
|
@ -66,7 +69,7 @@ package org.apache.commons.math;
|
|||
* </li>
|
||||
* </ul>
|
||||
* @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 {
|
||||
|
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math;
|
||||
package org.apache.commons.math.stat;
|
||||
|
||||
/**
|
||||
* Provides univariate measures for an array of doubles.
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math;
|
||||
package org.apache.commons.math.stat;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
|
@ -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;
|
|||
* </ul>
|
||||
*
|
||||
* @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 {
|
||||
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math;
|
||||
package org.apache.commons.math.stat;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math;
|
||||
package org.apache.commons.math.stat;
|
||||
|
||||
/**
|
||||
* StoreUnivariate implements the Univariate interface but maintains the set of values
|
|
@ -51,7 +51,10 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math;
|
||||
package org.apache.commons.math.stat;
|
||||
|
||||
import org.apache.commons.math.DoubleArray;
|
||||
import org.apache.commons.math.ContractableDoubleArray;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
|
|
@ -51,7 +51,7 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math;
|
||||
package org.apache.commons.math.stat;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -73,7 +73,7 @@
|
|||
* @author Phil Steitz
|
||||
* @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
|
||||
* @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 {
|
|
@ -51,10 +51,13 @@
|
|||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
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 <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
|
||||
* @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 {
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -59,11 +59,14 @@ 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 {
|
||||
|
|
|
@ -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
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
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 <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
|
||||
* @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 );
|
||||
|
||||
|
||||
} */
|
||||
|
||||
}
|
||||
|
|
@ -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
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
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 <a href="mailto:phil@steitz.com">Phil Steitz</a>
|
||||
* @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 );
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
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 <a href="mailto:phil@steitz.com">Phil Steitz</a>
|
||||
* @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 );
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
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
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue