001: /*
002: * Copyright 2004-2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.stat.inference;
017:
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021:
022: import org.apache.commons.math.stat.descriptive.SummaryStatistics;
023:
024: /**
025: * Test cases for the TTestImpl class.
026: *
027: * @version $Revision: 180065 $ $Date: 2005-06-04 20:58:14 -0700 (Sat, 04 Jun 2005) $
028: */
029:
030: public class TTestTest extends TestCase {
031:
032: protected TTest testStatistic = new TTestImpl();
033:
034: private double[] tooShortObs = { 1.0 };
035: private double[] nullObserved = null;
036: private double[] emptyObs = {};
037: private SummaryStatistics emptyStats = SummaryStatistics
038: .newInstance();
039: private SummaryStatistics nullStats = null;
040: SummaryStatistics tooShortStats = null;
041:
042: public TTestTest(String name) {
043: super (name);
044: }
045:
046: public void setUp() {
047: tooShortStats = SummaryStatistics.newInstance();
048: tooShortStats.addValue(0d);
049: }
050:
051: public static Test suite() {
052: TestSuite suite = new TestSuite(TTestTest.class);
053: suite.setName("TestStatistic Tests");
054: return suite;
055: }
056:
057: public void testOneSampleT() throws Exception {
058: double[] observed = { 93.0, 103.0, 95.0, 101.0, 91.0, 105.0,
059: 96.0, 94.0, 101.0, 88.0, 98.0, 94.0, 101.0, 92.0, 95.0 };
060: double mu = 100.0;
061: SummaryStatistics sampleStats = null;
062: sampleStats = SummaryStatistics.newInstance();
063: for (int i = 0; i < observed.length; i++) {
064: sampleStats.addValue(observed[i]);
065: }
066:
067: // Target comparison values computed using R version 1.8.1 (Linux version)
068: assertEquals("t statistic", -2.81976445346, testStatistic.t(mu,
069: observed), 10E-10);
070: assertEquals("t statistic", -2.81976445346, testStatistic.t(mu,
071: sampleStats), 10E-10);
072: assertEquals("p value", 0.0136390585873, testStatistic.tTest(
073: mu, observed), 10E-10);
074: assertEquals("p value", 0.0136390585873, testStatistic.tTest(
075: mu, sampleStats), 10E-10);
076:
077: try {
078: testStatistic.t(mu, nullObserved);
079: fail("arguments too short, IllegalArgumentException expected");
080: } catch (IllegalArgumentException ex) {
081: // expected
082: }
083:
084: try {
085: testStatistic.t(mu, nullStats);
086: fail("arguments too short, IllegalArgumentException expected");
087: } catch (IllegalArgumentException ex) {
088: // expected
089: }
090:
091: try {
092: testStatistic.t(mu, emptyObs);
093: fail("arguments too short, IllegalArgumentException expected");
094: } catch (IllegalArgumentException ex) {
095: // expected
096: }
097:
098: try {
099: testStatistic.t(mu, emptyStats);
100: fail("arguments too short, IllegalArgumentException expected");
101: } catch (IllegalArgumentException ex) {
102: // expected
103: }
104:
105: try {
106: testStatistic.t(mu, tooShortObs);
107: fail("insufficient data to compute t statistic, IllegalArgumentException expected");
108: } catch (IllegalArgumentException ex) {
109: // exptected
110: }
111: try {
112: testStatistic.tTest(mu, tooShortObs);
113: fail("insufficient data to perform t test, IllegalArgumentException expected");
114: } catch (IllegalArgumentException ex) {
115: // expected
116: }
117:
118: try {
119: testStatistic.t(mu, tooShortStats);
120: fail("insufficient data to compute t statistic, IllegalArgumentException expected");
121: } catch (IllegalArgumentException ex) {
122: // exptected
123: }
124: try {
125: testStatistic.tTest(mu, tooShortStats);
126: fail("insufficient data to perform t test, IllegalArgumentException expected");
127: } catch (IllegalArgumentException ex) {
128: // exptected
129: }
130: }
131:
132: public void testOneSampleTTest() throws Exception {
133: double[] oneSidedP = { 2d, 0d, 6d, 6d, 3d, 3d, 2d, 3d, -6d, 6d,
134: 6d, 6d, 3d, 0d, 1d, 1d, 0d, 2d, 3d, 3d };
135: SummaryStatistics oneSidedPStats = SummaryStatistics
136: .newInstance();
137: for (int i = 0; i < oneSidedP.length; i++) {
138: oneSidedPStats.addValue(oneSidedP[i]);
139: }
140: // Target comparison values computed using R version 1.8.1 (Linux version)
141: assertEquals("one sample t stat", 3.86485535541, testStatistic
142: .t(0d, oneSidedP), 10E-10);
143: assertEquals("one sample t stat", 3.86485535541, testStatistic
144: .t(0d, oneSidedPStats), 1E-10);
145: assertEquals("one sample p value", 0.000521637019637,
146: testStatistic.tTest(0d, oneSidedP) / 2d, 10E-10);
147: assertEquals("one sample p value", 0.000521637019637,
148: testStatistic.tTest(0d, oneSidedPStats) / 2d, 10E-5);
149: assertTrue("one sample t-test reject", testStatistic.tTest(0d,
150: oneSidedP, 0.01));
151: assertTrue("one sample t-test reject", testStatistic.tTest(0d,
152: oneSidedPStats, 0.01));
153: assertTrue("one sample t-test accept", !testStatistic.tTest(0d,
154: oneSidedP, 0.0001));
155: assertTrue("one sample t-test accept", !testStatistic.tTest(0d,
156: oneSidedPStats, 0.0001));
157:
158: try {
159: testStatistic.tTest(0d, oneSidedP, 95);
160: fail("alpha out of range, IllegalArgumentException expected");
161: } catch (IllegalArgumentException ex) {
162: // exptected
163: }
164:
165: try {
166: testStatistic.tTest(0d, oneSidedPStats, 95);
167: fail("alpha out of range, IllegalArgumentException expected");
168: } catch (IllegalArgumentException ex) {
169: // expected
170: }
171:
172: }
173:
174: public void testTwoSampleTHeterscedastic() throws Exception {
175: double[] sample1 = { 7d, -4d, 18d, 17d, -3d, -5d, 1d, 10d, 11d,
176: -2d };
177: double[] sample2 = { -1d, 12d, -1d, -3d, 3d, -5d, 5d, 2d, -11d,
178: -1d, -3d };
179: SummaryStatistics sampleStats1 = SummaryStatistics
180: .newInstance();
181: for (int i = 0; i < sample1.length; i++) {
182: sampleStats1.addValue(sample1[i]);
183: }
184: SummaryStatistics sampleStats2 = SummaryStatistics
185: .newInstance();
186: for (int i = 0; i < sample2.length; i++) {
187: sampleStats2.addValue(sample2[i]);
188: }
189:
190: // Target comparison values computed using R version 1.8.1 (Linux version)
191: assertEquals("two sample heteroscedastic t stat",
192: 1.60371728768, testStatistic.t(sample1, sample2), 1E-10);
193: assertEquals("two sample heteroscedastic t stat",
194: 1.60371728768, testStatistic.t(sampleStats1,
195: sampleStats2), 1E-10);
196: assertEquals("two sample heteroscedastic p value",
197: 0.128839369622, testStatistic.tTest(sample1, sample2),
198: 1E-10);
199: assertEquals("two sample heteroscedastic p value",
200: 0.128839369622, testStatistic.tTest(sampleStats1,
201: sampleStats2), 1E-10);
202: assertTrue("two sample heteroscedastic t-test reject",
203: testStatistic.tTest(sample1, sample2, 0.2));
204: assertTrue("two sample heteroscedastic t-test reject",
205: testStatistic.tTest(sampleStats1, sampleStats2, 0.2));
206: assertTrue("two sample heteroscedastic t-test accept",
207: !testStatistic.tTest(sample1, sample2, 0.1));
208: assertTrue("two sample heteroscedastic t-test accept",
209: !testStatistic.tTest(sampleStats1, sampleStats2, 0.1));
210:
211: try {
212: testStatistic.tTest(sample1, sample2, .95);
213: fail("alpha out of range, IllegalArgumentException expected");
214: } catch (IllegalArgumentException ex) {
215: // expected
216: }
217:
218: try {
219: testStatistic.tTest(sampleStats1, sampleStats2, .95);
220: fail("alpha out of range, IllegalArgumentException expected");
221: } catch (IllegalArgumentException ex) {
222: // expected
223: }
224:
225: try {
226: testStatistic.tTest(sample1, tooShortObs, .01);
227: fail("insufficient data, IllegalArgumentException expected");
228: } catch (IllegalArgumentException ex) {
229: // expected
230: }
231:
232: try {
233: testStatistic.tTest(sampleStats1, tooShortStats, .01);
234: fail("insufficient data, IllegalArgumentException expected");
235: } catch (IllegalArgumentException ex) {
236: // expected
237: }
238:
239: try {
240: testStatistic.tTest(sample1, tooShortObs);
241: fail("insufficient data, IllegalArgumentException expected");
242: } catch (IllegalArgumentException ex) {
243: // expected
244: }
245:
246: try {
247: testStatistic.tTest(sampleStats1, tooShortStats);
248: fail("insufficient data, IllegalArgumentException expected");
249: } catch (IllegalArgumentException ex) {
250: // expected
251: }
252:
253: try {
254: testStatistic.t(sample1, tooShortObs);
255: fail("insufficient data, IllegalArgumentException expected");
256: } catch (IllegalArgumentException ex) {
257: // expected
258: }
259:
260: try {
261: testStatistic.t(sampleStats1, tooShortStats);
262: fail("insufficient data, IllegalArgumentException expected");
263: } catch (IllegalArgumentException ex) {
264: // expected
265: }
266: }
267:
268: public void testTwoSampleTHomoscedastic() throws Exception {
269: double[] sample1 = { 2, 4, 6, 8, 10, 97 };
270: double[] sample2 = { 4, 6, 8, 10, 16 };
271: SummaryStatistics sampleStats1 = SummaryStatistics
272: .newInstance();
273: for (int i = 0; i < sample1.length; i++) {
274: sampleStats1.addValue(sample1[i]);
275: }
276: SummaryStatistics sampleStats2 = SummaryStatistics
277: .newInstance();
278: for (int i = 0; i < sample2.length; i++) {
279: sampleStats2.addValue(sample2[i]);
280: }
281:
282: // Target comparison values computed using R version 1.8.1 (Linux version)
283: assertEquals("two sample homoscedastic t stat", 0.73096310086,
284: testStatistic.homoscedasticT(sample1, sample2), 10E-11);
285: assertEquals("two sample homoscedastic p value", 0.4833963785,
286: testStatistic.homoscedasticTTest(sampleStats1,
287: sampleStats2), 1E-10);
288: assertTrue("two sample homoscedastic t-test reject",
289: testStatistic
290: .homoscedasticTTest(sample1, sample2, 0.49));
291: assertTrue("two sample homoscedastic t-test accept",
292: !testStatistic.homoscedasticTTest(sample1, sample2,
293: 0.48));
294: }
295:
296: public void testSmallSamples() throws Exception {
297: double[] sample1 = { 1d, 3d };
298: double[] sample2 = { 4d, 5d };
299:
300: // Target values computed using R, version 1.8.1 (linux version)
301: assertEquals(-2.2360679775, testStatistic.t(sample1, sample2),
302: 1E-10);
303: assertEquals(0.198727388935, testStatistic.tTest(sample1,
304: sample2), 1E-10);
305: }
306:
307: public void testPaired() throws Exception {
308: double[] sample1 = { 1d, 3d, 5d, 7d };
309: double[] sample2 = { 0d, 6d, 11d, 2d };
310: double[] sample3 = { 5d, 7d, 8d, 10d };
311: double[] sample4 = { 0d, 2d };
312:
313: // Target values computed using R, version 1.8.1 (linux version)
314: assertEquals(-0.3133, testStatistic.pairedT(sample1, sample2),
315: 1E-4);
316: assertEquals(0.774544295819, testStatistic.pairedTTest(sample1,
317: sample2), 1E-10);
318: assertEquals(0.001208, testStatistic.pairedTTest(sample1,
319: sample3), 1E-6);
320: assertFalse(testStatistic.pairedTTest(sample1, sample3, .001));
321: assertTrue(testStatistic.pairedTTest(sample1, sample3, .002));
322: }
323: }
|