Math glossary

Mean Squared Error: Formula, Worked Examples, and RMSE

Calculate mean squared error, compare MSE with RMSE and MAE, and identify the right denominator in regression with six worked examples.

TL;DR

Mean squared error (MSE) is the average squared difference between actual and predicted values. Compute one squared error per observation, then average. Zero means every prediction matches. Larger errors have more influence after squaring.

Key Points

  • Pair each prediction with the matching observation before subtracting.
  • Prediction MSE divides the sum of squared errors by the number of observations.
  • MSE uses squared units. Root mean squared error (RMSE) returns to the original units.
  • Squared error penalises large misses more strongly than absolute error.
  • Compare models on equivalent data, using the same units, weights, and evaluation method.

What is mean squared error?

For each observed value yiy_i and prediction y^i\hat y_i, take the difference ei=yi−y^ie_i=y_i-\hat y_i. Either subtraction order gives the same square. With n>0n>0 equally weighted observations,

MSE=1n∑i=1n(yi−y^i)2.\mathrm{MSE}=\frac1n\sum_{i=1}^n(y_i-\hat y_i)^2.

First calculate the errors, then square before averaging. An overprediction of 3 and an underprediction of 3 give signed errors that cancel in an ordinary mean. Their squared errors are both 9. The MSE is always at least zero, and all included errors have to be zero for an unweighted MSE of zero. scikit-learn, Mean Squared Error

A one-unit error makes a square of area one, while a three-unit error makes a square of area nine
Tripling an error’s magnitude multiplies its squared contribution by nine.

How do MSE, RMSE, MAE, and SSE differ?

Measure Calculation Output units What to check
SSE Sum the squared errors Squared units Larger sample sizes can increase a sum
MSE Divide SSE by the observation count Squared units Large errors receive greater weight through squaring
RMSE Take the square root of MSE Same units as the target Convenient when interpreting magnitude in original units
MAE Average the absolute errors Same units as the target Each error contributes in proportion to its size

RMSE and MSE rank the same models identically when computed from the same observations with the same weights because the square root is increasing. MAE can rank them differently. If the data are in metres, SSE and MSE are computed in square metres, and MAE and RMSE are in metres. None of these metrics by itself gives the direction of an error. Use the average signed error and examine individual residuals when direction matters. scikit-learn, Metrics and Scoring

Which denominator should you use in regression?

For evaluating predictions across nn observations, divide SSE by nn. Regression output may also label a separate quantity “MSE” or residual mean square. In a full-rank ordinary least-squares fit with pp estimated coefficients, including the intercept when present, the residual variance estimate is

s2=SSEn−p(n>p).s^2=\frac{\mathrm{SSE}}{n-p} \qquad(n>p).

A straight line with an estimated slope and intercept uses p=2p=2. Its residual mean square divides by n−2n-2. Its training mean squared prediction error on those same rows divides by nn. The two numbers serve different purposes. The least-squares guide shows how to compute the fitted predictions before either division. NIST, Linear Least Squares Regression Glossary

Why does minimising squared error favour the mean?

Among constant predictions on a fixed set of numbers, the arithmetic mean minimises MSE. Let the constant be cc and the observed mean be y¯\bar y. Expand yi−cy_i-c as the sum of yi−y¯y_i-\bar y and y¯−c\bar y-c. The cross terms sum to zero, giving

1n∑i(yi−c)2=1n∑i(yi−y¯)2+(y¯−c)2.\frac1n\sum_i(y_i-c)^2 = \frac1n\sum_i(y_i-\bar y)^2+(\bar y-c)^2.

The first term is fixed, and the second is smallest at c=y¯c=\bar y. This provides a baseline for judging whether a model predicts better than a constant. Compare both on held-out data. For model training, least squares applies the same squared-error idea to predictions that vary with the inputs. Portland State University, Regression and Least Squares

When is MSE a good comparison?

Use it when a few large errors matter more than many small ones, such as when large staffing forecast misses carry a disproportionate cost. Report RMSE alongside it if readers need the error scale in workers or hours. Check MAE when typical misses matter and compare signed error when overprediction and underprediction have different consequences.

If there are specified nonnegative weights wiw_i with positive total, compute the weighted error:

MSEw=∑iwi(yi−y^i)2∑iwi.\mathrm{MSE}_w=\frac{\sum_iw_i(y_i-\hat y_i)^2}{\sum_iw_i}.

A zero-weight observation contributes nothing. Apply identical weights to all models in a comparison. Changes in units matter too. Expressing an error in centimetres instead of metres multiplies it by 100, so its square grows by a factor of 10,000.

