~ubuntu-branches/ubuntu/maverick/commons-math/maverick

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/math/stat/inference/TTestTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-08-22 01:13:25 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090822011325-hi4peq1ua5weguwn
Tags: 2.0-1
* New upstream release.
* Set Maintainer field to Debian Java Team
* Add myself as Uploaders
* Switch to Quilt patch system:
  - Refresh all patchs
  - Remove B-D on dpatch, Add B-D on quilt
  - Include patchsys-quilt.mk in debian/rules
* Bump Standards-Version to 3.8.3:
  - Add a README.source to describe patch system
* Maven POMs:
  - Add a Build-Depends-Indep dependency on maven-repo-helper
  - Use mh_installpom and mh_installjar to install the POM and the jar to the
    Maven repository
* Use default-jdk/jre:
  - Depends on java5-runtime-headless
  - Build-Depends on default-jdk
  - Use /usr/lib/jvm/default-java as JAVA_HOME
* Move api documentation to /usr/share/doc/libcommons-math-java/api
* Build-Depends on junit4 instead of junit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 * contributor license agreements.  See the NOTICE file distributed with
 
4
 * this work for additional information regarding copyright ownership.
 
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 * (the "License"); you may not use this file except in compliance with
 
7
 * the License.  You may obtain a copy of the License at
 
8
 * 
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 * 
 
11
 * Unless required by applicable law or agreed to in writing, software
 
12
 * distributed under the License is distributed on an "AS IS" BASIS,
 
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 * See the License for the specific language governing permissions and
 
15
 * limitations under the License.
 
16
 */
 
17
package org.apache.commons.math.stat.inference;
 
18
 
 
19
import junit.framework.Test;
 
20
import junit.framework.TestCase;
 
21
import junit.framework.TestSuite;
 
22
 
 
23
import org.apache.commons.math.stat.descriptive.SummaryStatistics;
 
24
/**
 
25
 * Test cases for the TTestImpl class.
 
26
 *
 
27
 * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
 
28
 */
 
