Published Updated
A zero-count category makes statsmodels’ G-test return NaN
A valid three-category count table loses its G-test statistic and p-value because the calculation does not preserve the zero-count limit.
An unobserved category should still yield a result
A category can have no observations without invalidating a goodness-of-fit calculation. We found that statsmodels’ G-test instead returns neither a usable statistic nor a p-value when one observed count is zero—even when every expected count is 10. The default call produces NaN for both values.
Tasuku Kobayashi submitted statsmodels issue #10703 on November 9, 2026. The report is open. A contributor proposed a zero-count fix in PR #10704, which remains open. The contributor narrowed the patch to lambd=0 and its loglikeratio alias after review. The revised diff includes regression tests for both forms.
Three categories, a closed-form reference
The example uses observed counts [0, 15, 15] and expected counts [10, 10, 10]. Both total 30. It needs no missing values, large numbers or unusual options: powerdiscrepancy selects the G-test by default. Explicit lambd=0 and lambd="loglikeratio" give the same failure.
| Calculation | G statistic | p-value |
|---|---|---|
| statsmodels, default G-test | NaN | NaN |
| Closed-form reference | 24.327906486489862 | ≈ 5.21509505084657 × 10⁻⁶ |
| SciPy power_divergence, lambda_=0 | 24.327906486489862 | 5.215095050846569 × 10⁻⁶ |
For a positive expected count, the contribution O × log(O / E) tends to zero as the observed count tends to zero. The remaining two categories give G = 60 × log(1.5). With three categories and no fitted parameters, the reference has two degrees of freedom; its chi-square upper-tail probability is exp(−G / 2) = (2/3)³⁰. This independently checks the result rather than relying only on agreement with another library. The p-value is the test’s chi-square approximation, not a finite-sample exact probability.
import statsmodels.api as sm
from scipy import stats
observed = [0, 15, 15]
expected = [10, 10, 10]
print(sm.stats.powerdiscrepancy(observed, expected))
print(stats.power_divergence(observed, expected, lambda_=0))The issue reproduces in statsmodels 0.14.6 and 0.15.0, and in main at 772cb34. The 0.15.0 run used Python 3.12.14, NumPy 2.5.4, SciPy 1.18.1 and pandas 3.1.1 on Linux x86_64. The public report records the full environment.
Preserve the zero-count limit
The implementation evaluates log(0) and then multiplies the resulting negative infinity by zero. Floating-point arithmetic produces NaN rather than the limiting contribution of zero. Runtime warnings report division by zero and invalid multiplication.
The report identifies scipy.special.xlogy as a way to retain that limit in the G-test calculation. This observation concerns the G-test with positive expected counts; it does not assign a finite value to other divergence statistics that genuinely diverge at a zero count.
This dependency investigation establishes a reproducible finding, not a SciGround production incident or an upstream-adopted fix. Read the public report and reproducible example for the current upstream disposition.