Loss Functions & Risk Minimization

The mathematical framework connecting loss functions, empirical risk, and statistical risk — deriving squared loss from Gaussian noise, cross-entropy from MLE, and establishing PAC-style generalization bounds.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Prerequisites
  3. Notation
  4. Core Intuition
  5. The Decision-Theoretic Framework
  6. Common Loss Functions
  7. From Loss to Risk
  8. Empirical Risk Minimization
  9. Bayes Optimal Predictor
  10. Surrogate Loss Functions
  11. Generalization and Excess Risk
  12. Common Pitfalls
  13. Research Perspective
  14. Summary
  15. Exercises

Learning Objectives

  1. Formalize the supervised learning problem in terms of loss, risk, and hypothesis classes.
  2. Derive squared error loss from the Gaussian likelihood assumption.
  3. Derive cross-entropy loss from maximum likelihood for Bernoulli/categorical models.
  4. Distinguish between statistical (population) risk and empirical risk.
  5. State and interpret the Bayes optimal predictor for squared and 0-1 loss.
  6. Explain why surrogate losses are necessary for classification.
  7. Understand the decomposition of excess risk into estimation and approximation error.

Prerequisites


Notation

  • X\mathcal{X} — input space, Y\mathcal{Y} — output space
  • (X,Y)P(X, Y) \sim P — joint distribution over data
  • f:XYf: \mathcal{X} \to \mathcal{Y} — predictor (hypothesis)
  • F\mathcal{F} — hypothesis class
  • (f(x),y)\ell(f(x), y) — loss function
  • R(f)=E(X,Y)[(f(X),Y)]R(f) = \mathbb{E}_{(X,Y)}[\ell(f(X), Y)] — statistical (population) risk
  • R^n(f)=1ni=1n(f(xi),yi)\hat{R}_n(f) = \frac{1}{n}\sum_{i=1}^n \ell(f(x_i), y_i) — empirical risk
  • f=argminfR(f)f^* = \arg\min_{f} R(f) — Bayes optimal predictor
  • f^n=argminfFR^n(f)\hat{f}_n = \arg\min_{f \in \mathcal{F}} \hat{R}_n(f) — ERM solution

Core Intuition

