Skip to content
VibeFormer
Beginner26 min

Formulating a Learning Problem

Input and output spaces, hypothesis classes, loss functions, and turning a vague goal into an objective.

Formulating a Learning Problem

Intuition first

Before an algorithm can learn anything, three decisions have to be made, and they are decisions you make, not the algorithm.

What shapes of answer will I allow? A straight line? Any curve? A decision tree of depth at most five? This is the hypothesis space, and it is a restriction you impose deliberately — allowing every possible function sounds generous but makes learning impossible.

What counts as a bad answer? Being off by 10 on a house price is not the same kind of error as classifying a tumour incorrectly. The loss function encodes how much you care about each type of mistake.

What am I averaging over? You want to do well on future data, but you only have the data in front of you. Everything hinges on that substitution.

Get these three right and the algorithm is almost an afterthought. Get them wrong and no amount of model tuning will rescue the project.

Notation used in this lesson
SymbolMeaning
𝒳Input space — the set of all possible inputs
𝒴Output space — the set of possible targets
𝒟The unknown distribution generating (x, y) pairs
Hypothesis space — the functions we are willing to consider
hOne hypothesis, an element of ℋ
L(y, ŷ)Loss incurred by predicting ŷ when truth is y
θThe parameters indexing hypotheses in ℋ
R(h)True (population) risk of h

The four ingredients

1. Input and output spaces

XRd,Y=R   or   Y={1,,K}\mathcal{X} \subseteq \mathbb{R}^d, \qquad \mathcal{Y} = \mathbb{R} \;\text{ or }\; \mathcal{Y} = \{1, \dots, K\}

Deciding X\mathcal{X} is feature engineering: which measurements exist, in what units, encoded how. Deciding Y\mathcal{Y} is often where projects go wrong — the same business question can be posed as regression, binary classification, or ranking, and the three lead to different models and different metrics.

2. The data-generating distribution

We assume a fixed but unknown joint distribution D\mathcal{D} over X×Y\mathcal{X} \times \mathcal{Y}, and that our sample is drawn independently from it:

S={(x1,y1),,(xn,yn)}DnS = \{(x_1, y_1), \dots, (x_n, y_n)\} \sim \mathcal{D}^n

D\mathcal{D} is never observed. It is a modelling fiction that lets us say precisely what "generalise" means.

3. The hypothesis space

H\mathcal{H} is the set of functions the learner may return. Examples:

Hlinear={h(x)=wTx+b  :  wRd, bR}\mathcal{H}_{\text{linear}} = \{\, h(x) = w^{\mathsf{T}}x + b \;:\; w \in \mathbb{R}^d,\ b \in \mathbb{R} \,\} Hpoly-k={h(x)=j=0kajxj}\mathcal{H}_{\text{poly-}k} = \{\, h(x) = \textstyle\sum_{j=0}^{k} a_j x^j \,\}

Choosing H\mathcal{H} is choosing an inductive bias — an assumption about what kind of pattern the world contains. Linear models assume additive effects; decision trees assume axis-aligned thresholds; convolutional networks assume translation invariance.

4. The loss function

L(y,y^)0L(y, \hat{y}) \geq 0 measures the cost of predicting y^\hat{y} when the truth is yy. Standard choices:

LossFormulaUsed forCharacter
Squared(yy^)2(y - \hat{y})^2RegressionPunishes large errors heavily
Absoluteyy^\lvert y - \hat{y} \rvertRegressionRobust to outliers
0–11[yy^]\mathbb{1}[y \neq \hat{y}]ClassificationWhat accuracy measures; not differentiable
Cross-entropylogp^y-\log \hat{p}_yClassificationDifferentiable surrogate for 0–1
Hingemax(0,1yy^)\max(0,\, 1 - y\hat{y})SVMMargin-based

Risk: what we want versus what we can compute

The quantity we actually care about is the true risk — expected loss over the whole distribution:

R(h)=E(x,y)D[L(y,h(x))]R(h) = \E_{(x,y) \sim \mathcal{D}}\big[\, L(y, h(x)) \,\big]

This is uncomputable, because D\mathcal{D} is unknown. What we can compute is the empirical risk on our sample:

R^S(h)=1ni=1nL(yi,h(xi))\hat{R}_S(h) = \frac{1}{n} \sum_{i=1}^{n} L\big(y_i, h(x_i)\big)

Learning proceeds by minimising the second and hoping it tracks the first:

h^=arg minhHR^S(h)\hat{h} = \argmin_{h \in \mathcal{H}} \hat{R}_S(h)
Why empirical risk is a sensible substitute — and where it breaksAdvanced

For any fixed hh, the empirical risk is an average of nn i.i.d. random variables L(yi,h(xi))L(y_i, h(x_i)), each with mean R(h)R(h). So it is unbiased:

ES[R^S(h)]=1ni=1nE[L(yi,h(xi))]=R(h)\E_S\big[\hat{R}_S(h)\big] = \frac{1}{n}\sum_{i=1}^n \E\big[L(y_i, h(x_i))\big] = R(h)

and by the law of large numbers R^S(h)R(h)\hat{R}_S(h) \to R(h) as nn grows. Chebyshev's inequality even quantifies the gap: if the loss has variance σ2\sigma^2, then

P(R^S(h)R(h)>ϵ)σ2nϵ2\Prob\big(\lvert \hat{R}_S(h) - R(h) \rvert > \epsilon\big) \leq \frac{\sigma^2}{n\epsilon^2}

So far, so reassuring. The catch is the phrase for any fixed hh.

We do not evaluate a fixed hh — we choose h^\hat h by minimising R^S\hat{R}_S over all of H\mathcal{H}. That choice depends on SS, so R^S(h^)\hat{R}_S(\hat h) is no longer an unbiased estimate of R(h^)R(\hat h): the minimisation deliberately seeks out hypotheses that look good on this particular sample, including by exploiting its noise.

Concretely, ES[R^S(h^)]R(h^)\E_S[\hat{R}_S(\hat h)] \leq R(\hat h) — empirical risk is optimistically biased, and the bias grows with the richness of H\mathcal{H}. This single fact is the origin of overfitting, of why a held-out test set is mandatory, and of the entire theory of generalisation bounds.

In practice H\mathcal{H} is indexed by parameters θ\theta, so optimising over functions becomes optimising over vectors:

θ^=arg minθ1ni=1nL(yi,hθ(xi))\hat{\theta} = \argmin_{\theta} \frac{1}{n}\sum_{i=1}^{n} L\big(y_i, h_\theta(x_i)\big)

That is what every training loop in this curriculum computes — from the closed-form normal equations of linear regression to weeks of gradient descent on a language model.

Solved problem 1 · Formulating a problem completely

A hospital wants to predict, at admission, whether a patient will be readmitted within 30 days. Available data: 40,000 past admissions with 60 recorded features. Readmission rate is 11%. The hospital can enrol roughly 200 patients per month in an intensive follow-up programme.

Specify X\mathcal{X}, Y\mathcal{Y}, H\mathcal{H}, LL, and the evaluation metric.

Step 1 — output space, and why it is not what it first appears

The obvious choice is Y={0,1}\mathcal{Y} = \{0, 1\}. But the hospital cannot act on a hard label — it must rank patients to fill 200 slots. So the model should output a probability:

h:X[0,1],h(x)=P(readmittedx)h : \mathcal{X} \to [0, 1], \qquad h(x) = \Prob(\text{readmitted} \mid x)

Thresholding happens afterwards, as a separate decision driven by capacity.

Step 2 — input space

XRd\mathcal{X} \subseteq \mathbb{R}^d after encoding the 60 raw features: continuous ones standardised, categorical ones one-hot encoded. Critically, every feature must be available at admission — anything recorded during the stay is unavailable at prediction time and would leak.

Step 3 — hypothesis space

