You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ENH/REF GLM simplify computation if is_canonical #4269
triggered by looking at the computation of IRLS weights in #4267
We currently don't use any special casing and simplified computation in GLM when a canonical link function is used.
This would create some speed improvements, but the more important point is that it will most likely increase numerical stability and precision.
Example Pregibon 1981 on regression diagnostics points out that the weights in IRLS are zero for the corner case p=0 and p=1 in Binomial. However, we divide by the variance which divides by zero in this case, most likely 0/0. I guess we get currently around this with clipping away from the boundaries.
Other maybe more important cases are gamma and negative binomial where canonical link functions don't work so well, and the non-canonical log link works better.
I haven't checked, but my guess is that some of the messy instabilities in corner cases will likely disappear.
(a bit related: thequackdaddy added an EIM option for the hessian in fit to avoid the complicated OIM which is generic for non-canonical cases. However, we don't skip the unnecessary part for OIM in the canonical link case, where OIM==EIM, IIRC)
for gamma: link, link_deriv and varfunc are all power functions #3316 which refers to discussion and comments in #2208 (about the DomainWarning that we added for the canonical link in gamma)
family has the link as argument and attribute and could check for is_canonical
family computes the weights in generic method inherited by specific families
this could delegate to a family specific _weights_canonical and delegate to it if is_canonical
for gradient, nonlinear optimizers
loglike/loglikeobs is defined in families and it doesn't look like there is a straightforward simplification, argument is mu which already incorporates the link, so computation is independent of link.
score_factor has a product self.family.link.deriv(mu) * self.family.variance(mu) which will might in the canonical case. score uses score_obs which uses score_factor, so score_factor is the only relevant part (no code duplication here)
hessian_factor, there is a shortcut for observed=False, which AFAIR should apply for the canonical link case. the computation for eim_factor eim_factor = 1 / (self.family.link.deriv(mu)**2 * self.family.variance(mu))
is essentially the same as the irls weights and will simplify in canonical case.
oim has some messier terms which however won't be relevant for the canonical case
the remaining parts, including deviance, are mainly for post-estimation where speed or numerical robustness is less relevant.
so it looks like we might want or need to new special methods in the individual families
weights = eim_factor = 1 / (self.family.link.deriv(mu)**2 * self.family.variance(mu))
and likely also ??? = self.family.variance.deriv(mu) * self.family.link.deriv(mu)
Caveat: is_canonical assumes most likely that the lin_pred is linear, however derivatives are mostly with respect to mu
We need to avoid too much special casing or make the condition for special casing narrow enough, so that it also does the right thing if lin_pred is actually nonlinear, e.g. nonlinear GLM as subclass of GLM #2179 (comment)
I think the irls weights part is easy to implement, but requires a reference with the list of weight functions for the canonical cases, maybe SPSS which has a good list of formulas in the technical documentation. (I don't have it in my current pile of papers to quickly check.)
My understanding: I see that we want to optimize the Hessian/Information Matrix computation for GLMs. Specifically, when using a canonical link function, the observed information matrix coincides with the expected information matrix (Fisher Information). This means the second derivative term of the link function cancels out (or simplifies significantly) in the Hessian calculation, allowing for faster convergence and less computational overhead compared to non-canonical links.
Proposed Approach: I plan to inspect statsmodels/genmod/generalized_linear_model.py (specifically within the fit or Hessian methods). I will introduce a check for if self.family.link.is_canonical: to route the computation to the simplified formula, avoiding the unnecessary complex derivatives used for non-canonical links.
I am an AI Engineering student with a background in Economics/Econometrics, so I am comfortable with both the math and the implementation.
Could you please assign this to me? I can start working on a PR immediately.
Activity
josef-pkt commentedon Feb 24, 2018
for gamma: link, link_deriv and varfunc are all power functions
#3316 which refers to discussion and comments in #2208 (about the DomainWarning that we added for the canonical link in gamma)
josef-pkt commentedon Feb 24, 2018
implementation for irls weights
linkas argument and attribute and could check foris_canonical_weights_canonicaland delegate to it ifis_canonicalfor gradient, nonlinear optimizers
self.family.link.deriv(mu) * self.family.variance(mu)which will might in the canonical case. score uses score_obs which uses score_factor, so score_factor is the only relevant part (no code duplication here)observed=False, which AFAIR should apply for the canonical link case. the computation for eim_factoreim_factor = 1 / (self.family.link.deriv(mu)**2 * self.family.variance(mu))is essentially the same as the irls weights and will simplify in canonical case.
oim has some messier terms which however won't be relevant for the canonical case
the remaining parts, including deviance, are mainly for post-estimation where speed or numerical robustness is less relevant.
so it looks like we might want or need to new special methods in the individual families
weights = eim_factor = 1 / (self.family.link.deriv(mu)**2 * self.family.variance(mu))and likely also
??? = self.family.variance.deriv(mu) * self.family.link.deriv(mu)Caveat:
is_canonicalassumes most likely that the lin_pred is linear, however derivatives are mostly with respect to muWe need to avoid too much special casing or make the condition for special casing narrow enough, so that it also does the right thing if lin_pred is actually nonlinear, e.g. nonlinear GLM as subclass of GLM
#2179 (comment)
josef-pkt commentedon Feb 24, 2018
adding label good as first pr
I think the irls weights part is easy to implement, but requires a reference with the list of weight functions for the canonical cases, maybe SPSS which has a good list of formulas in the technical documentation. (I don't have it in my current pile of papers to quickly check.)
Zanzotta commentedon Feb 5, 2026
Hi Josef, I was wondering if figuring out if a simplified fisher scoring when the link is canonical is still an open question.
rikardo21-2bleA commentedon Feb 9, 2026
Hi all,
I would like to tackle this issue.
My understanding: I see that we want to optimize the Hessian/Information Matrix computation for GLMs. Specifically, when using a canonical link function, the observed information matrix coincides with the expected information matrix (Fisher Information). This means the second derivative term of the link function cancels out (or simplifies significantly) in the Hessian calculation, allowing for faster convergence and less computational overhead compared to non-canonical links.
Proposed Approach: I plan to inspect statsmodels/genmod/generalized_linear_model.py (specifically within the fit or Hessian methods). I will introduce a check for if self.family.link.is_canonical: to route the computation to the simplified formula, avoiding the unnecessary complex derivatives used for non-canonical links.
I am an AI Engineering student with a background in Economics/Econometrics, so I am comfortable with both the math and the implementation.
Could you please assign this to me? I can start working on a PR immediately.
anish19p-spec commentedon Nov 7, 2026
Hi @josef-pkt, I'd like to get started, can you point me in the right direction?