Unit 13, lesson 99

Machine learning losses regularization and calibration

How empirical risk and the ridge estimator define a regularized fit, why the Brier loss is a proper loss, what probability calibration means, and when covariate shift lets reweighting recover risk under distribution shift.

  • Video 99
  • Data, prediction and learning

A model that forecasts the probability of a down day fits its training sample almost perfectly. Does that say how well it will forecast next year, and can its outputs be read as probabilities? A small training error, a useful prediction and a calibrated probability are three different mathematical statements. This lesson defines empirical risk and regularization, derives the ridge estimator exactly, proves that the Brier loss rewards honest probabilities, defines calibration, and states when a change in the sampling law can be handled by reweighting the training data.

Key terms

Empirical risk

Definition Let fθf_\theta be a prediction rule with parameter θ∈Θ\theta \in \Theta and ℓ(y,y^)\ell(y, \hat{y}) a loss. The population risk under the law P\mathbb{P} of the pairs (X,Y)(X, Y) that the rule is meant to predict is

R(θ)=EP[ℓ(Y,fθ(X))],R(\theta) = \mathbb{E}_{\mathbb{P}}\bigl[\ell(Y, f_\theta(X))\bigr],

when the expectation exists. For a training sample (x1,y1),…,(xn,yn)(x_1, y_1), \dots, (x_n, y_n) the empirical risk is the average loss

R^n(θ)=1n∑i=1nℓ(yi,fθ(xi)).\begin{aligned} &\widehat{R}_n(\theta) \\[0.4em] &= \frac{1}{n} \sum_{i=1}^{n} \ell\bigl(y_i, f_\theta(x_i)\bigr). \end{aligned}

Regularized empirical risk minimization chooses a minimizer of R^n(θ)+λ Ω(θ)\widehat{R}_n(\theta) + \lambda\, \Omega(\theta), with a penalty Ω≥0\Omega \ge 0 and a strength λ≥0\lambda \ge 0. If each training pair has the law P\mathbb{P}, then E[R^n(θ)]=R(θ)\mathbb{E}[\widehat{R}_n(\theta)] = R(\theta) for every fixed θ\theta. A parameter chosen from the sample is not fixed, though. Under the same hypothesis and with λ=0\lambda = 0, a minimizer θ^\hat\theta of the empirical risk and a minimizer θ∗\theta^{*} of the population risk satisfy

E[R^n(θ^)]≤R(θ∗)≤E[R(θ^)],\begin{aligned} \mathbb{E}\bigl[\widehat{R}_n(\hat\theta)\bigr] &\le R(\theta^{*}) \\[0.4em] &\le \mathbb{E}\bigl[R(\hat\theta)\bigr], \end{aligned}

provided these expectations exist, because R^n(θ^)≤R^n(θ∗)\widehat{R}_n(\hat\theta) \le \widehat{R}_n(\theta^{*}) and R(θ∗)≤R(θ^)R(\theta^{*}) \le R(\hat\theta) hold on every sample. Here R(θ^)R(\hat\theta) is the expected loss of the fitted rule on a new pair drawn from P\mathbb{P} independently of the sample.

In plain words The empirical risk is the score on data already seen, and the population risk is the score on the data the rule will meet. Because an unpenalized fit picks its parameter to make the first score small, its training score is optimistic on average about the second. A penalty adds a preference, such as small coefficients, and trades some fit for less sensitivity to the particular sample. The best strength of that preference need not be the same for every population.

Example Fit fθ(x)=θxf_\theta(x) = \theta x with squared loss to the two constructed pairs (1,1)(1, 1) and (2,2)(2, 2). The empirical risk is

R^2(θ)=12(1−θ)2+12(2−2θ)2=52(1−θ)2,\begin{aligned} \widehat{R}_2(\theta) &= \tfrac12 (1 - \theta)^2 \\[0.4em] &\quad + \tfrac12 (2 - 2\theta)^2 \\[0.4em] &= \tfrac52 (1 - \theta)^2, \end{aligned}

which is 00 at θ=1\theta = 1 and 10/49≈0.204110/49 \approx 0.2041 at θ=5/7\theta = 5/7. Now suppose a constructed population in which XX is 11 or 22 with probability 0.50.5 each and Y=0.8X+εY = 0.8X + \varepsilon, where ε\varepsilon is independent of XX with mean 00 and variance 0.250.25. Since E[X2]=2.5\mathbb{E}[X^2] = 2.5, the population risk is R(θ)=2.5(0.8−θ)2+0.25R(\theta) = 2.5(0.8 - \theta)^2 + 0.25, so R(1)=0.35R(1) = 0.35 while R(5/7)≈0.2684R(5/7) \approx 0.2684. With Y=X+εY = X + \varepsilon instead, R(1)=0.25R(1) = 0.25 and R(5/7)≈0.4541R(5/7) \approx 0.4541. The same two training scores are consistent with either ranking of the two rules in the population.

Ridge estimator

Definition Let XX be an n×pn \times p design matrix, here a matrix of observed feature values, and y∈Rny \in \mathbb{R}^n the target vector, with no intercept and a strength λ>0\lambda > 0. The ridge estimator minimizes

J(θ)=1n∥y−Xθ∥2+λ∥θ∥2.\begin{aligned} J(\theta) &= \frac{1}{n} \lVert y - X\theta \rVert^2 \\[0.4em] &\quad + \lambda \lVert \theta \rVert^2 . \end{aligned}

Its gradient is ∇J(θ)=2nX⊤(Xθ−y)+2λθ\nabla J(\theta) = \tfrac{2}{n} X^{\top}(X\theta - y) + 2\lambda\theta. Setting it to zero and multiplying by n/2n/2 gives the normal equations

(X⊤X+nλI) θ=X⊤y.(X^{\top}X + n\lambda I)\,\theta = X^{\top}y .

For every nonzero v∈Rpv \in \mathbb{R}^p,

v⊤(X⊤X+nλI) v=∥Xv∥2+nλ∥v∥2>0,\begin{aligned} &v^{\top}(X^{\top}X + n\lambda I)\,v \\[0.4em] &= \lVert Xv \rVert^2 + n\lambda \lVert v \rVert^2 \\[0.4em] &> 0, \end{aligned}

so the matrix is positive definite and invertible even when the columns of XX are linearly dependent. The Hessian ∇2J=2nX⊤X+2λI\nabla^2 J = \tfrac{2}{n} X^{\top}X + 2\lambda I is positive definite as well, so JJ is strictly convex and its unique minimizer is

θ^λ=(X⊤X+nλI)−1X⊤y.\hat\theta_\lambda = (X^{\top}X + n\lambda I)^{-1} X^{\top} y .

With λ=0\lambda = 0 the problem is ordinary least squares, whose inverse formula needs full column rank. Without full rank its minimizers form an affine set, and the pseudoinverse solution X+yX^{+}y is the one with the smallest Euclidean norm. An unpenalized intercept in the first column changes the penalty to λ θ⊤Dθ\lambda\, \theta^{\top} D \theta with D=diag⁡(0,1,…,1)D = \operatorname{diag}(0, 1, \dots, 1) and the matrix to X⊤X+nλDX^{\top}X + n\lambda D, which is still positive definite when that column is the vector of ones. The factor nn appears because the fitting error is averaged and the penalty is not. An objective written without the factor 1/n1/n has λ\lambda in its place.

In plain words Ridge regression charges for large coefficients. That pulls the fit toward zero and gives a unique, stable solution even when some directions in the data are poorly identified. The penalty depends on the units of the features, so the features are usually put on a common scale first, with the scaling statistics taken from the fitting data only.

Example For the pairs (1,1)(1, 1) and (2,2)(2, 2) with n=2n = 2 and λ=1\lambda = 1, the products are X⊤X=1+4=5X^{\top}X = 1 + 4 = 5 and X⊤y=5X^{\top}y = 5, so θ^1=5/(5+2)=5/7≈0.714286\hat\theta_1 = 5/(5 + 2) = 5/7 \approx 0.714286. The fitted values are 5/75/7 and 10/710/7, the residuals y−Xθ^1y - X\hat\theta_1 are 2/72/7 and 4/74/7, the mean squared error is (4/49+16/49)/2=10/49(4/49 + 16/49)/2 = 10/49, the penalty is 25/4925/49 and the objective is 35/49=5/735/49 = 5/7. The coefficient 11 fits both points exactly but scores 0+1=10 + 1 = 1 on this objective. Here θ^λ=5/(5+2λ)\hat\theta_\lambda = 5/(5 + 2\lambda), so λ=0\lambda = 0 gives 11 and λ=5\lambda = 5 gives 1/31/3. Choosing λ\lambda by looking at an evaluation set makes that set part of model selection, so its score is no longer an untouched assessment, a point taken up in Time series validation and overlapping labels.

Brier loss

Definition For a binary outcome Y∈{0,1}Y \in \{0, 1\} and a reported probability q∈[0,1]q \in [0, 1], the Brier loss is (Y−q)2(Y - q)^2, and the Brier score of nn forecasts is the average 1n∑i(yi−qi)2\frac{1}{n} \sum_{i} (y_i - q_i)^2. Let p=P(Y=1∣X)p = \mathbb{P}(Y = 1 \mid X) be the true conditional probability given the features, and let the report qq be a function of XX. Weighting the two possible errors by their conditional probabilities and adding and subtracting p2p^2 gives

E[(Y−q)2∣X]=p(1−q)2+(1−p)q2=p−2pq+q2=p(1−p)+(p−q)2.\begin{aligned} &\mathbb{E}\bigl[(Y - q)^2 \mid X\bigr] \\[0.4em] &= p(1 - q)^2 + (1 - p)q^2 \\[0.4em] &= p - 2pq + q^2 \\[0.4em] &= p(1 - p) + (p - q)^2 . \end{aligned}

The first term is the conditional variance of the event indicator and does not depend on the report. The second is zero only at q=pq = p. The loss is dimensionless and lies in [0,1][0, 1].

In plain words The Brier loss is the squared error of a probability used as a forecast of an outcome that is zero or one. Its expected value splits into the uncertainty of the event itself, which no report can remove, and a penalty for reporting a probability other than the true one.

Example Let the event probability be p=0.7p = 0.7 in a constructed population. Reporting q=0.3q = 0.3 gives the expected loss 0.7⋅0.49+0.3⋅0.09=0.370.7 \cdot 0.49 + 0.3 \cdot 0.09 = 0.37, and reporting q=0.7q = 0.7 gives 0.7⋅0.09+0.3⋅0.49=0.210.7 \cdot 0.09 + 0.3 \cdot 0.49 = 0.21. The difference 0.160.16 is exactly (0.7−0.3)2(0.7 - 0.3)^2, while the part 0.21=0.7⋅0.30.21 = 0.7 \cdot 0.3 remains whatever is reported.

Proper loss

Definition A loss ℓ(y,q)\ell(y, q) for probability reports of a binary event is proper if, for every p∈[0,1]p \in [0, 1], the expected loss

L(p,q)=p ℓ(1,q)+(1−p) ℓ(0,q)\begin{aligned} L(p, q) &= p\, \ell(1, q) \\[0.4em] &\quad + (1 - p)\, \ell(0, q) \end{aligned}

is minimized over qq at q=pq = p, and strictly proper if q=pq = p is its only minimizer. The Brier loss is strictly proper, because L(p,q)=p(1−p)+(p−q)2L(p, q) = p(1 - p) + (p - q)^2, including the degenerate cases p=0p = 0 and p=1p = 1. The log loss ℓ(y,q)=−ylog⁡q−(1−y)log⁡(1−q)\ell(y, q) = -y \log q - (1 - y) \log(1 - q), with the natural logarithm, is strictly proper too. For pp and qq in (0,1)(0, 1),

∂qL(p,q)=−pq+1−p1−q=q−pq(1−q),\begin{aligned} \partial_q L(p, q) &= -\frac{p}{q} + \frac{1 - p}{1 - q} \\[0.4em] &= \frac{q - p}{q(1 - q)}, \end{aligned}

which is negative for q<pq < p and positive for q>pq > p, so q=pq = p is the unique minimizer, while the reports q=0q = 0 and q=1q = 1 give infinite expected loss. For p∈{0,1}p \in \{0, 1\} the minimum is at the matching endpoint, with the convention 0log⁡0=00 \log 0 = 0. The absolute loss ∣y−q∣\lvert y - q \rvert is not proper, since L(p,q)=p+(1−2p)qL(p, q) = p + (1 - 2p)q is linear in qq and is minimized only at q=1q = 1 whenever p>1/2p > 1/2.

In plain words Under a proper loss, a forecaster whose information gives the event probability pp cannot lower the expected loss by reporting anything else, so honest reporting is optimal on average. An improper loss such as absolute error rewards pushing reports toward zero or one, so its best reports are not probabilities at all.

Example With p=0.7p = 0.7, the expected log loss of the honest report is −0.7log⁡0.7−0.3log⁡0.3≈0.6109-0.7 \log 0.7 - 0.3 \log 0.3 \approx 0.6109, and that of q=0.3q = 0.3 is about 0.94980.9498, a gap of about 0.33890.3389. Under absolute loss the honest report q=0.7q = 0.7 has expected loss 0.7⋅0.3+0.3⋅0.7=0.420.7 \cdot 0.3 + 0.3 \cdot 0.7 = 0.42, while the report q=1q = 1 has 0.30.3. A forecaster scored by absolute error does better by claiming certainty.

Probability calibration

Definition Let Y∈{0,1}Y \in \{0, 1\} and let Q=Q(X)Q = Q(X) be a reported probability with values in [0,1][0, 1]. The forecast is calibrated under P\mathbb{P} if

E[Y∣Q]=Q\mathbb{E}[Y \mid Q] = Q

almost surely. When QQ takes finitely many values, this means P(Y=1∣Q=q)=q\mathbb{P}(Y = 1 \mid Q = q) = q for every value qq with P(Q=q)>0\mathbb{P}(Q = q) > 0. The true conditional probability P(Y=1∣X)\mathbb{P}(Y = 1 \mid X) is calibrated by the tower property, and so is the constant forecast equal to the base rate P(Y=1)\mathbb{P}(Y = 1). Write c(Q)=E[Y∣Q]c(Q) = \mathbb{E}[Y \mid Q]. The squared-error identity of Financial prediction and feature construction, applied with the information generated by QQ, and the law of total variance give

E[(Y−Q)2]=Var⁡(Y)−Var⁡(c(Q))+E[(c(Q)−Q)2].\begin{aligned} \mathbb{E}\bigl[(Y - Q)^2\bigr] &= \operatorname{Var}(Y) \\[0.4em] &\quad - \operatorname{Var}\bigl(c(Q)\bigr) \\[0.4em] &\quad + \mathbb{E}\bigl[(c(Q) - Q)^2\bigr]. \end{aligned}

The last term, the reliability term, is zero exactly when the forecast is calibrated. The middle term, the resolution, measures how far the forecast separates cases with different event rates, which calibration alone does not provide.

In plain words Among all the cases that receive the forecast 0.30.3, the event should occur with frequency 0.30.3 in the population the forecast is used on. A forecast can pass that test while being useless for telling high-risk cases from low-risk ones, and a forecast can rank cases perfectly while its numbers are too extreme. Calibration and discrimination are therefore checked separately, and a calibration map fitted from data is judged on predictions it was not fitted to, such as held-out or cross-fitted ones.

Example In the two-regime model of the previous lesson, let Y=1Y = 1 for the up move, so P(Y=1)=0.4\mathbb{P}(Y = 1) = 0.4 and Var⁡(Y)=0.24\operatorname{Var}(Y) = 0.24. The constant forecast 0.40.4 is calibrated, with Brier risk 0.240.24 and no resolution. The forecast 0.60.6 in the high regime and 0.20.2 in the low regime is calibrated, its resolution is 0.5⋅0.22+0.5⋅0.22=0.040.5 \cdot 0.2^2 + 0.5 \cdot 0.2^2 = 0.04 and its Brier risk is 0.200.20. The forecast 0.90.9 in the high regime and 0.10.1 in the low regime ranks the regimes the same way, but c(Q)c(Q) is still 0.60.6 and 0.20.2, so its reliability term is 0.5⋅0.32+0.5⋅0.12=0.050.5 \cdot 0.3^2 + 0.5 \cdot 0.1^2 = 0.05 and its Brier risk is 0.250.25, worse than the constant forecast.

Distribution shift and covariate shift

Definition Let Ptrain\mathbb{P}_{\text{train}} be the law of the training pairs (X,Y)(X, Y) and Pdeploy\mathbb{P}_{\text{deploy}} the law of the pairs on which the rule will be used, both physical laws of observations. A distribution shift is any difference between the two laws. Covariate shift is the case in which the law of XX changes while the conditional law of YY given X=xX = x stays the same, at least for deployment-almost every xx. Suppose in addition that the deployment law of XX gives probability zero to every set of training probability zero, which is absolute continuity, so that it has a density ratio w(x)w(x) with respect to the training law of XX. Where both laws have densities, w(x)=pdeploy(x)/ptrain(x)w(x) = p_{\text{deploy}}(x)/p_{\text{train}}(x) at points with ptrain(x)>0p_{\text{train}}(x) > 0. If also Etrain[w(X) ∣ℓ(Y,f(X))∣]<∞\mathbb{E}_{\text{train}}[w(X)\, \lvert \ell(Y, f(X)) \rvert] < \infty, then

Edeploy[ℓ(Y,f(X))]=Etrain[w(X) ℓ(Y,f(X))].\begin{aligned} &\mathbb{E}_{\text{deploy}}\bigl[\ell(Y, f(X))\bigr] \\[0.4em] &= \mathbb{E}_{\text{train}}\bigl[w(X)\, \ell(Y, f(X))\bigr]. \end{aligned}

The proof writes the deployment risk as an integral of the loss against the common conditional law of YY and the deployment law of XX, and then multiplies and divides by ptrain(x)p_{\text{train}}(x) where it is positive. If the conditional law changes as well, the weights alone no longer give the deployment risk. If deployment reaches a region the training law never visits, no weight exists, and the training sample carries no information about the loss there.

In plain words Under covariate shift the relation between features and outcome is unchanged and only the mix of situations differs, so training observations from situations that become more common get larger weights and the rest get smaller ones. The identity is exact for the population, but a finite weighted average can be unstable when a few weights are very large, and the weights usually have to be estimated. The conditional mean function is unchanged, yet the best rule within a restricted class, such as linear rules, can change with the law of XX. Both laws here are physical laws of observations, and nothing in this card is a change to a pricing measure.

Example Return to the two-regime model with the forecast 0.0160.016 in the high regime and −0.008-0.008 in the low regime, whose conditional squared-error losses are 0.0008640.000864 and 0.0005760.000576. In training the high regime has probability 0.50.5 and the risk is 0.000720.00072. In a constructed deployment period the high regime has probability 0.80.8 and the up probabilities stay 0.60.6 and 0.20.2. The weights are 0.8/0.5=1.60.8/0.5 = 1.6 and 0.2/0.5=0.40.2/0.5 = 0.4, and

0.5⋅1.6⋅0.000864+0.5⋅0.4⋅0.000576=0.0008064,\begin{aligned} &0.5 \cdot 1.6 \cdot 0.000864 \\[0.4em] &\quad + 0.5 \cdot 0.4 \cdot 0.000576 \\[0.4em] &= 0.0008064, \end{aligned}

which equals the deployment risk 0.8⋅0.000864+0.2⋅0.0005760.8 \cdot 0.000864 + 0.2 \cdot 0.000576. If instead the up probability in the high regime also falls to 0.40.4, the conditional mean there becomes 0.0040.004, and the forecast 0.0160.016 has loss 0.000864+0.0122=0.0010080.000864 + 0.012^2 = 0.001008 in that regime. The deployment risk is then 0.00092160.0009216, while the reweighted training risk still reports 0.00080640.0008064.

