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 org.apache.commons.math.MathException;
20 import org.apache.commons.math.distribution.ChiSquaredDistribution;
21 import org.apache.commons.math.distribution.ChiSquaredDistributionImpl;
22 import org.apache.commons.math.distribution.DistributionFactory;
23
24
25
26
27
28
29
30 public class ChiSquareTestImpl implements UnknownDistributionChiSquareTest {
31
32
33 private ChiSquaredDistribution distribution;
34
35
36
37
38 public ChiSquareTestImpl() {
39 this(new ChiSquaredDistributionImpl(1.0));
40 }
41
42
43
44
45
46
47
48 public ChiSquareTestImpl(ChiSquaredDistribution x) {
49 super();
50 setDistribution(x);
51 }
52
53
54
55
56
57
58
59
60
61
62
63
64 public double chiSquare(double[] expected, long[] observed)
65 throws IllegalArgumentException {
66 if ((expected.length < 2) || (expected.length != observed.length)) {
67 throw new IllegalArgumentException(
68 "observed, expected array lengths incorrect");
69 }
70 if (!isPositive(expected) || !isNonNegative(observed)) {
71 throw new IllegalArgumentException(
72 "observed counts must be non-negative and expected counts must be postive");
73 }
74 double sumExpected = 0d;
75 double sumObserved = 0d;
76 for (int i = 0; i < observed.length; i++) {
77 sumExpected += expected[i];
78 sumObserved += observed[i];
79 }
80 double ratio = 1.0d;
81 boolean rescale = false;
82 if (Math.abs(sumExpected - sumObserved) > 10E-6) {
83 ratio = sumObserved / sumExpected;
84 rescale = true;
85 }
86 double sumSq = 0.0d;
87 double dev = 0.0d;
88 for (int i = 0; i < observed.length; i++) {
89 if (rescale) {
90 dev = ((double) observed[i] - ratio * expected[i]);
91 sumSq += dev * dev / (ratio * expected[i]);
92 } else {
93 dev = ((double) observed[i] - expected[i]);
94 sumSq += dev * dev / expected[i];
95 }
96 }
97 return sumSq;
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112 public double chiSquareTest(double[] expected, long[] observed)
113 throws IllegalArgumentException, MathException {
114 distribution.setDegreesOfFreedom(expected.length - 1.0);
115 return 1.0 - distribution.cumulativeProbability(
116 chiSquare(expected, observed));
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public boolean chiSquareTest(double[] expected, long[] observed,
134 double alpha) throws IllegalArgumentException, MathException {
135 if ((alpha <= 0) || (alpha > 0.5)) {
136 throw new IllegalArgumentException(
137 "bad significance level: " + alpha);
138 }
139 return (chiSquareTest(expected, observed) < alpha);
140 }
141
142
143
144
145
146
147 public double chiSquare(long[][] counts) throws IllegalArgumentException {
148
149 checkArray(counts);
150 int nRows = counts.length;
151 int nCols = counts[0].length;
152
153
154 double[] rowSum = new double[nRows];
155 double[] colSum = new double[nCols];
156 double total = 0.0d;
157 for (int row = 0; row < nRows; row++) {
158 for (int col = 0; col < nCols; col++) {
159 rowSum[row] += (double) counts[row][col];
160 colSum[col] += (double) counts[row][col];
161 total += (double) counts[row][col];
162 }
163 }
164
165
166 double sumSq = 0.0d;
167 double expected = 0.0d;
168 for (int row = 0; row < nRows; row++) {
169 for (int col = 0; col < nCols; col++) {
170 expected = (rowSum[row] * colSum[col]) / total;
171 sumSq += (((double) counts[row][col] - expected) *
172 ((double) counts[row][col] - expected)) / expected;
173 }
174 }
175 return sumSq;
176 }
177
178
179
180
181
182
183
184 public double chiSquareTest(long[][] counts)
185 throws IllegalArgumentException, MathException {
186 checkArray(counts);
187 double df = ((double) counts.length -1) * ((double) counts[0].length - 1);
188 distribution.setDegreesOfFreedom(df);
189 return 1 - distribution.cumulativeProbability(chiSquare(counts));
190 }
191
192
193
194
195
196
197
198
199
200 public boolean chiSquareTest(long[][] counts, double alpha)
201 throws IllegalArgumentException, MathException {
202 if ((alpha <= 0) || (alpha > 0.5)) {
203 throw new IllegalArgumentException("bad significance level: " + alpha);
204 }
205 return (chiSquareTest(counts) < alpha);
206 }
207
208
209
210
211
212
213
214
215 public double chiSquareDataSetsComparison(long[] observed1, long[] observed2)
216 throws IllegalArgumentException {
217
218
219 if ((observed1.length < 2) || (observed1.length != observed2.length)) {
220 throw new IllegalArgumentException(
221 "oberved1, observed2 array lengths incorrect");
222 }
223
224 if (!isNonNegative(observed1) || !isNonNegative(observed2)) {
225 throw new IllegalArgumentException(
226 "observed counts must be non-negative");
227 }
228
229 long countSum1 = 0;
230 long countSum2 = 0;
231 boolean unequalCounts = false;
232 double weight = 0.0;
233 for (int i = 0; i < observed1.length; i++) {
234 countSum1 += observed1[i];
235 countSum2 += observed2[i];
236 }
237
238 if (countSum1 * countSum2 == 0) {
239 throw new IllegalArgumentException(
240 "observed counts cannot all be 0");
241 }
242
243 unequalCounts = (countSum1 != countSum2);
244 if (unequalCounts) {
245 weight = Math.sqrt((double) countSum1 / (double) countSum2);
246 }
247
248 double sumSq = 0.0d;
249 double dev = 0.0d;
250 double obs1 = 0.0d;
251 double obs2 = 0.0d;
252 for (int i = 0; i < observed1.length; i++) {
253 if (observed1[i] == 0 && observed2[i] == 0) {
254 throw new IllegalArgumentException(
255 "observed counts must not both be zero");
256 } else {
257 obs1 = (double) observed1[i];
258 obs2 = (double) observed2[i];
259 if (unequalCounts) {
260 dev = obs1/weight - obs2 * weight;
261 } else {
262 dev = obs1 - obs2;
263 }
264 sumSq += (dev * dev) / (obs1 + obs2);
265 }
266 }
267 return sumSq;
268 }
269
270
271
272
273
274
275
276
277
278 public double chiSquareTestDataSetsComparison(long[] observed1, long[] observed2)
279 throws IllegalArgumentException, MathException {
280 distribution.setDegreesOfFreedom((double) observed1.length - 1);
281 return 1 - distribution.cumulativeProbability(
282 chiSquareDataSetsComparison(observed1, observed2));
283 }
284
285
286
287
288
289
290
291
292
293
294
295 public boolean chiSquareTestDataSetsComparison(long[] observed1, long[] observed2,
296 double alpha) throws IllegalArgumentException, MathException {
297 if ((alpha <= 0) || (alpha > 0.5)) {
298 throw new IllegalArgumentException(
299 "bad significance level: " + alpha);
300 }
301 return (chiSquareTestDataSetsComparison(observed1, observed2) < alpha);
302 }
303
304
305
306
307
308
309
310
311
312 private void checkArray(long[][] in) throws IllegalArgumentException {
313
314 if (in.length < 2) {
315 throw new IllegalArgumentException("Input table must have at least two rows");
316 }
317
318 if (in[0].length < 2) {
319 throw new IllegalArgumentException("Input table must have at least two columns");
320 }
321
322 if (!isRectangular(in)) {
323 throw new IllegalArgumentException("Input table must be rectangular");
324 }
325
326 if (!isNonNegative(in)) {
327 throw new IllegalArgumentException("All entries in input 2-way table must be non-negative");
328 }
329
330 }
331
332
333
334
335
336
337
338 protected DistributionFactory getDistributionFactory() {
339 return DistributionFactory.newInstance();
340 }
341
342
343
344
345
346
347
348
349
350
351
352 private boolean isRectangular(long[][] in) {
353 for (int i = 1; i < in.length; i++) {
354 if (in[i].length != in[0].length) {
355 return false;
356 }
357 }
358 return true;
359 }
360
361
362
363
364
365
366
367
368
369 private boolean isPositive(double[] in) {
370 for (int i = 0; i < in.length; i ++) {
371 if (in[i] <= 0) {
372 return false;
373 }
374 }
375 return true;
376 }
377
378
379
380
381
382
383
384
385
386 private boolean isNonNegative(long[] in) {
387 for (int i = 0; i < in.length; i ++) {
388 if (in[i] < 0) {
389 return false;
390 }
391 }
392 return true;
393 }
394
395
396
397
398
399
400
401
402
403 private boolean isNonNegative(long[][] in) {
404 for (int i = 0; i < in.length; i ++) {
405 for (int j = 0; j < in[i].length; j++) {
406 if (in[i][j] < 0) {
407 return false;
408 }
409 }
410 }
411 return true;
412 }
413
414
415
416
417
418
419
420
421 public void setDistribution(ChiSquaredDistribution value) {
422 distribution = value;
423 }
424 }