Machine learning is fundamentally an optimization problem: we seek a function ff that predicts well on unseen data. But "predicts well" must be defined precisely. The loss function \ell quantifies the penalty for predicting y^\hat{y} when the truth is yy. The risk is the expected loss over the true data distribution. Since we cannot compute the risk (we don't know PP), we minimize the empirical risk over training data and hope it generalizes.

Loss Functions Compared

error (ŷ − y)L(e)
MSE = 1.440
MAE = 1.200
HUBER = 0.700
LOGCOSH = 0.594
Error e
1.20
Huber δ
1.00
MSEMAEHUBERLOGCOSH
Explore: MSE penalizes large errors quadratically (sensitive to outliers). MAE is linear. Huber blends both — adjust δ to see the transition. Log-cosh is smooth like MSE but robust.

The Decision-Theoretic Framework

Definition (Statistical Learning Problem). Given:

  • A joint distribution PP over X×Y\mathcal{X} \times \mathcal{Y} (unknown)
  • A loss function :Y×YR0\ell: \mathcal{Y} \times \mathcal{Y} \to \mathbb{R}_{\geq 0}
  • A hypothesis class F{f:XY}\mathcal{F} \subseteq \{f: \mathcal{X} \to \mathcal{Y}\}
  • Training data {(xi,yi)}i=1niidP\{(x_i, y_i)\}_{i=1}^n \overset{\text{iid}}{\sim} P

Find f^F\hat{f} \in \mathcal{F} that minimizes the population risk:

R(f)=E(X,Y)P[(f(X),Y)]=X×Y(f(x),y)dP(x,y).(1)R(f) = \mathbb{E}_{(X,Y) \sim P}\bigl[\ell(f(X), Y)\bigr] = \int_{\mathcal{X} \times \mathcal{Y}} \ell(f(x), y) \, dP(x,y). \tag{1}

The unconstrained minimizer (over all measurable functions) is the Bayes predictor ff^*, and R(f)R(f^*) is the Bayes risk — the irreducible error.


Common Loss Functions

Squared Error (Regression)

sq(y^,y)=(y^y)2.(2)\ell_{\text{sq}}(\hat{y}, y) = (\hat{y} - y)^2. \tag{2}

Probabilistic derivation. Assume Y=f(X)+ϵY = f(X) + \epsilon with ϵN(0,σ2)\epsilon \sim \mathcal{N}(0, \sigma^2). The negative log-likelihood of one observation is:

logp(yx,f)=12σ2(yf(x))2+const.(3)-\log p(y \mid x, f) = \frac{1}{2\sigma^2}(y - f(x))^2 + \text{const}. \tag{3}

Minimizing negative log-likelihood \Leftrightarrow minimizing squared loss.

Cross-Entropy (Classification)

For binary classification with Y{0,1}Y \in \{0, 1\} and f(x)=p[0,1]f(x) = p \in [0,1]:

CE(p,y)=ylogp(1y)log(1p).(4)\ell_{\text{CE}}(p, y) = -y\log p - (1-y)\log(1-p). \tag{4}

Derivation. If YXBernoulli(f(X))Y \mid X \sim \text{Bernoulli}(f(X)), the negative log-likelihood is exactly the cross-entropy.

For KK-class classification with one-hot y\mathbf{y} and predicted probabilities p\mathbf{p}:

CE(p,y)=k=1Kyklogpk.(5)\ell_{\text{CE}}(\mathbf{p}, \mathbf{y}) = -\sum_{k=1}^K y_k \log p_k. \tag{5}

0-1 Loss (Classification)

0-1(y^,y)=1[y^y].(6)\ell_{0\text{-}1}(\hat{y}, y) = \mathbf{1}[\hat{y} \neq y]. \tag{6}

This is the natural measure of classification error, but is non-convex and non-differentiable.

Absolute Loss

abs(y^,y)=y^y.(7)\ell_{\text{abs}}(\hat{y}, y) = |\hat{y} - y|. \tag{7}

More robust to outliers than squared loss. The optimal predictor under absolute loss is the conditional median med(YX=x)\text{med}(Y \mid X = x).


From Loss to Risk

Definition (Population Risk).

R(f)=E[(f(X),Y)].(8)R(f) = \mathbb{E}[\ell(f(X), Y)]. \tag{8}

Definition (Empirical Risk). Given nn iid samples:

R^n(f)=1ni=1n(f(xi),yi).(9)\hat{R}_n(f) = \frac{1}{n}\sum_{i=1}^n \ell(f(x_i), y_i). \tag{9}

By the law of large numbers, R^n(f)a.s.R(f)\hat{R}_n(f) \xrightarrow{a.s.} R(f) for fixed ff. The challenge is that we are choosing ff based on the data — so we need uniform convergence over F\mathcal{F}.


Empirical Risk Minimization

ERM Principle. Choose the predictor that minimizes empirical risk:

f^n=argminfFR^n(f).(10)\hat{f}_n = \arg\min_{f \in \mathcal{F}} \hat{R}_n(f). \tag{10}

Theorem (Consistency of ERM). If F\mathcal{F} has finite VC-dimension dd, then for any δ>0\delta > 0, with probability at least 1δ1 - \delta:

R(f^n)R(fF)O(dlog(n/d)+log(1/δ)n),(11)R(\hat{f}_n) - R(f^*_{\mathcal{F}}) \leq \mathcal{O}\left(\sqrt{\frac{d \log(n/d) + \log(1/\delta)}{n}}\right), \tag{11}

where fF=argminfFR(f)f^*_{\mathcal{F}} = \arg\min_{f \in \mathcal{F}} R(f) is the best-in-class predictor.


Bayes Optimal Predictor

Theorem. For squared loss, the Bayes optimal predictor is the conditional expectation:

f(x)=E[YX=x].(12)f^*(x) = \mathbb{E}[Y \mid X = x]. \tag{12}

Proof. Expand:

R(f)=E[(f(X)Y)2]=E[(f(X)f(X))2]+E[(f(X)Y)2].R(f) = \mathbb{E}[(f(X) - Y)^2] = \mathbb{E}[(f(X) - f^*(X))^2] + \mathbb{E}[(f^*(X) - Y)^2].

The cross term vanishes by the tower property. The second term is irreducible noise. The first is minimized when f=ff = f^*. \blacksquare

Theorem. For 0-1 loss, the Bayes optimal predictor is:

f(x)=argmaxyYP(Y=yX=x).(13)f^*(x) = \arg\max_{y \in \mathcal{Y}} P(Y = y \mid X = x). \tag{13}

Surrogate Loss Functions

The 0-1 loss is computationally intractable (NP-hard to minimize over linear classifiers). We use surrogate losses that upper-bound 0-1 loss and are convex:

  • Hinge loss (SVM): (m)=max(0,1m)\ell(m) = \max(0, 1-m) where m=yf(x)m = y \cdot f(x)
  • Logistic loss: (m)=log(1+em)\ell(m) = \log(1 + e^{-m})
  • Exponential loss (AdaBoost): (m)=em\ell(m) = e^{-m}

All satisfy 1[m0](m)\mathbf{1}[m \leq 0] \leq \ell(m) for all mm, providing classification-calibrated surrogates.

Theorem (Zhang, 2004; Bartlett et al., 2006). A convex surrogate ϕ\phi is classification-calibrated if and only if it is differentiable at 0 and ϕ(0)<0\phi'(0) < 0. In this case, minimizing the surrogate risk is consistent for 0-1 loss.


Generalization and Excess Risk

The excess risk decomposes as:

R(f^n)R(f)=R(f^n)R(fF)estimation error+R(fF)R(f)approximation error.(14)R(\hat{f}_n) - R(f^*) = \underbrace{R(\hat{f}_n) - R(f^*_{\mathcal{F}})}_{\text{estimation error}} + \underbrace{R(f^*_{\mathcal{F}}) - R(f^*)}_{\text{approximation error}}. \tag{14}
  • Estimation error: decreases with nn (more data), increases with complexity of F\mathcal{F}.
  • Approximation error: decreases with richer F\mathcal{F}, zero if fFf^* \in \mathcal{F}.

This is the formal counterpart of the bias-variance tradeoff.


Common Pitfalls

Pitfall 1. Choosing loss based on convenience rather than the actual problem. Squared loss is sensitive to outliers; in heavy-tailed settings, Huber loss or absolute loss is more appropriate.

Pitfall 2. Confusing empirical risk with population risk. Low training loss does not guarantee low test loss — the gap depends on the complexity of F\mathcal{F} relative to nn.

Pitfall 3. Assuming cross-entropy is always correct. Cross-entropy assumes the model outputs calibrated probabilities; if it doesn't (e.g., softmax temperatures), the loss may be misleading.


Research Perspective

The risk-minimization framework dates to Vapnik's statistical learning theory (1960s–90s). Modern deep learning challenges classical bounds (VC-dimension of neural networks is enormous, yet they generalize), leading to new frameworks: PAC-Bayes bounds, compression-based bounds, and the implicit regularization perspective from gradient flow analysis.


Summary

  • A loss function quantifies prediction errors; the choice is motivated by probabilistic assumptions.
  • Population risk is the expected loss; empirical risk is the training average.
  • ERM finds the hypothesis minimizing empirical risk; generalization depends on the complexity of F\mathcal{F}.
  • The Bayes predictor achieves irreducible error; excess risk = estimation + approximation error.
  • Surrogate losses make 0-1 classification computationally tractable while preserving consistency.

Exercises

Exercise 1. Derive that the Bayes optimal predictor under absolute loss f(x)y|f(x) - y| is the conditional median.

Exercise 2. Show that for squared loss, R(f)=Var(YX)+E[(f(X)E[YX])2]R(f) = \text{Var}(Y \mid X) + \mathbb{E}[(f(X) - \mathbb{E}[Y \mid X])^2].

Exercise 3. Prove that the hinge loss upper bounds the 0-1 loss: 1[yf(x)0]max(0,1yf(x))\mathbf{1}[yf(x) \leq 0] \leq \max(0, 1 - yf(x)).

Exercise 4. For exponential loss (f,y)=eyf(x)\ell(f, y) = e^{-yf(x)}, derive the population minimizer and show it equals 12logP(Y=1X=x)P(Y=1X=x)\frac{1}{2}\log\frac{P(Y=1|X=x)}{P(Y=-1|X=x)}.

Exercise 5. Show that ERM with squared loss and the class of linear functions f(x)=wTxf(x) = \mathbf{w}^T\mathbf{x} reduces to ordinary least squares.