Simple Algorithm
Machine Learning · Descent · 22 min

The Learning Problem

Data, hypotheses, losses and risk: the setup behind every model.

Every method in this course answers one question: given nn examples drawn from a distribution you will never see, choose a rule that does well on the next draw. This lesson writes that question down exactly. You will derive the best possible predictor for each loss, watch empirical risk minimization memorize when its model class is too rich, and split the error of any learned model into the two parts every method trades off.

What you’ll learn

  • Define the statistical learning setup: input and output spaces, an unknown distribution, an i.i.d. sample, a hypothesis class and a loss.
  • Distinguish risk from empirical risk, define empirical risk minimization, and show that the training error of its output is optimistic.
  • Derive the Bayes predictors: the conditional mean for squared loss and the most probable class for the 0–1 loss.
  • Decompose excess risk into approximation and estimation error, bound the estimation error of ERM over a finite class, and explain why some inductive bias is unavoidable.
  • Explain why convex surrogate losses replace the 0–1 loss in practice.

The setup, precisely

Inputs x\mathbf x live in an input space X\mathcal X (often Rd\R^d); outputs yy live in an output space Y\mathcal Y: R\R for regression, a finite set such as {0,1}\{0,1\}, {−1,+1}\{-1,+1\} or {1,…,K}\{1,\dots,K\} for classification. Examples are pairs (x,y)(\mathbf x,y) drawn from a joint data distribution PP on X×Y\mathcal X\times\mathcal Y that nobody knows. The training set D={(xi,yi)}i=1n\data=\{(\mathbf x_i,y_i)\}_{i=1}^n consists of nn draws that are independent and identically distributed (i.i.d.) from PP.

A learning algorithm is a map from datasets to predictors, D↦h^\data\mapsto\hat h. Usually it chooses from a hypothesis class H\mathcal H of functions h:X→Yh:\mathcal X\to\mathcal Y, or h:X→Rh:\mathcal X\to\R when the prediction is a score or a probability.

Loss ℓ(y^,y)\ell(\hat y,y)FormulaUsed forRemarks
0–1 loss1{y^≠y}\mathbf 1\{\hat y\ne y\}classificationwhat you usually care about; neither continuous nor convex
Squared loss(y^−y)2(\hat y-y)^2regressionsmooth; large errors dominate
Absolute loss∣y^−y∣\abs{\hat y-y}regressionless sensitive to outliers
Log loss−log⁡p^(y∣x)-\log\hat p(y\mid\mathbf x)probabilistic classificationp^\hat p is the predicted probability of the observed label; also called cross-entropy
Hinge lossmax⁡(0, 1−y s(x))\max\big(0,\,1-y\,s(\mathbf x)\big)classification with y∈{−1,+1}y\in\{-1,+1\}s(x)s(\mathbf x) is a real score, the prediction is sign⁡s(x)\sign s(\mathbf x); used by SVMs

The risk of hh is its expected loss on a fresh example; the empirical risk is its average loss on the training set:

R(h)=E(x,y)∼P[ℓ(h(x),y)],R^n(h)=1n∑i=1nℓ(h(xi),yi).R(h)=\E_{(\mathbf x,y)\sim P}\big[\ell(h(\mathbf x),y)\big],\qquad\hat R_n(h)=\frac1n\sum_{i=1}^n\ell\big(h(\mathbf x_i),y_i\big).

You want small RR and can only compute R^n\hat R_n. For a hypothesis fixed before the data are drawn, each term of R^n(h)\hat R_n(h) has mean R(h)R(h), so E R^n(h)=R(h)\E\,\hat R_n(h)=R(h), and the law of large numbers gives R^n(h)→R(h)\hat R_n(h)\to R(h). Empirical risk minimization (ERM) returns h^∈arg min⁡h∈HR^n(h)\hat h\in\argmin_{h\in\mathcal H}\hat R_n(h).

Check your understanding

For a fixed hh, R^n(h)\hat R_n(h) is an unbiased estimate of R(h)R(h). What about R^n(h^)\hat R_n(\hat h), the training error of the ERM output?

  1. Also unbiased, by linearity of expectation
  2. Biased upward: training error overstates the risk
  3. Biased downward: h^\hat h was chosen to make it small
  4. Unbiased once nn exceeds the number of parameters
Show the answer

C. Biased downward: h^\hat h was chosen to make it small

