Skip to content

BUG: Use the rank of exog_extra as the number of constraints in score_test - #10680

Merged
bashtage merged 2 commits into
statsmodels:mainfrom
bhaskargurram-ai:fix/score-test-redundant-exog-rank
Nov 16, 2026
Merged

bashtage merged 2 commits into
statsmodels:mainfrom
bhaskargurram-ai:fix/score-test-redundant-exog-rank

Conversation

@bhaskargurram-ai

@bhaskargurram-ai bhaskargurram-ai commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

The variable addition score test (GLM.score_test, and the score_test method of GLM and discrete model results through statsmodels.base._parameter_inference.score_test) used the number of columns of exog_extra as the number of constraints. As described in #5364, columns that are linear combinations of other columns of exog_extra or of the model's exog, for example the linear term that a spline basis contains, do not add a constraint, so the degrees of freedom were too large and the p-value too small. Exactly redundant columns made the covariance of the score singular and the test raised LinAlgError: Singular matrix.

On main, with a Poisson GLM with a constant and two regressors and an exog_extra with two new columns, a copy of a regressor and a linear combination of the two new columns, score_test raises LinAlgError; with the last two columns perturbed by 1e-9 noise it returns df 4 and p-value 0.935 instead of df 2 and 0.663.

The fix follows the reduction in the issue: exog_extra is projected on exog, the residual columns are divided by the norms of the columns of exog_extra, and the rank is the number of singular values of the scaled residuals above 1e-7, the column tolerance of R's lm (for one column this is exactly R's criterion). If the rank is smaller than the number of columns, exog_extra is replaced by the orthonormal basis of the residuals with that many columns. The score test statistic is invariant to a change of basis of the added block, so it is the same as the one obtained by dropping the redundant columns by hand, and the degrees of freedom are the rank. Independent columns are used exactly as before, so existing results do not change, and the separate z-tests (hypothesis="separate") still use the columns as given. k_constraints passed by the user is added to the rank as before. The 2-tuple form of exog_extra for two-part models is not covered; its docstring says so.

Tests in test_score_test.py, for the GLM result method, GLM.score_test and the Poisson result method, with cov_type="nonrobust" and "HC0": the exactly redundant block gives df 2 and the same statistic and p-value as the two independent columns to 1e-10 (on main: LinAlgError); the nearly redundant block (1e-10 noise) gives df 2 and the same statistic to 1e-6 (on main: df 4); the p-value equals chi2.sf(statistic, 2); a column of exog as exog_extra gives df 0 and a statistic of 0 instead of raising; independent columns, a one dimensional exog_extra and a block with a 1e-12 scale are passed through unchanged; the rank agrees with matrix_rank of the partial_project residual used in the issue. A last test reproduces the issue with the B-spline basis of two variables (BSplines, df 6 each) added to a model with their linear terms: the rank is 10 for 12 columns, the statistic equals the one of the hand-reduced basis (principal components of the projected residual, as in the issue) to 1e-8, and the p-value uses 10 degrees of freedom.

I could not compare with R here (it is not installed on the machine I used); anova(glm_small, glm_large, test="Rao") uses the rank of the added block and would be the reference to add if you would like one in the test file.

closes #5364

Also closes #7203.

AI Disclosure

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Claude Code.
    Used for: drafting the tests, running the reproductions and cross-checks.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

…_test

The variable addition score test used the number of columns of
exog_extra as the number of constraints. Columns that are linear
combinations of other columns of exog_extra or of the model's exog, for
example the linear term that a spline basis contains, do not add a
constraint, so the degrees of freedom were too large and the p-value too
small, and exactly redundant columns made the covariance of the score
singular and raised a LinAlgError.

The number of constraints is now the rank of exog_extra after projecting
out exog, with the column tolerance of R's lm, and redundant columns are
replaced by an orthonormal basis of the independent part. The statistic
is invariant to this change of basis, so it is the same as the one
obtained by dropping the redundant columns by hand. Independent columns
are used as given, and the separate z-tests are unchanged.

This applies to GLM.score_test and to the score_test method of GLM and
discrete model results.

closes statsmodels#5364
@bashtage

Copy link
Copy Markdown
Member

The PR body and the commit message say "the p-value too small". It is the other way round: too many degrees of freedom make the p-value too large (the test was conservative). The docstring Notes already say "too large". Please fix the wording and add :pr:10680 next to :issue:5364.

@bashtage

Copy link
Copy Markdown
Member

The helper can now return rank 0 for a non-empty exog_extra (every column in the span of exog, which test_all_redundant treats as supported). With cov_type="HC0" the generic branch builds np.eye(len(self.params) + k_constraints)[-k_constraints:] and [-0:] selects all rows, so the result has k_constraint equal to the number of parameters and a p-value of 1.0.

With nonrobust the result is df 0 and a p-value of nan. Before this PR those inputs raised LinAlgError (GLM) or returned df 1 (Poisson). Please handle k_rank == 0 explicitly, by raising a ValueError saying that exog_extra is in the span of exog

If every column of exog_extra is a linear combination of the columns of
exog, the rank of exog_extra given exog is 0 and there is no constraint
to test. The helper returned rank 0, so the nonrobust test had df 0 and
a p-value of nan, and with HC0 the constraint matrix selected all rows
of the identity, which gave a p-value of 1. Raise a ValueError instead.

Correct the release note: too many degrees of freedom made the p-value
too large, not too small. Add the PR reference.
@bashtage

Copy link
Copy Markdown
Member

Thanks.

@bashtage
bashtage merged commit c5fc756 into statsmodels:main Nov 16, 2026
11 of 12 checks passed
bashtage added a commit that referenced this pull request Nov 16, 2026
The check of the rank of exog_extra added in #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]>
bashtage added a commit that referenced this pull request Nov 16, 2026
The Notes of score_test in #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]>
bashtage added a commit that referenced this pull request Nov 16, 2026
Follow-up to #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 added a commit that referenced this pull request Nov 16, 2026
BUG: Follow up #10680 on score_test exog_extra, input checks, docs and tests
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.

ENH/BUG/Precision score_test collinearity problems with exog_extra BUG/ENH: LM, score_test variable addition with redundant variables (rank)

2 participants