Mean Squared Error Formula & How to Calculate It
In this article
The mean squared error (MSE) formula is MSE = (1/n) * Σ(yᵢ − ŷᵢ)², where n is the number of observations, yᵢ is the actual value, and ŷᵢ is the predicted value. In plain terms, you square each prediction error, add them all up, and divide by the number of data points—this gives you the average squared difference between predicted and actual values.
in actual | Actual values (comma separated) | - | 3, 5, 2.5, 7 | 0 | 100000 in predicted | Predicted values (comma separated) | - | 2.5, 5.5, 2, 8 | 0 | 100000 out mse | Mean Squared Error (MSE) | - | (1/count(actual)) * sum((actual - predicted)^2) | 4 out rmse | Root Mean Squared Error (RMSE) | - | sqrt((1/count(actual)) * sum((actual - predicted)^2)) | 4 out mae | Mean Absolute Error (MAE) | - | (1/count(actual)) * sum(abs(actual - predicted)) | 4
Formula
In mathematical notation, the MSE formula is written as:
MSE = (1/n) Σᵢ₌₁ⁿ (yᵢ − ŷᵢ)²
Where:
- n = number of data points (observations)
- yᵢ = the i-th actual (observed) value
- ŷᵢ = the i-th predicted (model output) value
- Σ = summation symbol, meaning “add up all the terms”
The result is in the squared units of the original data. For example, if your data is in dollars, the MSE is in dollars squared. To get back to the original units, take the square root—that’s the root mean squared error (RMSE).
Worked Example
Using the calculator’s default values: actual = [3, 5, 2.5, 7], predicted = [2.5, 5.5, 2, 8].
- Compute the errors (actual − predicted):
3 − 2.5 = 0.5
5 − 5.5 = −0.5
2.5 − 2 = 0.5
7 − 8 = −1 - Square each error:
0.5² = 0.25
(−0.5)² = 0.25
0.5² = 0.25
(−1)² = 1 - Sum the squared errors: 0.25 + 0.25 + 0.25 + 1 = 1.75
- Divide by n = 4: 1.75 / 4 = 0.4375
The MSE is 0.4375. The RMSE is √0.4375 ≈ 0.6614. The MAE (mean absolute error) is (0.5 + 0.5 + 0.5 + 1)/4 = 0.625. Notice that the MAE is smaller than the RMSE because squaring the errors gives more weight to larger errors (like the −1).
Why Square the Errors?
Squaring serves two main purposes. First, it removes the sign—errors of +0.5 and −0.5 both become 0.25, so they don’t cancel each other out. If you just averaged the raw errors, you’d get 0 for any model that over-predicts as often as it under-predicts, which would hide real problems.
Second, squaring penalizes large errors more heavily than small ones. A prediction that’s off by 5 units contributes 25 to the sum, while an error of 1 contributes only 1. This makes MSE sensitive to outliers—a single large error can dominate the result. That’s often desirable when large errors are especially bad (e.g., in financial forecasting), but it can be a drawback if your data has noisy outliers that aren’t meaningful.
MSE vs. RMSE vs. MAE
These three metrics are closely related but answer slightly different questions:
- MSE – average squared error. Good for mathematical optimization because it’s differentiable. Units are squared.
- RMSE – square root of MSE. Same units as the original data, so it’s easier to interpret. Still penalizes large errors more than MAE.
- MAE – average absolute error. Treats all errors equally. More robust to outliers, but not differentiable at zero, which can complicate some optimization algorithms.
In practice, RMSE is often preferred for reporting because it’s in the same units as the target variable. MAE is a better choice when you care about typical error magnitude and don’t want outliers to dominate.
How to Interpret MSE
There’s no universal “good” or “bad” MSE—it depends entirely on the scale of your data. An MSE of 0.5 is tiny if your values are in the thousands, but huge if they’re between 0 and 1. To make it interpretable, compare it to the variance of the actual values. A useful benchmark is the R² (coefficient of determination), which is 1 − (MSE / variance of actuals). An R² of 1 means perfect prediction, 0 means the model is no better than just predicting the mean.
Another practical approach is to compute the RMSE and compare it to the mean of the actual values. If RMSE is, say, 5% of the mean, that’s often considered good, but it depends on the domain.
Common Mistakes and Pitfalls
- Using the wrong n – Make sure you divide by the number of data points, not the number of parameters or something else. For sample data, some formulas divide by n−1, but that’s for variance, not MSE.
- Mixing up actual and predicted – The formula is symmetric in the sense that (y−ŷ)² = (ŷ−y)², so it doesn’t matter which you subtract from which, but be consistent.
- Ignoring units – MSE is in squared units. If you report it without converting, readers may think it’s in the original units.
- Comparing MSE across different datasets – MSE is not scale-invariant. You can’t compare the MSE of a model predicting house prices (in dollars) with one predicting temperature (in °C).
- Forgetting to take the square root for interpretation – If you want the error in the same units as your data, use RMSE, not MSE.
When to Use MSE (and When Not To)
MSE is the default loss function for many regression problems, especially in machine learning, because it’s convex and differentiable, making gradient-based optimization straightforward. It’s also used in linear regression to find the line of best fit (ordinary least squares).
However, if your data contains outliers that you don’t want to heavily penalize, consider MAE or Huber loss (a combination). If you’re dealing with count data, you might prefer Poisson deviance. For classification problems, MSE is rarely appropriate—cross-entropy is usually better.
Frequently Asked Questions
What is the difference between MSE and variance?
Variance measures how spread out the actual values are around their own mean. MSE measures how far the predictions are from the actual values. If your model always predicts the mean of the actuals, then MSE equals the variance. If your model is perfect, MSE is 0.
Can MSE be negative?
No. Since you square each error, every term is zero or positive. The sum of non-negative numbers is non-negative, so MSE is always ≥ 0. An MSE of 0 means perfect predictions.
What is a good MSE value?
There’s no absolute standard. A good MSE is one that is small relative to the variance of the target variable. A common rule of thumb: if RMSE is less than 10% of the mean of the actual values, the model is quite accurate, but this varies by field. Always compare to a baseline model (like predicting the mean).
Why do we use squared error instead of absolute error in linear regression?
Squared error is differentiable everywhere, which makes it easy to find the minimum analytically (via calculus). Absolute error is not differentiable at zero, which complicates optimization. Squared error also naturally penalizes larger errors more, which is often desirable.
How do I calculate MSE in Excel?
If your actual values are in A1:A10 and predicted in B1:B10, use the formula: =AVERAGE((A1:A10-B1:B10)^2) entered as an array formula (Ctrl+Shift+Enter in older Excel). In newer Excel, it works as a dynamic array. Alternatively, use =SUMSQ(A1:A10-B1:B10)/COUNT(A1:A10).