You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2005/05/02 07:14:49 UTC
svn commit: r165583 - in /jakarta/commons/proper/math/trunk:
src/experimental/R/TTestCases
src/java/org/apache/commons/math/stat/inference/TTestImpl.java
src/test/org/apache/commons/math/stat/inference/TTestTest.java
xdocs/changes.xml
Author: psteitz
Date: Sun May 1 22:14:49 2005
New Revision: 165583
URL: http://svn.apache.org/viewcvs?rev=165583&view=rev
Log:
Fixed error in TTestImpl.homoscedasticTtest. Implementation was
incorrectly using heteroscedastic t statistic. Also improved sensitivity
of test cases.
BZ #34677
Reported by: Gilles Gaillard
Added:
jakarta/commons/proper/math/trunk/src/experimental/R/TTestCases
Modified:
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/inference/TTestImpl.java
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/stat/inference/TTestTest.java
jakarta/commons/proper/math/trunk/xdocs/changes.xml
Added: jakarta/commons/proper/math/trunk/src/experimental/R/TTestCases
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/experimental/R/TTestCases?rev=165583&view=auto
==============================================================================
--- jakarta/commons/proper/math/trunk/src/experimental/R/TTestCases (added)
+++ jakarta/commons/proper/math/trunk/src/experimental/R/TTestCases Sun May 1 22:14:49 2005
@@ -0,0 +1,105 @@
+# Copyright 2005 The Apache Software Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#------------------------------------------------------------------------------
+# R source file to validate TTest tests in
+# org.apache.commons.math.inference.TTestImpl
+#
+# To run the test, install R, put this file and testFunctions
+# into the same directory, launch R from this directory and then enter
+# source("<name-of-this-file>")
+#
+# R functions used
+# t.test(x, y = NULL, alternative = c("two.sided", "less", "greater"),
+# mu = 0, paired = FALSE, var.equal = FALSE, ... )
+# Arguments
+# x a numeric vector of data values.
+# y an optional numeric vector data values.
+# alternative a character string specifying the alternative hypothesis,
+# must be one of "two.sided" (default), "greater" or "less". You can specify
+# just the initial letter.
+# mu a number indicating the true value of the mean (or difference in means
+# if you are performing a two sample test).
+# paired a logical indicating whether you want a paired t-test.
+# var.equal a logical variable indicating whether to treat the two
+# variances as being equal.
+# If TRUE then the pooled variance is used to estimate the variance,
+# otherwise the Welch (or Satterthwaite) approximation to the degrees
+# of freedom is used.
+#------------------------------------------------------------------------------
+tol <- 1E-10 # error tolerance for tests
+#------------------------------------------------------------------------------
+# Function definitions
+#------------------------------------------------------------------------------
+source("testFunctions") # utility test functions
+#------------------------------------------------------------------------------
+# Verification function
+#
+verifyTest <- function(out,expectedP, expectedT,
+ tol) {
+ if (assertEquals(expectedP, out$p.value, tol,
+ "Ttest p value")) {
+ displayPadded(output, SUCCEEDED, 80)
+ } else {
+ displayPadded(output, FAILED, 80)
+ }
+ output <- c("t test test statistic")
+ if (assertEquals(expectedT, out$statistic, tol,
+ "Ttest t statistic")) {
+ displayPadded(output, SUCCEEDED, 80)
+ } else {
+ displayPadded(output, FAILED, 80)
+ }
+ displayDashes(WIDTH)
+}
+
+cat("One-sample, two-sided TTest test cases \n")
+sample1 <- c(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)
+out <- t.test(sample1, mu=100.0)
+expectedP <- 0.0136390585873
+expectedT<- -2.81976445346
+verifyTest(out,expectedP, expectedT, tol)
+
+cat("One-sample, one-sided TTest test cases \n")
+sample1 <- c(2, 0, 6, 6, 3, 3, 2, 3, -6, 6, 6, 6, 3, 0, 1, 1, 0, 2, 3, 3)
+out <- t.test(sample1, mu=0.0, alternative="g")
+expectedP <- 0.000521637019637
+expectedT<- 3.86485535541
+verifyTest(out,expectedP, expectedT, tol)
+
+cat("Homoscedastic TTest test cases \n")
+sample1 <- c(2, 4, 6, 8, 10, 97)
+sample2 <- c(4, 6, 8, 10, 16)
+out <- t.test(sample1,sample2,var.equal = TRUE)
+expectedP <- 0.4833963785
+expectedT<- 0.73096310086
+verifyTest(out,expectedP, expectedT, tol)
+
+cat("Heteroscedastic TTest test cases \n")
+sample1 <- c(7, -4, 18, 17, -3, -5, 1, 10, 11, -2)
+sample2 <- c(-1, 12, -1, -3, 3, -5, 5, 2, -11, -1, -3)
+out <- t.test(sample1,sample2,var.equal = FALSE)
+expectedP <- 0.128839369622
+expectedT<- 1.60371728768
+verifyTest(out,expectedP, expectedT, tol)
+
+cat("Small sample, heteroscedastic test cases \n")
+sample1 <- c(1,3)
+sample2 <- c(4,5)
+out <- t.test(sample1,sample2,var.equal = FALSE)
+expectedP <- 0.198727388935
+expectedT<- -2.2360679775
+verifyTest(out,expectedP, expectedT, tol)
+
Modified: jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/inference/TTestImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/inference/TTestImpl.java?rev=165583&r1=165582&r2=165583&view=diff
==============================================================================
--- jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/inference/TTestImpl.java (original)
+++ jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/inference/TTestImpl.java Sun May 1 22:14:49 2005
@@ -1043,7 +1043,7 @@
protected double homoscedasticTTest(double m1, double m2, double v1,
double v2, double n1, double n2)
throws MathException {
- double t = Math.abs(t(m1, m2, v1, v2, n1, n2));
+ double t = Math.abs(homoscedasticT(m1, m2, v1, v2, n1, n2));
double degreesOfFreedom = 0;
degreesOfFreedom = (double) (n1 + n2 - 2);
TDistribution tDistribution =
Modified: jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/stat/inference/TTestTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/stat/inference/TTestTest.java?rev=165583&r1=165582&r2=165583&view=diff
==============================================================================
--- jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/stat/inference/TTestTest.java (original)
+++ jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/stat/inference/TTestTest.java Sun May 1 22:14:49 2005
@@ -1,5 +1,5 @@
/*
- * Copyright 2004 The Apache Software Foundation.
+ * Copyright 2004-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -62,8 +62,15 @@
sampleStats.addValue(observed[i]);
}
- assertEquals("t statistic", -2.82, testStatistic.t(mu, observed), 10E-3);
- assertEquals("t statistic", -2.82, testStatistic.t(mu, sampleStats), 10E-3);
+ // Target comparison values computed using R version 1.8.1 (Linux version)
+ assertEquals("t statistic", -2.81976445346,
+ testStatistic.t(mu, observed), 10E-10);
+ assertEquals("t statistic", -2.81976445346,
+ testStatistic.t(mu, sampleStats), 10E-10);
+ assertEquals("p value", 0.0136390585873,
+ testStatistic.tTest(mu, observed), 10E-10);
+ assertEquals("p value", 0.0136390585873,
+ testStatistic.tTest(mu, sampleStats), 10E-10);
try {
testStatistic.t(mu, nullObserved);
@@ -127,10 +134,15 @@
for (int i = 0; i < oneSidedP.length; i++) {
oneSidedPStats.addValue(oneSidedP[i]);
}
- assertEquals("one sample t stat", 3.86, testStatistic.t(0d, oneSidedP), 0.01);
- assertEquals("one sample t stat", 3.86, testStatistic.t(0d, oneSidedPStats), 0.01);
- assertEquals("one sample p value", 0.00052, testStatistic.tTest(0d, oneSidedP) / 2d, 10E-5);
- assertEquals("one sample p value", 0.00052, testStatistic.tTest(0d, oneSidedPStats) / 2d, 10E-5);
+ // Target comparison values computed using R version 1.8.1 (Linux version)
+ assertEquals("one sample t stat", 3.86485535541,
+ testStatistic.t(0d, oneSidedP), 10E-10);
+ assertEquals("one sample t stat", 3.86485535541,
+ testStatistic.t(0d, oneSidedPStats),1E-10);
+ assertEquals("one sample p value", 0.000521637019637,
+ testStatistic.tTest(0d, oneSidedP) / 2d, 10E-10);
+ assertEquals("one sample p value", 0.000521637019637,
+ testStatistic.tTest(0d, oneSidedPStats) / 2d, 10E-5);
assertTrue("one sample t-test reject", testStatistic.tTest(0d, oneSidedP, 0.01));
assertTrue("one sample t-test reject", testStatistic.tTest(0d, oneSidedPStats, 0.01));
assertTrue("one sample t-test accept", !testStatistic.tTest(0d, oneSidedP, 0.0001));
@@ -165,14 +177,14 @@
}
// Target comparison values computed using R version 1.8.1 (Linux version)
- assertEquals("two sample heteroscedastic t stat", 1.603717,
- testStatistic.t(sample1, sample2), 1E-6);
- assertEquals("two sample heteroscedastic t stat", 1.603717,
- testStatistic.t(sampleStats1, sampleStats2), 1E-6);
- assertEquals("two sample heteroscedastic p value", 0.1288394,
- testStatistic.tTest(sample1, sample2), 1E-7);
- assertEquals("two sample heteroscedastic p value", 0.1288394,
- testStatistic.tTest(sampleStats1, sampleStats2), 1E-7);
+ assertEquals("two sample heteroscedastic t stat", 1.60371728768,
+ testStatistic.t(sample1, sample2), 1E-10);
+ assertEquals("two sample heteroscedastic t stat", 1.60371728768,
+ testStatistic.t(sampleStats1, sampleStats2), 1E-10);
+ assertEquals("two sample heteroscedastic p value", 0.128839369622,
+ testStatistic.tTest(sample1, sample2), 1E-10);
+ assertEquals("two sample heteroscedastic p value", 0.128839369622,
+ testStatistic.tTest(sampleStats1, sampleStats2), 1E-10);
assertTrue("two sample heteroscedastic t-test reject",
testStatistic.tTest(sample1, sample2, 0.2));
assertTrue("two sample heteroscedastic t-test reject",
@@ -239,7 +251,7 @@
}
}
public void testTwoSampleTHomoscedastic() throws Exception {
- double[] sample1 ={2, 4, 6, 8, 10};
+ double[] sample1 ={2, 4, 6, 8, 10, 97};
double[] sample2 = {4, 6, 8, 10, 16};
SummaryStatistics sampleStats1 = SummaryStatistics.newInstance();
for (int i = 0; i < sample1.length; i++) {
@@ -251,14 +263,14 @@
}
// Target comparison values computed using R version 1.8.1 (Linux version)
- assertEquals("two sample homoscedastic t stat", -1.120897,
- testStatistic.homoscedasticT(sample1, sample2), 10E-6);
- assertEquals("two sample homoscedastic p value", 0.2948490,
- testStatistic.homoscedasticTTest(sampleStats1, sampleStats2), 1E-6);
+ assertEquals("two sample homoscedastic t stat", 0.73096310086,
+ testStatistic.homoscedasticT(sample1, sample2), 10E-11);
+ assertEquals("two sample homoscedastic p value", 0.4833963785,
+ testStatistic.homoscedasticTTest(sampleStats1, sampleStats2), 1E-10);
assertTrue("two sample homoscedastic t-test reject",
- testStatistic.homoscedasticTTest(sample1, sample2, 0.3));
+ testStatistic.homoscedasticTTest(sample1, sample2, 0.49));
assertTrue("two sample homoscedastic t-test accept",
- !testStatistic.homoscedasticTTest(sample1, sample2, 0.2));
+ !testStatistic.homoscedasticTTest(sample1, sample2, 0.48));
}
public void testSmallSamples() throws Exception {
@@ -266,8 +278,10 @@
double[] sample2 = {4d, 5d};
// Target values computed using R, version 1.8.1 (linux version)
- assertEquals(-2.2361, testStatistic.t(sample1, sample2), 1E-4);
- assertEquals(0.1987, testStatistic.tTest(sample1, sample2), 1E-4);
+ assertEquals(-2.2360679775, testStatistic.t(sample1, sample2),
+ 1E-10);
+ assertEquals(0.198727388935, testStatistic.tTest(sample1, sample2),
+ 1E-10);
}
public void testPaired() throws Exception {
Modified: jakarta/commons/proper/math/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/xdocs/changes.xml?rev=165583&r1=165582&r2=165583&view=diff
==============================================================================
--- jakarta/commons/proper/math/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/math/trunk/xdocs/changes.xml Sun May 1 22:14:49 2005
@@ -39,6 +39,11 @@
<body>
<release version="1.1" date="In Development"
description="Jakarta Commons Math 1.1 - Development">
+ <action dev="psteitz" type="fix" issue="34677" due-to="Gilles Gaillard">
+ Fixed error in TTestImpl.homoscedasticTtest. Implementation was
+ incorrectly using heteroscedastic t statistic. Also improved sensitivity
+ of test cases.
+ </action>
<action dev="psteitz" type="fix" issue="34448" due-to="Gilles Gaillard">
Fixed javadoc errors. One-sided t-test significance adjustment was
reversed in javadoc for boolean-valued test methods.
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org