Contents
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
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
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?
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
and the negative log-likelihood (2) of the data set is
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.