chiSquaredTest
Performs a chi-squared goodness-of-fit test.
The null hypothesis is that the observed frequency counts follow the expected distribution. This test compares how well observed category counts match the expected counts by computing the sum of squared differences between observed and expected, each divided by the expected count.
Example:
// Test if a die is fair (60 rolls, expect 10 per face)
val observed = intArrayOf(8, 12, 11, 9, 10, 10)
val result = chiSquaredTest(observed)
result.statistic // chi-squared statistic
result.pValue // p-value
result.degreesOfFreedom // 5.0 (six categories minus one)
result.isSignificant() // false (data is consistent with a fair die)Content copied to clipboard
Return
a TestResult containing the chi-squared statistic, p-value, and degrees of freedom (number of categories minus one).
Parameters
observed
the observed frequency counts for each category. Must have at least 2 categories.
expected
the expected frequency counts for each category. If null, assumes a uniform distribution where each category has the same expected count (total / number of categories). Defaults to null.