Foundations to Frontier
Tests
D. Classical Machine Learning › D1 Linear Models for Regression

Generalized Linear Models: Poisson, Gamma, Tweedie

Generalized linear models extend linear regression to counts, positive amounts and other non-Gaussian responses. A link function connects the mean to a linear predictor, and a variance function says how the spread grows with the mean. Poisson regression models counts (variance = mean), Gamma regression models positive skewed amounts (spread proportional to the mean), and Tweedie models with 1 < p < 2 handle exact zeros plus positive amounts. This page covers exponential families, links, IRLS (Fisher scoring), deviance, offsets for exposure, overdispersion, quasi-likelihood and sandwich standard errors. In the exercises you implement log-link GLMs by IRLS for all three families with exposure offsets; then you model single-event upsets in satellite memory, comparing the Poisson GLM with OLS for prediction and three kinds of standard errors under overdispersion.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Counts are not bell curves

How many radiation hits will a satellite’s memory take on this orbit? The answer is a count: 0, 1, 2, … never negative, and noisier when the typical count is larger. A straight line with constant noise (least squares) gets both wrong: it can predict −0.4 hits and pretends every orbit is equally uncertain. A generalized linear model (GLM) keeps the linear idea but runs it through an exponential, so the mean is always positive. It also uses the right noise model for the data: Poisson for counts, Gamma for positive amounts, and Tweedie for “mostly zero, sometimes a lot”.

GLMs: the right mean curve and the right spread
Poisson GLM, log link: a = 0.21 (true 0.20), b = 0.35 (true 0.35) · each unit of x multiplies the mean by 1.42 · OLS at x = 0: -1.51
Variance function V(μ): Gaussian constant, Poisson μ, Gamma μ² (spread proportional to the mean). The GLM weights each point by 1/V, so noisy high-mean points count less. OLS fits a straight line with constant spread and can predict impossible negative counts or amounts.
Why engineers care

Failure counts, particle hits, packet losses, anomaly reports and repair times are all non-Gaussian. GLMs give interpretable multiplicative effects (for example, “each SAA minute raises the upset rate by 8%”) and correct uncertainty. They are the standard tool for reliability and rate modelling.

📝 Quick check (Level 0)
0/3 answered
  1. 1. For counts, a natural GLM is…
  2. 2. A log link guarantees that the predicted mean is…
  3. 3. Gamma regression is suited to…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Reading a Poisson model

Model: log λ = −1 + 0.08·SAA − 0.3·solar, with λ in upsets per hour.

Orbit SAA minutes Solar index Linear predictor η Rate λ = e^η
A 0 0 −1.0 0.37 /h
B 10 0 −0.2 0.82 /h
C 10 1 −0.5 0.61 /h
  • Each SAA minute multiplies the rate by e^0.08 = 1.083, an 8.3% increase.
  • Ten minutes multiply it by e^0.8 = 2.23.
  • For a 3-hour pass, the expected count is 3λ. In the model, log(3) enters as an offset: a term with a fixed coefficient of 1.

Deviance by hand. The Poisson deviance compares the fitted means with the observed counts. For y = 0, 2, 5 and μ = 1, 2, 4, the per-point terms 2[y log(y/μ) − (y − μ)] are 2, 0 and 0.231, so the total is 2.231. A perfect fit has deviance 0.

The steps in plain English:

  1. Pick a family from the response type: counts → Poisson, positive amounts → Gamma, zeros plus amounts → Tweedie.
  2. Pick a link (log is the usual one) and add log(exposure) as an offset if counts depend on time or size.
  3. Fit by iteratively reweighted least squares.
  4. Read coefficients as multiplicative effects, e^β.
  5. Check for overdispersion (Pearson χ²/df ≫ 1) and fix the standard errors if needed.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Rate ratio for a coefficient of 0.08 and 10 units?
  2. 2. The exposure enters a Poisson model as…
  3. 3. Deviance of a perfect fit?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

