Search

Search papers and lessons

Generalized Linear Model

Level 3 · 10 min read

A generalized linear model keeps the linear predictor of linear regression but lets the outcome follow any exponential-family distribution, linked to the predictor through a function such as the logit or log.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Linear algebra
  • Discrete math
  • Inference
  • Classical models
  • Sign in to see your progress

TL;DR

Linear regression assumes the outcome is a straight-line function of the inputs plus Gaussian noise. Many outcomes break both assumptions: a yes or no answer, a count of events, a strictly positive amount of money. A generalized linear model (GLM) keeps the linear part, a weighted sum of the inputs, and changes two things. It lets the outcome follow any distribution from the Exponential Family, and it passes the mean of that distribution through a link function so the weighted sum can live on an unrestricted scale.

Where you'll see it: logistic regression for yes-or-no outcomes such as whether a part fails, Poisson regression for event counts such as calls to a support desk per day, and gamma regression for positive costs in insurance pricing.

Intuition

Linear regression asks: what is the expected value of yy, given the inputs? It answers with a line. That works when yy can be any real number and the scatter around the line is roughly symmetric. It fails in two common cases.

If yy is a probability-like quantity, a line will eventually predict values below 0 or above 1. If yy is a count, a line will eventually predict negative counts, and the spread of the counts usually grows with their size, which a constant-noise assumption cannot express.

A GLM fixes both problems with one move. Keep the score η=β⊤x\eta = \beta^\top x, which is unbounded and linear in the coefficients. Then pass it through a function gg that maps the mean of yy onto the whole real line. The inverse of that function turns the score back into a valid mean. For a yes-or-no outcome the link is the logit, and its inverse is the Sigmoid Function, which squashes any score into the interval from 0 to 1. For a count the link is the logarithm, and its inverse is the exponential, which is always positive.

−6−4−2024600.20.40.60.81linear predictor ηmean of y
  • logistic: mean = 1/(1+e^(-η))
The inverse link for a yes-or-no outcome. Any score η gives a probability between 0 and 1, and η = 0 gives probability one half.

The distribution family says how much noise to expect around the mean. The link says how the inputs push the mean. Choosing them separately is the point of the "generalized" in the name.

The math

A GLM has three parts.

  1. Random part. Given inputs xx, the outcome YY follows an exponential-family distribution with natural parameter θ\theta and mean μ=E[Y∣x]\mu = \mathbb{E}[Y \mid x]. Its density has the form f(y;θ,ϕ)=exp⁡ ⁣(yθ−b(θ)a(ϕ)+c(y,ϕ)),f(y;\theta,\phi) = \exp\!\left(\frac{y\theta - b(\theta)}{a(\phi)} + c(y,\phi)\right), where ϕ\phi is a dispersion parameter, and bb determines the mean through μ=b′(θ)\mu = b'(\theta) and the variance through Var(Y)=a(ϕ) b′′(θ)\mathrm{Var}(Y) = a(\phi)\, b''(\theta).

  2. Systematic part. A linear predictor η=β⊤x\eta = \beta^\top x, where β\beta is the coefficient vector and xx includes a constant 1 for the intercept.

  3. Link. A monotone, differentiable function gg with g(μ)=ηg(\mu) = \eta. Equivalently μ=g−1(η)\mu = g^{-1}(\eta).

