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:
parent
57b9151881
commit
43c787eb35
|
@ -45,6 +45,10 @@ The Math project is a library of lightweight, self-contained mathematics and sta
|
|||
<email>mdiggory@latte.harvard.edu</email>
|
||||
<roles></roles>
|
||||
</contributor>
|
||||
<contributor>
|
||||
<name>Brent Worden</name>
|
||||
<email>brent@worden.org</email>
|
||||
</contributor>
|
||||
</contributors>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -57,11 +57,14 @@ package org.apache.commons.math;
|
|||
* 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/05/15 21:58:23 $
|
||||
*
|
||||
*/
|
||||
* @author Phil Steitz
|
||||
* @version $Revision: 1.2 $ $Date: 2003/05/26 17:29:36 $
|
||||
*
|
||||
*/
|
||||
public interface TestStatistic {
|
||||
|
||||
/**
|
||||
|
@ -84,5 +87,20 @@ public interface TestStatistic {
|
|||
* 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);
|
||||
}
|
||||
|
||||
|
|
|
@ -56,14 +56,24 @@ package org.apache.commons.math;
|
|||
|
||||
/**
|
||||
* Implements the following test statistics <ul>
|
||||
* <li><a href = http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm>
|
||||
* Chi-Square</a></li></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/05/15 21:58:23 $
|
||||
* @version $Revision: 1.2 $ $Date: 2003/05/26 17:29:36 $
|
||||
*
|
||||
*/
|
||||
*/
|
||||
public class TestStatisticImpl implements TestStatistic {
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public TestStatisticImpl() {
|
||||
}
|
||||
|
||||
|
@ -84,14 +94,39 @@ public class TestStatisticImpl implements TestStatistic {
|
|||
throw new IllegalArgumentException
|
||||
("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]);
|
||||
sumSq += dev*dev/expected[i];
|
||||
sumSq += dev * dev / expected[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i< observed.length; 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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ import junit.framework.TestSuite;
|
|||
* Test cases for the TestStatistic class.
|
||||
*
|
||||
* @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 {
|
||||
|
@ -84,13 +84,13 @@ public final class TestStatisticTest extends TestCase {
|
|||
public void testChiSquare() {
|
||||
double[] observed = {11,24,69,96};
|
||||
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);
|
||||
|
||||
double[] tooShortObs = {0};
|
||||
double[] tooShortEx = {1};
|
||||
try {
|
||||
double x = testStatistic.chiSquare(tooShortObs,tooShortEx);
|
||||
testStatistic.chiSquare(tooShortObs,tooShortEx);
|
||||
fail("arguments too short, IllegalArgumentException expected");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
;
|
||||
|
@ -99,7 +99,7 @@ public final class TestStatisticTest extends TestCase {
|
|||
double[] unMatchedObs = {0,1,2,3};
|
||||
double[] unMatchedEx = {1,1,2};
|
||||
try {
|
||||
double x = testStatistic.chiSquare(unMatchedEx,unMatchedObs);
|
||||
testStatistic.chiSquare(unMatchedEx,unMatchedObs);
|
||||
fail("arrays have different lengths, IllegalArgumentException expected");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
;
|
||||
|
@ -110,5 +110,36 @@ public final class TestStatisticTest extends TestCase {
|
|||
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) {
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue