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