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 import org.hipparchus.exception.MathIllegalArgumentException;
25 import org.junit.Assert;
26 import org.junit.Test;
27
28
29
30
31
32
33
34 public class ChiSquareTestTest {
35
36 protected ChiSquareTest testStatistic = new ChiSquareTest();
37
38 @Test
39 public void testChiSquare() {
40
41
42
43
44
45
46 long[] observed = {10, 9, 11};
47 double[] expected = {10, 10, 10};
48 Assert.assertEquals("chi-square statistic", 0.2, testStatistic.chiSquare(expected, observed), 10E-12);
49 Assert.assertEquals("chi-square p-value", 0.904837418036, testStatistic.chiSquareTest(expected, observed), 1E-10);
50
51 long[] observed1 = { 500, 623, 72, 70, 31 };
52 double[] expected1 = { 485, 541, 82, 61, 37 };
53 Assert.assertEquals( "chi-square test statistic", 9.023307936427388, testStatistic.chiSquare(expected1, observed1), 1E-10);
54 Assert.assertEquals("chi-square p-value", 0.06051952647453607, testStatistic.chiSquareTest(expected1, observed1), 1E-9);
55 Assert.assertTrue("chi-square test reject", testStatistic.chiSquareTest(expected1, observed1, 0.08));
56 Assert.assertTrue("chi-square test accept", !testStatistic.chiSquareTest(expected1, observed1, 0.05));
57
58 try {
59 testStatistic.chiSquareTest(expected1, observed1, 95);
60 Assert.fail("alpha out of range, MathIllegalArgumentException expected");
61 } catch (MathIllegalArgumentException ex) {
62
63 }
64
65 long[] tooShortObs = { 0 };
66 double[] tooShortEx = { 1 };
67 try {
68 testStatistic.chiSquare(tooShortEx, tooShortObs);
69 Assert.fail("arguments too short, MathIllegalArgumentException expected");
70 } catch (MathIllegalArgumentException ex) {
71
72 }
73
74
75 long[] unMatchedObs = { 0, 1, 2, 3 };
76 double[] unMatchedEx = { 1, 1, 2 };
77 try {
78 testStatistic.chiSquare(unMatchedEx, unMatchedObs);
79 Assert.fail("arrays have different lengths, MathIllegalArgumentException expected");
80 } catch (MathIllegalArgumentException ex) {
81
82 }
83
84
85 expected[0] = 0;
86 try {
87 testStatistic.chiSquareTest(expected, observed, .01);
88 Assert.fail("bad expected count, MathIllegalArgumentException expected");
89 } catch (MathIllegalArgumentException ex) {
90
91 }
92
93
94 expected[0] = 1;
95 observed[0] = -1;
96 try {
97 testStatistic.chiSquareTest(expected, observed, .01);
98 Assert.fail("bad expected count, MathIllegalArgumentException expected");
99 } catch (MathIllegalArgumentException ex) {
100
101 }
102
103 }
104
105 @Test
106 public void testChiSquareIndependence() {
107
108
109
110 long[][] counts = { {40, 22, 43}, {91, 21, 28}, {60, 10, 22}};
111 Assert.assertEquals( "chi-square test statistic", 22.709027688, testStatistic.chiSquare(counts), 1E-9);
112 Assert.assertEquals("chi-square p-value", 0.000144751460134, testStatistic.chiSquareTest(counts), 1E-9);
113 Assert.assertTrue("chi-square test reject", testStatistic.chiSquareTest(counts, 0.0002));
114 Assert.assertTrue("chi-square test accept", !testStatistic.chiSquareTest(counts, 0.0001));
115
116 long[][] counts2 = {{10, 15}, {30, 40}, {60, 90} };
117 Assert.assertEquals( "chi-square test statistic", 0.168965517241, testStatistic.chiSquare(counts2), 1E-9);
118 Assert.assertEquals("chi-square p-value",0.918987499852, testStatistic.chiSquareTest(counts2), 1E-9);
119 Assert.assertTrue("chi-square test accept", !testStatistic.chiSquareTest(counts2, 0.1));
120
121
122 long[][] counts3 = { {40, 22, 43}, {91, 21, 28}, {60, 10}};
123 try {
124 testStatistic.chiSquare(counts3);
125 Assert.fail("Expecting MathIllegalArgumentException");
126 } catch (MathIllegalArgumentException ex) {
127
128 }
129
130
131 long[][] counts4 = {{40, 22, 43}};
132 try {
133 testStatistic.chiSquare(counts4);
134 Assert.fail("Expecting MathIllegalArgumentException");
135 } catch (MathIllegalArgumentException ex) {
136
137 }
138 long[][] counts5 = {{40}, {40}, {30}, {10}};
139 try {
140 testStatistic.chiSquare(counts5);
141 Assert.fail("Expecting MathIllegalArgumentException");
142 } catch (MathIllegalArgumentException ex) {
143
144 }
145
146
147 long[][] counts6 = {{10, -2}, {30, 40}, {60, 90} };
148 try {
149 testStatistic.chiSquare(counts6);
150 Assert.fail("Expecting MathIllegalArgumentException");
151 } catch (MathIllegalArgumentException ex) {
152
153 }
154
155
156 try {
157 testStatistic.chiSquareTest(counts, 0);
158 Assert.fail("Expecting MathIllegalArgumentException");
159 } catch (MathIllegalArgumentException ex) {
160
161 }
162 }
163
164 @Test
165 public void testChiSquareLargeTestStatistic() {
166 double[] exp = new double[] {
167 3389119.5, 649136.6, 285745.4, 25357364.76, 11291189.78, 543628.0,
168 232921.0, 437665.75
169 };
170
171 long[] obs = new long[] {
172 2372383, 584222, 257170, 17750155, 7903832, 489265, 209628, 393899
173 };
174 org.hipparchus.stat.inference.ChiSquareTest csti =
175 new org.hipparchus.stat.inference.ChiSquareTest();
176 double cst = csti.chiSquareTest(exp, obs);
177 Assert.assertEquals("chi-square p-value", 0.0, cst, 1E-3);
178 Assert.assertEquals( "chi-square test statistic",
179 114875.90421929007, testStatistic.chiSquare(exp, obs), 1E-9);
180 }
181
182
183 @Test
184 public void testChiSquareZeroCount() {
185
186 long[][] counts = { {40, 0, 4}, {91, 1, 2}, {60, 2, 0}};
187 Assert.assertEquals( "chi-square test statistic", 9.67444662263,
188 testStatistic.chiSquare(counts), 1E-9);
189 Assert.assertEquals("chi-square p-value", 0.0462835770603,
190 testStatistic.chiSquareTest(counts), 1E-9);
191 }
192
193
194 @Test
195 public void testChiSquareDataSetsComparisonEqualCounts()
196 {
197 long[] observed1 = {10, 12, 12, 10};
198 long[] observed2 = {5, 15, 14, 10};
199 Assert.assertEquals("chi-square p value", 0.541096,
200 testStatistic.chiSquareTestDataSetsComparison(
201 observed1, observed2), 1E-6);
202 Assert.assertEquals("chi-square test statistic", 2.153846,
203 testStatistic.chiSquareDataSetsComparison(
204 observed1, observed2), 1E-6);
205 Assert.assertFalse("chi-square test result",
206 testStatistic.chiSquareTestDataSetsComparison(
207 observed1, observed2, 0.4));
208 }
209
210
211 @Test
212 public void testChiSquareDataSetsComparisonUnEqualCounts()
213 {
214 long[] observed1 = {10, 12, 12, 10, 15};
215 long[] observed2 = {15, 10, 10, 15, 5};
216 Assert.assertEquals("chi-square p value", 0.124115,
217 testStatistic.chiSquareTestDataSetsComparison(
218 observed1, observed2), 1E-6);
219 Assert.assertEquals("chi-square test statistic", 7.232189,
220 testStatistic.chiSquareDataSetsComparison(
221 observed1, observed2), 1E-6);
222 Assert.assertTrue("chi-square test result",
223 testStatistic.chiSquareTestDataSetsComparison(
224 observed1, observed2, 0.13));
225 Assert.assertFalse("chi-square test result",
226 testStatistic.chiSquareTestDataSetsComparison(
227 observed1, observed2, 0.12));
228 }
229
230 @Test
231 public void testChiSquareDataSetsComparisonBadCounts()
232 {
233 long[] observed1 = {10, -1, 12, 10, 15};
234 long[] observed2 = {15, 10, 10, 15, 5};
235 try {
236 testStatistic.chiSquareTestDataSetsComparison(
237 observed1, observed2);
238 Assert.fail("Expecting MathIllegalArgumentException - negative count");
239 } catch (MathIllegalArgumentException ex) {
240
241 }
242 long[] observed3 = {10, 0, 12, 10, 15};
243 long[] observed4 = {15, 0, 10, 15, 5};
244 try {
245 testStatistic.chiSquareTestDataSetsComparison(
246 observed3, observed4);
247 Assert.fail("Expecting MathIllegalArgumentException - double 0's");
248 } catch (MathIllegalArgumentException ex) {
249
250 }
251 long[] observed5 = {10, 10, 12, 10, 15};
252 long[] observed6 = {0, 0, 0, 0, 0};
253 try {
254 testStatistic.chiSquareTestDataSetsComparison(
255 observed5, observed6);
256 Assert.fail("Expecting MathIllegalArgumentException - vanishing counts");
257 } catch (MathIllegalArgumentException ex) {
258
259 }
260 }
261 }