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 03 of 10

03Squared error: punish big mistakes

3.1Definition and intuition

The squared error of one prediction and the mean squared error (MSE) over a data set are

\ell_{\text{sq}}(y,\hat y)=(y-\hat y)^2,\qquad \operatorname{MSE}=\frac1N\sum_{n=1}^N(y_n-\hat y_n)^2 . \tag{3}

Picture each error as the side of a square: the loss is the square’s area. Doubling an error quadruples its cost, so the loss cares most about the largest mistakes.

Worked example 1 (Three house prices). A model guesses three house prices and is off by 1, 2 and 5 thousand dollars. The squared errors are 1, 4 and 25, a total of 30: the single big miss contributes 25/30=83\% of the loss. With absolute error the costs are 1, 2 and 5, and the big miss contributes 5/8=62.5\%.

3.2Gradient: the pull grows with the error

The derivative is \partial\ell_{\text{sq}}/\partial\hat y=-2(y-\hat y)=-2r. The pull of an example is proportional to its error: a point twice as far off pulls twice as hard, and a point 100 times as far off pulls 100 times as hard. For a linear model \hat y=w^\top x, setting the gradient of the total squared error to zero gives the normal equations and the closed-form least-squares solution

\sum_{n}(y_n-w^\top x_n)\,x_n=0\quad\Longleftrightarrow\quad X^\top X\,w=X^\top y\quad\Longrightarrow\quad \hat w=(X^\top X)^{-1}X^\top y , \tag{4}

where the rows of X are the inputs x_n^\top. Squared error is the only loss in this tutorial with a closed-form solution for linear models.

3.3The best constant guess is the mean

Suppose a model can only predict one number c for every example. Which c minimizes the total squared error?

\frac{d}{dc}\sum_{n}(y_n-c)^2=-2\sum_n(y_n-c)=0\quad\Longrightarrow\quad c^\star=\frac1N\sum_ny_n=\bar y .

The same argument conditional on x shows that the best predictor under squared error is the conditional mean, f^\star(x)=\E[y\mid x] (Bishop, 2006, §1.5.5). A model trained with squared error is trying to estimate an average.

3.4Squared error is maximum likelihood with Gaussian noise

Assume y=f_\theta(x)+\varepsilon with Gaussian noise \varepsilon\sim\mathcal N(0,\sigma^2). Then

p_\theta(y\mid x)=\frac{1}{\sqrt{2\pi\sigma^2}}\exp\!\Bigl(-\frac{(y-f_\theta(x))^2}{2\sigma^2}\Bigr),

and the negative log-likelihood (2) of the data set is

-\sum_{n}\log p_\theta(y_n\mid x_n)=\frac{1}{2\sigma^2}\sum_{n}(y_n-f_\theta(x_n))^2+\frac N2\log(2\pi\sigma^2). \tag{5}

The second term does not depend on \theta, and the factor 1/(2\sigma^2) does not change the minimizer. So minimizing squared error is maximum likelihood under Gaussian noise (Murphy, 2022, §4.2.6). This is Gauss’s justification of least squares, and it tells you when squared error is the right choice: when errors are roughly bell-shaped, with no gross outliers.

Pause and think. Why square the error and not just add up the signed errors \sum_n(y_n-\hat y_n)?

Show the answer

Signed errors cancel: predictions that are far too high and far too low can add up to zero. Any loss must be zero only for a perfect prediction and positive otherwise. Squaring is the smoothest way to achieve this; absolute value is the other natural way, and it leads to Section 4.