Let h∘h^\circ minimize RR over H\mathcal H. ERM guarantees R^n(h^)≤R^n(h∘)\hat R_n(\hat h)\le\hat R_n(h^\circ) on every sample, and h∘h^\circ is fixed, so E R^n(h^)≤E R^n(h∘)=R(h∘)≤E R(h^)\E\,\hat R_n(\hat h)\le\E\,\hat R_n(h^\circ)=R(h^\circ)\le\E\,R(\hat h). The same data chose h^\hat h and graded it.

The best possible predictor

Suppose you knew PP. Which function would you use? The answer, the Bayes predictor, depends on the loss. For squared loss (assuming E[y2]<∞\E[y^2]<\infty) it is the regression function m(x)=E[y∣x]m(\mathbf x)=\E[y\mid\mathbf x].

DerivationThe conditional mean minimizes the squared risk
  1. R(h)=E[(h(x)−y)2]=E[(h(x)−m(x)+m(x)−y)2]R(h)=\E\big[(h(\mathbf x)-y)^2\big]=\E\big[\big(h(\mathbf x)-m(\mathbf x)+m(\mathbf x)-y\big)^2\big]

    Take any hh and add and subtract m(x)m(\mathbf x) inside the square:

  2. E[(h(x)−m(x))(m(x)−y)]=E[(h(x)−m(x)) E[m(x)−y∣x]]=0\E\big[(h(\mathbf x)-m(\mathbf x))(m(\mathbf x)-y)\big]=\E\Big[(h(\mathbf x)-m(\mathbf x))\,\E\big[m(\mathbf x)-y\mid\mathbf x\big]\Big]=0

    The cross term vanishes. Condition on x\mathbf x: then h(x)−m(x)h(\mathbf x)-m(\mathbf x) is a constant, and m(x)−ym(\mathbf x)-y has conditional mean zero by the definition of mm.

  3. R(h)=E[(h(x)−m(x))2]+E[Var⁡(y∣x)]R(h)=\E\big[(h(\mathbf x)-m(\mathbf x))^2\big]+\E\big[\Var(y\mid\mathbf x)\big]

    Two nonnegative terms remain, and only the first depends on hh:

  4. R∗=inf⁡hR(h)=R(m)=E[Var⁡(y∣x)]R^*=\inf_h R(h)=R(m)=\E\big[\Var(y\mid\mathbf x)\big]

    So mm minimizes the risk (uniquely, up to changes on a set of probability zero), every other predictor pays its mean squared distance from mm, and the minimum is the average conditional variance: the noise floor.

For classification with the 0–1 loss, write ηk(x)=P(y=k∣x)\eta_k(\mathbf x)=P(y=k\mid\mathbf x). Predicting class kk at x\mathbf x is wrong with conditional probability 1−ηk(x)1-\eta_k(\mathbf x), so the best choice at each x\mathbf x, separately, is the most probable class. Averaging over x\mathbf x gives the Bayes classifier and its risk, the Bayes error:

h∗(x)∈arg max⁡k ηk(x),R∗=E[1−max⁡kηk(x)].h^*(\mathbf x)\in\argmax_k\,\eta_k(\mathbf x),\qquad R^*=\E\Big[1-\max_k\eta_k(\mathbf x)\Big].

Ties can be broken any way. The Convergence lesson Risk, Bayes Optimality & Excess Risk turns this pointwise argument into an exact formula for how much any other classifier loses.

LossBayes predictor at x\mathbf xBayes risk R∗R^*
Squaredconditional mean E[y∣x]\E[y\mid\mathbf x]E[Var⁡(y∣x)]\E\big[\Var(y\mid\mathbf x)\big]
Absolutea conditional median of yy given x\mathbf xaverage absolute deviation from that median
0–1most probable class arg max⁡kηk(x)\argmax_k\eta_k(\mathbf x)E[1−max⁡kηk(x)]\E\big[1-\max_k\eta_k(\mathbf x)\big]
Log lossthe true conditional distribution P(⋅∣x)P(\cdot\mid\mathbf x)the conditional entropy of yy given x\mathbf x (see Information Theory for ML)
Change the loss and the target changes. A model that predicts the mean is the wrong tool if errors are scored by absolute value and the noise is skewed.
Predict before you look

A blood test returns low for 60% of patients and high for 40%. Among low results, 10% of patients have the disease; among high results, 70% do. What is the Bayes error of predicting the disease from the test result alone, in percent?

Show the answer

18 %

The Bayes classifier predicts "healthy" for low results (wrong 10% of the time) and "disease" for high results (wrong 30% of the time). So R∗=0.6×0.10+0.4×0.30=0.06+0.12=0.18R^*=0.6\times0.10+0.4\times0.30=0.06+0.12=0.18. No classifier that sees only this test result can do better than 18% error, however much data it has.

