Adds the one sample, t-test statistic to TestStatistic and implementations.

Also add unit tests. - BW

PR: Issue #20231
Obtained from: Bugzilla
Submitted by: Brent Worden
Reviewed by: Tim O'Brien


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140859 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tim O'Brien 2003-05-26 17:29:36 +00:00
parent 57b9151881
commit 43c787eb35
4 changed files with 103 additions and 15 deletions

View File

@ -45,6 +45,10 @@ The Math project is a library of lightweight, self-contained mathematics and sta
<email>mdiggory@latte.harvard.edu</email> <email>mdiggory@latte.harvard.edu</email>
<roles></roles> <roles></roles>
</contributor> </contributor>
<contributor>
<name>Brent Worden</name>
<email>brent@worden.org</email>
</contributor>
</contributors> </contributors>
<dependencies> <dependencies>

View File

@ -57,11 +57,14 @@ package org.apache.commons.math;
* Interfaces for the following test statistics <ul> * Interfaces for the following test statistics <ul>
* <li><a href = http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm> * <li><a href = http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm>
* Chi-Square</a></li> * 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> * </ul>
* @author Phil Steitz
* @version $Revision: 1.1 $ $Date: 2003/05/15 21:58:23 $
* *
*/ * @author Phil Steitz
* @version $Revision: 1.2 $ $Date: 2003/05/26 17:29:36 $
*
*/
public interface TestStatistic { public interface TestStatistic {
/** /**
@ -84,5 +87,20 @@ public interface TestStatistic {
* or length is less than 2 * or length is less than 2
*/ */
public double chiSquare(double[] expected, double[] observed); 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

@ -56,14 +56,24 @@ package org.apache.commons.math;
/** /**
* Implements the following test statistics <ul> * Implements the following test statistics <ul>
* <li><a href = http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm> * <li>
* Chi-Square</a></li></ul> * <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 * @author Phil Steitz
* @version $Revision: 1.1 $ $Date: 2003/05/15 21:58:23 $ * @version $Revision: 1.2 $ $Date: 2003/05/26 17:29:36 $
* *
*/ */
public class TestStatisticImpl implements TestStatistic { public class TestStatisticImpl implements TestStatistic {
/**
* Default constructor.
*/
public TestStatisticImpl() { public TestStatisticImpl() {
} }
@ -84,14 +94,39 @@ public class TestStatisticImpl implements TestStatistic {
throw new IllegalArgumentException throw new IllegalArgumentException
("observed, expected array lengths incorrect"); ("observed, expected array lengths incorrect");
} }
for (int i = 0; i< observed.length; i++) { for (int i = 0; i < observed.length; i++) {
dev = (observed[i] - expected[i]); dev = (observed[i] - expected[i]);
sumSq += dev*dev/expected[i]; sumSq += dev * dev / expected[i];
} }
for (int i = 0; i< observed.length; i++) {
}
return sumSq; 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

@ -60,7 +60,7 @@ import junit.framework.TestSuite;
* Test cases for the TestStatistic class. * Test cases for the TestStatistic class.
* *
* @author Phil Steitz * @author Phil Steitz
* @version $Revision: 1.2 $ $Date: 2003/05/16 03:55:34 $ * @version $Revision: 1.3 $ $Date: 2003/05/26 17:29:36 $
*/ */
public final class TestStatisticTest extends TestCase { public final class TestStatisticTest extends TestCase {
@ -84,13 +84,13 @@ public final class TestStatisticTest extends TestCase {
public void testChiSquare() { public void testChiSquare() {
double[] observed = {11,24,69,96}; double[] observed = {11,24,69,96};
double[] expected = {8.2,25.2,65.8,100.8}; double[] expected = {8.2,25.2,65.8,100.8};
assertEquals("chi-square statistic", assertEquals("chi-square statistic",
1.39743495,testStatistic.chiSquare(expected,observed),10E-5); 1.39743495,testStatistic.chiSquare(expected,observed),10E-5);
double[] tooShortObs = {0}; double[] tooShortObs = {0};
double[] tooShortEx = {1}; double[] tooShortEx = {1};
try { try {
double x = testStatistic.chiSquare(tooShortObs,tooShortEx); testStatistic.chiSquare(tooShortObs,tooShortEx);
fail("arguments too short, IllegalArgumentException expected"); fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; ;
@ -99,7 +99,7 @@ public final class TestStatisticTest extends TestCase {
double[] unMatchedObs = {0,1,2,3}; double[] unMatchedObs = {0,1,2,3};
double[] unMatchedEx = {1,1,2}; double[] unMatchedEx = {1,1,2};
try { try {
double x = testStatistic.chiSquare(unMatchedEx,unMatchedObs); testStatistic.chiSquare(unMatchedEx,unMatchedObs);
fail("arrays have different lengths, IllegalArgumentException expected"); fail("arrays have different lengths, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; ;
@ -110,5 +110,36 @@ public final class TestStatisticTest extends TestCase {
testStatistic.chiSquare(expected,observed),Double.MIN_VALUE); 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) {
;
}
}
} }