TL;DR
Mean squared error (MSE) is the average of the squared differences between what a model predicted and what was true. Squaring makes every error positive and makes big misses count much more than small ones. Minimising MSE is the standard way to fit a model to numbers that vary continuously, such as prices, temperatures or heights.
Because the errors are squared, one prediction that is ten units off costs as much as a hundred predictions that are one unit off. That is the single most important thing to know about it.
Where you'll see it: as the training loss of almost every regression model, as the reconstruction loss in autoencoders, and as the "error" in a least-squares line fitted to a scatter plot.
Intuition
Punishing the big misses
Suppose you guess the weight of four parcels, and your guesses are off by 2 kg, 2 kg, 2 kg and 2 kg. Your average error is 2 kg. Now suppose a different guesser is off by 0 kg, 0 kg, 0 kg and 8 kg. Their average error, ignoring signs, is also 2 kg. Are they equally good?
Most people would say the second guesser is worse. One parcel was badly wrong, and in real life that single bad miss is what causes trouble: the wrong shipping class, the overloaded van. MSE captures that intuition. It squares each error before averaging, so the 8 kg miss contributes 64 while the four 2 kg misses contribute 4 each. The loss sees the large error as the problem.
A second reason to square is that it removes the sign. An error of plus 3 and an error of minus 3 are equally bad, and squaring treats them the same way. Adding raw errors would let them cancel out and hide a model that is wrong in both directions.
The plot below shows the loss for one target whose true value is 3, as a function of the prediction. The loss is zero at the truth and grows as the prediction moves away from it, faster and faster.
- squared error when the truth is 3
The math
Definition
For pairs of true values and predictions , the mean squared error is
Here is the number of examples, labels one example, is its true value, and is the model's prediction for it.
MSE is a loss function in the sense of the definition there: it is zero only when every prediction is exact, and it is never negative. When the model has parameters and predicts from an input , training minimises
Root mean squared error
Because the squares are in the wrong units (kg² if the data are in kg), people often take the square root:
RMSE is in the same units as the data, so it is easier to read. Minimising MSE and minimising RMSE give the same answer, because the square root is increasing.
The best single guess is the mean
Suppose you must predict one constant for every example. Which gives the smallest MSE? Set the derivative of with respect to to zero:
Rearranging gives , so . The best constant is the sample mean, which is why the variance is itself a mean squared distance. A model that predicts the mean has an MSE equal to the variance of the data. Any useful model must beat that number.
Gradient for training
Training needs the direction in which MSE gets smaller. For one example, the derivative of the squared error with respect to the prediction is
So if the prediction is too low (), the derivative is negative and the prediction should go up. The size of the push is proportional to the error. This is the signal that gradient descent follows, using partial derivatives with respect to each weight through the chain rule.
Why squares: the Gaussian connection
MSE is not an arbitrary choice. Suppose the data are generated as , where the noise is independent and Gaussian with mean zero and variance . Then the probability of the data is largest when the sum of squared errors is smallest. Taking the logarithm of the likelihood, the terms that do not depend on drop out, and maximum likelihood reduces to minimising . In other words, MSE is the right loss when you believe the errors look like Gaussian noise.
Worked examples
Let the true values be and the predictions .
- Errors : , , , .
- Squared errors: , , , .
- Sum of squared errors: .
- Divide by : .
- Root: .
Interpretation: on average the predictions are about 0.75 units from the truth, in the same units as the data. The two misses of size 1 contribute 2 of the 2.25 total, while the miss of 0.5 contributes only 0.25.
Two models predict four values. Model P is off by on every example, so its errors are . Model Q is perfect on three examples and off by on one, so its errors are .
- Model P: mean absolute error . MSE .
- Model Q: mean absolute error . MSE .
Interpretation: both models have the same average absolute error, but Model Q has four times the MSE. The squared loss says Q is worse because it makes one large mistake, and a large mistake is what the squaring is designed to catch.
A short Python check of the first example:
import numpy as np
y = np.array([3, 5, 2, 8])
y_hat = np.array([2.5, 6, 2, 7])
print(np.mean((y - y_hat) ** 2))Output: 0.5625
Tradeoffs and failure modes
MSE is a good default when errors are roughly symmetric and you want large misses punished. It fails in a few predictable ways.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Data has outliers you do not want to chase, such as a sensor glitch | One bad point dominates the fit and pulls the line towards it | Mean absolute error or Huber loss |
| Target spans several orders of magnitude | Large targets produce huge squared errors that swamp the rest | Predict the log of the target, then use MSE on the log |
| You want the median rather than the mean | MSE pulls predictions towards the mean of the conditional distribution | Absolute error, which targets the median |
| Classification with labels 0 or 1 | MSE gives weak gradients when the model is confidently wrong | [[cross-entropy-loss |
| Errors are heavy-tailed | Squared error overweights rare extreme values | Robust losses, or model the noise explicitly |
Scale matters too. If you add a feature measured in millimetres next to one measured in kilometres, the squared errors of the first can dwarf the second, so normalise inputs before judging MSE values across experiments. And a low training MSE is not enough: a model can drive it to zero by memorising the training points, which is Overfitting, so check MSE on held-out data.
History
The method of least squares, which chooses parameters to minimise the sum of squared residuals, was published by Adrien-Marie Legendre in 1805 for predicting the orbits of comets. Carl Friedrich Gauss said he had used the method earlier, and in 1809 he connected it to the normal distribution, which gave the squared error a probabilistic justification. The problem they were solving was practical: astronomers had many noisy measurements and needed one consistent estimate.
The term "mean squared error" became standard in statistics in the twentieth century, as estimation theory developed a vocabulary for comparing estimators by their average squared deviation. In machine learning it came to mean the regression loss of choice, largely because it has a closed-form solution for linear models and a smooth gradient that suits gradient-based training.
Check yourself
QuestionFor the true values and predictions , what is the MSE?Show answer
The errors are , and . Squared, they are , and . The sum is , and dividing by gives .
QuestionWhy does MSE prefer a model that is off by 1 on four examples over one that is off by 2 on two examples?Show answer
Both models have a total absolute error of . Under MSE the first model has and the second has . The squared loss makes the larger misses cost more, so the second model is penalised twice as hard even though the sum of raw errors is the same.
QuestionWhy is the best constant prediction the mean of the data?Show answer
Set the derivative of with respect to to zero. That gives , so . The stationary point of a convex quadratic is its minimum, so the mean is the unique best constant.
QuestionYou are fitting a line to house prices, and one listing is a data-entry error that is 100 times too large. What happens to the fit under MSE, and what would you change?Show answer
That one point produces a squared error about times that of a normal point, so the fitted line bends sharply towards it. The honest fix is to remove or correct the bad row first. If the outlier is real and you still want a robust fit, use absolute error or Huber loss, which grow more slowly for large errors.
QuestionA model's training MSE falls to nearly zero while its MSE on new data rises. What does this tell you?Show answer
The model has memorised the training examples rather than learning the pattern behind them. This is overfitting. The remedy is more data, a simpler model, or regularisation such as L2 penalties, and the test-set MSE is the number to watch.