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: 610795 $ $Date: 2008-01-10 06:53:42 -0700 (Thu, 10 Jan 2008) $
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      public void setUp() {
44          tooShortStats = new SummaryStatistics();
45          tooShortStats.addValue(0d);
46      }
47  
48      public static Test suite() {
49          TestSuite suite = new TestSuite(TTestTest.class);
50          suite.setName("TestStatistic Tests");
51          return suite;
52      }
53  
54      public void testOneSampleT() throws Exception {
55          double[] observed =
56              {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 };
57          double mu = 100.0;
58          SummaryStatistics sampleStats = null;
59          sampleStats = new SummaryStatistics();
60          for (int i = 0; i < observed.length; i++) {
61              sampleStats.addValue(observed[i]);
62          }
63  
64          // Target comparison values computed using R version 1.8.1 (Linux version)
65          assertEquals("t statistic",  -2.81976445346,
66                  testStatistic.t(mu, observed), 10E-10);
67          assertEquals("t statistic",  -2.81976445346,
68                  testStatistic.t(mu, sampleStats), 10E-10);
69          assertEquals("p value", 0.0136390585873,
70                  testStatistic.tTest(mu, observed), 10E-10);
71          assertEquals("p value", 0.0136390585873,
72                  testStatistic.tTest(mu, sampleStats), 10E-10);
73  
74          try {
75              testStatistic.t(mu, (double[]) null);
76              fail("arguments too short, IllegalArgumentException expected");
77          } catch (IllegalArgumentException ex) {
78              // expected
79          }
80  
81          try {
82              testStatistic.t(mu, (SummaryStatistics) null);
83              fail("arguments too short, IllegalArgumentException expected");
84          } catch (IllegalArgumentException ex) {
85              // expected
86          }
87  
88          try {
89              testStatistic.t(mu, emptyObs);
90              fail("arguments too short, IllegalArgumentException expected");
91          } catch (IllegalArgumentException ex) {
92              // expected
93          }
94   
95          try {
96              testStatistic.t(mu, emptyStats);
97              fail("arguments too short, IllegalArgumentException expected");
98          } catch (IllegalArgumentException ex) {
99              // expected
100         }
101 
102         try {
103             testStatistic.t(mu, tooShortObs);
104             fail("insufficient data to compute t statistic, IllegalArgumentException expected");
105         } catch (IllegalArgumentException ex) {
106             // expected
107         }
108         try {
109             testStatistic.tTest(mu, tooShortObs);
110             fail("insufficient data to perform t test, IllegalArgumentException expected");
111         } catch (IllegalArgumentException ex) {
112            // expected
113         }  
114 
115         try {
116             testStatistic.t(mu, tooShortStats);
117             fail("insufficient data to compute t statistic, IllegalArgumentException expected");
118         } catch (IllegalArgumentException ex) {
119             // expected
120         }
121         try {
122             testStatistic.tTest(mu, tooShortStats);
123             fail("insufficient data to perform t test, IllegalArgumentException expected");
124         } catch (IllegalArgumentException ex) {
125             // expected
126         }  
127     }
128     
129     public void testOneSampleTTest() throws Exception {
130         double[] oneSidedP =
131             {2d, 0d, 6d, 6d, 3d, 3d, 2d, 3d, -6d, 6d, 6d, 6d, 3d, 0d, 1d, 1d, 0d, 2d, 3d, 3d };
132         SummaryStatistics oneSidedPStats = new SummaryStatistics();    
133         for (int i = 0; i < oneSidedP.length; i++) {
134             oneSidedPStats.addValue(oneSidedP[i]);
135         }
136         // Target comparison values computed using R version 1.8.1 (Linux version)
137         assertEquals("one sample t stat", 3.86485535541, 
138                 testStatistic.t(0d, oneSidedP), 10E-10);
139         assertEquals("one sample t stat", 3.86485535541, 
140                 testStatistic.t(0d, oneSidedPStats),1E-10);
141         assertEquals("one sample p value", 0.000521637019637,
142                 testStatistic.tTest(0d, oneSidedP) / 2d, 10E-10);
143         assertEquals("one sample p value", 0.000521637019637,
144                 testStatistic.tTest(0d, oneSidedPStats) / 2d, 10E-5);
145         assertTrue("one sample t-test reject", testStatistic.tTest(0d, oneSidedP, 0.01));
146         assertTrue("one sample t-test reject", testStatistic.tTest(0d, oneSidedPStats, 0.01));
147         assertTrue("one sample t-test accept", !testStatistic.tTest(0d, oneSidedP, 0.0001));
148         assertTrue("one sample t-test accept", !testStatistic.tTest(0d, oneSidedPStats, 0.0001));
149          
150         try {
151             testStatistic.tTest(0d, oneSidedP, 95);
152             fail("alpha out of range, IllegalArgumentException expected");
153         } catch (IllegalArgumentException ex) {
154             // expected
155         }  
156         
157         try {
158             testStatistic.tTest(0d, oneSidedPStats, 95);
159             fail("alpha out of range, IllegalArgumentException expected");
160         } catch (IllegalArgumentException ex) {
161             // expected
162         }  
163         
164     }
165     
166     public void testTwoSampleTHeterscedastic() throws Exception {
167         double[] sample1 = { 7d, -4d, 18d, 17d, -3d, -5d, 1d, 10d, 11d, -2d };
168         double[] sample2 = { -1d, 12d, -1d, -3d, 3d, -5d, 5d, 2d, -11d, -1d, -3d };
169         SummaryStatistics sampleStats1 = new SummaryStatistics();  
170         for (int i = 0; i < sample1.length; i++) {
171             sampleStats1.addValue(sample1[i]);
172         }
173         SummaryStatistics sampleStats2 = new SummaryStatistics();    
174         for (int i = 0; i < sample2.length; i++) {
175             sampleStats2.addValue(sample2[i]);
176         }
177          
178         // Target comparison values computed using R version 1.8.1 (Linux version)
179         assertEquals("two sample heteroscedastic t stat", 1.60371728768, 
180                 testStatistic.t(sample1, sample2), 1E-10);
181         assertEquals("two sample heteroscedastic t stat", 1.60371728768, 
182                 testStatistic.t(sampleStats1, sampleStats2), 1E-10);
183         assertEquals("two sample heteroscedastic p value", 0.128839369622, 
184                 testStatistic.tTest(sample1, sample2), 1E-10);
185         assertEquals("two sample heteroscedastic p value", 0.128839369622, 
186                 testStatistic.tTest(sampleStats1, sampleStats2), 1E-10);     
187         assertTrue("two sample heteroscedastic t-test reject", 
188                 testStatistic.tTest(sample1, sample2, 0.2));
189         assertTrue("two sample heteroscedastic t-test reject", 
190                 testStatistic.tTest(sampleStats1, sampleStats2, 0.2));
191         assertTrue("two sample heteroscedastic t-test accept", 
192                 !testStatistic.tTest(sample1, sample2, 0.1));
193         assertTrue("two sample heteroscedastic t-test accept", 
194                 !testStatistic.tTest(sampleStats1, sampleStats2, 0.1));
195      
196         try {
197             testStatistic.tTest(sample1, sample2, .95);
198             fail("alpha out of range, IllegalArgumentException expected");
199         } catch (IllegalArgumentException ex) {
200             // expected
201         } 
202         
203         try {
204             testStatistic.tTest(sampleStats1, sampleStats2, .95);
205             fail("alpha out of range, IllegalArgumentException expected");
206         } catch (IllegalArgumentException ex) {
207             // expected 
208         }  
209         
210         try {
211             testStatistic.tTest(sample1, tooShortObs, .01);
212             fail("insufficient data, IllegalArgumentException expected");
213         } catch (IllegalArgumentException ex) {
214             // expected
215         }  
216         
217         try {
218             testStatistic.tTest(sampleStats1, tooShortStats, .01);
219             fail("insufficient data, IllegalArgumentException expected");
220         } catch (IllegalArgumentException ex) {
221             // expected
222         }  
223         
224         try {
225             testStatistic.tTest(sample1, tooShortObs);
226             fail("insufficient data, IllegalArgumentException expected");
227         } catch (IllegalArgumentException ex) {
228            // expected
229         }  
230         
231         try {
232             testStatistic.tTest(sampleStats1, tooShortStats);
233             fail("insufficient data, IllegalArgumentException expected");
234         } catch (IllegalArgumentException ex) {
235             // expected
236         }  
237         
238         try {
239             testStatistic.t(sample1, tooShortObs);
240             fail("insufficient data, IllegalArgumentException expected");
241         } catch (IllegalArgumentException ex) {
242             // expected
243         }
244         
245         try {
246             testStatistic.t(sampleStats1, tooShortStats);
247             fail("insufficient data, IllegalArgumentException expected");
248         } catch (IllegalArgumentException ex) {
249            // expected
250         }
251     }
252     public void testTwoSampleTHomoscedastic() throws Exception {
253         double[] sample1 ={2, 4, 6, 8, 10, 97};
254         double[] sample2 = {4, 6, 8, 10, 16};
255         SummaryStatistics sampleStats1 = new SummaryStatistics();  
256         for (int i = 0; i < sample1.length; i++) {
257             sampleStats1.addValue(sample1[i]);
258         }
259         SummaryStatistics sampleStats2 = new SummaryStatistics();    
260         for (int i = 0; i < sample2.length; i++) {
261             sampleStats2.addValue(sample2[i]);
262         }
263         
264         // Target comparison values computed using R version 1.8.1 (Linux version)
265         assertEquals("two sample homoscedastic t stat", 0.73096310086, 
266               testStatistic.homoscedasticT(sample1, sample2), 10E-11);
267         assertEquals("two sample homoscedastic p value", 0.4833963785, 
268                 testStatistic.homoscedasticTTest(sampleStats1, sampleStats2), 1E-10);     
269         assertTrue("two sample homoscedastic t-test reject", 
270                 testStatistic.homoscedasticTTest(sample1, sample2, 0.49));
271         assertTrue("two sample homoscedastic t-test accept", 
272                 !testStatistic.homoscedasticTTest(sample1, sample2, 0.48));
273     }
274     
275     public void testSmallSamples() throws Exception {
276         double[] sample1 = {1d, 3d};
277         double[] sample2 = {4d, 5d};        
278         
279         // Target values computed using R, version 1.8.1 (linux version)
280         assertEquals(-2.2360679775, testStatistic.t(sample1, sample2),
281                 1E-10);
282         assertEquals(0.198727388935, testStatistic.tTest(sample1, sample2),
283                 1E-10);
284     }
285     
286     public void testPaired() throws Exception {
287         double[] sample1 = {1d, 3d, 5d, 7d};
288         double[] sample2 = {0d, 6d, 11d, 2d};
289         double[] sample3 = {5d, 7d, 8d, 10d};
290 
291         // Target values computed using R, version 1.8.1 (linux version)
292         assertEquals(-0.3133, testStatistic.pairedT(sample1, sample2), 1E-4);
293         assertEquals(0.774544295819, testStatistic.pairedTTest(sample1, sample2), 1E-10);
294         assertEquals(0.001208, testStatistic.pairedTTest(sample1, sample3), 1E-6);
295         assertFalse(testStatistic.pairedTTest(sample1, sample3, .001));
296         assertTrue(testStatistic.pairedTTest(sample1, sample3, .002));    
297     }
298 }