Formulating a Learning Problem
Input and output spaces, hypothesis classes, loss functions, and turning a vague goal into an objective.
Assumes you know
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.
| Symbol | Meaning | Read aloud |
|---|---|---|
| 𝒳 | Input space — the set of all possible inputs | script X |
| 𝒴 | Output space — the set of possible targets | script Y |
| 𝒟 | The unknown distribution generating (x, y) pairs | script D |
| ℋ | Hypothesis space — the functions we are willing to consider | script H |
| h | One hypothesis, an element of ℋ | h |
| L(y, ŷ) | Loss incurred by predicting ŷ when truth is y | loss of y and y hat |
| θ | The parameters indexing hypotheses in ℋ | theta |
| R(h) | True (population) risk of h | risk of h |
The four ingredients
1. Input and output spaces
Deciding is feature engineering: which measurements exist, in what units, encoded how. Deciding 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 over , and that our sample is drawn independently from it:
is never observed. It is a modelling fiction that lets us say precisely what "generalise" means.
3. The hypothesis space
is the set of functions the learner may return. Examples:
Choosing 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
measures the cost of predicting when the truth is . Standard choices:
| Loss | Formula | Used for | Character |
|---|---|---|---|
| Squared | Regression | Punishes large errors heavily | |
| Absolute | Regression | Robust to outliers | |
| 0–1 | Classification | What accuracy measures; not differentiable | |
| Cross-entropy | Classification | Differentiable surrogate for 0–1 | |
| Hinge | SVM | Margin-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:
This is uncomputable, because is unknown. What we can compute is the empirical risk on our sample:
Learning proceeds by minimising the second and hoping it tracks the first:
Why empirical risk is a sensible substitute — and where it breaksAdvanced
For any fixed , the empirical risk is an average of i.i.d. random variables , each with mean . So it is unbiased:
and by the law of large numbers as grows. Chebyshev's inequality even quantifies the gap: if the loss has variance , then
So far, so reassuring. The catch is the phrase for any fixed .
We do not evaluate a fixed — we choose by minimising over all of . That choice depends on , so is no longer an unbiased estimate of : the minimisation deliberately seeks out hypotheses that look good on this particular sample, including by exploiting its noise.
Concretely, — empirical risk is optimistically biased, and the bias grows with the richness of . 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.
Parametrising the search
In practice is indexed by parameters , so optimising over functions becomes optimising over vectors:
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 , , , , and the evaluation metric.
Step 1 — output space, and why it is not what it first appears
The obvious choice is . But the hospital cannot act on a hard label — it must rank patients to fill 200 slots. So the model should output a probability:
Thresholding happens afterwards, as a separate decision driven by capacity.
Step 2 — input space
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:
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:
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
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 , with recall at that threshold reported alongside to show the fraction of readmissions captured.
Answer
from admission-time features only; predicted probability; = L2-regularised logistic regression; = 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
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 solutionHide 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:
- 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.
- 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 containing all possible functions from to makes learning impossible, even with a large sample.
Show solutionHide solution
If is unrestricted, then for any training sample there exist hypotheses that fit perfectly and disagree completely on every point outside . In fact for every unseen and every candidate label, some hypothesis in 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 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 and carefully — the gap that explains everything that goes wrong.