Articles

Mean Squared Error (MSE) in Machine Learning: Formula, RMSE, MAE, and R-Squared

Complete MSE guide for 2026. Formula, Python example, when MSE beats MAE or RMSE, R-squared comparison, outlier sensitivity, neural network loss use cases.

· Updated
· 15 min read
data quality llms rag
Mean squared error formula and bar chart comparing actual vs predicted values
Table of Contents

Mean Squared Error (MSE) in Machine Learning: Formula, RMSE, MAE, and R-Squared

MSE stands for Mean Squared Error. It is a regression metric equal to the average of the squared differences between a model’s predicted values and the actual values, written as MSE = (1/n) Σ (yᵢ − ŷᵢ)². Lower is better, zero means every prediction was exact, and the result carries the squared units of whatever you are predicting.

That is the whole definition. The rest of this guide covers why the squaring step matters, when MSE misleads you, and what to use instead when it does.

MSE is both an evaluation metric and a training loss: it is the function gradient descent minimizes when you train a regression network. This guide covers the MSE formula, a working Python example, when to prefer MSE over MAE or RMSE, the relationship to R-squared, and how MSE behaves in neural networks and ensemble methods.

TL;DR: MSE, RMSE, MAE, and R-Squared at a Glance

MetricFormulaUnitsOutlier sensitivityWhen to use
MSEmean of (y - y_hat) squaredTarget units squaredHighDefault regression loss, gradient descent
RMSEsquare root of MSETarget unitsHighReporting error in original units
MAEmean of absolute (y - y_hat)Target unitsLowOutlier-robust regression error
R-squared1 minus MSE divided by the population variance of the targetUnitless (1 perfect, 0 mean predictor, negative is worse)IndirectCommunicating fit quality across teams
Huber lossQuadratic for small errors, linear for largeMixed scale (delta-dependent)MediumMix of MSE smoothness and MAE robustness

Mean Squared Error Formula

The mean squared error formula is:

MSE = (1 / n) × Σⁿᵢ₌₁ (yᵢ − ŷᵢ)²

Where:

  • yᵢ is the actual value from the dataset
  • ŷᵢ is the predicted value from the model
  • n is the number of observations
  • Σ sums the squared error across all n observations

The squaring step ensures both over-predictions and under-predictions contribute positively, and it weights larger deviations more heavily than smaller ones. The result is non-negative, has the units of the target variable squared, and equals zero only when every prediction is exactly correct.

Worked example

Suppose a model predicts house prices for four houses, in thousands of dollars:

Actual (y)Predicted (ŷ)Error (y − ŷ)Squared error
200210−10100
35034010100
275295−20400
41040010100

Sum of squared errors = 100 + 100 + 400 + 100 = 700. Divide by n = 4, and MSE = 175.

Two things to notice. First, the units are thousands-of-dollars squared, which is not a quantity anyone can reason about directly, so you would report RMSE = √175 ≈ 13.2 thousand dollars instead. Second, the single 20-unit error contributed 400 of the 700 total, more than the other three errors combined. That is the squaring step doing exactly what it is designed to do, and it is also the reason a single bad outlier can dominate the score.

What Is Mean Squared Error

Conceptually, MSE answers one question: on average, how far off is this model, with big misses counting for disproportionately more than small ones?

It is worth being precise about why the errors get squared, because two other things would also work. Taking the raw average of (y − ŷ) fails immediately: over-predictions and under-predictions cancel, so a model that is wildly wrong in both directions can score zero. Taking absolute values fixes the cancellation and gives you MAE, which is a perfectly good metric. Squaring does something MAE does not: it makes the error surface smooth and differentiable everywhere, which is what lets gradient descent optimize it directly, and it encodes a specific opinion that one 10-unit error is worse than ten 1-unit errors.

That opinion is a modelling choice, not a mathematical fact. When it matches reality, MSE is the right metric. When your large errors are measurement noise rather than genuine model failure, it is the wrong one, and the sections below cover what to use instead.

Mean Squared Error calculation bar chart on a dark background showing Actual and Predicted values side by side for five data points, with the predicted bars close to but slightly off the actual bars

Figure 1: Mean Squared Error Calculation showing Actual vs Predicted values across five data points

How MSE Differs from MAE and RMSE

MAE (Mean Absolute Error) takes the absolute value of each error instead of squaring it. That keeps the metric in the original units and treats all errors linearly, so a single 10-unit error contributes the same as ten 1-unit errors. MAE is therefore more robust to outliers but harder to optimize with gradient methods because it is not differentiable at zero.

RMSE (Root Mean Squared Error) is the square root of MSE. It rescales MSE back into the original units of the target, which makes it easier to communicate. RMSE and MSE rank models identically (lower MSE always means lower RMSE), so they are interchangeable for model selection.

RMSE vs MSE: Which Should You Report?

Short answer: optimize MSE, report RMSE.

They contain identical information. RMSE is defined as √MSE, and because the square root is monotonic, any model that beats another on MSE also beats it on RMSE. There is no scenario where the two disagree about which model is better. The choice between them is entirely about what a number means to whoever reads it.

MSERMSE
UnitsTarget units, squaredTarget units
Interpretable to a stakeholderNoYes
Use as a training lossYes, standardRarely, adds a needless square root
Ranks modelsIdentically to RMSEIdentically to MSE
Outlier sensitivityHighHigh, but compressed by the root

In the worked example above, MSE = 175 “thousand-dollars squared”, which means nothing to anyone. RMSE ≈ 13.2 thousand dollars means the model is typically off by about thirteen thousand dollars, which a product manager can act on.

Two details people get wrong. First, RMSE is not the average error, and it is not MAE. Because of the squaring inside, RMSE is always greater than or equal to MAE, and the gap between them widens as your errors become more uneven. Comparing RMSE against MAE is actually a quick diagnostic: if RMSE is far above MAE, your error is concentrated in a few large misses rather than spread evenly. Second, taking the root does not make RMSE robust to outliers. It compresses the scale but the squaring already happened, so a single dominant outlier still drives the number.

For training, stick with MSE. The extra square root adds computation and changes the gradient scale without changing which minimum you reach.

MSE as a Loss Function: PyTorch and TensorFlow

Every major framework ships MSE as a built-in regression loss, because it is the default choice for continuous-target networks.

import torch
import torch.nn as nn

criterion = nn.MSELoss()          # reduction="mean" by default
predictions = torch.tensor([210.0, 340.0, 295.0, 400.0])
targets     = torch.tensor([200.0, 350.0, 275.0, 410.0])

loss = criterion(predictions, targets)
print(loss.item())                # 175.0, matching the worked example above
import tensorflow as tf

mse = tf.keras.losses.MeanSquaredError()
print(mse([200.0, 350.0, 275.0, 410.0], [210.0, 340.0, 295.0, 400.0]).numpy())  # 175.0

# Or as a compile-time argument
model.compile(optimizer="adam", loss="mse", metrics=["mae"])

Three things worth knowing when you use it as a loss rather than a report metric:

  • The reduction argument matters. PyTorch’s nn.MSELoss defaults to reduction="mean", which averages over every element. Switching to reduction="sum" makes your effective learning rate scale with batch size, which is a common source of training instability that looks like a bad learning rate.
  • Scale your targets. MSE gradients are proportional to the error magnitude, so an unscaled target in the tens of thousands produces enormous initial gradients. Standardize or normalize the target and the same architecture usually trains without exploding.
  • Use Huber when the data is dirty. nn.HuberLoss and tf.keras.losses.Huber behave quadratically for small errors and linearly for large ones, which keeps MSE’s smooth gradients near the optimum while capping the influence of outliers.

Why MSE Matters in Machine Learning

Measuring Regression Model Accuracy

MSE provides a single, quantitative number that summarises how close predictions are to truth. Lower MSE means predictions are tighter around the actual values. Higher MSE means the model is off, sometimes systematically and sometimes only on a few high-leverage points.

Classic use cases include:

  • Stock price prediction. MSE measures how far model output is from realized prices. Squaring penalises blowups more than chronic small drift, which matters when a single bad day can wipe out a quarter.
  • Sales forecasting. A retailer evaluates monthly forecasts with MSE to flag regions and seasons where prediction error is concentrated.
  • Weather prediction. Meteorologists track MSE of temperature and rainfall forecasts to decide when a model is good enough to release publicly.

Why Gradient Descent Likes MSE

The training loop computes MSE on a mini-batch, takes the gradient with respect to model parameters, and updates the parameters with an optimizer like Adam or SGD. See the framework code above for the PyTorch and TensorFlow calls.

Three properties make MSE well behaved for gradient descent:

  • Smoothness. The squared-error surface is differentiable everywhere, which keeps the gradient stable.
  • Convexity in linear models. For linear regression, the MSE surface is globally convex, so gradient descent converges to the unique optimum.
  • Strong penalty on outliers. The squared term keeps the model focused on large errors during training. This is helpful when large errors are costly and unhelpful when they are noise.

Insights MSE Provides About Model Performance

MSE alone tells you the average squared error. Pairing MSE with residual analysis reveals where the model struggles:

  • A handful of large residuals dragging up MSE usually points to outliers, leverage points, or missing features for a sub-population.
  • A flat residual plot with high MSE points to underfitting.
  • A residual plot that fans out with increasing predictions points to heteroscedasticity. One global MSE hides range-dependent error patterns in that case, so segmented metrics or residual plots are required to diagnose where the model is failing.

How to Calculate MSE: Step-By-Step

To compute MSE by hand:

  1. Subtract the predicted value from the actual value for each observation.
  2. Square each difference.
  3. Sum the squared differences.
  4. Divide by the number of observations.

Example:

  • Actual values: [5, 7, 9]
  • Predicted values: [6, 6, 10]
  • Errors: [-1, 1, -1]
  • Squared errors: [1, 1, 1]
  • MSE: (1 + 1 + 1) / 3 = 1.0

Working Python Example with NumPy and Scikit-Learn

import numpy as np
from sklearn.metrics import mean_squared_error

y_actual = np.array([5, 7, 9])
y_predicted = np.array([6, 6, 10])

# Manual calculation
mse_manual = np.mean((y_actual - y_predicted) ** 2)

# Scikit-learn equivalent
mse_sklearn = mean_squared_error(y_actual, y_predicted)

rmse = np.sqrt(mse_sklearn)
print(f"MSE: {mse_manual:.4f}")
print(f"MSE (sklearn): {mse_sklearn:.4f}")
print(f"RMSE: {rmse:.4f}")

mean_squared_error is the standard reference implementation. Use it for production code rather than rolling your own to avoid edge-case bugs with empty arrays or NaN handling.

Common Pitfalls in MSE Calculation

  • Outliers. A single 100-unit error contributes 10,000 to the sum of squares, which dwarfs ninety-nine 1-unit errors that total only 99. Inspect residuals before trusting MSE.
  • Scale dependency. MSE is in the squared units of the target. Comparing MSE across models that predict different targets is meaningless without normalization.
  • Train versus test split. Always report MSE on a held-out test set, not the training set, to detect overfitting.

What Is a Good MSE Value?

There is no universal threshold, and any source that gives you one is wrong.

The reason is in the units. MSE is measured in the squared units of your target variable, so its magnitude depends entirely on what you are predicting. An MSE of 175 is excellent when predicting house prices in thousands of dollars and catastrophic when predicting a probability between 0 and 1. The number is not comparable across datasets, across target scales, or even across the same dataset after you change the units.

What makes an MSE good or bad is always a comparison. Three that actually work:

  1. Against the mean baseline. Compute the MSE of a model that ignores every feature and always predicts the training mean. Your model must beat it, and by a margin worth the complexity. This comparison is exactly what R-squared formalises: R² = 1 − (MSE / variance of the target), where that variance is the population form dividing by n, matching MSE’s own denominator. R² = 0 means you tied the mean predictor and negative R² means you lost to it.
  2. Against the previous model. For an existing production system, the only MSE that matters is the incumbent’s on the same held-out set. A 5 percent reduction that holds up on test data is a real result.
  3. Against the cost of being wrong. Convert to RMSE, put it in target units, and ask whether an error of that size is acceptable to the business. If your demand forecast has an RMSE of 400 units and the warehouse holds 300, the model is not usable regardless of how the number compares to anything else.

The one absolute statement worth making: an MSE of exactly zero on training data is not a good result, it is a warning. It almost always means the model has memorised the training set or a feature is leaking the target.

How to Interpret MSE Results

High MSE

A high MSE indicates large prediction errors on average. Common causes:

  • Underfitting (model is too simple for the relationship)
  • Inadequate feature engineering or missing predictors
  • Data quality issues like mislabeled targets, missing values, or measurement noise

Low MSE

A low MSE indicates predictions are close to actual values. Always confirm the score on test data; a very low training MSE can hide overfitting. Cross-validation and a held-out test set give a more honest read.

Balancing MSE with RMSE and R-Squared

R-squared rescales error into a unitless fit score where 1 is perfect, 0 matches the mean predictor, and negative values mean the model is worse than the mean baseline. R-squared = 1 minus (MSE / variance of target). For a clear walkthrough see R-squared model accuracy. RMSE rescales MSE back into the target units. Most reports include all three, plus a residual plot for diagnostics.

Practical Applications of MSE

Forecasting and Time Series

Time-series models use MSE to track forecast error against realized values. A retail chain that forecasts monthly sales by region uses MSE to flag regions where forecast error has grown, often a signal of missing seasonal or regional features. For drift over time see model vs data drift.

Pricing and Recommendation Models

