From 43c787eb35206890f16c441a8b07ba07e1dd40c2 Mon Sep 17 00:00:00 2001 From: Tim O'Brien Date: Mon, 26 May 2003 17:29:36 +0000 Subject: [PATCH] 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 --- project.xml | 4 ++ .../apache/commons/math/TestStatistic.java | 24 +++++++-- .../commons/math/TestStatisticImpl.java | 51 ++++++++++++++++--- .../commons/math/TestStatisticTest.java | 39 ++++++++++++-- 4 files changed, 103 insertions(+), 15 deletions(-) diff --git a/project.xml b/project.xml index bb4b9e056..5b376852b 100644 --- a/project.xml +++ b/project.xml @@ -45,6 +45,10 @@ The Math project is a library of lightweight, self-contained mathematics and sta mdiggory@latte.harvard.edu + + Brent Worden + brent@worden.org + diff --git a/src/java/org/apache/commons/math/TestStatistic.java b/src/java/org/apache/commons/math/TestStatistic.java index 1273f8b88..e9ba07db2 100644 --- a/src/java/org/apache/commons/math/TestStatistic.java +++ b/src/java/org/apache/commons/math/TestStatistic.java @@ -57,11 +57,14 @@ package org.apache.commons.math; * Interfaces for the following test statistics - * @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); + + /** + * Description: + * Computes one sample, t-test statistic given observed values
+ * This statistic can be used to perform one sample tests for means.
+ * Definition: + * http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm
+ * Preconditions: + * @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); } diff --git a/src/java/org/apache/commons/math/TestStatisticImpl.java b/src/java/org/apache/commons/math/TestStatisticImpl.java index 9eee0a240..1b099b18b 100644 --- a/src/java/org/apache/commons/math/TestStatisticImpl.java +++ b/src/java/org/apache/commons/math/TestStatisticImpl.java @@ -56,14 +56,24 @@ package org.apache.commons.math; /** * Implements the following test statistics + *
  • + * + * Chi-Square + *
  • + *
  • + * + * One Sample t-test + *
  • + * * @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
    + * Algorithm: + * http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm
    + * Numerical considerations: none
    + * @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)); + } } diff --git a/src/test/org/apache/commons/math/TestStatisticTest.java b/src/test/org/apache/commons/math/TestStatisticTest.java index 65ff0765d..10970bd42 100644 --- a/src/test/org/apache/commons/math/TestStatisticTest.java +++ b/src/test/org/apache/commons/math/TestStatisticTest.java @@ -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) { + ; + } + } }