001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.stat.inference;
017:
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021:
022: /**
023: * Test cases for the ChiSquareTestImpl class.
024: *
025: * @version $Revision: 180065 $ $Date: 2005-06-04 20:58:14 -0700 (Sat, 04 Jun 2005) $
026: */
027:
028: public class ChiSquareTestTest extends TestCase {
029:
030: protected ChiSquareTest testStatistic = new ChiSquareTestImpl();
031:
032: public ChiSquareTestTest(String name) {
033: super (name);
034: }
035:
036: public void setUp() {
037: }
038:
039: public static Test suite() {
040: TestSuite suite = new TestSuite(ChiSquareTestTest.class);
041: suite.setName("TestStatistic Tests");
042: return suite;
043: }
044:
045: public void testChiSquare() throws Exception {
046:
047: // Target values computed using R version 1.8.1
048: // Some assembly required ;-)
049: // Use sum((obs - exp)^2/exp) for the chi-square statistic and
050: // 1 - pchisq(sum((obs - exp)^2/exp), length(obs) - 1) for the p-value
051:
052: long[] observed = { 10, 9, 11 };
053: double[] expected = { 10, 10, 10 };
054: assertEquals("chi-square statistic", 0.2, testStatistic
055: .chiSquare(expected, observed), 10E-12);
056: assertEquals("chi-square p-value", 0.904837418036,
057: testStatistic.chiSquareTest(expected, observed), 1E-10);
058:
059: long[] observed1 = { 500, 623, 72, 70, 31 };
060: double[] expected1 = { 485, 541, 82, 61, 37 };
061: assertEquals("chi-square test statistic", 16.4131070362,
062: testStatistic.chiSquare(expected1, observed1), 1E-10);
063: assertEquals("chi-square p-value", 0.002512096, testStatistic
064: .chiSquareTest(expected1, observed1), 1E-9);
065: assertTrue("chi-square test reject", testStatistic
066: .chiSquareTest(expected1, observed1, 0.003));
067: assertTrue("chi-square test accept", !testStatistic
068: .chiSquareTest(expected1, observed1, 0.002));
069:
070: try {
071: testStatistic.chiSquareTest(expected1, observed1, 95);
072: fail("alpha out of range, IllegalArgumentException expected");
073: } catch (IllegalArgumentException ex) {
074: // expected
075: }
076:
077: long[] tooShortObs = { 0 };
078: double[] tooShortEx = { 1 };
079: try {
080: testStatistic.chiSquare(tooShortEx, tooShortObs);
081: fail("arguments too short, IllegalArgumentException expected");
082: } catch (IllegalArgumentException ex) {
083: // expected
084: }
085:
086: // unmatched arrays
087: long[] unMatchedObs = { 0, 1, 2, 3 };
088: double[] unMatchedEx = { 1, 1, 2 };
089: try {
090: testStatistic.chiSquare(unMatchedEx, unMatchedObs);
091: fail("arrays have different lengths, IllegalArgumentException expected");
092: } catch (IllegalArgumentException ex) {
093: // expected
094: }
095:
096: // 0 expected count
097: expected[0] = 0;
098: try {
099: testStatistic.chiSquareTest(expected, observed, .01);
100: fail("bad expected count, IllegalArgumentException expected");
101: } catch (IllegalArgumentException ex) {
102: // expected
103: }
104:
105: // negative observed count
106: expected[0] = 1;
107: observed[0] = -1;
108: try {
109: testStatistic.chiSquareTest(expected, observed, .01);
110: fail("bad expected count, IllegalArgumentException expected");
111: } catch (IllegalArgumentException ex) {
112: // expected
113: }
114:
115: }
116:
117: public void testChiSquareIndependence() throws Exception {
118:
119: // Target values computed using R version 1.8.1
120:
121: long[][] counts = { { 40, 22, 43 }, { 91, 21, 28 },
122: { 60, 10, 22 } };
123: assertEquals("chi-square test statistic", 22.709027688,
124: testStatistic.chiSquare(counts), 1E-9);
125: assertEquals("chi-square p-value", 0.000144751460134,
126: testStatistic.chiSquareTest(counts), 1E-9);
127: assertTrue("chi-square test reject", testStatistic
128: .chiSquareTest(counts, 0.0002));
129: assertTrue("chi-square test accept", !testStatistic
130: .chiSquareTest(counts, 0.0001));
131:
132: long[][] counts2 = { { 10, 15 }, { 30, 40 }, { 60, 90 } };
133: assertEquals("chi-square test statistic", 0.168965517241,
134: testStatistic.chiSquare(counts2), 1E-9);
135: assertEquals("chi-square p-value", 0.918987499852,
136: testStatistic.chiSquareTest(counts2), 1E-9);
137: assertTrue("chi-square test accept", !testStatistic
138: .chiSquareTest(counts2, 0.1));
139:
140: // ragged input array
141: long[][] counts3 = { { 40, 22, 43 }, { 91, 21, 28 }, { 60, 10 } };
142: try {
143: testStatistic.chiSquare(counts3);
144: fail("Expecting IllegalArgumentException");
145: } catch (IllegalArgumentException ex) {
146: // expected
147: }
148:
149: // insufficient data
150: long[][] counts4 = { { 40, 22, 43 } };
151: try {
152: testStatistic.chiSquare(counts4);
153: fail("Expecting IllegalArgumentException");
154: } catch (IllegalArgumentException ex) {
155: // expected
156: }
157: long[][] counts5 = { { 40 }, { 40 }, { 30 }, { 10 } };
158: try {
159: testStatistic.chiSquare(counts5);
160: fail("Expecting IllegalArgumentException");
161: } catch (IllegalArgumentException ex) {
162: // expected
163: }
164:
165: // negative counts
166: long[][] counts6 = { { 10, -2 }, { 30, 40 }, { 60, 90 } };
167: try {
168: testStatistic.chiSquare(counts6);
169: fail("Expecting IllegalArgumentException");
170: } catch (IllegalArgumentException ex) {
171: // expected
172: }
173:
174: // bad alpha
175: try {
176: testStatistic.chiSquareTest(counts, 0);
177: fail("Expecting IllegalArgumentException");
178: } catch (IllegalArgumentException ex) {
179: // expected
180: }
181: }
182:
183: public void testChiSquareLargeTestStatistic() throws Exception {
184: double[] exp = new double[] { 3389119.5, 649136.6, 285745.4,
185: 25357364.76, 11291189.78, 543628.0, 232921.0, 437665.75 };
186:
187: long[] obs = new long[] { 2372383, 584222, 257170, 17750155,
188: 7903832, 489265, 209628, 393899 };
189: org.apache.commons.math.stat.inference.ChiSquareTestImpl csti = new org.apache.commons.math.stat.inference.ChiSquareTestImpl();
190: double cst = csti.chiSquareTest(exp, obs);
191: assertEquals("chi-square p-value", 0.0, cst, 1E-3);
192: assertEquals("chi-square test statistic", 3624883.342907764,
193: testStatistic.chiSquare(exp, obs), 1E-9);
194: }
195:
196: /** Contingency table containing zeros - PR # 32531 */
197: public void testChiSquareZeroCount() throws Exception {
198: // Target values computed using R version 1.8.1
199: long[][] counts = { { 40, 0, 4 }, { 91, 1, 2 }, { 60, 2, 0 } };
200: assertEquals("chi-square test statistic", 9.67444662263,
201: testStatistic.chiSquare(counts), 1E-9);
202: assertEquals("chi-square p-value", 0.0462835770603,
203: testStatistic.chiSquareTest(counts), 1E-9);
204: }
205: }
|