The GLM recipe

Three parts:

  • a random component: y from an exponential family with mean μ and variance φ·V(μ);
  • a linear predictor: η = xᵀβ (plus an offset);
  • a link: g(μ) = η.
Family V(μ) Canonical link Typical use
Gaussian 1 identity continuous, constant spread
Poisson μ log counts
Gamma μ² inverse (log in practice) positive amounts, constant CV
Tweedie, 1 < p < 2 μ^p log in practice zeros plus positive amounts
Binomial μ(1 − μ) logit proportions (D2)

IRLS (Fisher scoring). Repeat the following:

  • compute μ = g⁻¹(η);
  • compute the working response z = η + (y − μ)·g′(μ);
  • compute the weight w = 1/(V(μ)·g′(μ)²);
  • solve the weighted least squares problem (XᵀWX)β = XᵀWz.

With a log link, g′(μ) = 1/μ, so z = η + (y − μ)/μ and w = μ^(2 − p).

Deviance. D = 2φ(ℓ_saturated − ℓ_model) generalises the residual sum of squares: it is used for model comparison, as a loss, and in likelihood-ratio tests.

from sklearn.linear_model import PoissonRegressor, GammaRegressor, TweedieRegressor
PoissonRegressor(alpha=0.0).fit(X, counts / exposure, sample_weight=exposure)   # rate model, exposure-weighted
TweedieRegressor(power=1.5, link="log", alpha=0.0).fit(X, y)
import statsmodels.api as sm
sm.GLM(counts, sm.add_constant(X), family=sm.families.Poisson(), offset=np.log(exposure)).fit(cov_type="HC0")

Use them when the response is a count, a positive amount or a rate, and effects are naturally multiplicative. Check the dispersion and residuals: real counts are often overdispersed.

🐍 Exercise 1: log-link GLMs by IRLS
Implement IRLS for log-link GLMs with variance power p (Poisson p = 1, Gamma p = 2, Tweedie in between) and an optional offset, the corresponding deviances, and a compound Poisson–gamma sampler; recover known coefficients for Poisson, Gamma and Tweedie data and for counts with varying exposure.
📝 Quick check (Level 2)
0/3 answered
  1. 1. IRLS weight for a log-link Poisson GLM?
  2. 2. Tweedie with 1 < p < 2 can produce…
  3. 3. For a Gamma GLM the coefficient of variation is…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Theory and practice

Exponential dispersion family. The density is f(y; θ, φ) = exp[(yθ − b(θ))/φ + c(y, φ)], with mean b′(θ) and variance φ·b″(θ) = φV(μ). The canonical link sets θ = η. With it, the score is Xᵀ(y − μ)/φ, so with an intercept the fitted means sum to the observed total (Σ(yᵢ − μ̂ᵢ) = 0, as checked in exercise 1).

Fisher scoring equals IRLS. The expected Hessian is XᵀWX/φ. Newton’s method with the expected Hessian is exactly weighted least squares on the working response. For canonical links, it equals Newton’s method.

Overdispersion. Real counts often have Var > μ, because of unobserved heterogeneity such as varying shielding or particle bursts. Coefficients stay consistent if the mean model is right, but naive standard errors are too small. Three fixes:

  • quasi-Poisson: multiply the standard errors by √φ̂, with φ̂ = Pearson χ²/(n − p); this assumes Var = φμ;
  • sandwich (robust) standard errors: (XᵀWX)⁻¹ Xᵀ diag((y − μ)²) X (XᵀWX)⁻¹, which are valid for any variance shape;
  • negative binomial regression: Var = μ + μ²/k, a full likelihood.

Zeros. A Tweedie model with 1 < p < 2 has a point mass at 0. Zero-inflated and hurdle models separate the “whether” from the “how much”.

