O-ring damage on the 23 shuttle flights before Challenger
How many? A count.
Earthquakes of each magnitude on Earth, 2015 to 2024
Least squares answered the first, and logistic regression the second, with the same update rule. Today that stops being a coincidence: one recipe answers all three, and the only thing that changes between them is one function.
Likelihood, feltThe twenty houses from linear regression, with the noise model laid over them
Each house glows by how probable the model finds its price. The intercept follows the slope, so the line always passes through the average house.
Maximize Log-Likelihood
The punchline
Maximizing \( \ell(\theta) \) is minimizing
\( \tfrac12 \sum_i (y^{(i)} - \theta^\top x^{(i)})^2 \)
Least squares was never an arbitrary choice.
It is maximum likelihood under Gaussian noise.
The question that opens today
What if the noise is not Gaussian?
Part 2 of 6
Yes or no
What is the likelihood of an event that either happens or does not?
The night before Challenger27 January 1986: will the cold hurt the seals?
The joints of the shuttle's boosters are sealed by rubber O-rings, six per flight. On 7 of the 23 flights before Challenger, some came back scorched or eroded.
The forecast for launch morning was 31°F, −0.6°C: more than 12°C colder than any flight before.
The Rogers Commission report later set two charts side by side: the flights with damage alone, where temperature seems not to matter, and all the flights. Press the second button.
A zero is data
Every flight with no damage flew warm. The likelihood counts the flights that went fine as well as the ones that did not.
Data: Dalal, Fowlkes and Hoadley, Risk analysis of the space shuttle: pre-Challenger prediction of failure, JASA 84 (1989), via the UCI repository. Damage means at least one of the six O-rings came back eroded or scorched; on two flights, two of the six did. Flights launched at the same temperature are drawn side by side.
The output is a probability
The label is 0 or 1, never in between. Squared error treats a probability like a
measurement, and its noise is not Gaussian.
Each bar under the curve is one flight: the probability the model gave to what actually happened. Red flights had damage, blue did not. \(\ell(\theta)\) is the sum of the logs of the bars.
A confident wrong answer costs the most: a bar near zero has a log near minus infinity. At the maximum, the costliest flight is the one launched at 23.9°C: it was damaged, and the model gave damage only 8.5%.
The Poisson distribution counts events in a fixed window when each happens independently of the others: earthquakes in a decade, clicks in an hour, typos on a page. \(\lambda\) is the expected count. Rewrite it in exponential-family form:
Read the natural parameter
\( \eta = \log \lambda \), so \( \lambda = e^{\eta} \): the exponential appears on its own, as the sigmoid did for Bernoulli.
Set \( \eta = \theta^\top x \) and a count model is \( \log \lambda = \theta^\top x \).
Mean and variance come from derivatives of one function.
Learning is easy
The negative log-likelihood is convex in \(\theta\),
so gradient steps find the global optimum.
One function holds the mean and the varianceTurn \(\eta\), and read both off the curve \(a(\eta)\)
Part 4 of 6
Counts
What if the answer counts something?
Data Type Chooses the Distribution
Assumption: \( p(y \mid x; \theta) \) is an exponential family.
Binary Bernoulli
→ logistic regression
Real Gaussian
→ linear regression
Counts Poisson
→ Poisson regression, today
Positive real Gamma, exponential
Distributions Dirichlet
The rule
Pick the distribution that matches what \(y\) is, and the model follows.
How many earthquakes of each size?Every magnitude 5 and above on Earth, 2015 to 2024: 16,945 earthquakes
fit from
Each bar counts the earthquakes in one 0.1 slice of magnitude. The counts fall from hundreds to zero: no M 8.4 or larger happened in these ten years.
Data: US Geological Survey, ANSS Comprehensive Catalog, earthquake.usgs.gov/fdsnws/event/1/query, fetched 2026-09-28. Below M 5.5 half the magnitudes are body-wave estimates, which run low for large events; from M 6.0 almost all are moment magnitudes.
A linear model adds \(\theta_1\) for every unit of \(x\). A Poisson model multiplies the expected count by \(e^{\theta_1}\): on the log scale the line is straight, on the count scale it is a curve that never goes below zero.
A law, recovered
Gutenberg and Richter (1944): about ten times fewer earthquakes for every unit of magnitude. The regression found \(e^{\theta_1} = 0.0997\) in ten years of data.
Poisson says the variance equals the mean. Year by year, earthquakes of M 7.0 to 7.4 average 9.0 with variance 9.1. Smaller ones come in aftershock swarms and vary more: M 6.0 to 6.4 average 91.6, variance 203.8.
Part 5 of 6
The recipe
What do all three models share, and what are the parts called?
Same loop, three modelsOnly \(h\) changes
Building a GLM
The natural parameter is linear in the inputs:
\( \eta = \theta^\top x \). The predictor is a natural consequence, not a separate choice.
The prediction is the conditional mean:
\( h_\theta(x) = \mathbb{E}[y \mid x; \theta] \)
What does one GLM look like in ten lines of Python?
One function, two datasetsPress Run: this is real Python, in your browser
import numpy as np, pandas as pd
def fit_glm(X, y, h, alpha, steps=20000):
theta = np.zeros(X.shape[1])
for _ in range(steps): # the same update for every GLM
theta += alpha * X.T @ (y - h(X @ theta)) / len(y)
return theta
sigmoid = lambda z: 1 / (1 + np.exp(-z))
c = pd.read_csv('challenger_orings.csv') # yes or no: Bernoulli, h = sigmoid
X = np.column_stack([np.ones(len(c)), c.temp_c - 20])
th = fit_glm(X, c.damage.values, sigmoid, alpha=0.05)
print('Challenger theta:', th.round(3), ' P(damage at -0.6 C):', sigmoid(th[0] + th[1] * (-0.6 - 20)).round(4))
q = pd.read_csv('usgs_quakes_m5_2015_2024.csv') # a count: Poisson, h = exp
M = np.round(np.arange(6.0, 8.95, 0.1), 1)
y = np.array([(q.mag.round(1) == m).sum() for m in M])
X = np.column_stack([np.ones(len(M)), M - 6])
th = fit_glm(X, y, np.exp, alpha=0.01)
print('Earthquake theta:', th.round(3), ' ten times fewer? e^theta1 =', np.exp(th[1]).round(4))
You should get \(\theta = (-0.749, -0.417)\) and a 99.96% chance of damage, then \(\theta = (5.616, -2.306)\) and \(e^{\theta_1} = 0.0997\): the numbers on the slides. Try h = lambda z: z on a real-valued \(y\), and the same function is least squares.
Summary
The recipe
\( p(y; \eta) = b(y)\exp\{\eta^\top y - a(\eta)\} \)
least squares and cross-entropy were never two ideas.
They are one idea, seen through different distributions: Gaussian, Bernoulli, and today Poisson.
What to take away
The likelihood asks how probable the data is under the model. Least squares is its answer under Gaussian noise, cross-entropy its answer under Bernoulli noise.
Zeros are data. The Challenger flights that went fine carry the pattern; leave them out and it disappears.
Choose the distribution from what \(y\) is: a real number, a yes or no, a count. Its natural parameter \(\eta = \theta^\top x\) gives the model: the identity, the sigmoid, the exponential.
One function \(a(\eta)\) holds the mean and the variance, and the update \(\theta := \theta + \alpha \sum_i (y^{(i)} - h_\theta(x^{(i)}))\,x^{(i)}\) is the same for every member of the family.
A Poisson coefficient multiplies: \(e^{\theta_1} = 0.0997\) says ten times fewer earthquakes per unit of magnitude.