Repository navigation
Conversation
The cov_type entry of GLM.fit did not say which information matrix the robust (sandwich) covariances use in the bread: the observed information, or the expected information when optim_hessian="eim" is used with a method other than IRLS. For non-canonical links the two can differ. For GLMs, vcovHC(type = "HC0") in R's sandwich package uses the expected information. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01S94CAMKjg861xEYYtueSkH
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01S94CAMKjg861xEYYtueSkH
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.
The
cov_typeentry ofGLM.fitdoes not say which information matrix the robust (sandwich) covariances use as the bread._get_sandwich_arraysinstatsmodels/stats/sandwich_covariance.pyinvertsmodel.hessian(params)(the sign cancels in the sandwich). Withobserved=None,GLM.hessiangives the observed Hessian, unless_optim_hessianis"eim", whichfitsets while it runs a method other than IRLS withoptim_hessian="eim"; the robust covariance is computed during that fit. Every robust type GLM supports (HC0, HAC, one- and two-way cluster, hac-panel, hac-groupsum) gets its bread from_get_sandwich_arrays. For canonical links the two information matrices are the same. For non-canonical links they can differ, so with the default IRLS fitcov_type="HC0"can differ from sandwich estimators built on the expected information, such asvcovHCin the R package sandwich, and the docstring does not say why or how to get the other one.Example: a Gamma GLM with a log link on R's
treesdata (Volume ~ log(Girth) + log(Height), 31 observations, fitted on R's model matrix), statsmodels 0.15.0 (wheel), R 4.6.2, sandwich 3.1-3. Each number is the largest absolute difference divided by the largest absolute element of the reference. The two reference sandwiches are rebuilt fromscore_obs(b, scale=1.0)andhessian(b, scale=1.0, observed=True/False), wherebis the IRLS estimate attol=1e-14. The expected-information one matchesvcovHC(type = "HC0")to 8.9e-12.cov_type="HC0"vcovHC(type = "HC0")method="newton"method="newton", optim_hessian="eim"method="bfgs", optim_hessian="eim", gtol=1e-12, maxiter=5000optim_hessian="eim"With IRLS, passing
optim_hessian="eim"gives the same difference from R as without it. With prior weights (var_weights1 to 31),method="newton", optim_hessian="eim"matchesvcovHC(type = "HC0")to 3.1e-12. The same switch holds forcov_type="cluster"and"HAC": the meat recovered from each result with its own bread (IRLS with the observed information,method="newton", optim_hessian="eim"with the expected information) agrees to 3.0e-11 or better (HC0 1.3e-11, cluster 3.0e-11, HAC 4.6e-12). For the canonical links tested (Poisson/log onwarpbreaks, Poisson with an offset onMASS::Insurance, binomial/logit onesoph), the defaultcov_type="HC0"matchesvcovHC(type = "HC0")to 4.3e-12. GH#7223 discusses the use of OIM and EIM in GLM.This documents the bread in the
cov_typeentry ofGLM.fit. It changes a docstring only.Checks:
ruff checkpasses on the file.tools/validate_docstrings.pyonGLM.fitreports the same messages before and after the change (its output includes the new text). I could not build the package from source here (the Python development headers are not installed). With the 0.15.0 wheel and the Python files ofmaincopied over it,test_glm.pyandtest_glm_weights.pygive 1002 passed and 2 xfailed, with and without the change.This pull request was written with Claude Code under my direction.
NumPy's guide.
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 Opus 5.5), OpenAI Codex.Used for:
Claude Code compared GLM robust covariances with R's sandwich package, found the difference, drafted the docstring change and this description, and ran the checks. OpenAI Codex reviewed the diff and the description.I have personally read, understood, and can explain every line of this diff, and
have verified the statistical/numerical correctness of the change.