blob: 8bcc59264487602026070885154eeb2ee818eced [file] [log] [blame]
/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.stat.inference;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test cases for the ChiSquareTestImpl class.
*
* @version $Revision$ $Date$
*/
public class ChiSquareTestTest extends TestCase {
protected ChiSquareTest testStatistic = new ChiSquareTestImpl();
public ChiSquareTestTest(String name) {
super(name);
}
public void setUp() {
}
public static Test suite() {
TestSuite suite = new TestSuite(ChiSquareTestTest.class);
suite.setName("TestStatistic Tests");
return suite;
}
public void testChiSquare() throws Exception {
// Target values computed using R version 1.8.1
// Some assembly required ;-)
// Use sum((obs - exp)^2/exp) for the chi-square statistic and
// 1 - pchisq(sum((obs - exp)^2/exp), length(obs) - 1) for the p-value
long[] observed = {10, 9, 11};
double[] expected = {10, 10, 10};
assertEquals("chi-square statistic", 0.2, testStatistic.chiSquare(expected, observed), 10E-12);
assertEquals("chi-square p-value", 0.904837418036, testStatistic.chiSquareTest(expected, observed), 1E-10);
long[] observed1 = { 500, 623, 72, 70, 31 };
double[] expected1 = { 485, 541, 82, 61, 37 };
assertEquals( "chi-square test statistic", 16.4131070362, testStatistic.chiSquare(expected1, observed1), 1E-10);
assertEquals("chi-square p-value", 0.002512096, testStatistic.chiSquareTest(expected1, observed1), 1E-9);
assertTrue("chi-square test reject", testStatistic.chiSquareTest(expected1, observed1, 0.003));
assertTrue("chi-square test accept", !testStatistic.chiSquareTest(expected1, observed1, 0.002));
try {
testStatistic.chiSquareTest(expected1, observed1, 95);
fail("alpha out of range, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
// expected
}
long[] tooShortObs = { 0 };
double[] tooShortEx = { 1 };
try {
testStatistic.chiSquare(tooShortEx, tooShortObs);
fail("arguments too short, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
// expected
}
// unmatched arrays
long[] unMatchedObs = { 0, 1, 2, 3 };
double[] unMatchedEx = { 1, 1, 2 };
try {
testStatistic.chiSquare(unMatchedEx, unMatchedObs);
fail("arrays have different lengths, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
// expected
}
// 0 expected count
expected[0] = 0;
try {
testStatistic.chiSquareTest(expected, observed, .01);
fail("bad expected count, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
// expected
}
// negative observed count
expected[0] = 1;
observed[0] = -1;
try {
testStatistic.chiSquareTest(expected, observed, .01);
fail("bad expected count, IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testChiSquareIndependence() throws Exception {
// Target values computed using R version 1.8.1
long[][] counts = { {40, 22, 43}, {91, 21, 28}, {60, 10, 22}};
assertEquals( "chi-square test statistic", 22.709027688, testStatistic.chiSquare(counts), 1E-9);
assertEquals("chi-square p-value", 0.000144751460134, testStatistic.chiSquareTest(counts), 1E-9);
assertTrue("chi-square test reject", testStatistic.chiSquareTest(counts, 0.0002));
assertTrue("chi-square test accept", !testStatistic.chiSquareTest(counts, 0.0001));
long[][] counts2 = {{10, 15}, {30, 40}, {60, 90} };
assertEquals( "chi-square test statistic", 0.168965517241, testStatistic.chiSquare(counts2), 1E-9);
assertEquals("chi-square p-value",0.918987499852, testStatistic.chiSquareTest(counts2), 1E-9);
assertTrue("chi-square test accept", !testStatistic.chiSquareTest(counts2, 0.1));
// ragged input array
long[][] counts3 = { {40, 22, 43}, {91, 21, 28}, {60, 10}};
try {
testStatistic.chiSquare(counts3);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
// insufficient data
long[][] counts4 = {{40, 22, 43}};
try {
testStatistic.chiSquare(counts4);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
long[][] counts5 = {{40}, {40}, {30}, {10}};
try {
testStatistic.chiSquare(counts5);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
// negative counts
long[][] counts6 = {{10, -2}, {30, 40}, {60, 90} };
try {
testStatistic.chiSquare(counts6);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
// bad alpha
try {
testStatistic.chiSquareTest(counts, 0);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testChiSquareLargeTestStatistic() throws Exception {
double[] exp = new double[] {
3389119.5, 649136.6, 285745.4, 25357364.76, 11291189.78, 543628.0,
232921.0, 437665.75
};
long[] obs = new long[] {
2372383, 584222, 257170, 17750155, 7903832, 489265, 209628, 393899
};
org.apache.commons.math.stat.inference.ChiSquareTestImpl csti =
new org.apache.commons.math.stat.inference.ChiSquareTestImpl();
double cst = csti.chiSquareTest(exp, obs);
assertEquals("chi-square p-value", 0.0, cst, 1E-3);
assertEquals( "chi-square test statistic",
3624883.342907764, testStatistic.chiSquare(exp, obs), 1E-9);
}
/** Contingency table containing zeros - PR # 32531 */
public void testChiSquareZeroCount() throws Exception {
// Target values computed using R version 1.8.1
long[][] counts = { {40, 0, 4}, {91, 1, 2}, {60, 2, 0}};
assertEquals( "chi-square test statistic", 9.67444662263,
testStatistic.chiSquare(counts), 1E-9);
assertEquals("chi-square p-value", 0.0462835770603,
testStatistic.chiSquareTest(counts), 1E-9);
}
}