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