Skip to content

BUG: stats.gof: handle zero counts in powerdiscrepancy - #10704

Open
gr-0x076 wants to merge 2 commits into
statsmodels:mainfrom
gr-0x076:fix-powerdiscrepancy-zero-counts
Open

gr-0x076 wants to merge 2 commits into
statsmodels:mainfrom
gr-0x076:fix-powerdiscrepancy-zero-counts

Conversation

@gr-0x076

@gr-0x076 gr-0x076 commented Nov 9, 2026

Copy link
Copy Markdown

Description

In powerdiscrepancy with lambd=0 (or lambd="loglikeratio") and lambd=-1 (modified log-likelihood ratio), passing zero observed counts caused np.log(0) to evaluate to -inf, producing RuntimeWarning: divide by zero encountered in log and invalid value encountered in multiply, yielding (nan, nan).

Mathematically, $\lim_{x \to 0^+} x \ln(x) = 0$. By using scipy.special.xlogy, zero counts evaluate cleanly to 0.0 without warnings or NaNs, matching scipy.stats.power_divergence.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Google Antigravity.
    Used for: understanding the statistical concepts, researching numerical limit behavior, and drafting the regression test.
    I have personally read, understood, and can explain every line of this diff, and have verified the statistical/numerical correctness of the change.

Use scipy.special.xlogy in powerdiscrepancy for lambd=0 (loglikeratio)
and lambd=-1 (modified_loglikeratio) to prevent log(0) evaluating to -inf
and 0 * -inf evaluating to NaN.

Closes statsmodels#10703

@gr-0x076 gr-0x076 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Friendly check-in on this whenever you have a chance to take a look @josef-pkt!

All CI checks are passing green (✓), and regression tests are included for both $G$-test cases (lambd=0 and lambd=-1) with zero observed counts. Happy to make any adjustments or address feedback if needed.

@gr-0x076

Copy link
Copy Markdown
Author

I've pushed an update reverting the lambd=-1 line so this PR remains strictly focused on lambd=0 and its alias, directly matching the regression tests and resolving #10703.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: stats.powerdiscrepancy returns (nan, nan) for G-test (lambd=0) when observed counts contain zeros

1 participant