Check yourself

  1. Fit fθ(x)=θxf_\theta(x) = \theta x without an intercept to the pairs (1,2)(1, 2) and (3,3)(3, 3), using the ridge objective of this lesson with λ=0.5\lambda = 0.5. Find the ridge coefficient and the least-squares coefficient, and compare their objective values.
Show the answer

Here n=2n = 2, X⊤X=1+9=10X^{\top}X = 1 + 9 = 10, X⊤y=2+9=11X^{\top}y = 2 + 9 = 11 and nλ=1n\lambda = 1, so the ridge coefficient is 11/(10+1)=111/(10 + 1) = 1 and the least-squares coefficient is 11/10=1.111/10 = 1.1. At θ=1\theta = 1 the residuals are 11 and 00, the mean squared error is 0.50.5, the penalty is 0.5⋅1=0.50.5 \cdot 1 = 0.5 and the objective is 11. At θ=1.1\theta = 1.1 the residuals are 0.90.9 and −0.3-0.3, the mean squared error is (0.81+0.09)/2=0.45(0.81 + 0.09)/2 = 0.45, the penalty is 0.5⋅1.21=0.6050.5 \cdot 1.21 = 0.605 and the objective is 1.0551.055. The ridge fit has the larger training error and the smaller objective, as the unique minimizer must. Neither number says which coefficient predicts better on new data.

  1. Given everything a forecaster knows, an event has probability 0.20.2. The forecaster considers reporting 00 instead of 0.20.2 to sound decisive. Compare the expected Brier loss and the expected absolute loss of the two reports. Which of the two losses is proper?
Show the answer

Under the Brier loss the honest report has expected loss 0.2⋅0.64+0.8⋅0.04=0.160.2 \cdot 0.64 + 0.8 \cdot 0.04 = 0.16, which is 0.2⋅0.80.2 \cdot 0.8, and the report 00 has 0.2⋅1+0.8⋅0=0.20.2 \cdot 1 + 0.8 \cdot 0 = 0.2. The difference 0.040.04 is (0.2−0)2(0.2 - 0)^2, so honesty wins. Under absolute loss the honest report has 0.2⋅0.8+0.8⋅0.2=0.320.2 \cdot 0.8 + 0.8 \cdot 0.2 = 0.32, while the report 00 has 0.20.2, so the extreme report wins. The Brier loss is strictly proper. The absolute loss is not proper, because its expected value 0.2+0.6q0.2 + 0.6q is smallest at q=0q = 0.

  1. A model is fitted on a sample in which high-volatility days are 2020 percent of all days, and it will be used in a period in which they are 5050 percent. Its mean squared errors, conditional on the type of day, are 0.00090.0009 on high-volatility days and 0.00010.0001 on the others, and the relation between features and returns is assumed unchanged. Find the training risk, the weights and the deployment risk obtained by reweighting. What goes wrong if the deployment period also contains a crisis state that never occurs in training?
Show the answer

The training risk is 0.2⋅0.0009+0.8⋅0.0001=0.000260.2 \cdot 0.0009 + 0.8 \cdot 0.0001 = 0.00026. The weights are 0.5/0.2=2.50.5/0.2 = 2.5 for high-volatility days and 0.5/0.8=0.6250.5/0.8 = 0.625 for the others, and the reweighted training risk is

0.2⋅2.5⋅0.0009+0.8⋅0.625⋅0.0001=0.0005,\begin{aligned} &0.2 \cdot 2.5 \cdot 0.0009 \\[0.4em] &\quad + 0.8 \cdot 0.625 \cdot 0.0001 \\[0.4em] &= 0.0005, \end{aligned}

which equals 0.5⋅0.0009+0.5⋅0.00010.5 \cdot 0.0009 + 0.5 \cdot 0.0001. The same model has almost twice its training risk in deployment, only because the mix of days changes. A crisis state that has training probability zero but positive deployment probability breaks absolute continuity. No weight can represent it, the training sample contains no losses from that state, and the identity gives no estimate of the deployment risk. The identity also fails if the relation between features and returns changes, even on days of familiar types.

How this lesson connects