ERM, and how it memorizes

ERM needs a class, and the class matters. Let H\mathcal H contain every function, and consider the lookup-table learner: predict yiy_i at each training input xi\mathbf x_i and predict 00 everywhere else. Its empirical risk is zero, so it is an ERM solution.

Now let the inputs be continuous measurements. A new x\mathbf x coincides with a training input with probability zero, so the lookup table predicts 00 almost everywhere, and its 0–1 risk is P(y≠0)P(y\ne0), which can be 1/21/2 or worse, for every nn. Zero training error, no learning at all.

With nested classes H1⊆H2⊆⋯\mathcal H_1\subseteq\mathcal H_2\subseteq\cdots, such as polynomials of degree at most dd, the minimal training error can only fall as dd grows: every fit available in Hd\mathcal H_d is still available in Hd+1\mathcal H_{d+1}. The risk of the ERM solution behaves differently.

Check your understanding

You fit polynomials of degree dd and d+1d+1 by least squares to the same training set. Which statement is always true?

  1. The degree-(d+1)(d+1) fit has test error at most that of the degree-dd fit
  2. Both training and test error decrease
  3. The degree-(d+1)(d+1) fit has training error at most that of the degree-dd fit
  4. Neither error can be predicted without seeing the data
Show the answer

C. The degree-(d+1)(d+1) fit has training error at most that of the degree-dd fit

Every degree-dd polynomial is a degree-(d+1)(d+1) polynomial whose top coefficient is zero, so the larger class can match any fit of the smaller one: the minimal training error cannot go up. Test error has no such guarantee, which is the whole point of the U-curve.

Two errors and an inductive bias

R(h^)−R∗=R(h^)−inf⁡h∈HR(h)⏟estimation error+inf⁡h∈HR(h)−R∗⏟approximation errorR(\hat h)-R^*=\underbrace{R(\hat h)-\inf_{h\in\mathcal H}R(h)}_{\text{estimation error}}+\underbrace{\inf_{h\in\mathcal H}R(h)-R^*}_{\text{approximation error}}
The excess risk of a learned predictor. The approximation error is deterministic: it measures how well the best member of H\mathcal H can do, and it can only shrink as H\mathcal H grows. The estimation error is random: it measures what is lost by choosing from data, and it typically grows with the richness of H\mathcal H and shrinks with nn.
DerivationERM loses at most twice the worst deviation
  1. R(h^)−R(h∘)=[R(h^)−R^n(h^)]+[R^n(h^)−R^n(h∘)]+[R^n(h∘)−R(h∘)]R(\hat h)-R(h^\circ)=\big[R(\hat h)-\hat R_n(\hat h)\big]+\big[\hat R_n(\hat h)-\hat R_n(h^\circ)\big]+\big[\hat R_n(h^\circ)-R(h^\circ)\big]

    Let h∘h^\circ minimize RR over H\mathcal H (assume a minimizer exists; otherwise take one within any ϵ\epsilon). Split the estimation error of the ERM output h^\hat h into three pieces:

  2. R(h^)−R(h∘)≤2sup⁡h∈H∣R^n(h)−R(h)∣R(\hat h)-R(h^\circ)\le2\sup_{h\in\mathcal H}\big|\hat R_n(h)-R(h)\big|

    The middle bracket is at most 00 because h^\hat h minimizes R^n\hat R_n. Each outer bracket is at most the largest deviation between empirical and true risk over the class:

  3. sup⁡h∈H∣R^n(h)−R(h)∣≤ln⁡(2∣H∣/δ)2n⟹R(h^)−R(h∘)≤2ln⁡(2∣H∣/δ)n\sup_{h\in\mathcal H}\big|\hat R_n(h)-R(h)\big|\le\sqrt{\frac{\ln(2\abs{\mathcal H}/\delta)}{2n}}\quad\Longrightarrow\quad R(\hat h)-R(h^\circ)\le\sqrt{\frac{2\ln(2\abs{\mathcal H}/\delta)}{n}}

    For a finite class and a loss with values in [0,1][0,1], Hoeffding's inequality and a union bound over the ∣H∣\abs{\mathcal H} hypotheses (proved in the Convergence lesson Probability & Concentration Inequalities) give, with probability at least 1−δ1-\delta over the sample:

Predict before you look

ERM chooses among ∣H∣=106\abs{\mathcal H}=10^6 classifiers using n=10,000n=10{,}000 examples. With probability at least 95%, its estimation error under the 0–1 loss is at most how many percentage points, by the bound above?

