gTest
Performs a G-test (log-likelihood ratio test) for goodness-of-fit.
The G-test is an alternative to the chi-squared test that uses a log-likelihood ratio statistic. It computes twice the sum of each observed count times the log of the ratio of observed to expected. Categories with zero observed counts contribute nothing to the statistic. The G-test is asymptotically equivalent to the chi-squared test but can be more accurate for small samples. Under the null hypothesis, the test statistic follows a chi-squared distribution.
Example:
// Test if a die is fair (60 rolls, expect 10 per face)
val observed = intArrayOf(8, 12, 11, 9, 10, 10)
val result = gTest(observed)
result.statistic // G statistic
result.pValue // p-value
result.degreesOfFreedom // 5.0 (six categories minus one)
result.isSignificant() // false (data is consistent with a fair die)Return
a TestResult containing the G statistic, p-value, and degrees of freedom (number of categories minus one).
Parameters
the observed frequency counts for each category. Must have at least 2 categories.
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.