Skip to content

BUG: fix power_equivalence_poisson_2indep and power_equivalence_neginb_2indep for nobs_ratio != 1 - #10521

Merged
bashtage merged 2 commits into
statsmodels:mainfrom
Jason10191019999:fix-power-equivalence-nobs-ratio
Oct 24, 2026
Merged

bashtage merged 2 commits into
statsmodels:mainfrom
Jason10191019999:fix-power-equivalence-nobs-ratio

Conversation

@Jason10191019999

Copy link
Copy Markdown
Contributor

Summary

Fixes three bugs in the equivalence (TOST) power functions in statsmodels/stats/rates.py, all invisible at nobs_ratio = 1 (which is the only case the existing tests covered):

  1. power_equivalence_poisson_2indep passed nobs2 to _power_equivalence_het where the per-nobs1 variance definitions require nobs1 (consistent with power_poisson_ratio_2indep and with the sibling power_equivalence_neginb_2indep). At nobs_ratio=2, nobs1=100, rate1=rate2=1.0, low=0.8, upp=1.25, method_var='alt' this gave power 0.6486 vs a Monte Carlo reference of 0.143 (50k reps, MC se 0.002); the corrected value 0.1406 matches.
  2. Same function, method_var='score': the constrained-null variance used (1 + margin * nobs_ratio)**2 instead of the correct constrained-CMLE factor (margin + nobs_ratio)**2 (verified by brute-force constrained MLE, and matching power_poisson_ratio_2indep).
  3. power_equivalence_neginb_2indep: rate1/rate2 swapped in the alternative variance relative to the per-nobs1 delta-method variance of the log rate ratio (matching power_negbin_ratio_2indep).

Regression tests use nobs_ratio != 1 with reference values computed from an independent closed-form implementation (described in the test comments) and cross-checked against Monte Carlo simulation, plus a consistency test that power_equivalence_neginb_2indep(dispersion=0) agrees with the Poisson version.

Tests: pytest statsmodels/stats/tests/test_rates_poisson.py — 1232 passed, 4 skipped (full file, including the 12 new tests).

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

Tick exactly one of the following. If AI tools were used, replace the <...>
placeholders in the second option with the tool name(s) and how the tool's output was
used.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Kimi Code CLI (AI coding agent).
    Used for: drafting the fix and the regression tests, and running the numerical verification.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

This contribution was developed with the assistance of an AI coding tool; all changes were verified against independent numerical references (Monte Carlo simulation, brute-force constrained MLE, and a from-scratch closed-form implementation).

Details

Notes:

  • It is essential that you add a test when making code changes. Tests are not
    needed for doc changes.
  • When adding a new function, test values should usually be verified in another package (e.g., R/SAS/Stata).
  • When fixing a bug, you must add a test that would produce the bug in main and
    then show that it is fixed with the new code.
  • New code additions must be well formatted. Changes should pass ruff. You can
    verify your changes are well formatted by running
    ruff check . --fix
    
    assuming ruff is installed. While passing this test is not required, it is good practice and it help
    improve code quality in statsmodels.
  • Docstring additions must render correctly, including escapes and LaTeX.
  • If AI tools were used to help write this PR, see the
    AI Policy for what disclosure
    and review is expected of you before submitting.

Jason10191019999 and others added 2 commits October 21, 2026 01:35
power_equivalence_poisson_2indep passed nobs2 where the per-nobs1
variance definitions require nobs1, and the score (constrained null)
variance used (1 + margin * nobs_ratio)**2 instead of the correct
constrained-CMLE factor (margin + nobs_ratio)**2, matching
power_poisson_ratio_2indep. power_equivalence_neginb_2indep had rate1
and rate2 swapped in the alternative variance relative to the per-nobs1
scaling. All three bugs are invisible at nobs_ratio = 1.

Regression tests use nobs_ratio != 1 with reference values from an
independent closed-form implementation, cross-checked against Monte
Carlo simulation.
@bashtage
bashtage merged commit 0cf58cd into statsmodels:main Oct 24, 2026
12 of 13 checks passed
bashtage added a commit that referenced this pull request Oct 25, 2026
…ower

The ftotal null variance used (1 + value * nobs_ratio)**2 where the score variance has (value + nobs_ratio)**2, so ftotal differed from score at dispersion 0 for nobs_ratio != 1 (0.7121 versus 0.7841 for rates 1.1 and 1, nobs_ratio 4, nobs1 400, and 0.7874 in an exact enumeration of the Poisson TOST). This is fixed in power_equivalence_neginb_2indep and power_negbin_ratio_2indep. power_equivalence_poisson_2indep and power_equivalence_neginb_2indep now require a positive finite nobs_ratio, which gave nan or a ZeroDivisionError. Add versionchanged notes.

Follow up to gh-10521, see gh-10519.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
bashtage added a commit that referenced this pull request Oct 25, 2026
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: power_equivalence_poisson_2indep and power_equivalence_neginb_2indep wrong for nobs_ratio != 1

2 participants