Root Mean Squared Error (RMSE)

Start With The Problem — Where MSE Falls Short

You just calculated MSE for your house price model:

MSE = 20.8

Your manager asks — "so how wrong is our model on average?"

You say — "MSE is 20.8"

They ask — "20.8 what? Lakhs? Lakhs squared? What does that mean?"

You have no good answer. Because MSE unit is Lakhs² — completely uninterpretable in real world.

You want MSE's superpower (punishing big errors) but in a unit that actually makes sense.

Simple fix — just take the square root of MSE. That's RMSE.


What is RMSE?

RMSE = Square Root of MSE

That's the entire definition. Nothing new to learn conceptually — it's just MSE with a square root on top to fix the unit problem.

RMSE = √MSE = √( (1/n) × Σ (Actual - Predicted)² )

The Formula

RMSE = √ [ (1/n) × Σ (Actual - Predicted)² ]

Step by step:

  1. Find error (Actual − Predicted)
  2. Square each error
  3. Take average → this is MSE
  4. Take square root of MSE → this is RMSE

Manual Walkthrough — Step by Step

Same house price data:

House

Actual

Predicted

Error

Error²

1

50

45

5

25

2

80

85

-5

25

3

60

58

2

4

4

90

95

-5

25

5

70

65

5

25

Step 1 — Sum of squared errors:

25 + 25 + 4 + 25 + 25 = 104

Step 2 — MSE:

MSE = 104 / 5 = 20.8

Step 3 — RMSE:

RMSE = √20.8 = 4.56

Result: RMSE = 4.56 Lakhs

Now you can tell your manager — "on average, our model is wrong by ₹4.56 Lakhs" — and they actually understand it.


MAE vs RMSE — Same Unit, Different Behavior

Both are now in Lakhs. Let's compare on same data:

MAE  = 4.4  Lakhs
RMSE = 4.56 Lakhs

RMSE is slightly higher. Why? Because it penalizes bigger errors more, so it naturally comes out a bit higher than MAE.

This relationship is always true:

RMSE >= MAE   (always, without exception)

The gap between RMSE and MAE tells you something important about your model's errors.


The Gap Between RMSE and MAE — This is Gold

Gap

What it means

RMSE ≈ MAE (small gap)

Errors are consistent — no big outlier mistakes

RMSE >> MAE (big gap)

Model is making some very large errors somewhere

mae  = 4.4
rmse = 4.56
gap  = rmse - mae   # small gap = consistent errors, model is stable

mae2  = 4.4
rmse2 = 18.7
gap2  = rmse2 - mae2  # huge gap = some predictions are badly wrong

In real projects, checking this gap is a quick way to detect if your model has an outlier problem.


Python Program


    import numpy as np
    import pandas as pd
    from sklearn.metrics import mean_squared_error, mean_absolute_error
    import matplotlib.pyplot as plt

    # --- Data ---
    actual    = [50, 80, 60, 90, 70]
    predicted = [45, 85, 58, 95, 65]

    # --- Manual Calculation ---
    errors         = [a - p for a, p in zip(actual, predicted)]
    squared_errors = [e**2 for e in errors]
    mse_manual     = sum(squared_errors) / len(squared_errors)
    rmse_manual    = mse_manual ** 0.5   # square root

    print("=== Manual Calculation ===")
    print(f"Errors         : {errors}")
    print(f"Squared Errors : {squared_errors}")
    print(f"MSE            : {mse_manual}")
    print(f"RMSE           : {rmse_manual:.4f}")

    # --- Using NumPy ---
    rmse_numpy = np.sqrt(np.mean((np.array(actual) - np.array(predicted))**2))
    print(f"\nRMSE (numpy)   : {rmse_numpy:.4f}")

    # --- Using Scikit-learn ---
    mse     = mean_squared_error(actual, predicted)
    rmse    = np.sqrt(mse)
    mae     = mean_absolute_error(actual, predicted)

    print(f"RMSE (sklearn) : {rmse:.4f}")

    # --- The Gap Analysis ---
    print("\n=== MAE vs RMSE Gap Analysis ===")
    print(f"MAE  : {mae:.4f}")
    print(f"RMSE : {rmse:.4f}")
    print(f"Gap  : {rmse - mae:.4f}  ({'small - model is consistent' if (rmse - mae) < 2 else 'large - model has big error somewhere'})")

    # --- Outlier Comparison ---
    actual2    = [50, 80, 60, 90, 70]
    predicted2 = [50, 80, 60, 89, 30]   # last one is way off

    mae2  = mean_absolute_error(actual2, predicted2)
    rmse2 = np.sqrt(mean_squared_error(actual2, predicted2))

    print("\n=== Normal Model vs Outlier Model ===")
    print(f"Normal Model  → MAE: {mae:.2f}  | RMSE: {rmse:.2f}  | Gap: {rmse-mae:.2f}")
    print(f"Outlier Model → MAE: {mae2:.2f} | RMSE: {rmse2:.2f} | Gap: {rmse2-mae2:.2f}")
    print("Notice how RMSE explodes for outlier model but MAE stays modest")

    # --- Plot ---
    fig, axes = plt.subplots(1, 2, figsize=(14, 5))

    # Plot 1 - Actual vs Predicted with error lines
    axes[0].plot(range(1, 6), actual,    label='Actual',    marker='o', linewidth=2)
    axes[0].plot(range(1, 6), predicted, label='Predicted', marker='s', linewidth=2, linestyle='--')
    for i in range(5):
        axes[0].vlines(i+1, min(actual[i], predicted[i]),
                            max(actual[i], predicted[i]),
                            colors='red', linewidth=2, alpha=0.6)
    axes[0].set_title(f'Actual vs Predicted\nMAE={mae:.2f} | RMSE={rmse:.2f}')
    axes[0].set_xlabel('House')
    axes[0].set_ylabel('Price (Lakhs)')
    axes[0].legend()
    axes[0].grid(True)

    # Plot 2 - MAE vs RMSE bar comparison across both models
    metrics  = ['MAE', 'RMSE']
    normal   = [mae, rmse]
    outlier  = [mae2, rmse2]
    x        = np.arange(len(metrics))
    width    = 0.35

    axes[1].bar(x - width/2, normal,  width, label='Normal Model',  color='steelblue', alpha=0.8)
    axes[1].bar(x + width/2, outlier, width, label='Outlier Model', color='tomato',    alpha=0.8)
    axes[1].set_title('MAE vs RMSE — Normal vs Outlier Model')
    axes[1].set_xticks(x)
    axes[1].set_xticklabels(metrics)
    axes[1].set_ylabel('Error Value')
    axes[1].legend()
    axes[1].grid(True, axis='y')

    plt.tight_layout()
    plt.savefig('rmse_plot.png')
    plt.show()
    print("\nPlot saved!")


