Skip to content

fix: use normalized_cov_params as fallback when hessian inversion fails in GLM.fit - #9794

Merged
bashtage merged 1 commit into
statsmodels:mainfrom
amethystani:fix/glm-cov_p-unused-assignment
Aug 26, 2026
Merged

bashtage merged 1 commit into
statsmodels:mainfrom
amethystani:fix/glm-cov_p-unused-assignment

Conversation

@amethystani

Copy link
Copy Markdown
Contributor

Summary

Fixes #9776 (code scanning alert: dead assignment in GLM.fit).

In the non-IRLS gradient-optimizer path (_fit_gradient), the previous code
assigned cov_p from rslt.normalized_cov_params just before a try/except
block that unconditionally overwrote it. This made the initial assignment dead
code and, worse, caused the except LinAlgError branch to set cov_p = None
even when a valid covariance estimate was available from the optimizer.

Before (broken):

if rslt.normalized_cov_params is None:    # ← computed but overwritten
    cov_p = None
else:
    cov_p = rslt.normalized_cov_params / scale  # ← dead

try:
    hessian = -self.hessian(rslt.params, observed=oim)
    cov_p = np.linalg.inv(hessian) / scale
except LinAlgError:
    warnings.warn(...)
    cov_p = None   # ← discards rslt.normalized_cov_params when it had a value

After (fixed):

try:
    hessian = -self.hessian(rslt.params, observed=oim)
    cov_p = np.linalg.inv(hessian) / scale
except LinAlgError:
    warnings.warn(...)
    if rslt.normalized_cov_params is not None:
        cov_p = rslt.normalized_cov_params / scale  # ← actual fallback
    else:
        cov_p = None

A new regression test test_glm_fit_cov_p_fallback_on_linalg_error is added
alongside the existing test_non_invertible_hessian_fails_summary. It patches
numpy.linalg.inv to raise LinAlgError and verifies that
res.normalized_cov_params is not None when the BFGS optimizer has
produced a valid Hinv.

Changes

  • statsmodels/genmod/generalized_linear_model.py: remove dead block,
    move normalized_cov_params check into the except branch
  • statsmodels/genmod/tests/test_glm.py: add regression test

Test plan

  • New test test_glm_fit_cov_p_fallback_on_linalg_error directly
    exercises the fixed code path
  • Existing test_non_invertible_hessian_fails_summary continues to pass

@amethystani

Copy link
Copy Markdown
Contributor Author

@bashtage you are the primary committer to statsmodels/genmod/generalized_linear_model.py — would appreciate your review on this. The fix removes a dead cov_p assignment (flagged by code scanning in #9776) and moves the normalized_cov_params check into the except LinAlgError branch so it actually serves as a fallback.

@amethystani

Copy link
Copy Markdown
Contributor Author

@bashtage @Josef-a-pktd — requesting review when convenient. The fix removes a dead cov_p assignment before the try block and moves the normalized_cov_params fallback inside the except LinAlgError branch, which is the only path that needs it.

@Aniketsy

Copy link
Copy Markdown
Contributor

Thanks @amethystani for the PR, review may take some time, could you please look into the ci-fail, and feel free to ping me if you need help 😊

…ls in GLM.fit

In the non-IRLS (gradient optimizer) path, GLM.fit previously assigned
cov_p from rslt.normalized_cov_params before the try/except block, but
that assignment was immediately overwritten inside the try block, making
it dead code. When LinAlgError was raised the except branch set cov_p=None,
silently discarding the optimizer's own Hinv estimate.

Move the normalized_cov_params fallback into the except branch so it is
actually used when np.linalg.inv raises LinAlgError.

Fixes statsmodels#9776

Co-authored-by: amethystani <[email protected]>
@bashtage
bashtage force-pushed the fix/glm-cov_p-unused-assignment branch from c2754ed to d21ef50 Compare August 25, 2026 06:57
@github-actions

Copy link
Copy Markdown

Thanks for the pull request! The AI Disclosure checklist item in the PR description hasn't been completed yet.

Per the AI Policy, please edit the PR description and check the box that applies: either "No AI tools were used" or "AI tools were used" (naming the tool(s) and how they were used).

@bashtage
bashtage merged commit aa6ea52 into statsmodels:main Aug 26, 2026
3 of 4 checks passed
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/CLN unused assignment of cov_p in GLM fit

3 participants