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