Output:

=== Manual Calculation === Errors : [5, -5, 2, -5, 5] Squared Errors : [25, 25, 4, 25, 25] MSE : 20.8 RMSE : 4.5607 RMSE (numpy) : 4.5607 RMSE (sklearn) : 4.5607 === MAE vs RMSE Gap Analysis === MAE : 4.4000 RMSE : 4.5607 Gap : 0.1607 (small - model is consistent) === Normal Model vs Outlier Model === Normal Model → MAE: 4.40 | RMSE: 4.56 | Gap: 0.16 Outlier Model → MAE: 8.20 | RMSE: 17.89 | Gap: 9.69 Notice how RMSE explodes for outlier model but MAE stays modest Plot saved!

How to Read RMSE in Real Projects

RMSE is in the same unit as your target. So interpretation is direct:

Target Variable

RMSE = 4.56 means

House Price (Lakhs)

Wrong by ₹4.56L on average (with big error penalty)

Temperature (°C)

Wrong by 4.56°C on average

Sales (units)

Wrong by 4.56 units on average

Quick sanity check in code:


    rmse = np.sqrt(mean_squared_error(y_test, y_pred))

    target_range = y_test.max() - y_test.min()
    rmse_pct     = (rmse / target_range) * 100

    print(f"RMSE             : {rmse:.2f}")
    print(f"Target Range     : {target_range:.2f}")
    print(f"RMSE as % range  : {rmse_pct:.1f}%")

    # Rule of thumb
    if rmse_pct < 10:
        print("Model is very good")
    elif rmse_pct < 20:
        print("Model is decent")
    else:
        print("Model needs improvement")


MAE vs MSE vs RMSE — Full Picture

MAE

MSE

RMSE

Formula

avg of |errors|

avg of errors²

√MSE

Unit

Same as target

Squared

Same as target

Big error penalty

No

Yes, very heavy

Yes, heavy

Outlier sensitive

No — robust

Very sensitive

Sensitive

Interpretable

Best

Worst

Good

Used as loss function

Sometimes

Yes

Sometimes

Use when

Outliers exist

Training models

Evaluating models


The Golden Rule in Real Projects


    # Always report all three together
    mae  = mean_absolute_error(y_test, y_pred)
    mse  = mean_squared_error(y_test, y_pred)
    rmse = np.sqrt(mse)

    print(f"MAE  : {mae:.2f}")    # average error, simple
    print(f"MSE  : {mse:.2f}")    # for reference, used in training
    print(f"RMSE : {rmse:.2f}")   # main reporting metric

    # Then check the gap
    print(f"Gap (RMSE - MAE): {rmse - mae:.2f}")
    # Small gap = consistent model
    # Large gap = outlier errors hiding somewhere

In job interviews and real projects — RMSE is the most commonly reported regression metric. MAE is used when you need simplicity or have lots of outliers. MSE is mostly seen inside model training.


One Line Summary

RMSE is MSE with a square root — it keeps MSE's ability to punish large errors heavily, but brings the unit back to the same scale as your data, making it the most widely used and reported regression evaluation metric.

No comments:

Post a Comment

Mean — Complete Chapter for ML & Statistics

What is Mean? We calculate the mean (average) because it gives a single value that represents the whole dataset. Why we need mean (benefits...