BUG: reject alpha outside (0, 1) in Table2x2 confint methods - #10399
Merged
Merged
Conversation
Table2x2.log_oddsratio_confint and log_riskratio_confint silently accepted alpha outside (0, 1): stats.norm.ppf(alpha / 2) degenerates and oddsratio_confint returned (inf, 0.0). Raise ValueError; the exp-based oddsratio/riskratio confint wrappers delegate and are covered transitively.
Member
|
Thanks. |
4 of 5 tasks
bashtage
added a commit
that referenced
this pull request
Oct 5, 2026
The checks added in GH-10379, GH-10386, GH-10388, GH-10392, GH-10393, GH-10399, GH-10405, GH-10406, GH-10411, GH-10420, GH-10421, GH-10423, GH-10425, GH-10426 and GH-10431 compare the arguments with scalars, for example `if not 0 < alpha < 1`, `if low > upp` or `if dispersion < 0`. These raise "The truth value of an array with more than one element is ambiguous" for arrays and Series. The functions broadcast over these arguments and return the same values as the calls for the elements in 0.15.0, for example a confidence interval for several alpha, the power for several equivalence margins or a table of sample sizes. Test with np.all and the comparison ufuncs instead, which work for scalars, lists, arrays and Series, and which also reject NaN. The error messages are unchanged. A new test checks for 50 arguments that an array of two valid values gives the same result as the two calls with the scalars, and that one invalid element (including NaN) is rejected. Follow-up to GH-10379, GH-10386, GH-10388, GH-10392, GH-10393, GH-10399, GH-10405, GH-10406, GH-10411, GH-10420, GH-10421, GH-10423, GH-10425, GH-10426, GH-10431 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
bashtage
added a commit
that referenced
this pull request
Oct 5, 2026
The arguments that broadcast are documented as float or int, but arrays work, and the previous commit keeps them working. Document them as float or array_like in the functions of the previous commit and in chisquare_power, which says in its Notes that it works vectorized. Follow-up to GH-10379, GH-10386, GH-10388, GH-10392, GH-10393, GH-10399, GH-10405, GH-10406, GH-10411, GH-10420, GH-10421, GH-10423, GH-10425, GH-10426, GH-10431 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The `Table2x2` confidence-interval methods silently accept `alpha` outside (0, 1):
`log_oddsratio_confint` computes `f = -stats.norm.ppf(alpha / 2)`, which is `-inf` for `alpha = 2` and `nan` for `alpha = 0`; `np.exp` turns that into `inf`/`0` bounds. `log_riskratio_confint` has the identical pattern.
Fix
Raise `ValueError` (`alpha must be in the range (0, 1)`) after the `method` validation in both `log_oddsratio_confint` and `log_riskratio_confint`. The public wrappers `oddsratio_confint` and `riskratio_confint` delegate to them, so all four entry points are covered.
Tests
Added `test_table2x2_confint_invalid_alpha_raises` in `statsmodels/stats/tests/test_contingency_tables.py` covering all four methods (`alpha=2` and `alpha=0`) plus a finite sanity check. Verified red without the fix and green with it; the full test module passes (90 passed) and the diff introduces no new `ruff check` findings.
AI Disclosure
Contributions comply with the statsmodels AI Policy. Completed option:
Tool(s): `ZCode (GLM-based coding agent)`.
Used for: `drafting the repro script, the implementation, the tests, and the PR text; I executed all code locally, verified the red/green test cycle, and reviewed every line of the diff`.
I have personally read, understood, and can explain every line of this diff, and
have verified the statistical/numerical correctness of the change.