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