Show the answer

5.9 points

ln⁡(2×106/0.05)=ln⁡(4×107)≈17.50\ln(2\times10^6/0.05)=\ln(4\times10^7)\approx17.50, so the bound is 2×17.50/10,000=0.0035≈0.059\sqrt{2\times17.50/10{,}000}=\sqrt{0.0035}\approx0.059: about 5.9 points. The class size enters only through its logarithm: a million hypotheses cost about 14 nats, so even a thousandfold larger class would raise the bound only to about 7.0 points. Infinite classes need a different measure of size, the VC dimension of the topic Generalization & Model Selection.

Choosing H\mathcal H, and any preference among its members, before seeing the data is the learner's inductive bias. A smaller or better-aimed class lowers estimation error at the risk of raising approximation error. No learner can do without one: the Convergence lesson No Free Lunch & Inductive Bias proves that for every learner there is a distribution on which it fails badly while another learner succeeds.

LearnerIts inductive bias
kk-nearest neighborsnearby inputs have similar labels (locality)
Linear and logistic regressionthe response, or its log-odds, is linear in the features
Ridge and lassosmall coefficients; with the lasso, few nonzero ones (sparsity)
Decision trees grown greedilyaxis-aligned, piecewise-constant rules, simple ones first
Convolutional networksa pattern means the same thing wherever it appears in an image (translation equivariance)
Gaussian processeswith the usual kernels, smooth functions at a chosen length scale
Think first Inputs are 10 binary features, so there are 210=10242^{10}=1024 possible inputs. You see the correct label for 1,000 of them. How many functions {0,1}10→{0,1}\{0,1\}^{10}\to\{0,1\} agree with all 1,000 labels, and what do they say about the other 24 inputs?

224≈1.7×1072^{24}\approx1.7\times10^7 functions fit the data perfectly, one for each way of labeling the 24 unseen inputs. On every unseen input, exactly half of them say 00 and half say 11. The data rule out almost all functions yet decide no unseen label; only a bias (a smaller class, or a preference such as "similar inputs get similar labels") does.

Losses you can optimize

For classification the 0–1 loss is usually the loss you care about, but ERM with it is hard to carry out. The empirical 0–1 risk of a linear classifier sign⁡(w⊤x+b)\sign(\mathbf w^\top\mathbf x+b) is piecewise constant in (w,b)(\mathbf w,b), so its gradient is zero wherever it exists, and finding the best halfspace on data that no halfspace separates is NP-hard (the Convergence lesson The Computational Hardness of Learning has the details).

Practice therefore minimizes a convex surrogate φ\varphi of the margin m=y s(x)m=y\,s(\mathbf x), with labels y∈{−1,+1}y\in\{-1,+1\} and a real-valued score ss. A positive margin means a correct prediction; a large one means a confident correct prediction.

Lossφ(m)\varphi(m)Minimized by
0–11{m≤0}\mathbf 1\{m\le0\}the target, not minimized directly
Hingemax⁡(0,1−m)\max(0,1-m)support vector machines
Logisticlog⁡(1+e−m)\log(1+e^{-m})logistic regression
Exponentiale−me^{-m}AdaBoost
Squared(1−m)2(1-m)^2least-squares classification
The hinge, exponential and squared losses lie above the 0–1 loss; the logistic loss does too once written with log⁡2\log_2 (with the natural log it equals ln⁡2<1\ln 2<1 at m=0m=0). All four are convex and classification-calibrated: driving their risk to its minimum drives the 0–1 risk to the Bayes error.

Key takeaways

  • The learning problem: an unknown distribution PP on X×Y\mathcal X\times\mathcal Y, an i.i.d. sample D\data, a hypothesis class H\mathcal H and a loss ℓ\ell. Risk R(h)=E ℓR(h)=\E\,\ell is the target, empirical risk R^n(h)\hat R_n(h) its observable proxy, and ERM minimizes the proxy.
  • The Bayes predictor depends on the loss: the conditional mean for squared loss, a conditional median for absolute loss, the most probable class for the 0–1 loss. Its risk R∗R^* is a floor no learner can beat.
  • The training error of the ERM output is optimistic on average, and over a class that is too rich ERM memorizes: zero training error, no generalization.
  • Excess risk = estimation error + approximation error. For a finite class and a loss in [0,1][0,1], ERM's estimation error is at most 2ln⁡(2∣H∣/δ)/n\sqrt{2\ln(2\abs{\mathcal H}/\delta)/n} with probability at least 1−δ1-\delta. Choosing H\mathcal H is an inductive bias, and no learner can avoid having one.
  • In practice the 0–1 loss is replaced by convex, classification-calibrated surrogates (hinge, logistic, exponential), which are tractable to minimize and still aim at the Bayes classifier.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Under squared loss, which predictor has the smallest risk?

  1. The conditional median of yy given x\mathbf x
  2. The least-squares line
  3. The most probable value of yy given x\mathbf x
  4. The conditional mean E[y∣x]\E[y\mid\mathbf x]
