Stopped swallowing MathExceptions, cleaned up formatting.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141125 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2004-03-07 20:47:06 +00:00
parent b3641de2a7
commit bd460b62fb
1 changed files with 88 additions and 266 deletions

View File

@ -23,7 +23,7 @@ import junit.framework.TestSuite;
/** /**
* Test cases for the TestStatistic class. * Test cases for the TestStatistic class.
* *
* @version $Revision: 1.11 $ $Date: 2004/02/21 21:35:17 $ * @version $Revision: 1.12 $ $Date: 2004/03/07 20:47:06 $
*/ */
public final class TestStatisticTest extends TestCase { public final class TestStatisticTest extends TestCase {
@ -43,14 +43,10 @@ public final class TestStatisticTest extends TestCase {
return suite; return suite;
} }
public void testChiSquare() { public void testChiSquare() throws MathException {
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( assertEquals("chi-square statistic", 1.39743495, testStatistic.chiSquare(expected, observed), 10E-5);
"chi-square statistic",
1.39743495,
testStatistic.chiSquare(expected, observed),
10E-5);
double[] tooShortObs = { 0 }; double[] tooShortObs = { 0 };
double[] tooShortEx = { 1 }; double[] tooShortEx = { 1 };
@ -58,156 +54,91 @@ public final class TestStatisticTest extends TestCase {
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) {
; // expected
} }
try { try {
testStatistic.chiSquareTest(tooShortObs, tooShortEx); testStatistic.chiSquareTest(tooShortObs, tooShortEx);
fail("arguments too short, IllegalArgumentException expected"); fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} catch (MathException e) { }
// TODO Auto-generated catch block
e.printStackTrace();
}
double[] unMatchedObs = { 0, 1, 2, 3 }; double[] unMatchedObs = { 0, 1, 2, 3 };
double[] unMatchedEx = { 1, 1, 2 }; double[] unMatchedEx = { 1, 1, 2 };
try { try {
testStatistic.chiSquare(unMatchedEx, unMatchedObs); testStatistic.chiSquare(unMatchedEx, unMatchedObs);
fail( fail("arrays have different lengths, IllegalArgumentException expected");
"arrays have different lengths,"
+ " IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} }
expected[0] = 0; expected[0] = 0;
try { try {
testStatistic.chiSquareTest(expected, observed, .01); testStatistic.chiSquareTest(expected, observed, .01);
fail("bad expected count, IllegalArgumentException expected"); fail("bad expected count, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} catch (MathException e) { }
// TODO Auto-generated catch block
e.printStackTrace();
}
/** from http://www.vsenvirginia.org/stat/classpractice/Voter_Preferences_CP.pdf */ /** from http://www.vsenvirginia.org/stat/classpractice/Voter_Preferences_CP.pdf */
double[] observed1 = { 504, 523, 72, 70, 31 }; double[] observed1 = { 504, 523, 72, 70, 31 };
double[] expected1 = { 480, 540, 84, 60, 36 }; double[] expected1 = { 480, 540, 84, 60, 36 };
assertEquals( "chi-square test statistic", 5.81, testStatistic.chiSquare(expected1, observed1), 10E-2);
try { assertEquals("chi-square p-value", 0.21, testStatistic.chiSquareTest(expected1, observed1), 10E-2);
assertEquals( assertTrue("chi-square test reject", testStatistic.chiSquareTest(expected1, observed1, 0.3));
"chi-square test statistic", assertTrue("chi-square test accept", !testStatistic.chiSquareTest(expected1, observed1, 0.1));
5.81,
testStatistic.chiSquare(expected1, observed1),
10E-2);
assertEquals(
"chi-square p-value",
0.21,
testStatistic.chiSquareTest(expected1, observed1),
10E-2);
assertTrue(
"chi-square test reject",
testStatistic.chiSquareTest(expected1, observed1, 0.3));
assertTrue(
"chi-square test accept",
!testStatistic.chiSquareTest(expected1, observed1, 0.1));
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try { try {
testStatistic.chiSquareTest(expected1, observed1, 95); testStatistic.chiSquareTest(expected1, observed1, 95);
fail("alpha out of range, IllegalArgumentException expected"); fail("alpha out of range, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
} }
public void testT() { public void testT() throws Exception {
double[] observed = 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 };
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; double mu = 100.0;
SummaryStatistics sampleStats = null; SummaryStatistics sampleStats = null;
try { sampleStats = SummaryStatistics.newInstance();
sampleStats =
SummaryStatistics.newInstance(
SummaryStatisticsImpl.class);
} catch (InstantiationException e5) {
// TODO Auto-generated catch block
e5.printStackTrace();
} catch (IllegalAccessException e5) {
// TODO Auto-generated catch block
e5.printStackTrace();
}
for (int i = 0; i < observed.length; i++) { for (int i = 0; i < observed.length; i++) {
sampleStats.addValue(observed[i]); sampleStats.addValue(observed[i]);
} }
assertEquals( assertEquals("t statistic", -2.82, testStatistic.t(mu, observed), 10E-3);
"t statistic", assertEquals("t statistic", -2.82, testStatistic.t(mu, sampleStats), 10E-3);
-2.82,
testStatistic.t(mu, observed),
10E-3);
assertEquals(
"t statistic",
-2.82,
testStatistic.t(mu, sampleStats),
10E-3);
double[] nullObserved = null; double[] nullObserved = null;
try { try {
testStatistic.t(mu, nullObserved); testStatistic.t(mu, nullObserved);
fail("arguments too short, IllegalArgumentException expected"); fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} }
SummaryStatistics nullStats = SummaryStatistics.newInstance(); SummaryStatistics nullStats = null;
try { try {
testStatistic.t(mu, nullStats); testStatistic.t(mu, nullStats);
fail("arguments too short, IllegalArgumentException expected"); fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} }
double[] emptyObs = { double[] emptyObs = {};
};
try { try {
testStatistic.t(mu, emptyObs); testStatistic.t(mu, emptyObs);
fail("arguments too short, IllegalArgumentException expected"); fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} }
SummaryStatistics emptyStats =SummaryStatistics.newInstance(); SummaryStatistics emptyStats =SummaryStatistics.newInstance();
try { try {
testStatistic.t(mu, emptyStats); testStatistic.t(mu, emptyStats);
fail("arguments too short, IllegalArgumentException expected"); fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} }
double[] tooShortObs = { 1.0 }; double[] tooShortObs = { 1.0 };
@ -215,248 +146,139 @@ public final class TestStatisticTest extends TestCase {
testStatistic.t(mu, tooShortObs); testStatistic.t(mu, tooShortObs);
fail("arguments too short, IllegalArgumentException expected"); fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // exptected
} }
try { try {
testStatistic.tTest(mu, tooShortObs); testStatistic.tTest(mu, tooShortObs);
fail("arguments too short, IllegalArgumentException expected"); fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} catch (MathException e) { }
// TODO Auto-generated catch block
e.printStackTrace();
}
SummaryStatistics tooShortStats = SummaryStatistics.newInstance(); SummaryStatistics tooShortStats = SummaryStatistics.newInstance();
tooShortStats.addValue(0d); tooShortStats.addValue(0d);
tooShortStats.addValue(2d); tooShortStats.addValue(2d);
try { try {
testStatistic.t(mu, tooShortStats); testStatistic.t(mu, tooShortStats);
fail("arguments too short, IllegalArgumentException expected"); fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // exptected
} }
try { try {
testStatistic.tTest(mu, tooShortStats); testStatistic.tTest(mu, tooShortStats);
fail("arguments too short, IllegalArgumentException expected"); fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // exptected
} catch (MathException e) { }
// TODO Auto-generated catch block
e.printStackTrace();
}
/** Moore and McCabe Example 8.3, p 516 */ /** Moore and McCabe Example 8.3, p 516 */
double[] oneSidedP = double[] oneSidedP =
{ {2d, 0d, 6d, 6d, 3d, 3d, 2d, 3d, -6d, 6d, 6d, 6d, 3d, 0d, 1d, 1d, 0d, 2d, 3d, 3d };
2d, SummaryStatistics oneSidedPStats = SummaryStatistics.newInstance();
0d,
6d,
6d,
3d,
3d,
2d,
3d,
-6d,
6d,
6d,
6d,
3d,
0d,
1d,
1d,
0d,
2d,
3d,
3d };
SummaryStatistics oneSidedPStats = SummaryStatistics.newInstance();;
for (int i = 0; i < oneSidedP.length; i++) { for (int i = 0; i < oneSidedP.length; i++) {
oneSidedPStats.addValue(oneSidedP[i]); oneSidedPStats.addValue(oneSidedP[i]);
} }
assertEquals("one sample t stat", 3.86, testStatistic.t(0d, oneSidedP), 0.01);
try { assertEquals("one sample t stat", 3.86, testStatistic.t(0d, oneSidedPStats), 0.01);
assertEquals( assertEquals("one sample p value", 0.00052, testStatistic.tTest(0d, oneSidedP) / 2d, 10E-5);
"one sample t stat", assertEquals("one sample p value", 0.00052, testStatistic.tTest(0d, oneSidedPStats) / 2d, 10E-5);
3.86, assertTrue("one sample t-test reject", testStatistic.tTest(0d, oneSidedP, 0.01));
testStatistic.t(0d, oneSidedP), assertTrue("one sample t-test reject", testStatistic.tTest(0d, oneSidedPStats, 0.01));
0.01); assertTrue("one sample t-test accept", !testStatistic.tTest(0d, oneSidedP, 0.0001));
assertEquals( assertTrue("one sample t-test accept", !testStatistic.tTest(0d, oneSidedPStats, 0.0001));
"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);
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));
assertTrue(
"one sample t-test accept",
!testStatistic.tTest(0d, oneSidedPStats, 0.0001));
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try { try {
testStatistic.tTest(0d, oneSidedP, 95); testStatistic.tTest(0d, oneSidedP, 95);
fail("alpha out of range, IllegalArgumentException expected"); fail("alpha out of range, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // exptected
} catch (MathException e) { }
// TODO Auto-generated catch block
e.printStackTrace();
}
try { try {
testStatistic.tTest(0d, oneSidedPStats, 95); testStatistic.tTest(0d, oneSidedPStats, 95);
fail("alpha out of range, IllegalArgumentException expected"); fail("alpha out of range, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} catch (MathException e) { }
// TODO Auto-generated catch block
e.printStackTrace();
}
/** Moore and McCabe Example 8.12, p 552 */ /** Moore and McCabe Example 8.12, p 552 */
double[] sample1 = { 7d, -4d, 18d, 17d, -3d, -5d, 1d, 10d, 11d, -2d }; double[] sample1 = { 7d, -4d, 18d, 17d, -3d, -5d, 1d, 10d, 11d, -2d };
double[] sample2 = double[] sample2 = { -1d, 12d, -1d, -3d, 3d, -5d, 5d, 2d, -11d, -1d, -3d };
{ -1d, 12d, -1d, -3d, 3d, -5d, 5d, 2d, -11d, -1d, -3d }; SummaryStatistics sampleStats1 = SummaryStatistics.newInstance();
SummaryStatistics sampleStats1 = SummaryStatistics.newInstance();
for (int i = 0; i < sample1.length; i++) { for (int i = 0; i < sample1.length; i++) {
sampleStats1.addValue(sample1[i]); sampleStats1.addValue(sample1[i]);
} }
SummaryStatistics sampleStats2 = SummaryStatistics.newInstance();
SummaryStatistics sampleStats2 = SummaryStatistics.newInstance();
for (int i = 0; i < sample2.length; i++) { for (int i = 0; i < sample2.length; i++) {
sampleStats2.addValue(sample2[i]); sampleStats2.addValue(sample2[i]);
} }
try { //FIXME: textbook example reported t stat uses pooled variance
//FIXME: textbook example reported t stat uses pooled variance // should replace the following two tests with R-verified example
// should replace with R-verified example assertEquals("two sample t stat", 1.634, testStatistic.t(sample1, sample2), 0.1);
assertEquals( assertEquals("two sample t stat", 1.634, testStatistic.t(sampleStats1, sampleStats2), 0.1);
"two sample t stat",
1.634, // These tests are OK, since book reports non-pooled exact p-value
testStatistic.t(sample1, sample2), assertEquals("two sample p value", 0.059, testStatistic.tTest(sample1, sample2) / 2d, 10E-3);
0.1); assertEquals("two sample p value", 0.059, testStatistic.tTest(sampleStats1, sampleStats2) / 2d, 10E-3);
assertEquals( assertTrue("two sample t-test reject", testStatistic.tTest(sample1, sample2, 0.2));
"two sample t stat", assertTrue("two sample t-test reject", testStatistic.tTest(sampleStats1, sampleStats2, 0.2));
1.634, assertTrue("two sample t-test accept", !testStatistic.tTest(sample1, sample2, 0.1));
testStatistic.t(sampleStats1, sampleStats2), assertTrue("two sample t-test accept", !testStatistic.tTest(sampleStats1, sampleStats2, 0.1));
0.1);
// This test is OK, since book reports non-pooled exact p-value
assertEquals(
"two sample p value",
0.059,
testStatistic.tTest(sample1, sample2) / 2d,
10E-3);
assertEquals(
"two sample p value",
0.059,
testStatistic.tTest(sampleStats1, sampleStats2) / 2d,
10E-3);
assertTrue(
"two sample t-test reject",
testStatistic.tTest(sample1, sample2, 0.2));
assertTrue(
"two sample t-test reject",
testStatistic.tTest(sampleStats1, sampleStats2, 0.2));
assertTrue(
"two sample t-test accept",
!testStatistic.tTest(sample1, sample2, 0.1));
assertTrue(
"two sample t-test accept",
!testStatistic.tTest(sampleStats1, sampleStats2, 0.1));
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try { try {
testStatistic.tTest(sample1, sample2, 95); testStatistic.tTest(sample1, sample2, 95);
fail("alpha out of range, IllegalArgumentException expected"); fail("alpha out of range, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // exptected
} catch (MathException e) { }
// TODO Auto-generated catch block
e.printStackTrace();
}
try { try {
testStatistic.tTest(sampleStats1, sampleStats2, 95); testStatistic.tTest(sampleStats1, sampleStats2, 95);
fail("alpha out of range, IllegalArgumentException expected"); fail("alpha out of range, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} catch (MathException e) { }
// TODO Auto-generated catch block
e.printStackTrace();
}
try { try {
testStatistic.tTest(sample1, tooShortObs, .01); testStatistic.tTest(sample1, tooShortObs, .01);
fail("insufficient data, IllegalArgumentException expected"); fail("insufficient data, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} catch (MathException e) { }
// TODO Auto-generated catch block
e.printStackTrace();
}
try { try {
testStatistic.tTest(sampleStats1, tooShortStats, .01); testStatistic.tTest(sampleStats1, tooShortStats, .01);
fail("insufficient data, IllegalArgumentException expected"); fail("insufficient data, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} catch (MathException e) { }
// TODO Auto-generated catch block
e.printStackTrace();
}
try { try {
testStatistic.tTest(sample1, tooShortObs); testStatistic.tTest(sample1, tooShortObs);
fail("insufficient data, IllegalArgumentException expected"); fail("insufficient data, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} catch (MathException e) { }
// TODO Auto-generated catch block
e.printStackTrace();
}
try { try {
testStatistic.tTest(sampleStats1, tooShortStats); testStatistic.tTest(sampleStats1, tooShortStats);
fail("insufficient data, IllegalArgumentException expected"); fail("insufficient data, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} catch (MathException e) { }
// TODO Auto-generated catch block
e.printStackTrace();
}
try { try {
testStatistic.t(sample1, tooShortObs); testStatistic.t(sample1, tooShortObs);
fail("insufficient data, IllegalArgumentException expected"); fail("insufficient data, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // expected
} }
try { try {
testStatistic.t(sampleStats1, tooShortStats); testStatistic.t(sampleStats1, tooShortStats);
fail("insufficient data, IllegalArgumentException expected"); fail("insufficient data, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
; // exptected
} }
} }
} }