Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-1627

ChiSquareTest computes NaN with zero observations

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Trivial
    • Resolution: Fixed
    • 4.0
    • 4.0
    • None
    • None

    Description

      Zero observations input to the ChiSquareTest will compute NaN:

      ChiSquareTest chi2Test = new ChiSquareTest();
      final long[][] counts = new long[2][2];
      // NaN
      double chi2 = chi2Test.chiSquare(counts);
      

      This is due to a divide by zero error. This bug was identified by sonarcloud analysis.

      The unit tests use R as a reference. In R this case will raise an error that at least one entry must be positive. Setting a value to 1 allows R to compute a Chi-square test value but the value is not valid:

      > m <- array(c(1,0,0,0), dim = c(2,2))
      > chisq.test(m)
      
      	Pearson's Chi-squared test
      
      data:  m
      X-squared = NaN, df = 1, p-value = NA
      
      Warning message:
      In chisq.test(m) : Chi-squared approximation may be incorrect
      

      Other methods in the ChiSquareTest will raise a ZeroException if the observations are zero for an entire array of observations or if a pair of observations in a bin are both zero.

      The Chi square test has assumptions that do not hold when the number of observations are small. The limit for the number of observations per category is variable. The document referenced in the code javadoc recommends an expected level of 5 per bin. To avoid setting limits on the sample size a suggested fix is to raise a zero exception if the sum of all counts is zero. This will avoid a NaN computation. Use of a suitable number of observations is left to the caller.

      Attachments

        Activity

          People

            Unassigned Unassigned
            aherbert Alex Herbert
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: