    1   /*
    2    * Copyright 2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    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  /***
    23   * Test cases for the ChiSquareTestImpl class.
    24   *
    25   * @version $Revision: 180065 $ $Date: 2005-06-04 20:58:14 -0700 (Sat, 04 Jun 2005) $
    26   */
    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          // Target values computed using R version 1.8.1
    48          // Some assembly required ;-)
    49          //      Use sum((obs - exp)^2/exp) for the chi-square statistic and
    50          //      1 - pchisq(sum((obs - exp)^2/exp), length(obs) - 1) for the p-value
    51
    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              // expected
    69          }
    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              // expected
    78          }
    79
    80          // unmatched arrays
    81          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              // expected
    88          }
    89
    90          // 0 expected count
    91          expected[0] = 0;
    92          try {
    93              testStatistic.chiSquareTest(expected, observed, .01);
    94              fail("bad expected count, IllegalArgumentException expected");
    95          } catch (IllegalArgumentException ex) {
    96              // expected
    97          }
    98
    99          // negative observed count
    100         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             // expected
    107         }
    108
    109     }
    110
    111     public void testChiSquareIndependence() throws Exception {
    112
    113         // Target values computed using R version 1.8.1
    114
    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         // ragged input array
    127         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             // expected
    133         }
    134
    135         // insufficient data
    136         long[][] counts4 = {{40, 22, 43}};
    137         try {
    138             testStatistic.chiSquare(counts4);
    139             fail("Expecting IllegalArgumentException");
    140         } catch (IllegalArgumentException ex) {
    141             // expected
    142         }
    143         long[][] counts5 = {{40}, {40}, {30}, {10}};
    144         try {
    145             testStatistic.chiSquare(counts5);
    146             fail("Expecting IllegalArgumentException");
    147         } catch (IllegalArgumentException ex) {
    148             // expected
    149         }
    150
    151         // negative counts
    152         long[][] counts6 = {{10, -2}, {30, 40}, {60, 90} };
    153         try {
    154             testStatistic.chiSquare(counts6);
    155             fail("Expecting IllegalArgumentException");
    156         } catch (IllegalArgumentException ex) {
    157             // expected
    158         }
    159
    160         // bad alpha
    161         try {
    162             testStatistic.chiSquareTest(counts, 0);
    163             fail("Expecting IllegalArgumentException");
    164         } catch (IllegalArgumentException ex) {
    165             // expected
    166         }
    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     /*** Contingency table containing zeros - PR # 32531 */
    187     public void testChiSquareZeroCount() throws Exception {
    188         // Target values computed using R version 1.8.1
    189         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 }