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.jupiter.api.Test;
26  
27  import static org.junit.jupiter.api.Assertions.assertEquals;
28  import static org.junit.jupiter.api.Assertions.assertFalse;
29  import static org.junit.jupiter.api.Assertions.assertTrue;
30  import static org.junit.jupiter.api.Assertions.fail;
31  
32  
33  /**
34   * Test cases for the ChiSquareTestImpl class.
35   *
36   */
37  
38  class ChiSquareTestTest {
39  
40      protected ChiSquareTest testStatistic = new ChiSquareTest();
41  
42      @Test
43      void testChiSquare() {
44  
45          // Target values computed using R version 1.8.1
46          // Some assembly required ;-)
47          //      Use sum((obs - exp)^2/exp) for the chi-square statistic and
48          //      1 - pchisq(sum((obs - exp)^2/exp), length(obs) - 1) for the p-value
49  
50          long[] observed = {10, 9, 11};
51          double[] expected = {10, 10, 10};
52          assertEquals(0.2,  testStatistic.chiSquare(expected, observed), 10E-12, "chi-square statistic");
53          assertEquals(0.904837418036, testStatistic.chiSquareTest(expected, observed), 1E-10, "chi-square p-value");
54  
55          long[] observed1 = { 500, 623, 72, 70, 31 };
56          double[] expected1 = { 485, 541, 82, 61, 37 };
57          assertEquals( 9.023307936427388, testStatistic.chiSquare(expected1, observed1), 1E-10, "chi-square test statistic");
58          assertEquals(0.06051952647453607, testStatistic.chiSquareTest(expected1, observed1), 1E-9, "chi-square p-value");
59          assertTrue(testStatistic.chiSquareTest(expected1, observed1, 0.08), "chi-square test reject");
60          assertFalse(testStatistic.chiSquareTest(expected1, observed1, 0.05), "chi-square test accept");
61  
62          try {
63              testStatistic.chiSquareTest(expected1, observed1, 95);
64              fail("alpha out of range, MathIllegalArgumentException expected");
65          } catch (MathIllegalArgumentException ex) {
66              // expected
67          }
68  
69          long[] tooShortObs = { 0 };
70          double[] tooShortEx = { 1 };
71          try {
72              testStatistic.chiSquare(tooShortEx, tooShortObs);
73              fail("arguments too short, MathIllegalArgumentException expected");
74          } catch (MathIllegalArgumentException ex) {
75              // expected
76          }
77  
78          // unmatched arrays
79          long[] unMatchedObs = { 0, 1, 2, 3 };
80          double[] unMatchedEx = { 1, 1, 2 };
81          try {
82              testStatistic.chiSquare(unMatchedEx, unMatchedObs);
83              fail("arrays have different lengths, MathIllegalArgumentException expected");
84          } catch (MathIllegalArgumentException ex) {
85              // expected
86          }
87  
88          // 0 expected count
89          expected[0] = 0;
90          try {
91              testStatistic.chiSquareTest(expected, observed, .01);
92              fail("bad expected count, MathIllegalArgumentException expected");
93          } catch (MathIllegalArgumentException ex) {
94              // expected
95          }
96  
97          // negative observed count
98          expected[0] = 1;
99          observed[0] = -1;
100         try {
101             testStatistic.chiSquareTest(expected, observed, .01);
102             fail("bad expected count, MathIllegalArgumentException expected");
103         } catch (MathIllegalArgumentException ex) {
104             // expected
105         }
106 
107     }
108 
109     @Test
110     void testChiSquareIndependence() {
111 
112         // Target values computed using R version 1.8.1
113 
114         long[][] counts = { {40, 22, 43}, {91, 21, 28}, {60, 10, 22}};
115         assertEquals( 22.709027688, testStatistic.chiSquare(counts), 1E-9, "chi-square test statistic");
116         assertEquals(0.000144751460134, testStatistic.chiSquareTest(counts), 1E-9, "chi-square p-value");
117         assertTrue(testStatistic.chiSquareTest(counts, 0.0002), "chi-square test reject");
118         assertFalse(testStatistic.chiSquareTest(counts, 0.0001), "chi-square test accept");
119 
120         long[][] counts2 = {{10, 15}, {30, 40}, {60, 90} };
121         assertEquals( 0.168965517241, testStatistic.chiSquare(counts2), 1E-9, "chi-square test statistic");
122         assertEquals(0.918987499852, testStatistic.chiSquareTest(counts2), 1E-9, "chi-square p-value");
123         assertFalse(testStatistic.chiSquareTest(counts2, 0.1), "chi-square test accept");
124 
125         // ragged input array
126         long[][] counts3 = { {40, 22, 43}, {91, 21, 28}, {60, 10}};
127         try {
128             testStatistic.chiSquare(counts3);
129             fail("Expecting MathIllegalArgumentException");
130         } catch (MathIllegalArgumentException ex) {
131             // expected
132         }
133 
134         // insufficient data
135         long[][] counts4 = {{40, 22, 43}};
136         try {
137             testStatistic.chiSquare(counts4);
138             fail("Expecting MathIllegalArgumentException");
139         } catch (MathIllegalArgumentException ex) {
140             // expected
141         }
142         long[][] counts5 = {{40}, {40}, {30}, {10}};
143         try {
144             testStatistic.chiSquare(counts5);
145             fail("Expecting MathIllegalArgumentException");
146         } catch (MathIllegalArgumentException ex) {
147             // expected
148         }
149 
150         // negative counts
151         long[][] counts6 = {{10, -2}, {30, 40}, {60, 90} };
152         try {
153             testStatistic.chiSquare(counts6);
154             fail("Expecting MathIllegalArgumentException");
155         } catch (MathIllegalArgumentException ex) {
156             // expected
157         }
158 
159         // bad alpha
160         try {
161             testStatistic.chiSquareTest(counts, 0);
162             fail("Expecting MathIllegalArgumentException");
163         } catch (MathIllegalArgumentException ex) {
164             // expected
165         }
166     }
167 
168     @Test
169     void testChiSquareLargeTestStatistic() {
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.hipparchus.stat.inference.ChiSquareTest csti =
179             new org.hipparchus.stat.inference.ChiSquareTest();
180         double cst = csti.chiSquareTest(exp, obs);
181         assertEquals(0.0, cst, 1E-3, "chi-square p-value");
182         assertEquals( 114875.90421929007, testStatistic.chiSquare(exp, obs), 1E-9, "chi-square test statistic");
183     }
184 
185     /** Contingency table containing zeros - PR # 32531 */
186     @Test
187     void testChiSquareZeroCount() {
188         // Target values computed using R version 1.8.1
189         long[][] counts = { {40, 0, 4}, {91, 1, 2}, {60, 2, 0}};
190         assertEquals( 9.67444662263,
191                 testStatistic.chiSquare(counts), 1E-9, "chi-square test statistic");
192         assertEquals(0.0462835770603,
193                 testStatistic.chiSquareTest(counts), 1E-9, "chi-square p-value");
194     }
195 
196     /** Target values verified using DATAPLOT version 2006.3 */
197     @Test
198     void testChiSquareDataSetsComparisonEqualCounts()
199         {
200         long[] observed1 = {10, 12, 12, 10};
201         long[] observed2 = {5, 15, 14, 10};
202         assertEquals(0.541096,
203                 testStatistic.chiSquareTestDataSetsComparison(
204                 observed1, observed2), 1E-6, "chi-square p value");
205         assertEquals(2.153846,
206                 testStatistic.chiSquareDataSetsComparison(
207                 observed1, observed2), 1E-6, "chi-square test statistic");
208         assertFalse(testStatistic.chiSquareTestDataSetsComparison(
209                 observed1, observed2, 0.4),
210                 "chi-square test result");
211     }
212 
213     /** Target values verified using DATAPLOT version 2006.3 */
214     @Test
215     void testChiSquareDataSetsComparisonUnEqualCounts()
216         {
217         long[] observed1 = {10, 12, 12, 10, 15};
218         long[] observed2 = {15, 10, 10, 15, 5};
219         assertEquals(0.124115,
220                 testStatistic.chiSquareTestDataSetsComparison(
221                 observed1, observed2), 1E-6, "chi-square p value");
222         assertEquals(7.232189,
223                 testStatistic.chiSquareDataSetsComparison(
224                 observed1, observed2), 1E-6, "chi-square test statistic");
225         assertTrue(testStatistic.chiSquareTestDataSetsComparison(
226                 observed1, observed2, 0.13),
227                 "chi-square test result");
228         assertFalse(testStatistic.chiSquareTestDataSetsComparison(
229                 observed1, observed2, 0.12),
230                 "chi-square test result");
231     }
232 
233     @Test
234     void testChiSquareDataSetsComparisonBadCounts()
235         {
236         long[] observed1 = {10, -1, 12, 10, 15};
237         long[] observed2 = {15, 10, 10, 15, 5};
238         try {
239             testStatistic.chiSquareTestDataSetsComparison(
240                     observed1, observed2);
241             fail("Expecting MathIllegalArgumentException - negative count");
242         } catch (MathIllegalArgumentException ex) {
243             // expected
244         }
245         long[] observed3 = {10, 0, 12, 10, 15};
246         long[] observed4 = {15, 0, 10, 15, 5};
247         try {
248             testStatistic.chiSquareTestDataSetsComparison(
249                     observed3, observed4);
250             fail("Expecting MathIllegalArgumentException - double 0's");
251         } catch (MathIllegalArgumentException ex) {
252             // expected
253         }
254         long[] observed5 = {10, 10, 12, 10, 15};
255         long[] observed6 = {0, 0, 0, 0, 0};
256         try {
257             testStatistic.chiSquareTestDataSetsComparison(
258                     observed5, observed6);
259             fail("Expecting MathIllegalArgumentException - vanishing counts");
260         } catch (MathIllegalArgumentException ex) {
261             // expected
262         }
263     }
264 }