29
 
 
30
public class TTestTest extends TestCase {
 
31
 
 
32
    protected TTest testStatistic = new TTestImpl();
 
33
    
 
34
    private double[] tooShortObs = { 1.0 };
 
35
    private double[] emptyObs = {};
 
36
    private SummaryStatistics emptyStats = new SummaryStatistics();  
 
37
   SummaryStatistics tooShortStats = null;  
 
38
 
 
39
    public TTestTest(String name) {
 
40
        super(name);
 
41
    }
 
42
 
 
43
    @Override
 
44
    public void setUp() {
 
45
        tooShortStats = new SummaryStatistics();
 
46
        tooShortStats.addValue(0d);
 
47
    }
 
48
 
 
49
    public static Test suite() {
 
50
        TestSuite suite = new TestSuite(TTestTest.class);
 
51
        suite.setName("TestStatistic Tests");
 
52
        return suite;
 
53
    }
 
54
 
 
55
    public void testOneSampleT() throws Exception {
 
56
        double[] observed =
 
57
            {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 };
 
58
        double mu = 100.0;
 
59
        SummaryStatistics sampleStats = null;
 
60
        sampleStats = new SummaryStatistics();
 
61
        for (int i = 0; i < observed.length; i++) {
 
62
            sampleStats.addValue(observed[i]);
 
63
        }
 
64
 
 
65
        // Target comparison values computed using R version 1.8.1 (Linux version)
 
66
        assertEquals("t statistic",  -2.81976445346,
 
67
                testStatistic.t(mu, observed), 10E-10);
 
68
        assertEquals("t statistic",  -2.81976445346,
 
69
                testStatistic.t(mu, sampleStats), 10E-10);
 
70
        assertEquals("p value", 0.0136390585873,
 
71
                testStatistic.tTest(mu, observed), 10E-10);
 
72
        assertEquals("p value", 0.0136390585873,
 
73
                testStatistic.tTest(mu, sampleStats), 10E-10);
 
74
 
 
75
        try {
 
76
            testStatistic.t(mu, (double[]) null);
 
77
            fail("arguments too short, IllegalArgumentException expected");
 
78
        } catch (IllegalArgumentException ex) {
 
79
            // expected
 
80
        }
 
81
 
 
82
        try {
 
83
            testStatistic.t(mu, (SummaryStatistics) null);
 
84
            fail("arguments too short, IllegalArgumentException expected");
 
85
        } catch (IllegalArgumentException ex) {
 
86
            // expected
 
87
        }
 
88
 
 
89
        try {
 
90
            testStatistic.t(mu, emptyObs);
 
91
            fail("arguments too short, IllegalArgumentException expected");
 
92
        } catch (IllegalArgumentException ex) {
 
93
            // expected
 
94
        }
 
95
 
 
96
        try {
 
97
            testStatistic.t(mu, emptyStats);
 
98
            fail("arguments too short, IllegalArgumentException expected");
 
99
        } catch (IllegalArgumentException ex) {
 
100
            // expected
 
101
        }
 
102
 
 
103
        try {
 
104
            testStatistic.t(mu, tooShortObs);
 
105
            fail("insufficient data to compute t statistic, IllegalArgumentException expected");
 
106
        } catch (IllegalArgumentException ex) {
 
107
            // expected
 
108
        }
 
109
        try {
 
110
            testStatistic.tTest(mu, tooShortObs);
 
111
            fail("insufficient data to perform t test, IllegalArgumentException expected");
 
112
        } catch (IllegalArgumentException ex) {
 
113
           // expected
 
114
        }  
 
115
 
 
116
        try {
 
117
            testStatistic.t(mu, tooShortStats);
 
118
            fail("insufficient data to compute t statistic, IllegalArgumentException expected");
 
119
        } catch (IllegalArgumentException ex) {
 
120
            // expected
 
121
        }
 
122
        try {
 
123
            testStatistic.tTest(mu, tooShortStats);
 
124
            fail("insufficient data to perform t test, IllegalArgumentException expected");
 
125
        } catch (IllegalArgumentException ex) {
 
126
            // expected
 
127
        }  
 
128
    }
 
129
    
 
130
    public void testOneSampleTTest() throws Exception {
 
131
        double[] oneSidedP =
 
132
            {2d, 0d, 6d, 6d, 3d, 3d, 2d, 3d, -6d, 6d, 6d, 6d, 3d, 0d, 1d, 1d, 0d, 2d, 3d, 3d };
 
133
        SummaryStatistics oneSidedPStats = new SummaryStatistics();    
 
134
        for (int i = 0; i < oneSidedP.length; i++) {
 
135
            oneSidedPStats.addValue(oneSidedP[i]);
 
136
        }
 
137
        // Target comparison values computed using R version 1.8.1 (Linux version)
 
138
        assertEquals("one sample t stat", 3.86485535541, 
 
139
                testStatistic.t(0d, oneSidedP), 10E-10);
 
140
        assertEquals("one sample t stat", 3.86485535541, 
 
141
                testStatistic.t(0d, oneSidedPStats),1E-10);
 
142
        assertEquals("one sample p value", 0.000521637019637,
 
143
                testStatistic.tTest(0d, oneSidedP) / 2d, 10E-10);
 
144
        assertEquals("one sample p value", 0.000521637019637,
 
145
                testStatistic.tTest(0d, oneSidedPStats) / 2d, 10E-5);
 
146
        assertTrue("one sample t-test reject", testStatistic.tTest(0d, oneSidedP, 0.01));
 
147
        assertTrue("one sample t-test reject", testStatistic.tTest(0d, oneSidedPStats, 0.01));
 
148
        assertTrue("one sample t-test accept", !testStatistic.tTest(0d, oneSidedP, 0.0001));
 
149
        assertTrue("one sample t-test accept", !testStatistic.tTest(0d, oneSidedPStats, 0.0001));
 
150
         
 
151
        try {
 
152
            testStatistic.tTest(0d, oneSidedP, 95);
 
153
            fail("alpha out of range, IllegalArgumentException expected");
 
154
        } catch (IllegalArgumentException ex) {
 
155
            // expected
 
156
        }  
 
157
        
 
158
        try {
 
159
            testStatistic.tTest(0d, oneSidedPStats, 95);
 
160
            fail("alpha out of range, IllegalArgumentException expected");
 
161
        } catch (IllegalArgumentException ex) {
 
162
            // expected
 
163
        }  
 
164
        
 
165
    }
 
166
    
 
167
    public void testTwoSampleTHeterscedastic() throws Exception {
 
168
        double[] sample1 = { 7d, -4d, 18d, 17d, -3d, -5d, 1d, 10d, 11d, -2d };
 
169
        double[] sample2 = { -1d, 12d, -1d, -3d, 3d, -5d, 5d, 2d, -11d, -1d, -3d };
 
170
        SummaryStatistics sampleStats1 = new SummaryStatistics();  
 
171
        for (int i = 0; i < sample1.length; i++) {
 
172
            sampleStats1.addValue(sample1[i]);
 
173
        }
 
174
        SummaryStatistics sampleStats2 = new SummaryStatistics();    
 
175
        for (int i = 0; i < sample2.length; i++) {
 
176
            sampleStats2.addValue(sample2[i]);
 
177
        }
 
178
         
 
179
        // Target comparison values computed using R version 1.8.1 (Linux version)
 
180
        assertEquals("two sample heteroscedastic t stat", 1.60371728768, 
 
181
                testStatistic.t(sample1, sample2), 1E-10);
 
182
        assertEquals("two sample heteroscedastic t stat", 1.60371728768, 
 
183
                testStatistic.t(sampleStats1, sampleStats2), 1E-10);
 
184
        assertEquals("two sample heteroscedastic p value", 0.128839369622, 
 
185
                testStatistic.tTest(sample1, sample2), 1E-10);
 
186
        assertEquals("two sample heteroscedastic p value", 0.128839369622, 
 
187
                testStatistic.tTest(sampleStats1, sampleStats2), 1E-10);     
 
188
        assertTrue("two sample heteroscedastic t-test reject", 
 
189
                testStatistic.tTest(sample1, sample2, 0.2));
 
190
        assertTrue("two sample heteroscedastic t-test reject", 
 
191
                testStatistic.tTest(sampleStats1, sampleStats2, 0.2));
 
192
        assertTrue("two sample heteroscedastic t-test accept", 
 
193
                !testStatistic.tTest(sample1, sample2, 0.1));
 
194
        assertTrue("two sample heteroscedastic t-test accept", 
 
195
                !testStatistic.tTest(sampleStats1, sampleStats2, 0.1));
 
196
     
 
197
        try {
 
198
            testStatistic.tTest(sample1, sample2, .95);
 
199
            fail("alpha out of range, IllegalArgumentException expected");
 
200
        } catch (IllegalArgumentException ex) {
 
201
            // expected
 
202
        } 
 
203
        
 
204
        try {
 
205
            testStatistic.tTest(sampleStats1, sampleStats2, .95);
 
206
            fail("alpha out of range, IllegalArgumentException expected");
 
207
        } catch (IllegalArgumentException ex) {
 
208
            // expected 
 
209
        }  
 
210
        
 
211
        try {
 
212
            testStatistic.tTest(sample1, tooShortObs, .01);
 
213
            fail("insufficient data, IllegalArgumentException expected");
 
214
        } catch (IllegalArgumentException ex) {
 
215
            // expected
 
216
        }  
 
217
        
 
218
        try {
 
219
            testStatistic.tTest(sampleStats1, tooShortStats, .01);
 
220
            fail("insufficient data, IllegalArgumentException expected");
 
221
        } catch (IllegalArgumentException ex) {
 
222
            // expected
 
223
        }  
 
224
        
 
225
        try {
 
226
            testStatistic.tTest(sample1, tooShortObs);
 
227
            fail("insufficient data, IllegalArgumentException expected");
 
228
        } catch (IllegalArgumentException ex) {
 
229
           // expected
 
230
        }  
 
231
        
 
232
        try {
 
233
            testStatistic.tTest(sampleStats1, tooShortStats);
 
234
            fail("insufficient data, IllegalArgumentException expected");
 
235
        } catch (IllegalArgumentException ex) {
 
236
            // expected
 
237
        }  
 
238
        
 
239
        try {
 
240
            testStatistic.t(sample1, tooShortObs);
 
241
            fail("insufficient data, IllegalArgumentException expected");
 
242
        } catch (IllegalArgumentException ex) {
 
243
            // expected
 
244
        }
 
245
        
 
246
        try {
 
247
            testStatistic.t(sampleStats1, tooShortStats);
 
248
            fail("insufficient data, IllegalArgumentException expected");
 
249
        } catch (IllegalArgumentException ex) {
 
250
           // expected
 
251
        }
 
252
    }
 
253
    public void testTwoSampleTHomoscedastic() throws Exception {
 
254
        double[] sample1 ={2, 4, 6, 8, 10, 97};
 
255
        double[] sample2 = {4, 6, 8, 10, 16};
 
256
        SummaryStatistics sampleStats1 = new SummaryStatistics();  
 
257
        for (int i = 0; i < sample1.length; i++) {
 
258
            sampleStats1.addValue(sample1[i]);
 
259
        }
 
260
        SummaryStatistics sampleStats2 = new SummaryStatistics();    
 
261
        for (int i = 0; i < sample2.length; i++) {
 
262
            sampleStats2.addValue(sample2[i]);
 
263
        }
 
264
        
 
265
        // Target comparison values computed using R version 1.8.1 (Linux version)
 
266
        assertEquals("two sample homoscedastic t stat", 0.73096310086, 
 
267
              testStatistic.homoscedasticT(sample1, sample2), 10E-11);
 
268
        assertEquals("two sample homoscedastic p value", 0.4833963785, 
 
269
                testStatistic.homoscedasticTTest(sampleStats1, sampleStats2), 1E-10);     
 
270
        assertTrue("two sample homoscedastic t-test reject", 
 
271
                testStatistic.homoscedasticTTest(sample1, sample2, 0.49));
 
272
        assertTrue("two sample homoscedastic t-test accept", 
 
273
                !testStatistic.homoscedasticTTest(sample1, sample2, 0.48));
 
274
    }
 
275
    
 
276
    public void testSmallSamples() throws Exception {
 
277
        double[] sample1 = {1d, 3d};
 
278
        double[] sample2 = {4d, 5d};        
 
279
        
 
280
        // Target values computed using R, version 1.8.1 (linux version)
 
281
        assertEquals(-2.2360679775, testStatistic.t(sample1, sample2),
 
282
                1E-10);
 
283
        assertEquals(0.198727388935, testStatistic.tTest(sample1, sample2),
 
284
                1E-10);
 
285
    }
 
286
    
 
287
    public void testPaired() throws Exception {
 
288
        double[] sample1 = {1d, 3d, 5d, 7d};
 
289
        double[] sample2 = {0d, 6d, 11d, 2d};
 
290
        double[] sample3 = {5d, 7d, 8d, 10d};
 
291
 
 
292
        // Target values computed using R, version 1.8.1 (linux version)
 
293
        assertEquals(-0.3133, testStatistic.pairedT(sample1, sample2), 1E-4);
 
294
        assertEquals(0.774544295819, testStatistic.pairedTTest(sample1, sample2), 1E-10);
 
295
        assertEquals(0.001208, testStatistic.pairedTTest(sample1, sample3), 1E-6);
 
296
        assertFalse(testStatistic.pairedTTest(sample1, sample3, .001));
 
297
        assertTrue(testStatistic.pairedTTest(sample1, sample3, .002));    
 
298
    }
 
299
}