E-commerce platforms use MSE to evaluate predicted optimal prices against actual customer behavior. Recommendation engines that predict ratings (Netflix-style 1-to-5 stars before they switched to thumbs) use MSE on the predicted rating vector against held-out ratings.

Computer Vision Regression Tasks

CNNs that regress bounding-box coordinates or pixel values use MSE on the coordinate vector. Object-detection losses like the Smooth L1 loss in Fast R-CNN combine MSE for small errors and MAE for large errors, which avoids exploding gradients while keeping smooth optimization.

Comparing Models Using MSE: Linear Regression vs Decision Trees vs Random Forests

A data scientist testing three regression models on housing-price prediction might see:

ModelMSERMSE
Linear Regression120,000~346
Decision Tree95,000~308
Random Forest80,000~283

The Random Forest has the lowest MSE, so it is the best fit on this dataset. Always confirm the ranking on a cross-validation split before deploying. Hyperparameter tuning (tree depth, learning rate, regularization strength) typically continues to lower MSE until it plateaus or test MSE starts to climb (overfitting signal).

MSE in Optimization Algorithms: Gradient Descent

Gradient descent uses MSE as the objective function for regression. For each mini-batch, the algorithm computes the gradient of MSE with respect to model parameters and steps in the negative-gradient direction. Over many iterations the parameters settle into a minimum.

The intuition is a landscape where MSE is the elevation. Gradient descent is a ball that rolls downhill, and each step is an update to model parameters. With an appropriate learning rate, gradient descent on convex MSE surfaces (like linear regression) converges to the global minimum. Non-convex surfaces (like deep neural networks) have many local minima, and the optimizer settles into one of them.

MSE in Deep Learning Practice

For continuous-output deep networks, MSE is the default training loss. For image-to-image regression (super-resolution, denoising), MSE is often combined with perceptual losses because pixel-wise MSE alone produces blurry outputs. For tabular regression, MSE is usually sufficient.

Pro Tips for Using MSE Effectively

  • Normalize features so that scales do not bias gradient updates. MSE is sensitive to feature scaling because parameters with larger feature scales also have larger gradients.
  • Pair MSE with residual plots to catch heteroscedasticity, outliers, and systematic bias that the summary number hides.
  • Combine MSE with domain knowledge. A model with low MSE that misses a known operational constraint (negative prices, missing-class predictions) is not deployable, even if the average error looks good.
  • Use Huber loss when you need MSE-like smoothness for small errors and MAE-like robustness for large ones. Huber is the standard middle-ground choice.

Advantages and Limitations of MSE

Advantages

  • Penalizes larger errors heavily, which is the right behaviour when large errors are costly.
  • Smooth and differentiable, which makes it well behaved for gradient descent.
  • Convex in linear models, which guarantees gradient descent finds the global minimum.

Limitations

  • Sensitive to outliers (a single outlier can dominate the metric).
  • Squared units complicate direct interpretation (use RMSE to recover original units).
  • Not the right metric for classification (use cross-entropy or F1, depending on the task; see F1 score).
  • Not the right metric for highly skewed targets (consider log-transforming the target).

Where MSE Fits in LLM and Agent Evaluation

MSE is a numeric-output metric, which makes it a poor fit for LLM evaluation where outputs are free-form text. LLM evals instead use LLM-as-a-judge metrics like faithfulness, instruction-following, and toxicity, plus deterministic checks for things like schema conformance. See deterministic LLM evaluation metrics for a survey.

The Future AGI Agent Learning Kit (Apache 2.0 on GitHub) covers the LLM and agent side of the same problem space MSE solves for regression: scoring model outputs against ground truth at scale. The closest direct analog is wrapping a custom regression scorer through CustomLLMJudge for cases where the LLM extracts numeric fields that you want to score with MSE-like metrics.

# Requires: pip install ai-evaluation
# Env: FI_API_KEY, FI_SECRET_KEY
from fi.evals import evaluate

# Faithfulness scoring on an LLM answer that quoted numbers from a source doc.
result = evaluate(
    "faithfulness",
    output="The forecasted Q3 revenue is 2.4M USD with a 12 percent confidence band.",
    context="Q3 revenue forecast: 2.4M USD, confidence interval +/- 12 percent.",
    model="turing_flash",
)

print(result.score, result.reason)

Future AGI Evals catalog showing the LLM-as-a-judge evaluator library across faithfulness, instruction following, toxicity, RAG, hallucination, and more categories as alternatives to MSE for scoring LLM and agent outputs

Summary: When MSE Wins and When to Switch to MAE or Huber

