Foundations to Frontier
Tests
E. Deep Learning › E2 Loss Functions (one page each)

Mean squared error and mean absolute error

Mean squared error averages the squared residuals; mean absolute error averages their absolute values. They are the two basic regression losses, and their difference is fundamental: MSE is minimised by the conditional mean and is optimal under Gaussian noise, while MAE is minimised by the conditional median and is robust to outliers and heavy tails. This page covers both losses, their gradients, the constants and estimators they lead to, the bias–variance decomposition, scale and units, and how to choose between them. In the exercises you derive the best constants and gradients, then measure the accuracy of the mean and the median under Gaussian, Laplace, Student-t and outlier-contaminated noise.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Punishing errors: squared or absolute?

A regression model’s mistakes are its residuals, the gaps between the true and predicted values. Mean squared error squares each gap before averaging, so one error of 10 costs as much as a hundred errors of 1. Mean absolute error just averages the gaps’ sizes. The choice decides what the model chases: MSE chases the average, MAE the middle value, and MSE lets a few wild points dominate.

Regression losses: how hard does each point pull?
residual r from −4 to 4
squared error (MSE): ŷ = 1.62 + 0.47x · least squares ŷ = 1.62 + 0.47x · true line 1.00 + 0.50x
Orange points are outliers. Solid line: fit under the chosen loss; dashed: least squares. Right: the loss (solid) and its derivative (dashed), the pull a point exerts at each residual. Squared error's pull grows without limit, so a few outliers drag its line upward; absolute error, Huber and log-cosh cap the pull and stay near the true line. The quantile loss pulls with τ above the line and 1 − τ below it, so its line settles where a share τ of the points lies below; with noise that grows with x, the quantile lines fan out.
Why engineers care

Sensor data contain glitches: a GPS jump, a dropped packet read as zero, a saturated reading. Under MSE, a handful of such points can drag a calibration or a learned model far off; under MAE they barely matter. On the other hand, when noise really is Gaussian, MSE uses the data more efficiently. Knowing which regime you are in is part of designing the loss.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Which loss is more affected by a single huge error?
  2. 2. The constant that minimises MSE on a dataset is…
  3. 3. The constant that minimises MAE is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • MSE = (1/n) Σ (yᵢ − ŷᵢ)². For y = (1, 2, 4, 10) and ŷ = 2 throughout: (1 + 0 + 4 + 64)/4 = 17.25.
  • MAE = (1/n) Σ |yᵢ − ŷᵢ|. Same data: (1 + 0 + 2 + 8)/4 = 2.75.
  • RMSE = √MSE, in the units of y: √17.25 ≈ 4.15.
  • Gradients with respect to a prediction:
    • MSE: 2(ŷᵢ − yᵢ)/n, proportional to the error, so big errors pull hardest.
    • MAE: sign(ŷᵢ − yᵢ)/n, the same size for every error.
  • Best constants: for y = (1, 2, 4, 10), MSE is minimised at the mean, 4.25. MAE is minimised at any point between the two middle values, 2 and 4.
  • Units and scale: multiplying y by 10 multiplies MSE by 100 and MAE by 10.
📝 Quick check (Level 1)
0/3 answered
  1. 1. MSE of predictions (3, 4) for targets (0, 0)?
  2. 2. MAE's gradient magnitude for any nonzero error is…
  3. 3. Converting metres to centimetres multiplies MSE by…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Minimisers, gradients and bias–variance

Exercise 1. The two losses and their gradients are checked by finite differences. A brute-force search confirms that the best constant for MSE is the mean (4.25) and that for MAE it is a median, 2.000, the lower end of the flat interval [2, 4].

Bias–variance decomposition. For an estimator θ̂ of θ, E[(θ̂ − θ)²] = (E θ̂ − θ)² + Var(θ̂). The exercise confirms this numerically with bias 0.3 and standard deviation 0.5: 0.09 + 0.25.

In training.

  • MSE is smooth, and its gradient vanishes as errors shrink, which helps convergence.
  • MAE’s gradient has constant size, so with a fixed learning rate it overshoots near the optimum. Use a decaying learning rate, or Huber or log-cosh (E2-2).
  • Large MSE gradients from outliers can destabilise training; clipping them is equivalent to switching to a Huber-like loss.
