1 16 package org.apache.commons.math.stat.inference; 17 18 import junit.framework.Test; 19 import junit.framework.TestCase; 20 import junit.framework.TestSuite; 21 22 27 28 public class ChiSquareTestTest extends TestCase { 29 30 protected ChiSquareTest testStatistic = new ChiSquareTestImpl(); 31 32 public ChiSquareTestTest(String name) { 33 super(name); 34 } 35 36 public void setUp() { 37 } 38 39 public static Test suite() { 40 TestSuite suite = new TestSuite(ChiSquareTestTest.class); 41 suite.setName("TestStatistic Tests"); 42 return suite; 43 } 44 45 public void testChiSquare() throws Exception { 46 47 52 long[] observed = {10, 9, 11}; 53 double[] expected = {10, 10, 10}; 54 assertEquals("chi-square statistic", 0.2, testStatistic.chiSquare(expected, observed), 10E-12); 55 assertEquals("chi-square p-value", 0.904837418036, testStatistic.chiSquareTest(expected, observed), 1E-10); 56 57 long[] observed1 = { 500, 623, 72, 70, 31 }; 58 double[] expected1 = { 485, 541, 82, 61, 37 }; 59 assertEquals( "chi-square test statistic", 16.4131070362, testStatistic.chiSquare(expected1, observed1), 1E-10); 60 assertEquals("chi-square p-value", 0.002512096, testStatistic.chiSquareTest(expected1, observed1), 1E-9); 61 assertTrue("chi-square test reject", testStatistic.chiSquareTest(expected1, observed1, 0.003)); 62 assertTrue("chi-square test accept", !testStatistic.chiSquareTest(expected1, observed1, 0.002)); 63 64 try { 65 testStatistic.chiSquareTest(expected1, observed1, 95); 66 fail("alpha out of range, IllegalArgumentException expected"); 67 } catch (IllegalArgumentException ex) { 68 } 70 71 long[] tooShortObs = { 0 }; 72 double[] tooShortEx = { 1 }; 73 try { 74 testStatistic.chiSquare(tooShortEx, tooShortObs); 75 fail("arguments too short, IllegalArgumentException expected"); 76 } catch (IllegalArgumentException ex) { 77 } 79 80 long[] unMatchedObs = { 0, 1, 2, 3 }; 82 double[] unMatchedEx = { 1, 1, 2 }; 83 try { 84 testStatistic.chiSquare(unMatchedEx, unMatchedObs); 85 fail("arrays have different lengths, IllegalArgumentException expected"); 86 } catch (IllegalArgumentException ex) { 87 } 89 90 expected[0] = 0; 92 try { 93 testStatistic.chiSquareTest(expected, observed, .01); 94 fail("bad expected count, IllegalArgumentException expected"); 95 } catch (IllegalArgumentException ex) { 96 } 98 99 expected[0] = 1; 101 observed[0] = -1; 102 try { 103 testStatistic.chiSquareTest(expected, observed, .01); 104 fail("bad expected count, IllegalArgumentException expected"); 105 } catch (IllegalArgumentException ex) { 106 } 108 109 } 110 111 public void testChiSquareIndependence() throws Exception { 112 113 115 long[][] counts = { {40, 22, 43}, {91, 21, 28}, {60, 10, 22}}; 116 assertEquals( "chi-square test statistic", 22.709027688, testStatistic.chiSquare(counts), 1E-9); 117 assertEquals("chi-square p-value", 0.000144751460134, testStatistic.chiSquareTest(counts), 1E-9); 118 assertTrue("chi-square test reject", testStatistic.chiSquareTest(counts, 0.0002)); 119 assertTrue("chi-square test accept", !testStatistic.chiSquareTest(counts, 0.0001)); 120 121 long[][] counts2 = {{10, 15}, {30, 40}, {60, 90} }; 122 assertEquals( "chi-square test statistic", 0.168965517241, testStatistic.chiSquare(counts2), 1E-9); 123 assertEquals("chi-square p-value",0.918987499852, testStatistic.chiSquareTest(counts2), 1E-9); 124 assertTrue("chi-square test accept", !testStatistic.chiSquareTest(counts2, 0.1)); 125 126 long[][] counts3 = { {40, 22, 43}, {91, 21, 28}, {60, 10}}; 128 try { 129 testStatistic.chiSquare(counts3); 130 fail("Expecting IllegalArgumentException"); 131 } catch (IllegalArgumentException ex) { 132 } 134 135 long[][] counts4 = {{40, 22, 43}}; 137 try { 138 testStatistic.chiSquare(counts4); 139 fail("Expecting IllegalArgumentException"); 140 } catch (IllegalArgumentException ex) { 141 } 143 long[][] counts5 = {{40}, {40}, {30}, {10}}; 144 try { 145 testStatistic.chiSquare(counts5); 146 fail("Expecting IllegalArgumentException"); 147 } catch (IllegalArgumentException ex) { 148 } 150 151 long[][] counts6 = {{10, -2}, {30, 40}, {60, 90} }; 153 try { 154 testStatistic.chiSquare(counts6); 155 fail("Expecting IllegalArgumentException"); 156 } catch (IllegalArgumentException ex) { 157 } 159 160 try { 162 testStatistic.chiSquareTest(counts, 0); 163 fail("Expecting IllegalArgumentException"); 164 } catch (IllegalArgumentException ex) { 165 } 167 } 168 169 public void testChiSquareLargeTestStatistic() throws Exception { 170 double[] exp = new double[] { 171 3389119.5, 649136.6, 285745.4, 25357364.76, 11291189.78, 543628.0, 172 232921.0, 437665.75 173 }; 174 175 long[] obs = new long[] { 176 2372383, 584222, 257170, 17750155, 7903832, 489265, 209628, 393899 177 }; 178 org.apache.commons.math.stat.inference.ChiSquareTestImpl csti = 179 new org.apache.commons.math.stat.inference.ChiSquareTestImpl(); 180 double cst = csti.chiSquareTest(exp, obs); 181 assertEquals("chi-square p-value", 0.0, cst, 1E-3); 182 assertEquals( "chi-square test statistic", 183 3624883.342907764, testStatistic.chiSquare(exp, obs), 1E-9); 184 } 185 186 187 public void testChiSquareZeroCount() throws Exception { 188 long[][] counts = { {40, 0, 4}, {91, 1, 2}, {60, 2, 0}}; 190 assertEquals( "chi-square test statistic", 9.67444662263, 191 testStatistic.chiSquare(counts), 1E-9); 192 assertEquals("chi-square p-value", 0.0462835770603, 193 testStatistic.chiSquareTest(counts), 1E-9); 194 } 195 } 196 | Popular Tags |