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.

Not startedJump to quiz

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

LossOne observationBehaviour
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.
PercentageRelative error (MAPE). Breaks down when is near zero.
0246-3-2-10123error y − ŷloss
Squared (y − ŷ)²Absolute |y − ŷ|
Squared loss grows quadratically, so one large error can outweigh many small ones. Absolute loss grows linearly.

Classification losses

LossFormulaBehaviour
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-weightedMistakes priced differently. Used to choose thresholds (4.9).
HingeUsed 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)

. Punishes big errors; outlier-sensitive; OLS, ridge, LASSO.

Absolute loss (L1)

. Linear; robust to outliers; MAE.

Cross-entropy / log loss

. Logistic regression. Confidently wrong = huge loss.

0–1 loss

. Averages to the error rate; not smooth.

Loss vs metric

Loss: minimised during fitting (must be convenient). Metric: judges the final model for the task (AUC, cost, RMSE).

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1calculateA prediction is 8 and the true value is 11. What are the squared loss and the absolute loss?
Q2calculateThe true label is and the model predicts for class 1. What is the cross-entropy loss?
Q3applyYou're predicting house prices. A handful of mansions sell for 20 times the typical price, and you don't want them to dominate the fit. Which loss is the better choice?
Q4interpretFor a case with true label 1, model A predicts and model B predicts . How do their cross-entropy losses compare?
Q5conceptA logistic regression is fitted by minimising cross-entropy, then judged by its expected cost at a chosen threshold. Is that inconsistent?