Lesson 2.1 · 9 min read · about 55 min to master at a very slow pace
Loss Functions and Risk
A loss function scores one prediction; risk averages it. Squared, absolute, percentage, 0–1, cross-entropy, cost-weighted and hinge loss: what each punishes, where each is used, and how to compute them by hand.
What a loss function does
A loss function gives a number for how bad one prediction is: zero for perfect, larger for worse. Risk is the average loss (lesson 1.5). Choosing the loss is a modelling decision: it should reflect the task, the data and what errors actually cost.
Regression losses
| Loss | One observation | Behaviour |
|---|---|---|
| Squared (L2) | An error of 4 costs 16, an error of 2 costs 4. Large errors dominate. Smooth and easy to minimise, so linear models get a closed-form solution. Sensitive to outliers. Used in OLS, ridge and LASSO. | |
| Absolute (L1) | An error of 4 costs 4. Less affected by outliers. Not smooth at zero. Averaged, it is the MAE. | |
| Percentage | Relative error (MAPE). Breaks down when is near zero. |
Classification losses
| Loss | Formula | Behaviour |
|---|---|---|
| 0–1 (misclassification) | 1 if wrong, 0 if right. Averages to the error rate. Not smooth, so hard to optimise directly. | |
| Cross-entropy (log loss) | Uses the predicted probability . Confident and right: near 0. Confident and wrong: huge. Fits logistic regression (4.5). | |
| Cost-weighted | Mistakes priced differently. Used to choose thresholds (4.9). | |
| Hinge | Used by SVMs (6.6). No loss once a point is correct and outside the margin. |
Worked example: cross-entropy by hand
True label . Model A says ; model B says .
. . B is punished about 7 times more because it gave low probability to what actually happened.
If instead and : . Being confidently wrong is costly in either direction.
Loss vs evaluation metric
The loss is what the algorithm minimises while fitting; it has to be mathematically convenient. The metric is how you judge the final model for the real task. They can differ: a logistic regression is fitted with cross-entropy but may be judged by AUC, or by expected cost at a chosen threshold.
Typical questions
"Why is squared loss a poor choice when the data contain a few extreme outliers?" It squares large errors, so a single outlier can dominate the fit; absolute loss grows only linearly.
"Why not fit logistic regression by minimising squared error on 0/1 labels?" Cross-entropy is the maximum-likelihood loss for a yes/no outcome; it punishes confident wrong probabilities heavily and gives a well-behaved optimisation problem.
Stuck? Common doubts
Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.
First passWhat is the difference between a loss, the risk and the objective?
- Loss : the error on one example.
- Empirical risk : the average loss over the training data.
- Objective: whatever training actually minimises. That is the empirical risk, plus a penalty if you regularise: .
Books and software often use "loss" for all three. In an exam, say which one you mean.
Going deeperDoes the objective represent the error?
Mostly. With no penalty, the objective is the average error on the training data. Two caveats:
- With a penalty (ridge, LASSO, the SVM's ), it is error plus a cost for complexity, so a lower objective doesn't always mean a lower training error.
- It is training error, not the error on new data. That is the population risk, which you estimate on held-out data (lesson 1.5).
Cheatsheet
Squared loss (L2)
Absolute loss (L1)
Cross-entropy / log loss
0–1 loss
Loss vs metric
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.