Repository navigation
BUG: Follow up #10680 on score_test exog_extra, input checks, docs and tests - #10768
Merged
bashtage merged 3 commits intoNov 16, 2026
Merged
Conversation
The check of the rank of exog_extra added in statsmodels#10680 made bad input fail with confusing errors: an exog_extra with the wrong number of rows raised LinAlgError "Incompatible dimensions" (ValueError from column_stack before), and a nan or inf in exog_extra raised LinAlgError "SVD did not converge" (a nan statistic and p-value before). Validate the shape and the finiteness at the top of the helper that computes the rank. Both cases now raise a ValueError that names the problem, for GLM.score_test, the score_test of the GLM and discrete results and the generic score_test. The hypothesis="separate" path does not use the helper and is unchanged. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The Notes of score_test in statsmodels#10680 said that a column of exog_extra is redundant if its norm after projecting out exog and the other columns is less than 1e-7 times its norm. That is the sequential rule of R's lm, but the helper uses the singular values of the column-normalised residuals of the projection on exog. For one column the two agree, for several columns they differ (checked against lm in R 4.6.1). State the rule that the code uses, that the tolerance is the one of R's lm while R's glm and anova(test="Rao") use min(1e-7, epsilon / 1000), and that the projection is unweighted so a column that differs from the span of exog only on observations with weights near zero is counted in the degrees of freedom. Add versionchanged 0.15.1 to the docstrings of both score_test functions. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Follow-up to statsmodels#10680. The tests of the rank of exog_extra only had exactly redundant columns and clearly independent columns, so they did not pin the other side of the 1e-7 tolerance: a mutant with tol=1e-3 passed all of them. - Add a block with noise of relative size 1e-5: the columns are kept, df is 4 and the statistic equals the closed form of the Poisson score statistic. - Test that an exog_extra with nan or infinite values or with the wrong number of rows raises a ValueError for the GLM and Poisson results, the HC0 covariance, GLM.score_test and the generic score_test. - Test the documented limitation that the projection is unweighted: a column that differs from the span of exog only on observations with weight 1e-12 is kept, adds nothing to the statistic and is counted in the degrees of freedom. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
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.
Follow up to #10680 (the rank of
exog_extraas the number of constraints inscore_test). Its review left the points below. One of them is a small change of the code: the exceptions for badexog_extra. The rest is the docstrings, the release note and the tests.NumPy's guide.
Changes
BUG:With BUG: Use the rank of exog_extra as the number of constraints in score_test #10680 ananor infinite value inexog_extraraisedLinAlgError: SVD did not converge(the code before BUG: Use the rank of exog_extra as the number of constraints in score_test #10680 returned ananstatistic and p-value) and anexog_extrawith the wrong number of rows raisedLinAlgError: Incompatible dimensions(before: aValueErrorfromcolumn_stack). Reproduced forGLM.score_test, thescore_testmethod of GLM results (default andcov_type="HC0"),Poissonresults and the genericscore_test, with anan, aninf, too few rows and too few rows as a 1-d array. The helper_full_rank_exog_extranow checks the number of rows and finiteness first and raises aValueErrorfor both ('exog_extra must have the same number of rows as exog, ...', 'exog_extra contains nan or infinite values.');hypothesis="separate"does not use the helper and is unchanged. Decision for the maintainer: ananinexog_extrareturned ananresult before BUG: Use the rank of exog_extra as the number of constraints in score_test #10680 and raises aValueErrornow (no test or documentation relied on thenan:grep nanfinds nothing intest_score_test.py); revert that half if you prefer the old result. The release-note bullet has a sentence on it.DOC:TheNotessaid that a column is redundant if its norm 'after projecting out the exog and the other columns' is less than 1e-7 times its norm, which is the sequential rule of R'slm. The code counts the singular values above 1e-7 of the matrix of the residuals of the projection with each column divided by the norm of the column ofexog_extra. Checked against R 4.6.1: for a single column it agrees withlm(relative distance 9e-8 dropped by both, 1.1e-7 kept by both); for a third columnz1 + z2 + d * ethe rank here is 2 for d = 9e-8, 1.1e-7 and 1.3e-7 and 3 for 1.5e-7 and 3e-7, whilelmkeeps the column from d = 1.1e-7; for two columns at 0.8e-7 with a common residual direction the rank here is 1 andlmdrops both. The helper,GLM.score_testand the genericscore_testnow say this.DOC:R's ownglm, and soanova(..., test="Rao"), usesmin(1e-7, epsilon / 1000)(read fromdeparse(glm.fit)), 1e-11 with the defaultepsilon. Poisson example (n = 300) with noise of relative size 1e-12, 1e-9, 1e-8 and 1e-6 added to the redundant columns:glmdrops them at 1e-12 (Rao df 2, 0.7224369; here df 2, 0.7224364) and keeps them from 1e-9 (df 4, 3.0526) where this code still gives df 2 and 0.7224364 up to 1e-8 and df 4 and 3.0514 at 1e-6. The docstrings say that the tolerance is the one oflmand thatglmis tighter.DOC:One sentence on weights: the projection is unweighted, so for a GLM withfreq_weightsorvar_weightsa column that differs from the span ofexogonly on observations with weights near zero (checked with 1e-12 on 5 rows, both kinds of weights) is counted in the degrees of freedom although it adds nothing to the statistic (df 3 against 2, the statistic equal to 6e-13 relative; the code before BUG: Use the rank of exog_extra as the number of constraints in score_test #10680 behaves the same way for the statistic). The claim in the review that weights of exactly 0 givenandid not hold in general (the statistic isnanonly for the defaultobserved=True;observed=Falseis finite, the same before BUG: Use the rank of exog_extra as the number of constraints in score_test #10680), so it is not in the text.DOC:.. versionchanged:: 0.15.1in the docstrings ofGLM.score_testand the genericscore_test.TST:test_nearly_redundant_above_tolerance(noise of relative size 1e-5 is kept: df 4 and the statistic of the closed-form Poisson score statistic,rtol=1e-4; the information matrix has a condition number of about 4e7) pins the other side of the tolerance;test_invalid_exog_extra(7 inputs for 5 callers);test_weights_not_used_in_rank(both kinds of weights). All of them pass on the code before this PR except the tests of the new errors.Mutation checks (the tree mutated, restored afterwards):
tol=1e-3fails onlytest_nearly_redundant_above_tolerance(the existing tests all pass with it);tol=0.2fails 3 (that test and both weights tests); removing the finiteness check fails 15 cases and the shape check 20.Not changed (noted in the review)
lm_test_glm,linear_reset) and should stay open; the cost of the extralstsqand SVD of an n x k_extra matrix; the choice of an SVD rule against R's sequential rule; the tolerance itself. Zero weights withobserved=Truegivenanwith or without BUG: Use the rank of exog_extra as the number of constraints in score_test #10680, which was not investigated.Tests
statsmodels/genmod/tests/test_score_test.py: 63 passed on Python 3.13 and on Python 3.10 (NumPy 1.23.5, SciPy 1.8.1);statsmodels/genmod/tests/test_glm.py -k score: 5 andstatsmodels/discrete/tests/test_discrete.py -k score_test: 4 passed on Python 3.13.ruff checkof the changed files andgit diff --checkpass.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.
Tool(s): Claude Code (Claude Sonnet 5.5).
Used for: reviewing BUG: Use the rank of exog_extra as the number of constraints in score_test #10680, and writing and checking the follow-up changes.
I have personally read, understood, and can explain every line of this diff, and
have verified the statistical/numerical correctness of the change.
🤖 Generated with Claude Code