nn.MSELoss(); nn.L1Loss()
🐍 Exercise 1: MSE, MAE and their minimisers
Implement MSE, MAE and their gradients, find the best constant prediction for each by search, and verify the bias–variance decomposition.
📝 Quick check (Level 2)
0/3 answered
  1. 1. MSE of an estimator equals…
  2. 2. Training with MAE at a fixed learning rate tends to…
  3. 3. PyTorch's MAE loss is called…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Probabilistic view.

  • Minimising MSE is maximum likelihood under Gaussian noise: −log N(y; ŷ, σ²) = (y − ŷ)²/(2σ²) + constant.
  • Minimising MAE is maximum likelihood under Laplace noise: −log Laplace(y; ŷ, b) = |y − ŷ|/b + constant.

Conditional mean versus median. Over a flexible model class, the MSE minimiser predicts E[y | x] and the MAE minimiser predicts median[y | x]. They differ when the noise is skewed.

Efficiency. For a sample of size n:

  • Under Gaussian noise, the variance of the median is asymptotically π/2 ≈ 1.571 times that of the mean.
  • Under Laplace noise it is half.
  • In general it is 1/(4f(0)²n) for a noise density f, against σ²/n for the mean.

So the mean wins for light tails and the median for heavy tails.

Robustness.

  • The mean has breakdown point 0: one arbitrarily bad point can move it arbitrarily far.
  • The median has breakdown point 50%.
  • Equivalently, MSE’s influence function (its derivative) is unbounded, while MAE’s is bounded.
📝 Quick check (Level 3)
0/3 answered
  1. 1. MAE is the maximum-likelihood loss for…
  2. 2. The median's breakdown point is…
  3. 3. Under Gaussian noise, Var(median)/Var(mean) tends to…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Mean or median: which is more accurate?

History.

  • R. Boscovich (1757) and P.-S. Laplace (1788) fitted lines by minimising absolute deviations.
  • A.-M. Legendre (1805) and C. F. Gauss (1809) introduced least squares, which Gauss justified with the normal distribution.
  • P. Huber (1964) founded robust statistics, interpolating between the two (E2-2).

Exercise 2. We estimate the centre of a distribution (true value 0) from samples of n = 25 (20 000 trials) and n = 401 (2000 trials), using the MSE-optimal estimate (the mean) and the MAE-optimal estimate (the median). All noise distributions have median 0, and all but the contaminated one have variance 1.

noise n MSE of the mean MSE of the median ratio median / mean
Gaussian 25 0.0400 0.0618 1.548
Gaussian 401 0.00248 0.00391 1.578
Laplace 25 0.0397 0.0262 0.661
Laplace 401 0.00252 0.00143 0.568
Student t, 3 dof 25 0.0385 0.0250 0.649
Student t, 3 dof 401 0.00239 0.00154 0.642
5% gross outliers (σ = 10) 25 0.2438 0.0677 0.278
5% gross outliers (σ = 10) 401 0.0143 0.00432 0.302
  • Gaussian noise favoured the mean. The ratio approached the theoretical π/2 ≈ 1.571.
  • Heavy tails favoured the median. For Laplace noise the ratio fell toward the asymptotic 0.5 as n grew. For t₃ it was near its theoretical 0.62.
  • With 5% gross outliers, the median was about 3.5 times as accurate. That is the realistic sensor-glitch case.
  • Neither loss is best everywhere. Huber’s loss (E2-2) is designed to be nearly as efficient as the mean under Gaussian noise while keeping most of the median’s robustness.
🐍 Exercise 2 (research): mean or median?
Estimate the centre of Gaussian, Laplace, Student-t and outlier-contaminated samples with the MSE-optimal mean and the MAE-optimal median, at n = 25 and 401, and compare their mean squared errors.

MSE and MAE in AI, robotics and aerospace

  • State estimation: Kalman filters are MSE-optimal under Gaussian noise. Robust variants downweight measurements with large residuals, moving toward MAE-like behaviour.
  • Learned dynamics and calibration: MSE is the default, but logs from flight tests contain spikes and dropouts; MAE or Huber losses keep them from dominating.
  • Evaluation: report RMSE and MAE together. A large gap between them signals a few large errors.

References

  1. C. F. Gauss (1809). Theoria motus corporum coelestium.
  2. P. J. Huber (1964). “Robust estimation of a location parameter.” Annals of Mathematical Statistics 35(1).
  3. P. J. Huber, E. M. Ronchetti (2009). Robust Statistics, 2nd ed. Wiley.
  4. T. Hastie, R. Tibshirani, J. Friedman (2009). The Elements of Statistical Learning, 2nd ed., chapter 7. Springer.