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  import org.hipparchus.exception.MathIllegalArgumentException;
25  import org.hipparchus.util.FastMath;
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  /**
30   * Test cases for the GTest class.
31   *
32   * Data for the tests are from p64-69 in: McDonald, J.H. 2009. Handbook of
33   * Biological Statistics (2nd ed.). Sparky House Publishing, Baltimore,
34   * Maryland.
35   *
36   */
37  public class GTestTest {
38  
39      protected GTest testStatistic = new GTest();
40  
41      @Test
42      public void testGTestGoodnesOfFit1() throws Exception {
43          final double[] exp = new double[]{
44              3d, 1d
45          };
46  
47          final long[] obs = new long[]{
48              423, 133
49          };
50  
51          Assert.assertEquals("G test statistic",
52                  0.348721, testStatistic.g(exp, obs), 1E-6);
53          final double p_gtgf = testStatistic.gTest(exp, obs);
54          Assert.assertEquals("g-Test p-value", 0.55483, p_gtgf, 1E-5);
55  
56          Assert.assertFalse(testStatistic.gTest(exp, obs, 0.05));
57      }
58  
59      @Test
60      public void testGTestGoodnesOfFit2() throws Exception {
61          final double[] exp = new double[]{
62              0.54d, 0.40d, 0.05d, 0.01d
63          };
64  
65          final long[] obs = new long[]{
66              70, 79, 3, 4
67          };
68          Assert.assertEquals("G test statistic",
69                  13.144799, testStatistic.g(exp, obs), 1E-6);
70          final double p_gtgf = testStatistic.gTest(exp, obs);
71          Assert.assertEquals("g-Test p-value", 0.004333, p_gtgf, 1E-5);
72  
73          Assert.assertTrue(testStatistic.gTest(exp, obs, 0.05));
74      }
75  
76      @Test
77      public void testGTestGoodnesOfFit3() throws Exception {
78          final double[] exp = new double[]{
79              0.167d, 0.483d, 0.350d
80          };
81  
82          final long[] obs = new long[]{
83              14, 21, 25
84          };
85  
86          Assert.assertEquals("G test statistic",
87                  4.5554, testStatistic.g(exp, obs), 1E-4);
88          // Intrinisic (Hardy-Weinberg proportions) P-Value should be 0.033
89          final double p_gtgf = testStatistic.gTestIntrinsic(exp, obs);
90          Assert.assertEquals("g-Test p-value", 0.0328, p_gtgf, 1E-4);
91  
92          Assert.assertFalse(testStatistic.gTest(exp, obs, 0.05));
93      }
94  
95      @Test
96      public void testGTestIndependance1() throws Exception {
97          final long[] obs1 = new long[]{
98              268, 199, 42
99          };
100 
101         final long[] obs2 = new long[]{
102             807, 759, 184
103         };
104 
105         final double g = testStatistic.gDataSetsComparison(obs1, obs2);
106 
107         Assert.assertEquals("G test statistic",
108                 7.3008170, g, 1E-6);
109         final double p_gti = testStatistic.gTestDataSetsComparison(obs1, obs2);
110 
111         Assert.assertEquals("g-Test p-value", 0.0259805, p_gti, 1E-6);
112         Assert.assertTrue(testStatistic.gTestDataSetsComparison(obs1, obs2, 0.05));
113     }
114 
115     @Test
116     public void testGTestIndependance2() throws Exception {
117         final long[] obs1 = new long[]{
118             127, 99, 264
119         };
120 
121         final long[] obs2 = new long[]{
122             116, 67, 161
123         };
124 
125         final double g = testStatistic.gDataSetsComparison(obs1, obs2);
126 
127         Assert.assertEquals("G test statistic",
128                 6.227288, g, 1E-6);
129         final double p_gti = testStatistic.gTestDataSetsComparison(obs1, obs2);
130 
131         Assert.assertEquals("g-Test p-value", 0.04443, p_gti, 1E-5);
132         Assert.assertTrue(testStatistic.gTestDataSetsComparison(obs1, obs2, 0.05));
133     }
134 
135     @Test
136     public void testGTestIndependance3() throws Exception {
137         final long[] obs1 = new long[]{
138             190, 149
139         };
140 
141         final long[] obs2 = new long[]{
142             42, 49
143         };
144 
145         final double g = testStatistic.gDataSetsComparison(obs1, obs2);
146         Assert.assertEquals("G test statistic",
147                 2.8187, g, 1E-4);
148         final double p_gti = testStatistic.gTestDataSetsComparison(obs1, obs2);
149         Assert.assertEquals("g-Test p-value", 0.09317325, p_gti, 1E-6);
150 
151         Assert.assertFalse(testStatistic.gTestDataSetsComparison(obs1, obs2, 0.05));
152     }
153 
154     @Test
155     public void testGTestSetsComparisonBadCounts() {
156         long[] observed1 = {10, -1, 12, 10, 15};
157         long[] observed2 = {15, 10, 10, 15, 5};
158         try {
159             testStatistic.gTestDataSetsComparison(
160                     observed1, observed2);
161             Assert.fail("Expecting MathIllegalArgumentException - negative count");
162         } catch (MathIllegalArgumentException ex) {
163             // expected
164         }
165         long[] observed3 = {10, 0, 12, 10, 15};
166         long[] observed4 = {15, 0, 10, 15, 5};
167         try {
168             testStatistic.gTestDataSetsComparison(
169                     observed3, observed4);
170             Assert.fail("Expecting MathIllegalArgumentException - double 0's");
171         } catch (MathIllegalArgumentException ex) {
172             // expected
173         }
174         long[] observed5 = {10, 10, 12, 10, 15};
175         long[] observed6 = {0, 0, 0, 0, 0};
176         try {
177             testStatistic.gTestDataSetsComparison(
178                     observed5, observed6);
179             Assert.fail("Expecting MathIllegalArgumentException - vanishing counts");
180         } catch (MathIllegalArgumentException ex) {
181             // expected
182         }
183     }
184 
185     @Test
186     public void testUnmatchedArrays() {
187         final long[] observed = { 0, 1, 2, 3 };
188         final double[] expected = { 1, 1, 2 };
189         final long[] observed2 = {3, 4};
190         try {
191             testStatistic.gTest(expected, observed);
192             Assert.fail("arrays have different lengths, MathIllegalArgumentException expected");
193         } catch (MathIllegalArgumentException ex) {
194             // expected
195         }
196         try {
197             testStatistic.gTestDataSetsComparison(observed, observed2);
198             Assert.fail("arrays have different lengths, MathIllegalArgumentException expected");
199         } catch (MathIllegalArgumentException ex) {
200             // expected
201         }
202     }
203 
204     @Test
205     public void testNegativeObservedCounts() {
206         final long[] observed = { 0, 1, 2, -3 };
207         final double[] expected = { 1, 1, 2, 3};
208         final long[] observed2 = {3, 4, 5, 0};
209         try {
210             testStatistic.gTest(expected, observed);
211             Assert.fail("negative observed count, MathIllegalArgumentException expected");
212         } catch (MathIllegalArgumentException ex) {
213             // expected
214         }
215         try {
216             testStatistic.gTestDataSetsComparison(observed, observed2);
217             Assert.fail("negative observed count, MathIllegalArgumentException expected");
218         } catch (MathIllegalArgumentException ex) {
219             // expected
220         }
221     }
222 
223     @Test
224     public void testZeroExpectedCounts() {
225         final long[] observed = { 0, 1, 2, -3 };
226         final double[] expected = { 1, 0, 2, 3};
227         try {
228             testStatistic.gTest(expected, observed);
229             Assert.fail("zero expected count, MathIllegalArgumentException expected");
230         } catch (MathIllegalArgumentException ex) {
231             // expected
232         }
233     }
234 
235     @Test
236     public void testBadAlpha() {
237         final long[] observed = { 0, 1, 2, 3 };
238         final double[] expected = { 1, 2, 2, 3};
239         final long[] observed2 = { 0, 2, 2, 3 };
240         try {
241             testStatistic.gTest(expected, observed, 0.8);
242             Assert.fail("zero expected count, MathIllegalArgumentException expected");
243         } catch (MathIllegalArgumentException ex) {
244             // expected
245         }
246         try {
247             testStatistic.gTestDataSetsComparison(observed, observed2, -0.5);
248             Assert.fail("zero expected count, MathIllegalArgumentException expected");
249         } catch (MathIllegalArgumentException ex) {
250             // expected
251         }
252     }
253 
254     @Test
255     public void testScaling() {
256       final long[] observed = {9, 11, 10, 8, 12};
257       final double[] expected1 = {10, 10, 10, 10, 10};
258       final double[] expected2 = {1000, 1000, 1000, 1000, 1000};
259       final double[] expected3 = {1, 1, 1, 1, 1};
260       final double tol = 1E-15;
261       Assert.assertEquals(
262               testStatistic.gTest(expected1, observed),
263               testStatistic.gTest(expected2, observed),
264               tol);
265       Assert.assertEquals(
266               testStatistic.gTest(expected1, observed),
267               testStatistic.gTest(expected3, observed),
268               tol);
269     }
270 
271     @Test
272     public void testRootLogLikelihood() {
273         // positive where k11 is bigger than expected.
274         Assert.assertTrue(testStatistic.rootLogLikelihoodRatio(904, 21060, 1144, 283012) > 0.0);
275 
276         // negative because k11 is lower than expected
277         Assert.assertTrue(testStatistic.rootLogLikelihoodRatio(36, 21928, 60280, 623876) < 0.0);
278 
279         Assert.assertEquals(FastMath.sqrt(2.772589), testStatistic.rootLogLikelihoodRatio(1, 0, 0, 1), 0.000001);
280         Assert.assertEquals(-FastMath.sqrt(2.772589), testStatistic.rootLogLikelihoodRatio(0, 1, 1, 0), 0.000001);
281         Assert.assertEquals(FastMath.sqrt(27.72589), testStatistic.rootLogLikelihoodRatio(10, 0, 0, 10), 0.00001);
282 
283         Assert.assertEquals(FastMath.sqrt(39.33052), testStatistic.rootLogLikelihoodRatio(5, 1995, 0, 100000), 0.00001);
284         Assert.assertEquals(-FastMath.sqrt(39.33052), testStatistic.rootLogLikelihoodRatio(0, 100000, 5, 1995), 0.00001);
285 
286         Assert.assertEquals(FastMath.sqrt(4730.737), testStatistic.rootLogLikelihoodRatio(1000, 1995, 1000, 100000), 0.001);
287         Assert.assertEquals(-FastMath.sqrt(4730.737), testStatistic.rootLogLikelihoodRatio(1000, 100000, 1000, 1995), 0.001);
288 
289         Assert.assertEquals(FastMath.sqrt(5734.343), testStatistic.rootLogLikelihoodRatio(1000, 1000, 1000, 100000), 0.001);
290         Assert.assertEquals(FastMath.sqrt(5714.932), testStatistic.rootLogLikelihoodRatio(1000, 1000, 1000, 99000), 0.001);
291     }
292 }