Moving TestStatistic implementation / interface into stat package.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140938 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2003-06-21 23:00:39 +00:00
parent 8d3d1889fb
commit 5e7fe90154
4 changed files with 386 additions and 1 deletions

View File

@ -0,0 +1,106 @@
/* ====================================================================
* 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;
/**
* Interfaces for the following test statistics <ul>
* <li><a href ="http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm">
* Chi-Square</a></li>
* <li><a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm">
* One Sample t-test</a></li>
* </ul>
*
* @author Phil Steitz
* @version $Revision: 1.1 $ $Date: 2003/06/21 23:00:39 $
*
*/
public interface TestStatistic {
/**
* <strong>Description</strong>:
* Computes Chi-Square statistic given observed and expected freqeuncy counts <br>
* This statistic can be used to perform Chi-Square tests for goodness
* of fit.<br>
* <strong>Definition</strong>:
* http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm <br>
* <strong>Preconditions</strong>: <ul>
* <li>Expected counts should all be positive. If any expected
* counts are 0, the test will return INFINITY. Negative expected or observed counts
* make the statistic meaningless.</li>
* <li>The observed and expected arrays <i>must</i> have the same length and
* their common length must be at least 2 </li>
* </ul>
* @param observed array of observed frequency counts
* @param expected array of exptected frequency counts
* @throws IllegalArgumentException if input arrays have different lengths
* or length is less than 2
*/
public double chiSquare(double[] expected, double[] observed);
/**
* <strong>Description</strong>:
* Computes one sample, t-test statistic given observed values <br/>
* This statistic can be used to perform one sample tests for means.<br/>
* <strong>Definition</strong>:
* http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm<br/>
* <strong>Preconditions</strong>: <ul>
* <li>The observed array length <i>must</i> be at least 2.</li>
* </ul>
* @param mu hypothesized mean value.
* @param observed array of observed values
* @throws IllegalArgumentException if input array length is less than 2
*/
public double t(double mu, double[] observed);
}

View File

@ -0,0 +1,133 @@
/* ====================================================================
* 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;
/**
* Implements the following test statistics <ul>
* <li>
* <a href ="http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm">
* Chi-Square</a>
* </li>
* <li>
* <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm">
* One Sample t-test</a>
* </li>
* </ul>
* @author Phil Steitz
* @version $Revision: 1.1 $ $Date: 2003/06/21 23:00:39 $
*
*/
public class TestStatisticImpl implements TestStatistic {
/**
* Default constructor.
*/
public TestStatisticImpl() {
}
/**
* Computes Chi-Square statistic given observed and expected counts <br>
* <strong>Algorithm</strong>:
* http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm <br>
* <strong>Numerical considerations</strong>: none <br>
* @param observed array of observed frequency counts
* @param expected array of expected frequency counts
* @throws IllegalArgumentException if input arrays have different lengths
* or length is less than 2
*/
public double chiSquare(double[] expected, double[] observed) {
double sumSq = 0.0d;
double dev = 0.0d;
if ((expected.length < 2) || (expected.length != observed.length)) {
throw new IllegalArgumentException
("observed, expected array lengths incorrect");
}
for (int i = 0; i < observed.length; i++) {
dev = (observed[i] - expected[i]);
sumSq += dev * dev / expected[i];
}
return sumSq;
}
/**
* Computes t statistic given observed values<br/>
* <strong>Algorithm</strong>:
* http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm<br/>
* <strong>Numerical considerations</strong>: none <br>
* @param mu hypothesized mean value.
* @param observed array of observed values
* @return t-test statistic for the hypothesized mean and observed values.
* @throws IllegalArgumentException if input array length is less than 2
*/
public double t(double mu, double[] observed) {
if((observed == null) || (observed.length < 2)) {
throw new IllegalArgumentException
("observed array length incorrect");
}
// leverage Univariate to compute statistics
Univariate univariate = new UnivariateImpl();
for (int i = 0; i < observed.length; i++) {
univariate.addValue(observed[i]);
}
double n = univariate.getN();
double xbar = univariate.getMean();
double std = univariate.getStandardDeviation();
return (xbar - mu) / (std / Math.sqrt(n));
}
}

View File

@ -62,6 +62,7 @@ import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.HashSet;
import org.apache.commons.math.stat.TestStatisticImpl;
import org.apache.commons.math.stat.Univariate;
import org.apache.commons.math.stat.UnivariateImpl;
@ -69,7 +70,7 @@ import org.apache.commons.math.stat.UnivariateImpl;
* Test cases for the RandomData class.
*
* @author Phil Steitz
* @version $Revision: 1.5 $ $Date: 2003/06/04 02:45:49 $
* @version $Revision: 1.6 $ $Date: 2003/06/21 23:00:39 $
*/
public final class RandomDataTest extends TestCase {

View File

@ -0,0 +1,145 @@
/* ====================================================================
* 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/06/21 23:00:39 $
*/
public final class TestStatisticTest extends TestCase {
private TestStatisticImpl testStatistic = new TestStatisticImpl();
public TestStatisticTest(String name) {
super(name);
}
public void setUp() {
}
public static Test suite() {
TestSuite suite = new TestSuite(TestStatisticTest.class);
suite.setName("TestStatistic Tests");
return suite;
}
public void testChiSquare() {
double[] observed = {11,24,69,96};
double[] expected = {8.2,25.2,65.8,100.8};
assertEquals("chi-square statistic",
1.39743495,testStatistic.chiSquare(expected,observed),10E-5);
double[] tooShortObs = {0};
double[] tooShortEx = {1};
try {
testStatistic.chiSquare(tooShortObs,tooShortEx);
fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
double[] unMatchedObs = {0,1,2,3};
double[] unMatchedEx = {1,1,2};
try {
testStatistic.chiSquare(unMatchedEx,unMatchedObs);
fail("arrays have different lengths, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
expected[0] = 0;
assertEquals("chi-square statistic", Double.POSITIVE_INFINITY,
testStatistic.chiSquare(expected,observed),Double.MIN_VALUE);
}
public void testT(){
double[] observed = {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0,
94.0, 101.0, 88.0, 98.0, 94.0, 101.0, 92.0, 95.0};
double mu = 100.0;
assertEquals("t statistic", -2.82, testStatistic.t(mu, observed),
10E-3);
double[] nullObserved = null;
try {
testStatistic.t(mu, nullObserved);
fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
double[] emptyObs = {};
try {
testStatistic.t(mu, emptyObs);
fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
double[] tooShortObs = {1.0};
try {
testStatistic.t(mu, tooShortObs);
fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
}
}