Skip to content

BUG: Follow up #10680 on score_test exog_extra, input checks, docs and tests - #10768

Merged
bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-score-test-rank-followup
Nov 16, 2026
Merged

bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-score-test-rank-followup

Conversation

@bashtage

Copy link
Copy Markdown
Member

Follow up to #10680 (the rank of exog_extra as the number of constraints in score_test). Its review left the points below. One of them is a small change of the code: the exceptions for bad exog_extra. The rest is the docstrings, the release note and the tests.

  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    NumPy's guide.

Changes

  • BUG: With BUG: Use the rank of exog_extra as the number of constraints in score_test #10680 a nan or infinite value in exog_extra raised LinAlgError: SVD did not converge (the code before BUG: Use the rank of exog_extra as the number of constraints in score_test #10680 returned a nan statistic and p-value) and an exog_extra with the wrong number of rows raised LinAlgError: Incompatible dimensions (before: a ValueError from column_stack). Reproduced for GLM.score_test, the score_test method of GLM results (default and cov_type="HC0"), Poisson results and the generic score_test, with a nan, an inf, too few rows and too few rows as a 1-d array. The helper _full_rank_exog_extra now checks the number of rows and finiteness first and raises a ValueError for 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: a nan in exog_extra returned a nan result before BUG: Use the rank of exog_extra as the number of constraints in score_test #10680 and raises a ValueError now (no test or documentation relied on the nan: grep nan finds nothing in test_score_test.py); revert that half if you prefer the old result. The release-note bullet has a sentence on it.
  • DOC: The Notes said 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's lm. 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 of exog_extra. Checked against R 4.6.1: for a single column it agrees with lm (relative distance 9e-8 dropped by both, 1.1e-7 kept by both); for a third column z1 + z2 + d * e the rank here is 2 for d = 9e-8, 1.1e-7 and 1.3e-7 and 3 for 1.5e-7 and 3e-7, while lm keeps the column from d = 1.1e-7; for two columns at 0.8e-7 with a common residual direction the rank here is 1 and lm drops both. The helper, GLM.score_test and the generic score_test now say this.
  • DOC: R's own glm, and so anova(..., test="Rao"), uses min(1e-7, epsilon / 1000) (read from deparse(glm.fit)), 1e-11 with the default epsilon. Poisson example (n = 300) with noise of relative size 1e-12, 1e-9, 1e-8 and 1e-6 added to the redundant columns: glm drops 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 of lm and that glm is tighter.
  • DOC: One sentence on weights: the projection is unweighted, so for a GLM with freq_weights or var_weights a column that differs from the span of exog only 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 give nan did not hold in general (the statistic is nan only for the default observed=True; observed=False is 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.1 in the docstrings of GLM.score_test and the generic score_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-3 fails only test_nearly_redundant_above_tolerance (the existing tests all pass with it); tol=0.2 fails 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)

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 and statsmodels/discrete/tests/test_discrete.py -k score_test: 4 passed on Python 3.13.
  • ruff check of the changed files and git diff --check pass.

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): 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

bashtage and others added 3 commits November 16, 2026 22:25
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]>
@bashtage
bashtage marked this pull request as ready for review November 16, 2026 23:49
@bashtage
bashtage merged commit 331a255 into statsmodels:main Nov 16, 2026
11 of 12 checks passed
@bashtage
bashtage deleted the wip-score-test-rank-followup branch November 20, 2026 10:56
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.

1 participant