Introduction >

# Root Mean Square Error (RMSE): The cornerstone for evaluating regression models Or Jacobi

Or is a software engineer at Aporia and an avid gaming enthusiast "All I need is a cold brew and the controller in my hand, and I'm good to go."

7 min read Aug 15, 2023

Today’s spotlight is on Root Mean Square Error (RMSE) – a pivotal evaluation metric commonly used in regression problems. Through the lens of our Production ML Academy, we’ll peel back the layers of RMSE, probing its purpose and practicality across applications such as sales forecasting, energy consumption prediction, and medical data analysis. Let’s also examine how this metric fits snugly into the production lifecycle of ML systems.

### What is RMSE (Root Mean Square Error)?

The Root Mean Square Error (RMSE) is an oft-employed measure to gauge the prediction errors of a regression model. In essence, it tells us about the distribution of the residuals (prediction errors). A lower RMSE is indicative of a better fit for the data.

#### RMSE Formula

RMSE is mathematically represented as:

In simpler terms, it’s the square root of the mean of the squared differences between the prediction and actual observation. This measure emphasizes larger errors over smaller ones, thus providing a more conservative estimate of model accuracy when large errors are particularly undesirable.

### A practical example

To make the concept of RMSE more relatable, let’s explore a straightforward example. We have a model that predicts the daily energy consumption of a building.

Here are some hypothetical data for five days:

Day 1: Actual = 500 units, Predicted = 520 units

Day 2: Actual = 600 units, Predicted = 570 units

Day 3: Actual = 580 units, Predicted = 590 units

Day 4: Actual = 650 units, Predicted = 630 units

Day 5: Actual = 700 units, Predicted = 710 units

By applying the RMSE formula, we find that the RMSE for the model’s predictions over these five days is approximately 19.49 units. This suggests that, on average, the model’s predictions deviate from the actual values by around 19.49 units, with larger errors being weighted more heavily.

Here’s how we would calculate the RMSE in Python for the data provided above:

import numpy as np

# Actual values
actual = np.array([500, 600, 580, 650, 700])

# Predicted values
predicted = np.array([520, 570, 590, 630, 710])

# Calculate the difference between predicted and actual
difference = predicted - actual

# Square the differences
squared_difference = difference ** 2

# Compute the mean squared difference
mean_squared_difference = np.mean(squared_difference)

# Finally, take the square root of the mean squared difference to get the RMSE
rmse = np.sqrt(mean_squared_difference)

print(f"The RMSE of the model's predictions over these five days is approximately {rmse:.2f} units.")

When you run this script, it outputs: The RMSE of the model’s predictions over these five days is approximately 19.49 units.

To highlight the difference between RMSE and Mean Absolute Error (MAE), let’s calculate the MAE for the same set of data. The MAE is calculated as the average absolute difference between the actual and predicted values.

## Distinguishing RMSE from MAE (Mean Absolute Error)

While both RMSE and MAE measure the difference between the predicted and observed values, RMSE puts more weight on larger errors due to the squaring operation. Consequently, RMSE is more sensitive to outlier values as compared to MAE.ֿ

Let’s highlight the difference between RMSE and Mean Absolute Error (MAE), by calculating the MAE for the same set of data. The MAE is calculated as the average absolute difference between the actual and predicted values.

In Python, this can be done as follows:

import numpy as np

# Actual values
actual = np.array([500, 600, 580, 650, 700])

# Predicted values
predicted = np.array([520, 570, 590, 630, 710])

# Calculate the absolute difference between predicted and actual
absolute_difference = np.abs(predicted - actual)

# Compute the mean absolute difference
mae = np.mean(absolute_difference)

print(f"The MAE of the model's predictions over these five days is approximately {mae:.2f} units.")

Output: The MAE of the model’s predictions over these five days is approximately 18.0 units. This indicates that, on average, our energy consumption predictions are off by around 18 units.

Comparing the two, we can see that the RMSE value is higher than the MAE value. This is because RMSE squares the differences before averaging them, thus giving more weight to larger errors. This makes RMSE a more conservative measure of model accuracy, especially when large errors are particularly undesirable.

## Practical Applications of RMSE

RMSE finds its footing in diverse domains where regression problems are at the forefront:

• Sales forecasting: Businesses often employ machine learning models to predict future sales based on historical data, including past sales, seasonal trends, promotional activities, and more. For example, let’s consider a clothing retailer predicting sales for the upcoming winter season. The model might be trained on features like sales data from previous years, the average temperature, the type and amount of marketing promotions, etc.

Once the predictions are made, the business can compare these predictions against the actual sales (when they occur) to measure the accuracy of the model. RMSE could be an excellent metric in this case because it would provide an estimate of how much the predicted sales figures deviate from the actual ones, in the original sales units. High RMSE would indicate large deviations and could suggest that the model’s predictions are not reliable, prompting further investigation and model tuning.
• Energy consumption prediction: Predicting energy consumption is a critical task for power companies for capacity planning and demand response purposes. Features in the predictive model might include weather data, time of day, day of the week, and historical energy consumption data.

Let’s say a power company uses such a model to predict the next day’s power consumption for a city. If the RMSE between the predicted and actual energy consumption is low, it indicates that the model is doing a good job and its predictions are trustworthy. However, a high RMSE would signify large discrepancies between the predicted and actual values, which could have significant implications, such as an overburdened or underutilized power grid.
• Medical data analysis: RMSE can be equally useful in healthcare, especially when predicting continuous outcomes. Let’s consider a model predicting patient recovery times based on features like the type of illness, age, pre-existing conditions, and treatment plan.

A lower RMSE in this scenario would mean the model’s predictions are generally close to the actual recovery times, indicating a well-performing model. This could aid in efficiently planning post-treatment care and hospital resource allocation. Conversely, a high RMSE would mean the model’s predictions are far off from the actual recovery times, signaling the need for model improvement.

## The Role of RMSE in Model Evaluation and Monitoring

RMSE has a central role in both the model evaluation phase and after the model is deployed, i.e., the monitoring phase. During model evaluation, RMSE serves as a measure to understand the model’s performance. Specifically, it reveals how close the predicted values are to the actual ones. An RMSE of zero indicates perfect predictions, which, in practice, is highly unlikely if not impossible.

In machine learning, RMSE is commonly used to compare the performance of different models on the same dataset. The model with the lowest RMSE is generally considered the best performer, although other metrics should also be considered for a comprehensive understanding of performance.

Once the model is deployed and starts making predictions on new data, RMSE becomes a key part of model monitoring. Continually computing and tracking RMSE for the predictions can help in identifying anomalies or a potential “concept drift” – situations where model performance degrades over time due to changes in the incoming data’s distribution.

## Pitfalls and Precautions with RMSE

While RMSE is an invaluable metric, it’s essential to bear in mind the following caveats:

• Susceptibility to Outliers: Because of the squaring of residuals, RMSE is more vulnerable to outliers than metrics like MAE.
• Scale Dependence: RMSE values are influenced by the scale of the target variable. Thus, comparing RMSE values across different datasets may lead to skewed conclusions.

## Wrapping up with RMSE

Through this guide, we’ve journeyed into the heart of RMSE, its calculation, significance, and practical application across various domains. While it’s a go-to metric for regression problems, it also has limitations and should be used judiciously alongside other evaluation metrics. The intelligent application and understanding of RMSE can significantly augment the effectiveness of production ML systems. 