# Mean Squared Error (MSE)

> Mean squared error measures how wrong a model's guesses are by squaring each miss, so big misses count much more, and then averaging them.

Page: https://formulabro.yunusemrevurgun.com/ai-ml/mean-squared-error/  
Category: AI & Machine Learning  
Published: 2026-10-22  
Updated: 2026-10-22

## Formula

$$
\mathrm{MSE} = \frac{1}{n}\sum_{i=1}^{n} (y_i - \hat{y}_i)^2
$$

Read it out loud: "M S E equals one over n, times the sum, for i from 1 to n, of the true value y i minus the predicted value y hat i, all squared."

## What each symbol means

- $\mathrm{MSE}$: The mean squared error: one number that sums up how wrong the predictions are. It is in squared units, so if the values are in meters, the MSE is in square meters.
- $n$: How many predictions you are scoring.
- $i$: The position of one prediction in the list, counting from 1 up to $n$.
- $\sum_{i=1}^{n}$: "Add up for every $i$ from 1 to $n$": go through all the predictions and total the results.
- $y_i$: The true value for case $i$, the answer you know is correct.
- $\hat{y}_i$: The predicted value for case $i$, the model's guess. The hat over the $y$ is the usual sign for a prediction.

## What the formula is doing

A model makes a guess for each case, and for each case you know the true answer. The error for one case is the gap between them: true value minus prediction. The gap can be positive or negative, depending on which way the miss went.

Squaring each gap fixes two problems. It makes every error positive, so a miss of $-2$ and a miss of $+2$ do not cancel out. It also makes big misses count far more: a gap of 4 adds 16 to the total, while a gap of 2 adds only 4.

Next, the formula adds up all the squared gaps and divides by $n$, the number of cases. Dividing turns the total into an average, so the score does not grow merely because you tested more cases.

A smaller MSE means the predictions sit closer to the truth. An MSE of 0 means every prediction was exactly right. Because of the squaring, the result is in squared units and is harder to read than the original values. The square root of the MSE, called RMSE, brings it back to the original units.

## An everyday picture

Think of a darts coach scoring practice throws. Each throw's distance from the bullseye is recorded, and a throw that lands far off costs much more than one that lands close. The coach averages those penalties across all the throws, so one wild throw shows up clearly in the score even when the rest are good.

## Worked example: Three temperature predictions

A model predicts the temperature in three cities, in degrees Celsius. The true temperatures are $y = (3, 5, 2)$ and the predictions are $\hat{y} = (2, 6, 4)$.

1. Find each gap: $3 - 2 = 1$, $5 - 6 = -1$ and $2 - 4 = -2$.
2. Square each gap: $1^2 = 1$, $(-1)^2 = 1$ and $(-2)^2 = 4$.
3. Add the squares: $1 + 1 + 4 = 6$.
4. Divide by the number of predictions, $n = 3$: $\mathrm{MSE} = \frac{6}{3} = 2.0$.

**Answer:** The MSE is **2.0 square degrees Celsius**. The biggest miss, the $-2$ for the third city, contributed 4 of the 6 squared points, so it dominates the score. A perfect model would score 0.

## Common mistakes

- Squaring after averaging, or averaging after taking the gaps without squaring. The order is: gap, square, add, divide by $n$.
- Adding up the squared gaps and forgetting to divide by $n$. The score then grows with the size of the test set, and two models can no longer be compared fairly.
- Comparing MSE values across problems with different units. An MSE of 2 square degrees means nothing next to an MSE of 2 square dollars.
- Expecting an MSE of 0 on real data. A score of 0 means every prediction was exact, which almost never happens outside toy examples.

## Where you will meet it

- Linear regression, where the line is chosen to make the MSE as small as possible.
- Training neural networks for regression tasks, such as predicting a price or a temperature.
- Image compression and signal processing, where MSE measures how far the output is from the original.
- Evaluating forecasts, such as weather or energy-demand predictions.

## Questions people ask

### Why square the errors instead of taking absolute values?

Squaring gives a smooth curve with no sharp corner at zero, which makes it easier for gradient descent to follow. Squaring also punishes large misses more heavily. Some problems want that, and some do not.

### What is the difference between MSE and RMSE?

RMSE is the square root of MSE. It comes back to the same units as the original values, so it is easier to read.

### Is a small MSE always good?

Not always. A small MSE on the data used for training can hide a model that fails on new data. Check the score on data the model has not seen.

### Can MSE be used for yes-or-no answers?

It can, but it is usually a poor choice. For classes, cross-entropy loss is the usual choice, because it punishes confident wrong answers much harder.

## Related formulas

- [Gradient Descent](https://formulabro.yunusemrevurgun.com/ai-ml/gradient-descent/)
- [Cross-Entropy Loss](https://formulabro.yunusemrevurgun.com/ai-ml/cross-entropy-loss/)
- [Artificial Neuron (Weighted Sum)](https://formulabro.yunusemrevurgun.com/ai-ml/artificial-neuron/)

## Sources

- [Mean squared error, Wikipedia](https://en.wikipedia.org/wiki/Mean_squared_error)
- [Loss function, Wikipedia](https://en.wikipedia.org/wiki/Loss_function)

From Formula Bro (https://formulabro.yunusemrevurgun.com/), a side project developed by yunusemrevurgun.com.
