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