View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.stat.inference;
23  
24  import org.hipparchus.exception.MathIllegalArgumentException;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  
29  /**
30   * Test cases for the ChiSquareTestImpl class.
31   *
32   */
33  
34  public class ChiSquareTestTest {
35  
36      protected ChiSquareTest testStatistic = new ChiSquareTest();
37  
38      @Test
39      public void testChiSquare() {
40  
41          // Target values computed using R version 1.8.1
42          // Some assembly required ;-)
43          //      Use sum((obs - exp)^2/exp) for the chi-square statistic and
44          //      1 - pchisq(sum((obs - exp)^2/exp), length(obs) - 1) for the p-value
45  
46          long[] observed = {10, 9, 11};
47          double[] expected = {10, 10, 10};
48          Assert.assertEquals("chi-square statistic", 0.2,  testStatistic.chiSquare(expected, observed), 10E-12);
49          Assert.assertEquals("chi-square p-value", 0.904837418036, testStatistic.chiSquareTest(expected, observed), 1E-10);
50  
51          long[] observed1 = { 500, 623, 72, 70, 31 };
52          double[] expected1 = { 485, 541, 82, 61, 37 };
53          Assert.assertEquals( "chi-square test statistic", 9.023307936427388, testStatistic.chiSquare(expected1, observed1), 1E-10);
54          Assert.assertEquals("chi-square p-value", 0.06051952647453607, testStatistic.chiSquareTest(expected1, observed1), 1E-9);
55          Assert.assertTrue("chi-square test reject", testStatistic.chiSquareTest(expected1, observed1, 0.08));
56          Assert.assertTrue("chi-square test accept", !testStatistic.chiSquareTest(expected1, observed1, 0.05));
57  
58          try {
59              testStatistic.chiSquareTest(expected1, observed1, 95);
60              Assert.fail("alpha out of range, MathIllegalArgumentException expected");
61          } catch (MathIllegalArgumentException ex) {
62              // expected
63          }
64  
65          long[] tooShortObs = { 0 };
66          double[] tooShortEx = { 1 };
67          try {
68              testStatistic.chiSquare(tooShortEx, tooShortObs);
69              Assert.fail("arguments too short, MathIllegalArgumentException expected");
70          } catch (MathIllegalArgumentException ex) {
71              // expected
72          }
73  
74          // unmatched arrays
75          long[] unMatchedObs = { 0, 1, 2, 3 };
76          double[] unMatchedEx = { 1, 1, 2 };
77          try {
78              testStatistic.chiSquare(unMatchedEx, unMatchedObs);
79              Assert.fail("arrays have different lengths, MathIllegalArgumentException expected");
80          } catch (MathIllegalArgumentException ex) {
81              // expected
82          }
83  
84          // 0 expected count
85          expected[0] = 0;
86          try {
87              testStatistic.chiSquareTest(expected, observed, .01);
88              Assert.fail("bad expected count, MathIllegalArgumentException expected");
89          } catch (MathIllegalArgumentException ex) {
90              // expected
91          }
92  
93          // negative observed count
94          expected[0] = 1;
95          observed[0] = -1;
96          try {
97              testStatistic.chiSquareTest(expected, observed, .01);
98              Assert.fail("bad expected count, MathIllegalArgumentException expected");
99          } catch (MathIllegalArgumentException ex) {
100             // expected
101         }
102 
103     }
104 
105     @Test
106     public void testChiSquareIndependence() {
107 
108         // Target values computed using R version 1.8.1
109 
110         long[][] counts = { {40, 22, 43}, {91, 21, 28}, {60, 10, 22}};
111         Assert.assertEquals( "chi-square test statistic", 22.709027688, testStatistic.chiSquare(counts), 1E-9);
112         Assert.assertEquals("chi-square p-value", 0.000144751460134, testStatistic.chiSquareTest(counts), 1E-9);
113         Assert.assertTrue("chi-square test reject", testStatistic.chiSquareTest(counts, 0.0002));
114         Assert.assertTrue("chi-square test accept", !testStatistic.chiSquareTest(counts, 0.0001));
115 
116         long[][] counts2 = {{10, 15}, {30, 40}, {60, 90} };
117         Assert.assertEquals( "chi-square test statistic", 0.168965517241, testStatistic.chiSquare(counts2), 1E-9);
118         Assert.assertEquals("chi-square p-value",0.918987499852, testStatistic.chiSquareTest(counts2), 1E-9);
119         Assert.assertTrue("chi-square test accept", !testStatistic.chiSquareTest(counts2, 0.1));
120 
121         // ragged input array
122         long[][] counts3 = { {40, 22, 43}, {91, 21, 28}, {60, 10}};
123         try {
124             testStatistic.chiSquare(counts3);
125             Assert.fail("Expecting MathIllegalArgumentException");
126         } catch (MathIllegalArgumentException ex) {
127             // expected
128         }
129 
130         // insufficient data
131         long[][] counts4 = {{40, 22, 43}};
132         try {
133             testStatistic.chiSquare(counts4);
134             Assert.fail("Expecting MathIllegalArgumentException");
135         } catch (MathIllegalArgumentException ex) {
136             // expected
137         }
138         long[][] counts5 = {{40}, {40}, {30}, {10}};
139         try {
140             testStatistic.chiSquare(counts5);
141             Assert.fail("Expecting MathIllegalArgumentException");
142         } catch (MathIllegalArgumentException ex) {
143             // expected
144         }
145 
146         // negative counts
147         long[][] counts6 = {{10, -2}, {30, 40}, {60, 90} };
148         try {
149             testStatistic.chiSquare(counts6);
150             Assert.fail("Expecting MathIllegalArgumentException");
151         } catch (MathIllegalArgumentException ex) {
152             // expected
153         }
154 
155         // bad alpha
156         try {
157             testStatistic.chiSquareTest(counts, 0);
158             Assert.fail("Expecting MathIllegalArgumentException");
159         } catch (MathIllegalArgumentException ex) {
160             // expected
161         }
162     }
163 
164     @Test
165     public void testChiSquareLargeTestStatistic() {
166         double[] exp = new double[] {
167             3389119.5, 649136.6, 285745.4, 25357364.76, 11291189.78, 543628.0,
168             232921.0, 437665.75
169         };
170 
171         long[] obs = new long[] {
172             2372383, 584222, 257170, 17750155, 7903832, 489265, 209628, 393899
173         };
174         org.hipparchus.stat.inference.ChiSquareTest csti =
175             new org.hipparchus.stat.inference.ChiSquareTest();
176         double cst = csti.chiSquareTest(exp, obs);
177         Assert.assertEquals("chi-square p-value", 0.0, cst, 1E-3);
178         Assert.assertEquals( "chi-square test statistic",
179                 114875.90421929007, testStatistic.chiSquare(exp, obs), 1E-9);
180     }
181 
182     /** Contingency table containing zeros - PR # 32531 */
183     @Test
184     public void testChiSquareZeroCount() {
185         // Target values computed using R version 1.8.1
186         long[][] counts = { {40, 0, 4}, {91, 1, 2}, {60, 2, 0}};
187         Assert.assertEquals( "chi-square test statistic", 9.67444662263,
188                 testStatistic.chiSquare(counts), 1E-9);
189         Assert.assertEquals("chi-square p-value", 0.0462835770603,
190                 testStatistic.chiSquareTest(counts), 1E-9);
191     }
192 
193     /** Target values verified using DATAPLOT version 2006.3 */
194     @Test
195     public void testChiSquareDataSetsComparisonEqualCounts()
196         {
197         long[] observed1 = {10, 12, 12, 10};
198         long[] observed2 = {5, 15, 14, 10};
199         Assert.assertEquals("chi-square p value", 0.541096,
200                 testStatistic.chiSquareTestDataSetsComparison(
201                 observed1, observed2), 1E-6);
202         Assert.assertEquals("chi-square test statistic", 2.153846,
203                 testStatistic.chiSquareDataSetsComparison(
204                 observed1, observed2), 1E-6);
205         Assert.assertFalse("chi-square test result",
206                 testStatistic.chiSquareTestDataSetsComparison(
207                 observed1, observed2, 0.4));
208     }
209 
210     /** Target values verified using DATAPLOT version 2006.3 */
211     @Test
212     public void testChiSquareDataSetsComparisonUnEqualCounts()
213         {
214         long[] observed1 = {10, 12, 12, 10, 15};
215         long[] observed2 = {15, 10, 10, 15, 5};
216         Assert.assertEquals("chi-square p value", 0.124115,
217                 testStatistic.chiSquareTestDataSetsComparison(
218                 observed1, observed2), 1E-6);
219         Assert.assertEquals("chi-square test statistic", 7.232189,
220                 testStatistic.chiSquareDataSetsComparison(
221                 observed1, observed2), 1E-6);
222         Assert.assertTrue("chi-square test result",
223                 testStatistic.chiSquareTestDataSetsComparison(
224                 observed1, observed2, 0.13));
225         Assert.assertFalse("chi-square test result",
226                 testStatistic.chiSquareTestDataSetsComparison(
227                 observed1, observed2, 0.12));
228     }
229 
230     @Test
231     public void testChiSquareDataSetsComparisonBadCounts()
232         {
233         long[] observed1 = {10, -1, 12, 10, 15};
234         long[] observed2 = {15, 10, 10, 15, 5};
235         try {
236             testStatistic.chiSquareTestDataSetsComparison(
237                     observed1, observed2);
238             Assert.fail("Expecting MathIllegalArgumentException - negative count");
239         } catch (MathIllegalArgumentException ex) {
240             // expected
241         }
242         long[] observed3 = {10, 0, 12, 10, 15};
243         long[] observed4 = {15, 0, 10, 15, 5};
244         try {
245             testStatistic.chiSquareTestDataSetsComparison(
246                     observed3, observed4);
247             Assert.fail("Expecting MathIllegalArgumentException - double 0's");
248         } catch (MathIllegalArgumentException ex) {
249             // expected
250         }
251         long[] observed5 = {10, 10, 12, 10, 15};
252         long[] observed6 = {0, 0, 0, 0, 0};
253         try {
254             testStatistic.chiSquareTestDataSetsComparison(
255                     observed5, observed6);
256             Assert.fail("Expecting MathIllegalArgumentException - vanishing counts");
257         } catch (MathIllegalArgumentException ex) {
258             // expected
259         }
260     }
261 }