You do not choose a loss function. You choose a noise model, and the loss is whatever its negative log-likelihood turns out to be. One principle generates squared error, absolute error, cross-entropy and Poisson loss — and tells you exactly when each is the wrong choice.
Phase A Lecture 14 established the pattern. It is worth stating once as a procedure, because every loss in this course is an instance of it.
The loss is a measure of surprise. \(-\log p\) is large when the model assigned low probability to what actually happened, and zero when it assigned probability 1. Training minimises total surprise. That reading also explains why the units are called nats (base \(e\)) or bits (base 2), and why a language model's loss is reported as perplexity \(e^{\mathcal{L}}\) — the effective number of equally-likely options the model was choosing between.
\[\begin{array}{lll} \textbf{noise model} & \textbf{negative log-likelihood} & \textbf{known as} \\[4pt] y \sim \mathcal{N}(\hat y, \sigma^2) & \tfrac{1}{2\sigma^2}(y-\hat y)^2 + \text{const} & \text{squared error} \\[4pt] y \sim \mathcal{N}(\hat y, \sigma_i^2) & \tfrac{1}{2\sigma_i^2}(y-\hat y)^2 & \text{weighted least squares} \\[4pt] y \sim \text{Laplace}(\hat y, b) & \tfrac1b|y-\hat y| + \text{const} & \text{mean absolute error} \\[4pt] y \sim \text{Student-}t_\nu(\hat y) & \tfrac{\nu+1}{2}\log\!\big(1+\tfrac{(y-\hat y)^2}{\nu}\big) & \text{robust regression} \\[4pt] y \sim \text{Bernoulli}(\hat p) & -y\log\hat p - (1-y)\log(1-\hat p) & \text{binary cross-entropy} \\[4pt] y \sim \text{Categorical}(\hat{\boldsymbol\pi}) & -\sum_k y_k\log\hat\pi_k & \text{cross-entropy} \\[4pt] y \sim \text{Poisson}(\hat\lambda) & \hat\lambda - y\log\hat\lambda + \text{const} & \text{Poisson loss} \end{array}\]
Every one of those is derived the same way — write the density, take the log, drop constants. Two of them are worth doing in full because their consequences are large.
For \(y\in\{0,1\}\) the Bernoulli gives binary cross-entropy, derived in Phase A Lecture 14. For \(K\) classes the generalisation is immediate.
log_softmax / CrossEntropyLoss for this reason.That recurring \(\mX^\top(\hat{\vy}-\vy)\) is not a coincidence. It is a theorem about a family of distributions.
A distribution is in the exponential family with natural parameter \(\theta\) if
\[ p(y\mid\theta) = h(y)\,\exp\big(\theta\, T(y) - A(\theta)\big), \]where \(A\) is the log-partition function. Gaussian (fixed variance), Bernoulli, Categorical, Poisson, Exponential, Gamma and Beta are all members.
A generalised linear model sets \(\theta_i = \vw^\top\vx_i\) — the canonical link.
Under regularity conditions (identifiability, smoothness, the true parameter in the interior of the space), as \(n\to\infty\):
The proofs are a chapter of a statistics course and are deferred; the statements are what you need.
The Fisher information is the curvature of the log-likelihood at its peak — literally the expected Hessian, with a sign flip. A sharply peaked likelihood means the data strongly distinguishes nearby parameter values, so the estimate is precise; a flat one means many parameters explain the data almost equally well, so the estimate is uncertain. That is exactly the "peak sharpens as \(n\) grows" behaviour you dragged in Phase A Lecture 14's likelihood figure, and \(\mathcal{I}^{-1}/n\) is the variance of the estimate.
Three failures of maximum likelihood worth holding onto.
Derive the loss implied by each noise model, simplifying and dropping constants.
(a) \(y\sim\text{Exponential}(\text{rate}=1/\hat y)\), i.e. mean \(\hat y\).
(b) \(y\sim\mathcal{N}(\hat y,\sigma_i^2)\) with known, varying \(\sigma_i\).
(c) \(y\sim\text{Bernoulli}(\hat p)\) but with label noise: the observed label is flipped with known
probability \(\epsilon\).
(a) Density \(p(y)=\frac{1}{\hat y}e^{-y/\hat y}\) for \(y>0\). So
\[-\log p = \log\hat y + \frac{y}{\hat y}, \qquad \mathcal{L}=\sum_i\left(\log\hat y_i + \frac{y_i}{\hat y_i}\right).\]Note it is scale-invariant: multiplying both \(y\) and \(\hat y\) by \(c\) changes the loss by the constant \(\log c\). Appropriate for durations and waiting times, where a 10% error matters equally at 1 second and 1 hour. (This is the Itakura–Saito divergence, used in audio spectrogram modelling for exactly that reason.)
(b) \(-\log p = \frac{(y_i-\hat y_i)^2}{2\sigma_i^2} + \log\sigma_i + \text{const}\). The \(\log\sigma_i\) does not involve \(\vw\), so
\[\mathcal{L}(\vw)=\sum_i \frac{(y_i-\hat y_i)^2}{\sigma_i^2} \qquad\text{— weighted least squares, weights } 1/\sigma_i^2.\]Noisier observations are down-weighted, which is exactly right: they carry less information. In matrix form the solution is \(\hat{\vw}=(\mX^\top\mathbf{\Omega}\mX)^{-1}\mX^\top\mathbf{\Omega}\vy\) with \(\mathbf{\Omega}=\operatorname{diag}(1/\sigma_i^2)\).
(c) The observed label is 1 either when the true label is 1 and no flip occurs, or when the true label is 0 and a flip does:
\[P(\tilde y=1) = \hat p(1-\epsilon) + (1-\hat p)\epsilon = \epsilon + (1-2\epsilon)\hat p =: \tilde p.\]So the loss is ordinary cross-entropy applied to \(\tilde p\):
\[\mathcal{L} = -\sum_i\big[\tilde y_i\log\tilde p_i + (1-\tilde y_i)\log(1-\tilde p_i)\big], \qquad \tilde p_i = \epsilon+(1-2\epsilon)\sigma(\vw^\top\vx_i).\]Why this matters: \(\tilde p\) is confined to \([\epsilon, 1-\epsilon]\), so the model can never be asked to output a probability of 0 or 1. That caps the loss for any single example, which is precisely what stops noisy labels from producing unbounded gradients. This is the principled version of label smoothing — and knowing the derivation tells you the smoothing parameter should be your estimated label-noise rate, not an arbitrary 0.1.
Prove that the Huber loss is continuously differentiable at \(|r|=\delta\), and find the value of \(\delta\) that makes it the negative log-likelihood of a specific density. Sketch that density and compare it with a Gaussian.
Continuity of value. At \(r=\delta\): the quadratic branch gives \(\tfrac12\delta^2\); the linear branch gives \(\delta(\delta-\tfrac12\delta)=\tfrac12\delta^2\). Equal ✓.
Continuity of derivative. Quadratic branch: \(\ell'(r)=r\), so \(\ell'(\delta^-)=\delta\). Linear branch (for \(r>\delta\)): \(\ell(r)=\delta r-\tfrac12\delta^2\), so \(\ell'(\delta^+)=\delta\). Equal ✓. So \(\ell_\delta\in C^1\) for every \(\delta\), which is why it is safe for gradient-based optimisation where absolute error is not.
(The second derivative is discontinuous — 1 inside, 0 outside — so \(\ell_\delta\notin C^2\). That is why Newton-type methods need care with Huber, and why log-cosh, which is smooth everywhere, is sometimes preferred.)
The implied density. Exponentiating \(-\ell_\delta\) and normalising:
\[p(r) \propto \begin{cases} e^{-r^2/2} & |r|\le\delta\\[3pt] e^{-\delta|r|+\delta^2/2} & |r|>\delta\end{cases}\]This is the Huber density: Gaussian in the middle, Laplace in the tails, glued together smoothly at \(\pm\delta\). Any \(\delta\) gives a valid density (the normalising constant depends on \(\delta\)), so there is no single "correct" value — \(\delta\) parameterises a family interpolating between Gaussian (\(\delta\to\infty\)) and Laplace (\(\delta\to0\)).
Compared with a Gaussian: identical near the centre, but with far heavier tails — decaying like \(e^{-\delta|r|}\) rather than \(e^{-r^2/2}\). At \(r=6\delta\) the Huber density is larger by a factor of roughly \(e^{12.5\delta^2}\). That is the entire content of "robust": the model considers a large residual surprising but possible, rather than astronomically impossible, and so declines to distort itself to eliminate one.
Choosing \(\delta\): the standard is \(\delta=1.345\hat\sigma\), which gives 95% efficiency relative to OLS when the noise really is Gaussian while retaining robustness when it is not. Note \(\hat\sigma\) must be estimated robustly (e.g. from the median absolute deviation) or the outliers you are guarding against will inflate it.
Compute the Fisher information and hence the asymptotic standard error for:
(a) Bernoulli\((p)\) (b) Poisson\((\lambda)\) (c) Gaussian mean with known \(\sigma^2\).
For (a), at which \(p\) is the estimate most precise, and at which least? Explain the practical consequence for measuring click-through rates.
(a) Bernoulli. \(\log p(y\mid p)=y\log p+(1-y)\log(1-p)\). \(\frac{\partial^2}{\partial p^2} = -\frac{y}{p^2}-\frac{1-y}{(1-p)^2}\). Taking \(-\E\) with \(\E[Y]=p\):
\[\mathcal{I}(p) = \frac{p}{p^2}+\frac{1-p}{(1-p)^2} = \frac1p+\frac1{1-p} = \frac{1}{p(1-p)}.\]Standard error \(=\sqrt{p(1-p)/n}\) — the familiar formula, now derived rather than quoted.
(b) Poisson. \(\log p = y\log\lambda-\lambda-\log y!\), second derivative \(-y/\lambda^2\), so \(\mathcal{I}(\lambda)=\E[Y]/\lambda^2 = 1/\lambda\). Standard error \(=\sqrt{\lambda/n}\) — consistent with \(\Var=\lambda\).
(c) Gaussian mean. \(\log p = -\frac{(y-\mu)^2}{2\sigma^2}+\text{const}\), second derivative \(-1/\sigma^2\), so \(\mathcal{I}(\mu)=1/\sigma^2\), constant in \(\mu\). Standard error \(=\sigma/\sqrt n\) — Phase A Lecture 13, recovered.
Most and least precise. \(\text{se}=\sqrt{p(1-p)/n}\) is maximised at \(p=\tfrac12\) (value \(0.5/\sqrt n\)) and shrinks towards zero as \(p\to0\) or \(1\). So rare events are estimated with the smallest absolute error.
But that is the wrong way round for click-through rates, and this is the practically important point. What matters is relative precision:
\[\frac{\text{se}}{p} = \sqrt{\frac{1-p}{np}} \approx \frac{1}{\sqrt{np}} \quad\text{for small } p.\]This blows up as \(p\to0\). At \(p=0.001\) you need \(n\approx10^6\) for a 3% relative error, and \(n\approx10^4\) gives you \(\pm32\%\). The absolute error is tiny and completely useless.
Hence the rule of thumb that you need on the order of \(1/p\) events, not observations: A/B tests on rare conversions need enormous samples, and a reported "CTR improved from 0.10% to 0.12%" on ten thousand impressions is roughly seven conversions against six. Always compute the interval.
Coding. Build a heteroscedastic regression that learns its own noise level.
3. The objective. With \(\mu_i=\vw^\top\vx_i\) and \(\log\sigma_i=\vv^\top\vx_i\),
\[\mathcal{L}(\vw,\vv) = \sum_{i=1}^n\left[\vv^\top\vx_i + \frac{(y_i-\vw^\top\vx_i)^2}{2\exp(2\vv^\top\vx_i)}\right].\]Note the structure: the first term penalises claiming large noise, the second rewards it (it shrinks the residual penalty). The model must trade them off, which is what stops it from declaring infinite noise everywhere and calling the job done.
import numpy as np
from scipy.optimize import minimize
rng = np.random.default_rng(0)
n, d = 600, 3
X = np.column_stack([np.ones(n), rng.normal(size=(n, d))])
w_true = np.array([1.0, 2.0, -1.0, 0.5])
v_true = np.array([-0.5, 0.9, 0.0, 0.0]) # noise grows with feature 1
sd = np.exp(X @ v_true)
y = X @ w_true + sd*rng.normal(size=n)
w_ols, *_ = np.linalg.lstsq(X, y, rcond=None)
res = y - X @ w_ols
print("OLS w:", np.round(w_ols, 3), " true:", w_true)
print("funnel check corr(|res|, x1) =",
round(np.corrcoef(np.abs(res), X[:,1])[0,1], 4), " (should be ~0 if homoscedastic)")
def nll(theta):
w, v = theta[:d+1], theta[d+1:]
ls = np.clip(X @ v, -8, 8)
return np.sum(ls + (y - X@w)**2 / (2*np.exp(2*ls)))
opt = minimize(nll, np.concatenate([w_ols, np.zeros(d+1)]), method="L-BFGS-B")
w_h, v_h = opt.x[:d+1], opt.x[d+1:]
print("\\nHet w:", np.round(w_h, 3))
print("Het v:", np.round(v_h, 3), " true:", v_true)
print(f"\\n||w_ols - w_true|| = {np.linalg.norm(w_ols-w_true):.4f}")
print(f"||w_het - w_true|| = {np.linalg.norm(w_h -w_true):.4f}")
# calibration of 90% intervals
z = 1.645
cov_ols = np.mean(np.abs(y - X@w_ols) <= z*res.std())
cov_het = np.mean(np.abs(y - X@w_h) <= z*np.exp(X@v_h))
print(f"\\n90% interval coverage: OLS {cov_ols:.3f} heteroscedastic {cov_het:.3f}")
2. The OLS residual plot funnels visibly, and \(\operatorname{corr}(|r|, x_1)\) comes out around \(+0.35\) rather than 0 — the quantitative version of the funnel in Lecture 02's diagnostic figure.
4. The answer to "which improves more". The uncertainty, by a large margin.
The general lesson. Getting the mean right and getting the uncertainty right are separate problems, and modelling the noise fixes the second far more than the first. This is exactly why modern probabilistic regression predicts both \(\mu(\vx)\) and \(\sigma(\vx)\), why quantile regression exists, and why a model that outputs a point estimate cannot tell you when to distrust it.
Next: the classification case in full. Lecture 05 builds logistic regression and softmax from the Bernoulli likelihood, derives the decision boundary, and confronts the separable-data pathology head on.