Skip to content

DOC: State which information matrix GLM robust covariances use - #11328

Open
MrRolie wants to merge 2 commits into
statsmodels:mainfrom
MrRolie:doc-glm-robust-hessian
Open

MrRolie wants to merge 2 commits into
statsmodels:mainfrom
MrRolie:doc-glm-robust-hessian

Conversation

@MrRolie

@MrRolie MrRolie commented Nov 28, 2026

Copy link
Copy Markdown

The cov_type entry of GLM.fit does not say which information matrix the robust (sandwich) covariances use as the bread. _get_sandwich_arrays in statsmodels/stats/sandwich_covariance.py inverts model.hessian(params) (the sign cancels in the sandwich). With observed=None, GLM.hessian gives the observed Hessian, unless _optim_hessian is "eim", which fit sets while it runs a method other than IRLS with optim_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 fit cov_type="HC0" can differ from sandwich estimators built on the expected information, such as vcovHC in 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 trees data (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 from score_obs(b, scale=1.0) and hessian(b, scale=1.0, observed=True/False), where b is the IRLS estimate at tol=1e-14. The expected-information one matches vcovHC(type = "HC0") to 8.9e-12.

statsmodels fit, cov_type="HC0" vs observed-information sandwich vs expected-information sandwich vs R vcovHC(type = "HC0")
default (IRLS) 1.0e-12 1.2e-02 1.2e-02
method="newton" 7.1e-12 1.2e-02
method="newton", optim_hessian="eim" 1.2e-02 1.3e-11 3.9e-12
method="bfgs", optim_hessian="eim", gtol=1e-12, maxiter=5000 1.2e-02 3.8e-10
default (IRLS), optim_hessian="eim" 1.2e-02

With IRLS, passing optim_hessian="eim" gives the same difference from R as without it. With prior weights (var_weights 1 to 31), method="newton", optim_hessian="eim" matches vcovHC(type = "HC0") to 3.1e-12. The same switch holds for cov_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 on warpbreaks, Poisson with an offset on MASS::Insurance, binomial/logit on esoph), the default cov_type="HC0" matches vcovHC(type = "HC0") to 4.3e-12. GH#7223 discusses the use of OIM and EIM in GLM.

This documents the bread in the cov_type entry of GLM.fit. It changes a docstring only.

Checks: ruff check passes on the file. tools/validate_docstrings.py on GLM.fit reports 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 of main copied over it, test_glm.py and test_glm_weights.py give 1002 passed and 2 xfailed, with and without the change.

This pull request was written with Claude Code under my direction.

  • code/documentation is well formatted.
  • properly formatted commit message. See
    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.

  • No AI tools were used to develop this pull request.
  • AI tools were 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.

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
Copilot AI balanced review requested due to automatic review settings November 28, 2026 21:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

2 participants