Is a die fair? Are signups spread evenly across weekdays? The goodness-of-fit statistic compares observed counts with expected counts :
Implement gof(observed, expected_probs) returning (chi2, dof, p_value), where expected counts are n * p and dof = k - 1. For the p-value, use the regularised upper incomplete gamma function , computed with this series for (then ):
Sum until terms fall below 1e-15 relative to the total.