Start with regularised logistic regression:

H={h(x)=σ(wTx+b)  :  w2C},σ(z)=11+ez\mathcal{H} = \Big\{\, h(x) = \sigma(w^{\mathsf{T}}x + b) \;:\; \lVert w \rVert_2 \leq C \,\Big\}, \qquad \sigma(z) = \frac{1}{1 + e^{-z}}

Justification: 40,000 examples with roughly 4,400 positives is not much for a high-capacity model, and clinical settings require coefficients a doctor can inspect. Gradient boosting is the natural next step to compare against.

Step 4 — loss function

Cross-entropy, because we want calibrated probabilities:

L(y,p^)=[ylogp^+(1y)log(1p^)]L(y, \hat{p}) = -\big[\, y \log \hat{p} + (1-y)\log(1-\hat{p}) \,\big]

The 0–1 loss is unsuitable: it is not differentiable, and it treats a confident wrong prediction the same as a borderline one.

Step 5 — evaluation metric, driven by the constraint

Accuracy is useless here. Predicting "never readmitted" scores

10.11=0.89=89%1 - 0.11 = 0.89 = 89\%

while helping nobody. Since only 200 patients can be enrolled per month, the operative question is how many of the top 200 ranked patients are genuine readmissions. So the metric is precision at k=200k = 200, with recall at that threshold reported alongside to show the fraction of readmissions captured.

Answer

XRd\mathcal{X} \subseteq \mathbb{R}^d from admission-time features only; Y=[0,1]\mathcal{Y} = [0,1] predicted probability; H\mathcal{H} = L2-regularised logistic regression; LL = cross-entropy; metric = precision@200 with recall reported. Note that the loss used for fitting and the metric used for judging are deliberately different.

Choosing the hypothesis space is choosing your errors

too rigidabout righttoo flexible
Three hypothesis spaces on the same eight points. Too rigid misses the curvature; too flexible chases the noise; the middle choice captures the trend.

The left model cannot represent the truth however much data you give it — its error is bias. The right model can represent the truth but is so sensitive to the particular sample that it latches onto noise — its error is variance. Naming and quantifying this split is the next two lessons.

Exercise 1

You are asked to predict delivery time in minutes. A colleague proposes using 0–1 loss on whether the prediction is within 5 minutes. Give one advantage and two disadvantages.

Show solution

Advantage: it directly encodes the business requirement. If customers only care whether the estimate was roughly right, a metric that says "within 5 minutes or not" matches reality better than squared error, which penalises a 40-minute miss 320 times more than a 2-minute one.

Disadvantages:

  1. Not usable for fitting. The 0–1 loss has zero gradient almost everywhere and is discontinuous at the threshold, so gradient-based optimisation cannot move. You would fit with squared or absolute loss and only evaluate with this.
  2. It discards information about magnitude. A prediction off by 6 minutes and one off by 3 hours score identically. A model optimised for this metric has no incentive to avoid catastrophic misses, which are exactly the ones that lose customers.

A reasonable resolution: fit with Huber loss, report both mean absolute error and the within-5-minutes rate.

Exercise 2

Explain why H\mathcal{H} containing all possible functions from X\mathcal{X} to Y\mathcal{Y} makes learning impossible, even with a large sample.

Show solution

If H\mathcal{H} is unrestricted, then for any training sample SS there exist hypotheses that fit SS perfectly and disagree completely on every point outside SS. In fact for every unseen xx and every candidate label, some hypothesis in H\mathcal{H} achieves zero empirical risk while predicting that label.

Empirical risk minimisation therefore cannot distinguish between them — they all score zero — so the training data provides no information about how to behave on new inputs. Learning requires preferring some functions over others before seeing data, and an unrestricted H\mathcal{H} expresses no such preference.

This is the informal content of the No Free Lunch theorem, covered later in this module.


Next: Empirical Risk Minimisation, which examines the gap between R^S\hat{R}_S and RR carefully — the gap that explains everything that goes wrong.