MoonSay /Tutorials/Machine Learning Interview #7
Contents
  1. 01Introduction
  2. 02What a loss function does
  3. 03Squared error
  4. 04Absolute error and Huber loss
  5. 05Cross-entropy
  6. 06Why not squared error for classification?
  7. 07Hinge loss
  8. 08Choosing a loss
  9. 09Interview questions and model answers
  10. 10Summary
  11. References
Loss Functions/Chapter 04 of 10

04Absolute error and Huber loss: robust to outliers

4.1One bad label

Because the pull of squared error grows with the error, a single wildly wrong label can drag the whole model. Figure 1 shows ten houses whose prices follow a straight line with slope 2, plus a little noise. Least squares recovers slope 2.00. Now one price is mistyped, 30 too high. Least squares tilts to slope 3.27: the bad point’s residual of 22.6 pulls 22.6 times harder than a point off by 1. Absolute error gives slope 2.01 and Huber loss 2.05; both barely notice the typo.

One mistyped label (red, raised by 30) and three straight-line fits. Least squares is pulled to slope 3.27; absolute error (2.01) and Huber loss with \delta=1 (2.05) stay close to the true slope 2. Computed for this tutorial, with the same data as the companion video.
Figure 1. One mistyped label (red, raised by 30) and three straight-line fits. Least squares is pulled to slope 3.27; absolute error (2.01) and Huber loss with \delta=1 (2.05) stay close to the true slope 2. Computed for this tutorial, with the same data as the companion video.
Try itOne bad label against three losses

Make the typo in the ninth house's price bigger or smaller, and change Huber's threshold \delta. Least squares is solved in closed form, equation (4); Huber and absolute error are fitted by iteratively reweighted least squares, as in Section 4.

4.2Absolute error and the median

The absolute error is \ell_{\text{abs}}(y,\hat y)=|y-\hat y|, and its average is the mean absolute error (MAE). Its derivative is \partial\ell_{\text{abs}}/\partial\hat y=-\operatorname{sign}(r): every example pulls with the same strength, \pm1, however large its error. A point off by 30 pulls no harder than a point off by 0.1.

For the best constant guess c, the derivative of \sum_n|y_n-c| is

\frac{d}{dc}\sum_n|y_n-c|=\#\{n:y_n<c\}-\#\{n:y_n>c\},

which is zero when as many points lie below c as above it: c^\star is the median. (At c=y_n the absolute value has a corner; the derivative is replaced by a subgradient, and the condition becomes “at most half the points on each side”.) Conditional on x, the best predictor under absolute error is the conditional median (Bishop, 2006, §1.5.5). And just as squared error is Gaussian maximum likelihood, absolute error is maximum likelihood under Laplace noise, p(\varepsilon)=\frac{1}{2b}e^{-|\varepsilon|/b}, whose negative log is |\varepsilon|/b+\log(2b) (Murphy, 2022, §11.6). The Laplace density has heavier tails than the Gaussian, so large errors are less surprising and are penalized less.

Worked example 2 (A street with one mansion). Five houses on a street sell for 200, 250, 300 and 350 thousand dollars and 5 million. The mean is 1.22 million and the median 300 thousand. A squared-error model would predict 1.22 million for a typical house on this street, more than any of the four ordinary houses is worth. Figure 2 shows the two total losses as functions of the guess: the squared-error bowl has its bottom at the mean, the absolute-error valley at the median.

The street of Example 2 (prices marked on the axis). (a) Total squared error, minimized by the mean, 1220 thousand dollars. (b) Total absolute error, minimized by the median, 300 thousand. Computed for this tutorial.
Figure 2. The street of Example 2 (prices marked on the axis). (a) Total squared error, minimized by the mean, 1220 thousand dollars. (b) Total absolute error, minimized by the median, 300 thousand. Computed for this tutorial.
Try itMean or median: the mansion on the street

Change the price of the fifth house. The mean is the best single guess under squared error, the median under absolute error (Example 2).

4.3Huber loss: the best of both

Absolute error is robust but has a corner at zero, and when the noise really is Gaussian the median is a noisier estimate than the mean. Huber (1964) combined the two:

\ell_\delta(r)=\begin{cases}\tfrac12 r^2, & |r|\le\delta,\\[2pt] \delta|r|-\tfrac12\delta^2, & |r|>\delta,\end{cases} \qquad \ell_\delta'(r)=\operatorname{clip}(r,-\delta,\delta)=\begin{cases}r, & |r|\le\delta,\\ \delta\,\operatorname{sign}(r), & |r|>\delta.\end{cases} \tag{6}

Small errors are treated like squared error, large ones like absolute error. At |r|=\delta both pieces equal \tfrac12\delta^2 and both slopes equal \delta, so the loss is smooth (Murphy, 2022, §5.1.5). The threshold \delta says what counts as an outlier, in the units of y; it is set from the noise level or by cross-validation (Murphy, 2022, §11.6).

Figure 3 compares the three losses and, more tellingly, their derivatives, the pull of one example. Squared error’s pull grows without limit; absolute error’s is constant; Huber’s grows and then stops at \delta. A bounded pull is what makes a loss robust: no single point can drag the fit arbitrarily far.

(a) Squared error (drawn at half height, to match Huber’s quadratic part), absolute error and Huber loss with \delta=1. (b) Their derivatives: the pull of one example with residual r. Only squared error has an unbounded pull. Computed for this tutorial.
Figure 3. (a) Squared error (drawn at half height, to match Huber’s quadratic part), absolute error and Huber loss with \delta=1. (b) Their derivatives: the pull of one example with residual r. Only squared error has an unbounded pull. Computed for this tutorial.

Huber loss has no closed-form solution, but it is easy to fit by iteratively reweighted least squares: solve a weighted least-squares problem with weights w_n=\ell_\delta'(r_n)/r_n=\min(1,\delta/|r_n|), recompute the residuals and repeat. Points with small residuals get weight 1. Starting from least squares, the mistyped price of Figure 1 (residual 22.6) gets weight 1/22.6 in the first round; at convergence its residual is 29.8 and its weight 1/29.8, so it hardly affects the fit. The same idea with weights 1/|r_n| fits absolute error.

Pause and think. If absolute error is robust, why is squared error still the default for regression?

Show the answer

It is smooth, has a closed-form solution for linear models, and is the most efficient choice when the noise really is Gaussian. Its weakness only matters when there are outliers or heavy tails. When in doubt, Huber loss behaves like squared error on clean data and like absolute error on outliers.