The link is called canonical when it sets the natural parameter equal to the linear predictor, θ=η\theta = \eta. Then g=(b′)−1g = (b')^{-1}. The canonical links for common families are:

DistributionTypical outcomeCanonical linkMean from score
Gaussianany real numberidentity, g(μ)=μg(\mu) = \muμ=η\mu = \eta
Bernoulliyes or nologit, g(μ)=ln⁡μ1−μg(\mu) = \ln\frac{\mu}{1-\mu}μ=11+e−η\mu = \frac{1}{1+e^{-\eta}}
Poissoncountslog, g(μ)=ln⁡μg(\mu) = \ln \muμ=eη\mu = e^{\eta}

With a canonical link, the log-likelihood for nn independent observations is

ℓ(β)=1a(ϕ)∑i=1n(yi xi⊤β−b(xi⊤β))+const.\ell(\beta) = \frac{1}{a(\phi)}\sum_{i=1}^{n}\left(y_i\, x_i^\top\beta - b(x_i^\top\beta)\right) + \text{const}.

This is concave in β\beta, so maximising it has a single optimum when the design is not degenerate. Differentiating gives the score equations

∑i=1n(yi−μi) xi=0,\sum_{i=1}^{n} (y_i - \mu_i)\, x_i = 0,

which say that the model's fitted values, weighted by the inputs, must match the observed values on average. For a Poisson model with an intercept, the first component says the fitted total count equals the observed total count.

Fitting by iteratively reweighted least squares

The score equations have no closed form for most links, so software solves them iteratively. The standard method is iteratively reweighted least squares. For the current estimate, compute

wi=1Var(Yi) (g′(μi))2,zi=ηi+(yi−μi) g′(μi),w_i = \frac{1}{\mathrm{Var}(Y_i)\,\big(g'(\mu_i)\big)^2}, \qquad z_i = \eta_i + (y_i - \mu_i)\, g'(\mu_i),

then update β\beta by weighted least squares of zz on xx with weights ww. Repeat until the coefficients stop changing. Each step is an ordinary regression, which is why GLMs are cheap to fit.

Algorithm
Fitting a GLM
  1. Choose the family (what the noise looks like) and the link (how the inputs move the mean).
  2. Start with μi\mu_i set to a reasonable value, such as the observed yiy_i shifted away from the boundary, and compute ηi=g(μi)\eta_i = g(\mu_i).
  3. Compute the working weights wiw_i and working responses ziz_i.
  4. Solve the weighted least-squares problem for β\beta, update η\eta and μ\mu.
  5. Stop when the change in deviance or coefficients is below a tolerance.
  6. Check the fit with residuals appropriate to the family, not with the raw residuals of linear regression.

Worked examples

Example
A tiny logistic model with one binary input

Four people. Group A (the input is 1): outcomes 1, 1, 1, 0. Group B (the input is 0): outcomes 0, 1, 0, 0. Model: logit P(y=1)=β0+β1x\mathrm{logit}\,P(y=1) = \beta_0 + \beta_1 x.

  1. Group B has 11 success out of 44, so its fitted probability is μB=1/4\mu_B = 1/4. Its score is β0=ln⁡1/43/4=ln⁡13=−1.0986\beta_0 = \ln\frac{1/4}{3/4} = \ln\frac{1}{3} = -1.0986.
  2. Group A has 33 successes out of 44, so μA=3/4\mu_A = 3/4 and ηA=ln⁡3=1.0986\eta_A = \ln 3 = 1.0986.
  3. The slope is β1=ηA−β0=1.0986−(−1.0986)=2.1972\beta_1 = \eta_A - \beta_0 = 1.0986 - (-1.0986) = 2.1972.
  4. The odds ratio is eβ1=e2.1972=9e^{\beta_1} = e^{2.1972} = 9. Check: the odds in group A are 3/1=33/1 = 3, the odds in group B are 1/31/3, and 3/(1/3)=93 / (1/3) = 9.

Interpretation: with one binary input and an intercept, the logistic GLM reproduces the observed group proportions exactly. The coefficient is a log odds ratio, so switching groups multiplies the odds of success by 9. Four people are far too few to trust that number, and the example is only about the arithmetic.

Example
A realistic count model with a log link

A support desk records the number of calls per day in two teams over three days. Team X: 4, 6, 5 calls. Team Y: 2, 3, 1 calls. Model: ln⁡μ=β0+β1x\ln \mu = \beta_0 + \beta_1 x, where x=1x = 1 for team X and 00 for team Y. The outcome is Poisson.

  1. For Poisson with a log link, the maximum likelihood estimate of each group's mean is its sample mean: μ^Y=6/3=2\hat\mu_Y = 6/3 = 2 and μ^X=15/3=5\hat\mu_X = 15/3 = 5.
  2. Intercept: β0=ln⁡2=0.6931\beta_0 = \ln 2 = 0.6931.
  3. Slope: β1=ln⁡5−ln⁡2=ln⁡2.5=0.9163\beta_1 = \ln 5 - \ln 2 = \ln 2.5 = 0.9163.
  4. Rate ratio: eβ1=2.5e^{\beta_1} = 2.5. Team X handles 2.5 times as many calls per day as team Y, on average.
  5. Check the score equation: the fitted totals equal the observed totals within each team, 5×3=155 \times 3 = 15 and 2×3=62 \times 3 = 6.

Interpretation: the coefficient on the log scale is the log of a rate ratio, which is a natural quantity for counts. The model predicts a positive mean number of calls for every input, unlike a straight line. The spread of counts is tied to their mean, so a day with twice as many calls is expected to be noisier, which the model handles automatically.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Counts vary more than the Poisson mean allows (overdispersion)Standard errors are too small and effects look significantNegative binomial, or a quasi-Poisson dispersion correction
Logistic model with a predictor that separates the outcomes perfectlySome coefficients diverge to infinity and the fit does not convergePenalised (ridge or Firth) logistic regression, or collect more data
Outcome is continuous but skewed and positiveA Gaussian GLM can predict negative valuesGamma or log-normal family, or a log link on a transformed outcome
Relationship is curved in the inputsA linear predictor misses the curveAdd transformed inputs or splines to the linear predictor
Observations are correlated (repeated measures, clusters)Independence assumption fails, errors are too smallMixed-effects GLM or generalised estimating equations
Only prediction matters, not interpretationA GLM may be outperformed by flexible modelsTree ensembles or neural networks, judged on held-out data

A GLM is only as good as its family and link. Check residuals and dispersion before trusting the coefficients.

History

Probit and logit models for binary dose-response data were developed in biological work in the 1930s and 1940s, and they were used long before there was a general framework. In 1972 John Nelder and Robert Wedderburn showed that a large class of these models could be fitted by one algorithm, iteratively reweighted least squares, and named the class generalised linear models. Their paper made the shared structure visible: linear predictor, link, and exponential-family noise. The method caught on because a single fitting routine covered logistic, Poisson and gamma regression, and software packages could ship one implementation. The book by McCullagh and Nelder (first edition 1983) became the standard reference and set the terminology still used today.

Check yourself

QuestionRecall: what are the three parts of a generalized linear model?Show answer

A random part: the outcome follows an exponential-family distribution with a mean μ\mu. A systematic part: a linear predictor η=β⊤x\eta = \beta^\top x. A link function gg with g(μ)=ηg(\mu) = \eta that connects the two. Linear regression is the special case with a Gaussian distribution and the identity link.

QuestionComputation: in a logistic model with an intercept and one binary input, group A has 3 successes out of 4 and group B has 2 successes out of 4. Find β0\beta_0, β1\beta_1, and the odds ratio.Show answer

Group B has μB=2/4=1/2\mu_B = 2/4 = 1/2, so β0=ln⁡1/21/2=0\beta_0 = \ln\frac{1/2}{1/2} = 0. Group A has μA=3/4\mu_A = 3/4, so ηA=ln⁡3=1.0986\eta_A = \ln 3 = 1.0986 and β1=ηA−β0=1.0986\beta_1 = \eta_A - \beta_0 = 1.0986. The odds ratio is eβ1=3e^{\beta_1} = 3, which matches the odds in group A (3) divided by the odds in group B (1).

QuestionComputation: a Poisson GLM with a log link on a single group has observed counts 3, 5, 4, 8. What is the fitted mean and the intercept?Show answer

The maximum likelihood estimate of the Poisson mean is the sample mean, μ^=(3+5+4+8)/4=20/4=5\hat\mu = (3+5+4+8)/4 = 20/4 = 5. The intercept on the log scale is β0=ln⁡5=1.6094\beta_0 = \ln 5 = 1.6094. Every observation gets a fitted value of 5, and the total fitted count, 20, matches the observed total, as the score equation requires.

QuestionWhy: a Poisson model predicts that counts with a mean of 10 have variance 10. Real call-centre counts often show variance well above their mean. What happens if you fit a Poisson GLM anyway, and what does that teach you about choosing a family?Show answer

The point estimates of the coefficients can still be reasonable, but the model's assumed variance is too small. Standard errors, confidence intervals and p-values are then too narrow, so effects look more certain than they are. The lesson is that the family encodes a claim about the noise, and the mean-variance relationship must be checked against the data. An overdispersed family such as the negative binomial makes that claim explicit.

QuestionApplied: a clinic wants to predict whether a patient returns within 30 days, with a yes-or-no outcome recorded for each patient. Why is a linear regression with a 0-or-1 outcome a poor choice, and what would a GLM use instead?Show answer

A linear regression can predict values below 0 or above 1, which cannot be probabilities. A logistic GLM uses a Bernoulli family with the logit link, so the fitted probability always lies between 0 and 1 and the coefficients are log odds ratios. This is an educational example of the model, not a clinical tool, and any real use would need validation on held-out patients.

Sources