Machine LearningRegression Loss Functions
Why Is Mean Squared Error Often Used Instead of Absolute Error?
Question 296 of 303 · Updated
On this page
- 1. The two loss functions
- 2. Why MSE is convenient to optimize
- 3. MSE makes large errors matter much more
- Worked example
- 4. MSE and MAE estimate different things
- 5. Why MAE can be the better choice
- 6. A practical regression example
- 7. Training loss and evaluation metric are not the same decision
- 8. Quick comparison
Mean Squared Error (MSE) is often chosen because it is smooth and differentiable, gives large errors extra weight, and leads to convenient optimization and statistical properties. Mean Absolute Error (MAE) is still valuable: it is more robust to outliers and directly measures average absolute error. The right choice depends on what kinds of mistakes matter and how the model will be trained.
1. The two loss functions#
For actual values yᵢ and predictions ŷᵢ, define the residual eᵢ = yᵢ − ŷᵢ.
Mean Squared Error
Each residual is squared before averaging. The loss is non-negative, equals zero for perfect predictions, and has squared target units.
Mean Absolute Error
Each residual contributes its absolute magnitude. MAE is also non-negative and zero for perfect predictions, but it stays in the target's original units.
Both are valid regression objectives. Neither is universally better.
2. Why MSE is convenient to optimize#
The squared-error function is smooth and differentiable with respect to the prediction. For one observation:
Its derivative with respect to ŷ is:
This gives gradient-based optimizers a continuously changing signal that indicates both the direction and magnitude of the correction. For a linear model, squared-error optimization is a convex problem; ordinary least squares even has a closed-form solution when the required matrix conditions hold.
Absolute error is:
Its derivative is −1 when ŷ < y and +1 when ŷ > y, but it is not differentiable at ŷ = y. Optimization is still possible using subgradients and specialized methods, so it is inaccurate to say MAE cannot be optimized. However, the flat-magnitude gradient away from zero and the kink at zero can make some optimization setups less convenient than squared error.
3. MSE makes large errors matter much more#
Squaring changes the relative contribution of residuals:
| Absolute error | Squared error |
|---|---|
| 1 | 1 |
| 2 | 4 |
| 5 | 25 |
| 10 | 100 |
An error of 10 contributes 100 to squared loss, while an error of 1 contributes only 1. Under absolute loss, those same errors contribute 10 and 1. Thus MSE strongly discourages occasional large misses.
Worked example#
Suppose a model makes four predictions with residuals:
[1, −2, 2, 10]
Then:
MAE = (1 + 2 + 2 + 10) / 4 = 3.75
MSE = (1² + 2² + 2² + 10²) / 4 = (1 + 4 + 4 + 100) / 4 = 27.25
The single error of 10 contributes 100 of the total 109 squared-error points. This is useful when a large miss is genuinely much more costly than several small misses. It is harmful when that extreme observation is a data error or an unrepresentative outlier.
4. MSE and MAE estimate different things#
Consider a model that must predict one constant value for every example.
- The value that minimizes the sum of squared errors is the arithmetic mean of the target values.
- The value that minimizes the sum of absolute errors is a median of the target values.
This reflects their statistical interpretation. Under squared-error loss, the optimal prediction for a given feature vector is the conditional mean E[Y | X]. Under absolute-error loss, an optimal prediction is a conditional median.
If the noise is approximately Gaussian, squared error is closely connected to maximum-likelihood estimation under that assumption. This is one reason it is a natural default in many classical regression settings. It is an assumption and modeling choice, not a guarantee that real-world residuals are Gaussian.
5. Why MAE can be the better choice#
MSE is sensitive to outliers because large residuals are squared. A few unusual observations can dominate training and pull the fitted model toward them.
MAE is more robust to extreme residual magnitudes. Its penalty grows linearly, so doubling an error doubles its contribution rather than quadrupling it. It is often a better fit when:
- the target contains genuine or unavoidable extreme values;
- the typical absolute deviation is more important than rare large misses;
- the business wants an interpretable average error in the original unit;
- the conditional median is a more appropriate prediction target than the conditional mean.
MAE has its own trade-offs. Its gradient does not grow with residual magnitude, and optimization at zero requires handling a non-differentiable point.
6. A practical regression example#
Imagine predicting daily product demand. Most days have demand between 90 and 120 units, but a major promotion produces a day with demand of 400 units.
- If promotional spikes are real and stockouts are very costly, MSE may be appropriate because it makes the model pay more attention to large misses.
- If the 400-unit observation is a one-off data error, or the goal is to predict typical daily demand, MAE may be more stable.
- If both ordinary errors and extreme errors matter, consider Huber loss. It behaves quadratically for small residuals and linearly for sufficiently large residuals, combining smooth behavior near zero with reduced outlier sensitivity.
Always inspect the residual distribution and investigate extreme observations before choosing a loss function. Do not automatically remove outliers: they may represent important business events.
7. Training loss and evaluation metric are not the same decision#
A model can be trained with one loss and evaluated with another. For example, training with MSE but reporting MAE may be reasonable when optimization convenience matters but stakeholders want average absolute error.
However, optimizing MSE does not guarantee the lowest MAE, and optimizing MAE does not guarantee the lowest MSE. Choose metrics that reflect the real cost of errors, evaluate on held-out data, compare with a baseline, and consider important customer or business segments.
In scikit-learn, the metrics can be calculated directly:
PYTHONfrom sklearn.metrics import mean_absolute_error, mean_squared_error mae = mean_absolute_error(y_true, y_pred) mse = mean_squared_error(y_true, y_pred) print("MAE:", mae) print("MSE:", mse)
8. Quick comparison#
- Optimization: MSE is smooth and differentiable; MAE has a kink at zero and can be optimized with subgradient methods.
- Large errors: MSE penalizes them quadratically; MAE penalizes them linearly.
- Outliers: MSE is more sensitive; MAE is more robust.
- Constant prediction: MSE selects the mean; MAE selects a median.
- Units: MSE has squared target units; MAE has the original target units.
- Use MSE when: large mistakes deserve disproportionately high penalties and squared-error optimization matches the objective.
- Use MAE when: robustness and typical absolute error matter more than heavily penalizing rare large misses.
Interview takeaway: MSE is often used instead of absolute error because its smooth quadratic form is convenient for gradient-based optimization, has useful least-squares solutions, and strongly penalizes large errors. But that same sensitivity makes it vulnerable to outliers. MAE is preferable when robustness or median-oriented predictions matter. The loss should be selected from the problem's error costs and data characteristics—not simply because one metric is more common.
References
- 1.Linear regression: Loss — Google Machine Learning Crash Coursedevelopers.google.com
- 2.
- 3.Regression loss functions — Stanford CS221 lecture notesweb.stanford.edu
- 4.
- 5.
- 6.Mean squared error — scikit-learn APIscikit-learn.org
Related questions
Explore other topics:Deep LearningMLOps