Skip to content
Permalink
Browse files
Update chi-square test
Increase accuracy of the chi2 critical value.

Use String.format for the assertion message, including the chi2 critical
value.
  • Loading branch information
aherbert committed Mar 2, 2022
1 parent 52a5b80 commit 1d2722f0c01fb07febae5b94502a91cfa81485ad
Showing 1 changed file with 7 additions and 7 deletions.
@@ -60,14 +60,12 @@ private static void check(long sampleSize,
int numFailures = 0;

final double[] expected = new double[numBins];
for (int k = 0; k < numBins; k++) {
expected[k] = sampleSize / (double) numBins;
}
Arrays.fill(expected, sampleSize / (double) numBins);

final long[] observed = new long[numBins];
// Chi-square critical value with 9 degrees of freedom
// and 1% significance level.
final double chi2CriticalValue = 21.67;
final double chi2CriticalValue = 21.665994333461924;

// For storing chi2 larger than the critical value.
final List<Double> failedStat = new ArrayList<>();
@@ -114,9 +112,11 @@ private static void check(long sampleSize,
// 3 0.0016

if (numFailures > 3) { // Test will fail with 0.16% probability
Assertions.fail(sampler + ": Too many failures for sample size = " + sampleSize +
" (" + numFailures + " out of " + numTests + " tests failed, " +
"chi2=" + Arrays.toString(failedStat.toArray(new Double[0])) + ")");
Assertions.fail(String.format(
"%s: Too many failures for sample size = %d " +
" (%d out of %d tests failed, chi2 > %.3f=%s)",
sampler, sampleSize, numFailures, numTests, chi2CriticalValue,
Arrays.toString(failedStat.toArray(new Double[0]))));
}
}
}

0 comments on commit 1d2722f

Please sign in to comment.