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