Repository navigation
fix: use normalized_cov_params as fallback when hessian inversion fails in GLM.fit - #9794
Conversation
|
@bashtage you are the primary committer to |
|
@bashtage @Josef-a-pktd — requesting review when convenient. The fix removes a dead |
|
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]>
c2754ed to
d21ef50
Compare
|
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). |
Summary
Fixes #9776 (code scanning alert: dead assignment in
GLM.fit).In the non-IRLS gradient-optimizer path (
_fit_gradient), the previous codeassigned
cov_pfromrslt.normalized_cov_paramsjust before atry/exceptblock that unconditionally overwrote it. This made the initial assignment dead
code and, worse, caused the
except LinAlgErrorbranch to setcov_p = Noneeven when a valid covariance estimate was available from the optimizer.
Before (broken):
After (fixed):
A new regression test
test_glm_fit_cov_p_fallback_on_linalg_erroris addedalongside the existing
test_non_invertible_hessian_fails_summary. It patchesnumpy.linalg.invto raiseLinAlgErrorand verifies thatres.normalized_cov_paramsis notNonewhen the BFGS optimizer hasproduced a valid
Hinv.Changes
statsmodels/genmod/generalized_linear_model.py: remove dead block,move
normalized_cov_paramscheck into theexceptbranchstatsmodels/genmod/tests/test_glm.py: add regression testTest plan
test_glm_fit_cov_p_fallback_on_linalg_errordirectlyexercises the fixed code path
test_non_invertible_hessian_fails_summarycontinues to pass