MSE is the default metric for regression in classical ML and the default loss function for regression neural networks. It is smooth, well behaved for gradient descent, and the right call when large errors are disproportionately costly. Switch to MAE when outliers are noise rather than signal, Huber loss when you want a balance, and R-squared when you need a unitless score that translates across stakeholders. For classification and LLM outputs, MSE does not apply; use cross-entropy and LLM-as-a-judge metrics instead.

Frequently Asked Questions

What is Mean Squared Error in machine learning?

Mean Squared Error is a regression metric defined as the average of the squared differences between predicted and actual values. Lower MSE indicates a closer fit. Because the errors are squared, MSE penalizes a single large error more than several small errors of the same total magnitude, which makes it sensitive to outliers and well suited as a training loss for gradient descent.

What is the formula for MSE?

MSE equals one over n times the sum from i equals 1 to n of (y_i minus y_hat_i) squared, where y_i is the actual value, y_hat_i is the predicted value, and n is the number of observations. The result is a non-negative number in the squared units of the target variable, so a sales-prediction model in dollars produces MSE in dollars squared.

What is the difference between MSE, RMSE, and MAE?

MSE squares the errors before averaging. RMSE is the square root of MSE and is back in the original units of the target, which makes it easier to interpret. MAE is the average absolute error and treats all errors linearly, which makes it more robust to outliers than MSE or RMSE. Pick MSE or RMSE when large errors are disproportionately costly; pick MAE when outliers are noise rather than signal.

What does MSE stand for?

MSE stands for Mean Squared Error. In machine learning and statistics it is a regression metric equal to the average of the squared differences between predicted and actual values, written MSE = (1/n) times the sum of (y_i minus y_hat_i) squared. Note that the same three letters are used for unrelated things in other fields, including mental status examination in medicine, mechanically stabilised earth in civil engineering, and Master of Science in Engineering as a degree.

What is a good MSE value?

There is no universal threshold, because MSE is measured in the squared units of whatever you are predicting. An MSE of 175 is excellent for house prices in thousands of dollars and catastrophic for a probability between 0 and 1. Judge it by comparison instead: against a baseline model that always predicts the training mean (which is what R-squared formalises), against the MSE of the model currently in production on the same held-out data, or by converting to RMSE and asking whether an error of that size in real units is acceptable. An MSE of exactly zero on training data is a warning sign of memorisation or target leakage, not a good result.

Should I report MSE or RMSE?

Optimize MSE, report RMSE. They rank models identically because RMSE is the square root of MSE and the square root is monotonic, so neither can disagree with the other about which model is better. The difference is interpretability: MSE carries squared units that mean nothing to a stakeholder, while RMSE is back in the units of the target. Keep MSE as the training loss, since the extra square root adds computation and changes gradient scale without changing the minimum you converge to.

What does a low MSE indicate?

A low MSE indicates that the model's predictions are close to the actual values on the dataset you measured. Always validate the same MSE on held-out test data, because a very low training MSE can hide overfitting. Pair MSE with R-squared, residual plots, or cross-validation before deciding the model is production-ready.

Is MSE used in neural networks?

Yes. MSE is a common regression loss exposed by PyTorch (`torch.nn.MSELoss`), TensorFlow (`tf.keras.losses.MeanSquaredError`), and most other frameworks. Gradient descent uses the gradient of MSE with respect to weights to update parameters during training, and the squared-error surface is smooth and differentiable everywhere, which makes it well behaved for optimizers like Adam and SGD.

When should you not use MSE?

Avoid MSE when outliers are noise rather than signal, because the squaring step makes one large outlier dominate the metric. Switch to MAE or Huber loss in that case. Also avoid MSE as the sole metric for classification (use cross-entropy) and when the target variable is highly skewed (consider log-transforming the target or using a quantile loss).

How is MSE related to R-squared?

R-squared equals 1 minus the ratio of MSE to the variance of the target. So lowering MSE on a fixed dataset raises R-squared mechanically. R-squared is unitless: 1 is a perfect fit, 0 means the model is no better than predicting the mean, and negative values mean the model is worse than the mean baseline. This makes it easier to communicate model quality across teams without exposing raw squared units.

Does Future AGI use MSE for LLM evaluation?

Future AGI's eval catalog is designed for LLM and agent output, where MSE does not apply directly because outputs are text rather than numbers. Future AGI uses LLM-as-a-judge evaluators such as faithfulness, instruction-following, and toxicity. The closest LLM equivalent of MSE is regression-style metrics over numeric extracted fields, which can be wrapped in a CustomLLMJudge through the Apache 2.0 Agent Learning Kit.
Related Articles
View all