Show the answer

D. The conditional mean E[y∣x]\E[y\mid\mathbf x]

R(h)=E[(h(x)−E[y∣x])2]+E[Var⁡(y∣x)]R(h)=\E[(h(\mathbf x)-\E[y\mid\mathbf x])^2]+\E[\Var(y\mid\mathbf x)], so the conditional mean minimizes the risk and attains the noise floor E[Var⁡(y∣x)]\E[\Var(y\mid\mathbf x)].

Question 2

For binary classification, when is the Bayes error zero?

  1. When the two classes are equally frequent
  2. When the label is a deterministic function of the input: η(x)∈{0,1}\eta(\mathbf x)\in\{0,1\} for almost every x\mathbf x
  3. When the training set is large enough
  4. Never: there is always some noise
Show the answer

B. When the label is a deterministic function of the input: η(x)∈{0,1}\eta(\mathbf x)\in\{0,1\} for almost every x\mathbf x

R∗=E[min⁡(η(x),1−η(x))]R^*=\E[\min(\eta(\mathbf x),1-\eta(\mathbf x))], which is zero exactly when η(x)\eta(\mathbf x) is 00 or 11 almost everywhere, that is, when x\mathbf x determines yy.

Question 3

You replace a hypothesis class H\mathcal H by a strictly larger class H′⊃H\mathcal H'\supset\mathcal H and keep using ERM. Which of these is guaranteed?

  1. The approximation error does not increase
  2. The estimation error does not increase
  3. The excess risk decreases
  4. The training error increases
Show the answer

A. The approximation error does not increase

The approximation error is inf⁡HR−R∗\inf_{\mathcal H}R-R^*, and an infimum over a larger set can only be smaller or equal. The estimation error usually grows, so the excess risk can go either way.

Question 4

A learner stores its training set, predicts the stored label for any input it has seen before, and predicts "no" otherwise. The inputs are continuous measurements. As n→∞n\to\infty, what happens to its risk?

  1. It tends to the Bayes error, because the table eventually covers everything
  2. It tends to zero
  3. It stays at P(y≠no)P(y\ne\text{no}), because new inputs almost never repeat a stored one
  4. It tends to 1/21/2 for every distribution
Show the answer

C. It stays at P(y≠no)P(y\ne\text{no}), because new inputs almost never repeat a stored one

A continuous input repeats a stored one with probability zero, so the learner answers "no" almost always. Its training error is zero and its risk never improves: ERM over the class of all functions memorizes without learning.

Question 5

Why do logistic regression and SVMs minimize the logistic and hinge losses instead of the 0–1 loss?

  1. The 0–1 loss is not what practitioners care about
  2. Surrogate losses always give lower test error than the 0–1 loss
  3. The 0–1 loss cannot be evaluated on real-valued scores
  4. Empirical 0–1 risk is piecewise constant and NP-hard to minimize over halfspaces in general, while convex, calibrated surrogates can be minimized efficiently and still aim at the Bayes classifier
Show the answer

D. Empirical 0–1 risk is piecewise constant and NP-hard to minimize over halfspaces in general, while convex, calibrated surrogates can be minimized efficiently and still aim at the Bayes classifier

Convexity makes the optimization tractable, and classification calibration ensures that minimizing the surrogate risk over all functions yields the Bayes classifier. The 0–1 loss remains the yardstick for evaluation.

Go deeper

Further reading

  • S. Shalev-Shwartz & S. Ben-David, Understanding Machine Learning: From Theory to Algorithms (Cambridge University Press, 2014), chapters 2–5.
  • T. Hastie, R. Tibshirani & J. Friedman, The Elements of Statistical Learning, 2nd ed. (Springer, 2009), §2.4 (statistical decision theory).
  • M. Mohri, A. Rostamizadeh & A. Talwalkar, Foundations of Machine Learning, 2nd ed. (MIT Press, 2018), chapters 2 and 4.
  • F. Bach, Learning Theory from First Principles (MIT Press, 2024).