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