Worked Mean Squared Error Examples

Example 1

Find MSE for actual values 12, 17, and 9 and predictions 10, 18, and 12.

  1. The signed errors are 2, −1-1, and −3-3.
  2. The squared errors are 4, 1, and 9. Their sum is 14.
  3. The MSE is 14/3≈4.6714/3\approx4.67. RMSE is 14/3≈2.16\sqrt{14/3}\approx2.16 in original units.

Example 2

Model A has errors 1,1,1,51,1,1,5. Model B has errors 3,3,2,23,3,2,2. Which looks better under each loss?

  1. A has MAE 8/4=28/4=2 and MSE 28/4=728/4=7.
  2. B has MAE 10/4=2.510/4=2.5 and MSE 26/4=6.526/4=6.5.
  3. By MAE, A has the lower average absolute error. By MSE, B performs better because A’s error of 5 has more influence.

Example 3

Three delivery errors are 1,−2,41,-2,4 minutes. The first two observations have weight 1, and the third has weight 2.

  1. Square the errors to get 1, 4, and 16.
  2. Weight and sum the squares: 1+4+2(16)=371+4+2(16)=37.
  3. Divide by the total weight 4. The weighted MSE is 9.259.25 square minutes.

Example 4

A fitted straight line has SSE 30 across 12 training observations and estimates a slope and intercept.

  1. Mean squared prediction error on the training rows is 30/12=2.530/12=2.5.
  2. Residual degrees of freedom are 12−2=1012-2=10.
  3. The residual mean square is 30/10=330/10=3. Use the denominator that matches the question.

Example 5

Find the constant with lowest MSE for observations 4, 5, and 12.

  1. Their mean is (4+5+12)/3=7(4+5+12)/3=7.
  2. Predicting 7 gives squares 9, 4, and 25, so the MSE is 38/338/3.
  3. Predicting 6 instead gives squares 4, 1, and 36. Its MSE is 41/341/3, exactly 1 larger, the squared distance from 6 to 7.

Example 6

A model has MSE 0.040.04 square metres. Express its RMSE in centimetres.

  1. The RMSE is 0.04=0.2\sqrt{0.04}=0.2 metres.
  2. Multiply by 100 to get 20 centimetres.
  3. The equivalent MSE in the new units is 202=40020^2=400 square centimetres.

Mistakes That Change MSE

Mistake 1: Squaring the average signed error

For errors −2-2 and 2, the signed mean is zero. Squaring each first gives MSE 4.

Mistake 2: Comparing models on different random test sets

The measured loss also depends on the examples included. Use the same held-out observations or cross-validation splits to make the scores comparable.

Mistake 3: Interpreting MSE as an error in the target unit

If the target is dollars, MSE is in dollars squared. Use RMSE or MAE to express error in dollars.

Try It Yourself

  1. Find MSE for errors −4-4 and 2.
  2. An RMSE is 6 kilometres. What is the corresponding MSE?
  3. Three observations have errors 0, 4, and 1 and weights 1, 0, and 3. What is the weighted MSE?

Answers and Reasoning

  1. (16+4)/2=10(16+4)/2=10.
  2. 62=366^2=36 square kilometres.
  3. (0+0+3)/4=0.75(0+0+3)/4=0.75. The observation with error 4 has zero weight.

Frequently Asked Questions

Can MSE be negative?

The ordinary error metric cannot. Some model-selection tools report a sign-flipped score so that higher is better. Read the metric’s name and documentation before interpreting it.

Is any particular MSE good?

No universal threshold applies. Judge a value against the units, a relevant baseline on the same data, and the size of error the task can tolerate.

Is MSE the same as variance?

Training or test MSE uses predictions as the reference for every observation. If the prediction for every row is the observed mean, dividing by nn produces the average squared deviation from that mean. A sample variance often uses n−1n-1 instead. See the standard deviation guide.

Conclusion

  • Pair predictions with actual values and average the squared errors.
  • Check whether the label refers to prediction MSE or residual mean square.
  • Compare equivalent data and use RMSE or MAE when original units are easier to interpret.

Read More

Research Behind This Article

  1. Scikit-learn developers, Metrics and Scoring: Mean Squared Error. scikit-learn
  2. Scikit-learn developers, mean_squared_error API Reference. scikit-learn
  3. National Institute of Standards and Technology, Linear Least Squares Regression Glossary. NIST
  4. David Gerbing, Portland State University, Introduction to Machine Learning: Linear Regression. Portland State