Generalized Linear Models
Exponential families and link functions: one idea behind many regressions.
Linear, logistic, Poisson and gamma regression look like four different models. Nelder and Wedderburn showed in 1972 that they are one: a distribution from an exponential dispersion family, a linear predictor, and a link. From that structure come the mean and variance of every family, a concave likelihood under the canonical link, one fitting algorithm for all of them, iteratively reweighted least squares, and one measure of fit, the deviance.
What you’ll learn
- Write an exponential dispersion family in canonical form, derive and , and read off the variance functions of the common families.
- Derive the GLM score and Fisher information, and show that under the canonical link the log-likelihood is concave and the observed and expected information coincide.
- Derive iteratively reweighted least squares as Fisher scoring, with its working response and weights, and recognize when it fails.
- Define the deviance as twice a sum of Kullback–Leibler divergences, use it for nested comparisons, and estimate the dispersion.
- Handle overdispersion with quasi-likelihood and the negative binomial, and describe how classical GLM theory fails when is not small.
Exponential dispersion families
for every . Differentiate under the integral sign; the derivative of the exponent is , and its expectation must vanish:
Differentiate once more. The second derivative of the exponent is , and the expectation of (second derivative) + (first derivative) vanishes too:
For a nondegenerate family , so is strictly increasing and is one-to-one. The variance is therefore a function of the mean, the variance function :
| Family | ||||
|---|---|---|---|---|
| Normal | ||||
| Poisson | ||||
| Bernoulli | ||||
| Gamma (mean , shape ) | ||||
| Inverse Gaussian (mean , shape ) |
Think first Claim sizes follow a gamma distribution with mean and shape . What is their standard deviation, and what happens to it for claims with mean ?
, so the standard deviation is . Doubling the mean doubles it to : the coefficient of variation stays at . That constant relative spread is why the gamma (often with a log link) suits costs and durations.
Likelihood, score and the canonical link
A GLM has independent responses from one such family with natural parameters and common dispersion , means , linear predictors , and a monotone link with . The link is canonical when , that is, when : the identity for the normal, the log for the Poisson, the logit for the Bernoulli.
The log-likelihood is plus terms free of . Follow the chain , using and :
The Fisher information is the covariance of the score; with and independent observations,
Non-canonical links are common and often sensible: the probit for binary data, the log for the gamma (the canonical would force a sign constraint on the linear predictor), the identity for counts when effects are additive. They keep the score equations above but lose the simplifications: the observed and expected information differ, and concavity is no longer automatic.
Which of these holds for every GLM with a canonical link, but not for GLMs in general?
- The maximum-likelihood estimate exists for every data set
- The Hessian of the log-likelihood does not depend on the observed responses
- The variance of does not depend on its mean
- The mean is a linear function of
Show the answer
B. The Hessian of the log-likelihood does not depend on the observed responses
With , the term is linear in , so the second derivatives come from alone: . That is why Newton's method and Fisher scoring coincide for canonical links.
Fitting: Fisher scoring is IRLS
Fisher scoring is Newton's method with the Hessian replaced by minus the expected information:
Write the score with the same weights: , so with . Multiply through by :
Each step is therefore a weighted least-squares regression of the working response on , a first-order linearization of around the current mean. The dispersion cancels, so it never affects :
| Model | Working response | Weight |
|---|---|---|
| Normal, identity link | : one step of ordinary least squares | |
| Logistic (Bernoulli, logit) | ||
| Poisson, log link | ||
| Gamma, log link |
import numpy as np
def irls_poisson(X, y, offset=0.0, tol=1e-10, max_iter=50):
"""Poisson GLM with log link by Fisher scoring (= Newton for this canonical link)."""
eta = np.log(y + 0.5) # start from the data, nudged away from log 0
w = np.zeros(X.shape[1])
for _ in range(max_iter):
mu = np.exp(eta)
z = eta - offset + (y - mu) / mu # working response: g'(mu) = 1/mu
W = mu # weights 1 / (V(mu) g'(mu)^2) = mu
w_new = np.linalg.solve(X.T @ (W[:, None] * X), X.T @ (W * z))
eta = X @ w_new + offset
if np.max(np.abs(w_new - w)) < tol:
break
w = w_new
return w_new, np.sqrt(np.diag(np.linalg.inv(X.T @ (W[:, None] * X))))The second output holds the usual standard errors, from the inverse Fisher information at the final weights (here ). A production implementation adds step halving when the deviance increases and solves each weighted least-squares problem by QR.
Near the optimum, Fisher scoring converges quadratically for canonical links, where it is Newton's method, and typically needs only a handful of iterations. It can fail when the maximum-likelihood estimate does not exist: under complete or quasi-complete separation in logistic regression, the likelihood keeps increasing as the coefficients go to infinity (Albert & Anderson, 1984), and the weights of the separated points collapse toward zero. Penalization (ridge, or Firth's bias-reducing penalty) restores a finite answer.
Under standard regularity conditions with fixed and , the estimate is consistent and asymptotically normal, , which gives the standard errors and Wald tests printed by GLM software.
The deviance
The saturated model gives every observation its own parameter and fits it perfectly, (natural parameter ). The deviance measures how far the fitted model falls short of it:
which does not depend on ; is the scaled deviance. Each term is a Bregman divergence of , and since the saturated distribution has mean , it equals times a Kullback–Leibler divergence:
Fitting a GLM by maximum likelihood is minimizing a sum of KL divergences from the data's own distributions, a direct generalization of least squares, whose deviance is the residual sum of squares.
| Family | Deviance |
|---|---|
| Normal | |
| Poisson | , with |
| Bernoulli | : twice the summed log loss |
| Gamma |
Comparing nested models. If model with coefficients is nested in with , the likelihood-ratio statistic is , approximately under for large . When must be estimated (normal, gamma, quasi-likelihood), use the -like ratio .
Goodness of fit, carefully. only under small-dispersion asymptotics, such as Poisson counts that are all large. For 0/1 data it fails completely: with the canonical link, gives , so depends on the data only through the fitted probabilities and cannot detect lack of fit.
Dispersion. It is usually estimated from Pearson's statistic, with . For Poisson and binomial models in theory, so well above 1 signals overdispersion.
A Poisson GLM with coefficients is fitted to counts, and Pearson's statistic is . By what factor should the Poisson standard errors be inflated?
Show the answer
1.58
, and standard errors scale with : . Uncorrected, the Poisson analysis would claim intervals about 37% too narrow and declare too many effects significant.
Overdispersion and beyond
The Poisson forces . Real counts are often more variable, because of unmeasured differences between units or clustering of events. Two standard repairs:
Quasi-likelihood (Wedderburn, 1974) specifies only the first two moments, and , and solves the same estimating equations as the GLM score. The quasi-Poisson model therefore has exactly the Poisson coefficients, with standard errors multiplied by and -tests in place of -tests.
The negative binomial models the extra variation explicitly as a Poisson–gamma mixture: each unit has its own rate , drawn from a gamma distribution with mean and shape , and .
Condition on the unit's rate and use the law of total variance:
A gamma with mean and shape has variance , so
As the rates stop varying and the Poisson returns. For fixed the negative binomial is a one-parameter exponential family in , hence a GLM (usually with a log link); is estimated by maximum likelihood, alternating with IRLS.
The two repairs weight observations differently. Quasi-Poisson's variance grows linearly with the mean; the negative binomial's grows quadratically, so it gives large counts less weight. Plotting squared residuals against fitted means shows which pattern the data follow. When the excess is concentrated at zero, zero-inflated or hurdle models add a separate process that produces extra zeros.
Key takeaways
- An exponential dispersion family has mean and variance ; the variance function (, , , ) is what distinguishes the families.
- The GLM score is and the information ; the canonical link makes the log-likelihood concave with a Hessian free of , and turns the likelihood equations into .
- Fisher scoring is IRLS: regress the working response on with weights ; it fails when the MLE does not exist, as under separation.
- The deviance is times a sum of KL divergences from the saturated fit; differences of deviances test nested models, but for 0/1 data the deviance cannot test goodness of fit.
- Overdispersion inflates Poisson standard errors by (quasi-Poisson) or is modeled by the negative binomial, ; when is not small, classical GLM inference fails and needs high-dimensional corrections.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
In IRLS for a gamma GLM with a log link, what are the weights ?
- All equal to 1
Show the answer
C. All equal to 1
The gamma variance function is and the log link has , so . Each IRLS step is an unweighted least-squares fit of the working response .
For a GLM with canonical link and an intercept, the likelihood equations imply what about the fitted means?
- Each fitted mean equals its observation
- The fitted means sum to the sum of the observations
- The fitted means are all equal
- Nothing in general; only the coefficients are constrained
Show the answer
B. The fitted means sum to the sum of the observations
The intercept's column is all ones, so one of the equations is . Each other column gives a matching weighted sum: the model reproduces the sufficient statistic exactly.
Why is the residual deviance of a logistic regression on 0/1 data not a valid goodness-of-fit test?
- Because the deviance can be negative for binary data
- Because logistic regression has no saturated model
- Because the dispersion is unknown
- Because for 0/1 data the deviance is a function of the fitted probabilities alone, so it carries no information about lack of fit
Show the answer
D. Because for 0/1 data the deviance is a function of the fitted probabilities alone, so it carries no information about lack of fit
Under the canonical link the likelihood equations give , so . Two data sets with the same fitted probabilities have the same deviance however badly the model fits either one. Grouped binomial data with many trials per group do support a deviance test.
Counts in some group have mean 4 and variance 12. Under the negative binomial model , what is ?
Show the answer
A.
gives , so . A Poisson would have variance 4; the extra 8 comes from variation of the underlying rates between units.
Further reading
- J. A. Nelder & R. W. M. Wedderburn, "Generalized linear models," Journal of the Royal Statistical Society, Series A 135(3), 370–384 (1972).
- P. McCullagh & J. A. Nelder, Generalized Linear Models, 2nd ed. (Chapman & Hall, 1989).
- R. W. M. Wedderburn, "Quasi-likelihood functions, generalized linear models, and the Gauss–Newton method," Biometrika 61(3), 439–447 (1974).
- P. Sur & E. J. Candès, "A modern maximum-likelihood theory for high-dimensional logistic regression," Proceedings of the National Academy of Sciences 116(29), 14516–14525 (2019).