Skip to content

ENH: Optimize GLM Hessian calculation for canonical links - #9753

Open
rikardo21-2bleA wants to merge 1 commit into
statsmodels:mainfrom
rikardo21-2bleA:enh/glm-canonical-optimization
Open

rikardo21-2bleA wants to merge 1 commit into
statsmodels:mainfrom
rikardo21-2bleA:enh/glm-canonical-optimization

Conversation

@rikardo21-2bleA

Copy link
Copy Markdown

Description

This PR implements an optimization for the GLM Hessian calculation (Observed Information Matrix).
When a canonical link is used, the Observed Information Matrix (OIM) coincides with the Expected Information Matrix (EIM) / Fisher Information.
Mathematically, the curvature term involving $(y - \mu)$ vanishes. This PR detects canonical links and skips this unnecessary computation.

Closes #4269

Changes

  1. Family Logic: Added is_canonical_link property to Family class and _canonical_link_class to subclasses (Binomial, Poisson, Gaussian, etc.).
    • Note: I used type(link) is ... instead of isinstance intentionally to strictly match the canonical link implementation and avoid inheritance issues.
  2. GLM Optimization: Updated hessian_factor in generalized_linear_model.py. If is_canonical_link is True, it sets observed=False, forcing the use of the EIM formula ($-X^T W X$), which is faster and numerically equivalent.
  3. Documentation: Added detailed NumPy-style docstrings with mathematical justification in hessian_factor.
  4. Testing: Added statsmodels/genmod/tests/test_glm_canonical.py with 11 tests covering all major families to ensure numerical equivalence between the optimized path and the original path.

Verification

  • Ran new tests: pytest statsmodels/genmod/tests/test_glm_canonical.py (11 passed).
  • Verified code formatting (flake8).
  • Docstrings added with LaTeX math support.

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/REF GLM simplify computation if is_canonical

1 participant