1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
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
77 }
78
79 try {
80 testStatistic.t(mu, (StreamingStatistics) null);
81 Assert.fail("arguments too short, NullArgumentException expected");
82 } catch (NullArgumentException ex) {
83
84 }
85
86 try {
87 testStatistic.t(mu, emptyObs);
88 Assert.fail("arguments too short, MathIllegalArgumentException expected");
89 } catch (MathIllegalArgumentException ex) {
90
91 }
92
93 try {
94 testStatistic.t(mu, emptyStats);
95 Assert.fail("arguments too short, MathIllegalArgumentException expected");
96 } catch (MathIllegalArgumentException ex) {
97
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
105 }
106 try {
107 testStatistic.tTest(mu, tooShortObs);
108 Assert.fail("insufficient data to perform t test, MathIllegalArgumentException expected");
109 } catch (MathIllegalArgumentException ex) {
110
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
118 }
119 try {
120 testStatistic.tTest(mu, tooShortStats);
121 Assert.fail("insufficient data to perform t test, MathIllegalArgumentException expected");
122 } catch (MathIllegalArgumentException ex) {
123
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
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
154 }
155
156 try {
157 testStatistic.tTest(0d, oneSidedPStats, 95);
158 Assert.fail("alpha out of range, MathIllegalArgumentException expected");
159 } catch (MathIllegalArgumentException ex) {
160
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
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
201 }
202
203 try {
204 testStatistic.tTest(sampleStats1, sampleStats2, .95);
205 Assert.fail("alpha out of range, MathIllegalArgumentException expected");
206 } catch (MathIllegalArgumentException ex) {
207
208 }
209
210 try {
211 testStatistic.tTest(sample1, tooShortObs, .01);
212 Assert.fail("insufficient data, MathIllegalArgumentException expected");
213 } catch (MathIllegalArgumentException ex) {
214
215 }
216
217 try {
218 testStatistic.tTest(sampleStats1, tooShortStats, .01);
219 Assert.fail("insufficient data, MathIllegalArgumentException expected");
220 } catch (MathIllegalArgumentException ex) {
221
222 }
223
224 try {
225 testStatistic.tTest(sample1, tooShortObs);
226 Assert.fail("insufficient data, MathIllegalArgumentException expected");
227 } catch (MathIllegalArgumentException ex) {
228
229 }
230
231 try {
232 testStatistic.tTest(sampleStats1, tooShortStats);
233 Assert.fail("insufficient data, MathIllegalArgumentException expected");
234 } catch (MathIllegalArgumentException ex) {
235
236 }
237
238 try {
239 testStatistic.t(sample1, tooShortObs);
240 Assert.fail("insufficient data, MathIllegalArgumentException expected");
241 } catch (MathIllegalArgumentException ex) {
242
243 }
244
245 try {
246 testStatistic.t(sampleStats1, tooShortStats);
247 Assert.fail("insufficient data, MathIllegalArgumentException expected");
248 } catch (MathIllegalArgumentException ex) {
249
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
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
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
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 }