Interview questions.

  • Why not log-transform counts and use OLS? log(0) is undefined, the +1 hack biases the result, and E[log y] ≠ log E[y], so predictions of the mean are biased low.
  • What is an offset? A predictor with a known coefficient of 1, used for exposure, area or time.
📝 Quick check (Level 3)
0/3 answered
  1. 1. With overdispersion, naive Poisson standard errors are…
  2. 2. Sandwich standard errors are valid when…
  3. 3. With a canonical link and intercept, Σ(yᵢ − μ̂ᵢ) is…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Counting radiation upsets

History.

  • Nelder and Wedderburn (1972) unified regression for exponential families as GLMs, with IRLS as the general fitting algorithm.
  • Wedderburn (1974) introduced quasi-likelihood.
  • McCullagh and Nelder’s book (1983, 2nd ed. 1989) made GLMs standard.
  • Tweedie (1984) described the power-variance family.
  • Jørgensen (1987) developed exponential dispersion models; Tweedie GLMs became standard in insurance pricing.
  • Huber (1967) and White (1980) gave the sandwich estimator.

Exercise 1. 3000 points with true β = (0.5, 0.8, −0.4). The fits are (0.500, 0.818, −0.399) for Poisson, (0.493, 0.803, −0.416) for Gamma, and (0.484, 0.794, −0.404) for Tweedie with p = 1.5 (10% exact zeros). Poisson with random exposure and an offset gives (0.494, 0.802, −0.402). The fitted Poisson means sum exactly to the observed total.

Exercise 2: single-event upsets. Setup: counts per pass with exposure 0.5–4 h, SAA minutes and a solar index; true log-rate −1 + 0.08·SAA − 0.3·solar. Train on 400 passes, test on 4000, 200 repetitions, in two scenarios.

Scenario Test deviance: Poisson GLM OLS on rate OLS on log(1 + y) φ̂ Naive CI Quasi-Poisson CI Sandwich CI
Poisson counts 1.080 1.243 1.487 1.00 94.5% 95.0% 94.5%
Overdispersed (gamma-mixed) 1.704 1.892 2.178 1.75 78.0% 90.5% 95.5%

The three CI columns give the coverage of nominal 95% intervals for the SAA effect.

  • The GLM predicts best in both scenarios. OLS on the log scale is worst: it models E[log y], not log E[y].
  • Under overdispersion, naive Poisson intervals cover only 78% instead of 95%.
  • Quasi-Poisson helps (90.5%) but not fully. The true variance here is μ + μ²/2, not φμ.
  • The sandwich estimator restores 95.5% coverage, because it makes no assumption about the variance shape.
🐍 Exercise 2 (research): modelling single-event upsets
Simulate satellite memory upsets per pass with exposure, SAA minutes and solar activity, with Poisson or overdispersed counts; compare a Poisson GLM with offset against OLS on the rate and on log(1 + y) by test deviance, and compare naive, quasi-Poisson and sandwich 95% intervals for the SAA effect by coverage.

GLMs in AI, robotics and aerospace

  • Radiation effects: upset and latch-up rates versus orbit, shielding and solar activity.
  • Reliability: failure counts with operating-hour offsets, and Gamma models for repair times.
  • Networking: packet-loss counts and latency amounts on links.
  • Neural nets: Poisson and Tweedie output losses for count and intermittent-demand forecasting.

References

  1. J. A. Nelder, R. W. M. Wedderburn (1972). “Generalized linear models.” JRSS A 135(3).
  2. P. McCullagh, J. A. Nelder (1989). Generalized Linear Models, 2nd ed. Chapman & Hall.
  3. R. W. M. Wedderburn (1974). “Quasi-likelihood functions, generalized linear models, and the Gauss–Newton method.” Biometrika 61(3).
  4. B. Jørgensen (1987). “Exponential dispersion models.” JRSS B 49(2).
  5. H. White (1980). “A heteroskedasticity-consistent covariance matrix estimator and a direct test for heteroskedasticity.” Econometrica 48(4).