Details

Bug

Status: Resolved

Trivial

Resolution: Fixed

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 Chisquare test value but the value is not valid:
> m < array(c(1,0,0,0), dim = c(2,2)) > chisq.test(m) Pearson's Chisquared test data: m Xsquared = NaN, df = 1, pvalue = NA Warning message: In chisq.test(m) : Chisquared 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.