Home » How to Calculate Mean Squared Error (MSE) in MATLAB

How to Calculate Mean Squared Error (MSE) in MATLAB

by Tutor Aspire

One of the most common metrics used to measure the forecast accuracy of a model is MSE, which stands for mean squared error.

It is calculated as:

MSE = (1/n) * Σ(actual – forecast)2

where:

  • Σ – a fancy symbol that means “sum”
  • n – sample size
  • actual – the actual data value
  • forecast – the forecasted data value

The lower the value for MSE, the better a model is able to forecast values accurately.

To calculate MSE in MATLAB, we can use the mse(X, Y) function.

The following example shows how to use this function in practice.

Example: How to Calculate MSE in MATLAB

Suppose we have the following two arrays in MATLAB that show the actual values and the forecasted values for some model:

%create array of actual values and array of predicted values
actual = [34 37 44 47 48 48 46 43 32 27 26 24];
predicted = [37 40 46 44 46 50 45 44 34 30 22 23];

We can use the mse(X, Y) function to calculate the mean squared error (MSE) between the two arrays:

%calculate MSE between actual values and predicted values
mse(actual, predicted)

ans = 5.9167

The mean squared error (MSE) of this model turns out to be 5.917.

We interpret this to mean that the average squared difference between the predicted values and the actual values is 5.917.

We can compare this value to the MSE produced by other models to determine which model is “best.”

The model with the lowest MSE is the model that is best able to predict the actual values of the dataset.

Additional Resources

The following tutorials explain how to calculate mean squared error using other statistical software:

How to Calculate Mean Squared Error (MSE) in Excel
How to Calculate Mean Squared Error (MSE) in Python
How to Calculate Mean Squared Error (MSE) in R

You may also like