Where do loss functions come from? So far this curriculum has treated them as givens — squared error here, cross-entropy there — with occasional hints that something deeper was choosing them. This lesson is the reveal. Maximum likelihood is a single principle — pick the parameters under which the observed data would have been least surprising — and applied to different noise models it generates the curriculum’s losses one by one. After this lesson, “choosing a loss” and “asserting a noise model” are the same act, and you will know which assertion you are making.
The principle
A model with parameters assigns the observed data a probability — the likelihood, read as a function of with the data held fixed:
The maximum likelihood estimator is — in practice always via the log (products become sums, tiny numbers become sane, optima are unchanged by monotone transforms):
And here is the bridge this curriculum has been building toward: minimizing negative log-likelihood is an optimization problem — the Foundations Optimization module’s machinery applies verbatim, and “training” is MLE (or its regularized cousins) running on gradient descent.
Two derivations that unmask the curriculum’s losses
Bernoulli → cross-entropy. Coin flips with : with successes in flips,
The sample frequency — reassuringly. Now let depend on features through a sigmoid, , and is the logistic-regression loss from Supervised Learning, term for term. Cross-entropy was never a choice; it was Bernoulli MLE all along — and the language-model training objective (next token as a categorical variable) is the same derivation with more categories.
Gaussian → least squares. Model observations as signal plus Gaussian noise, , :
Maximizing over ignores the constant and minimizes the sum of squared errors: Gauss’s least squares — the method that found Ceres in the Start Here story — is exactly MLE under Gaussian noise, which is the argument Gauss himself ran (in the other direction: he derived the normal distribution as the noise law that justifies the mean). The corollaries write themselves: absolute-error loss is MLE under Laplace noise (heavier tails → robust regression), and the CLT from last lesson explains why the Gaussian assertion is so often defensible — noise that is a sum of many small effects is Gaussian-ish by theorem.
Drag your θ along the curve and read ℓ(θ): the MLE (green) sits at the peak — for Bernoulli, exactly the sample frequency, which at n = 20 is visibly NOT the truth (red): estimators have sampling error, and the flat-topped curve is the picture of that uncertainty. Now slide n upward: the peak sharpens dramatically — the curvature of the log-likelihood at its peak is the Fisher information, and its growth with n is lesson 2’s 1/√n law seen from the likelihood side (the asymptotic variance of the MLE is 1/(nI(θ))). Switch to the Gaussian-mean model and notice the curve is an exact parabola — because log of a Gaussian IS a quadratic, which is the entire least-squares connection in one visual.
What MLE promises, and what it doesn’t
The classical guarantees, stated at working precision: under regularity conditions, the MLE is consistent ( as ), asymptotically normal ( with the Fisher information — the peak curvature from the widget), and asymptotically efficient (no consistent estimator does better, by the Cramér–Rao bound). That trio is why MLE is the default estimator of working statistics.
The fine print is equally load-bearing:
- Small-n MLE overfits. The likelihood rewards fitting the data you have; with little data that means fitting its accidents. Three flips, three heads → : a confident absurdity. The fixes are this curriculum’s two favourite medicines, now unified: regularization is literally MLE plus a penalty, and lesson 4 shows the penalty is a prior in disguise.
- MLE is optimization, with everything that entails. Bernoulli and Gaussian gave closed forms; logistic regression needed gradient descent; deep networks give non-convex likelihoods where “the” MLE is a fiction and you get whatever the optimizer finds — the Non-convex Landscapes lesson, now with a statistical reading.
- The model can be wrong. MLE finds the best parameters within the family you wrote down (formally: it converges to the KL-closest member). Gaussian MLE on heavy-tailed data confidently estimates a mean the outliers own — the robust losses exist precisely because the noise assertion behind squared error is sometimes false.
One more unification, because it completes the picture: maximizing likelihood is minimizing , whose population limit is the cross-entropy between the true distribution and the model — i.e., MLE minimizes KL divergence to the truth. That information-theoretic phrasing is the one the LLM track will use, and it is also this module’s quiet claim to the throne: every “loss curve” any student of this platform has ever watched descend was a KL divergence being ground down.
import numpy as np
from scipy.optimize import minimize_scalar
rng = np.random.default_rng(3)
x = rng.binomial(1, 0.7, size=40) # hidden truth p = 0.7
nll = lambda p: -(x.sum() * np.log(p) + (len(x) - x.sum()) * np.log(1 - p))
res = minimize_scalar(nll, bounds=(1e-6, 1 - 1e-6), method="bounded")
print(res.x, x.mean()) # optimizer meets closed form
Exercises
Work these before the next lesson
- Derive both Gaussian MLEs: with and free, show and . The variance MLE is biased (the vs affair) — compute and reconcile “biased” with “consistent.”
Solution
Worked solutions are part of Premium — unlock all of them for £5/month →
- 5 more exercises — each with a worked solution — are part of Premium. Unlock everything for £5/month →
References
- R. A. Fisher, “On the Mathematical Foundations of Theoretical Statistics”, 1922 — likelihood’s founding document; readable and startlingly modern.
- L. Wasserman, All of Statistics, ch. 9 — MLE’s properties at exactly this lesson’s level of rigor.
- S. Stigler, “Gauss and the Invention of Least Squares”, Annals of Statistics 1981 — the history behind the Gaussian derivation.