- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Root Mean Square Error (RMSE) in MATLAB
Root Mean Square Error (RMSE) is an error estimation technique used to calculate the difference between estimated values and actual values. This method provides the average value of errors as a single value. We can use MATLAB to calculate the root mean square error. For this, MATLAB provides various built-in functions. In this tutorial, I will explain how to calculate the Root Mean Square Error (RMSE) in MATLAB.
What is Root Mean Square Error (RMSE)?
The root mean square error (RMSE) is a method of measuring error or accuracy of a predictive tool or model. It is calculated by finding the difference between estimated values and actual values. This method provides a way to represent the average of errors as a single value.
Mathematically, we can use the following formula to calculate the root mean square error between estimated value and actual value −
$$\mathrm{RMSE \: = \: \sqrt{\frac{1}{n}\sum_{i=1}^{n}(x_{i} \: − \:y_{i})^{2}}}$$
Where, n is the number of data points, $\mathrm{x_{i}}$ is the predicted value of the $\mathrm{i^{th}}$ data point, and $\mathrm{y_{i}}$ is the actual value of the $\mathrm{i^{th}}$ data point.
How to Calculate Root Mean Square Error?
We can follow the four steps given below to calculate the root mean square error −
Step 1 − Calculate the difference between estimated/predicted values and actual values which gives the error between them.
$$\mathrm{e_{i} \: = \: x_{i} \: − \: y_{i}}$$
Step 2 − Calculate the square of the error to eliminate the effect of positive or negative differences.
$$\mathrm{Square \: of \: error \: = \: e_{i}^{2} }$$
Step 3 − Calculate the average value of squared errors to find the mean square error.
$$\mathrm{MSE \: = \: \frac{\sum_{i=1}^{n} e_{i}^{2}}{n}}$$
Step 4 − Calculate the root mean square error by taking the square root of MSE, i.e.,
$$\mathrm{RMSE \: = \: \sqrt{MSE}}$$
The root mean square error is the measure of accuracy and quality of a predictive model. Thus, if a predictive model has lower values of root mean square error, then it is a better model in terms of performance.
How to Calculate Root Mean Square Error in MATLAB?
MATLAB provides a built-in function "rmse" to calculate the root mean square error. This function allows us to find the value of RMSE in an easy way. This function can have different syntax formats based on needs, they are −
e = rmse(F, A);
e = rmse(F, A, "all");
e = rmse(F, A, dim);
e = rmse(F, A, nanflag);
e = rmse(F, A, 'Weight', W);
In these functions, F is the array of predicated or forecasted values and A is the array of actual values.
Let us discuss different cases of calculating the root mean square error in MATLAB using these functions.
Calculate Root Mean Square Error Manually
In MATLAB, we can implement the formula of the root mean square error. This involves the implementation of steps explained above.
Example
The following MATLAB example shows how to calculate the root mean square error manually.
% MATLAB code to calculate RMSE manually % Create matrices of sample data A = [3, 5, 2, 1, 6]; % Actual array F = [2, 6, 1, 2, 7]; % Predicted array % Calculate the squared error between A and F e = (F - A).^2; % Calculate the mean square error MSE = mean(e); % Calculate the root mean square error RMSE = sqrt(MSE); % Display the root mean square error disp(['Root Mean Square Error (RMSE): ', num2str(RMSE)]);
Output
It will produce the following output −
Root Mean Square Error (RMSE): 1
This example demonstrates the manual method of calculating the root mean square error using MATLAB.
Let now discuss the methods of finding the root mean square error using the "rmse" function.
Calculate Root Mean Square Error between Two Datasets
In MATLAB, we can use the "rmse(F, A)" function to calculate the root mean square error between two datasets.
Example
Let us understand this method of finding the root mean square error with the help of an example.
% MATLAB code to calculate RMSE between two datasets % Create arrays of sample data F = [5, 3, 9, 2, 8]; % Predicted array A = [6, 2, 8, 3, 9]; % Actual array % Calculate the root mean square error e = rmse(F, A); % Display the root mean square error disp(['Root Mean Square Error (RMSE): ', num2str(e)]);
Output
It will produce the following output −
Root Mean Square Error (RMSE): 1
Code Explanation − In this example, firstly we input the arrays of actual and predicted values as "A" and "F" respectively. Then, we calculate the root mean square error using the "rmse(F, A)" function and store the result in a variable "e". Finally, we use the "disp" function to display the result.
Calculate Root Mean Square Error for all Elements
In MATLAB, we can use the "rmse(F, A, "all")" function to calculate the root mean square error for all elements in the arrays F and A.
Example
Let us take an example to understand this method of calculating the RMSE in MATLAB.
% MATLAB code to calculate RMSE for all elements % Create arrays of sample data F = [6, 4, 7, 1, 3]; % Predicted array A = [7, 1, 4, 4, 1]; % Actual array % Calculate the root mean square error for all elements e = rmse(F, A, "all"); % Display the root mean square error disp(['Root Mean Square Error (RMSE): ', num2str(e)]);
Output
It will produce the following output −
Root Mean Square Error (RMSE): 2.5298
Code Explanation − In this example, the code implementation is similar to the previous example. Here, we have specified the "all" option to calculate the root mean square error for all elements of the arrays A and F.
Calculate Root Mean Square Error along Specified Dimension
In MATLAB, we can also use the "rmse()" function to calculate the root mean square error along a specified dimension of the arrays of actual and predicated values. For that, the following syntax of the "rmse()" function is used −
e = rmse(F, A, dim);
Here, we set dim = 1 to calculate the root mean square error along the columns and dim = 2 to calculate the error along the rows.
Example
Let understand this function with the help of an example in MATLAB.
% MATLAB code to calculate RMSE along specified dimension % Create arrays of sample data F = [6, 4; 7, 3; 3, 1]; % Predicted array A = [4, 1; 2, 5; 4, 1]; % Actual array % Calculate the root mean square error along specified dimensions e1 = rmse(F, A, 1); % Calculating along columns e2 = rmse(F, A, 2); % Calculating along rows % Display the root mean square errors disp('Root Mean Square Error (RMSE) along Rows: '); disp(e1); disp('Root Mean Square Error (RMSE) along Columns: '); disp(e2);
Output
It will produce the following output −
Root Mean Square Error (RMSE) along Rows: 3.1623 2.0817 Root Mean Square Error (RMSE) along Columns: 2.5495 3.8079 0.7071
Code Explanation − In this example, I have demonstrated the way of calculating the root mean square error along the rows and columns of the arrays A and F.
Calculate Root Mean Square Error between Arrays with NaN Values
MATLAB also provides a way for handling the NaN (Not a Number) values in the data while calculating the root mean square error. For this, we use the following syntax of the "rsme" function −
e = rmse(F, A, nanflag);
In this function, if nanflag = omitnan, the NaN values will be excluded while calculating the root mean square error. If nanflag = includenan, the NaN values will be included in the calculation.
Example
Let us see an example to understand how this function works.
% MATLAB code to calculate RMSE with NaN values % Create arrays of sample data F = [6, 4, NaN, 3, 5, 1]; % Predicted array A = [4, 2, 1, 5, 4, 1]; % Actual array % Calculate the root mean square error with nanflag e1 = rmse(F, A, 'omitnan'); % Calculating with omitnan e2 = rmse(F, A, 'includenan'); % Calculating with includenan % Display the root mean square errors disp('Root Mean Square Error (RMSE) with Excluded NaN Values: '); disp(e1); disp('Root Mean Square Error (RMSE) with Included NaN Values: '); disp(e2);
Output
It will produce the following output −
Root Mean Square Error (RMSE) with Excluded NaN Values: 1.6125 Root Mean Square Error (RMSE) with Included NaN Values: NaN
Code Explanation − The code implementation of this program is same as the previous. This example shows how can can calculate the root mean square error in MATLAB when the arrays F and A contains a NaN value.
Calculate Root Mean Square Error with Inclusion of Weights
We can also use the "rmse()" function to calculate the root mean square error with inclusion of weights in the calculation. For this, we use the following syntax of the "rmse()" function −
e = rmse(F, A, 'Weight', W);
Here, "W" is a vector of weights.
Example
Let us see the use of this function in MATLAB programming to calculate the root mean square error.
% MATLAB code to calculate RMSE with inclusion of weights % Create arrays of sample data F = [6, 4, 7, 3, 5, 1]; % Predicted array A = [4, 2, 1, 5, 4, 1]; % Actual array W = [0.5, 0.1, 0.2, 0.1, 0.2, 0.1]; % Weight array % Calculate the root mean square error e = rmse(F, A, 'Weight', W); % Display the root mean square error disp('Root Mean Square Error (RMSE) with Inclusion of Weights: '); disp(e);
Output
It will produce the following output −
Root Mean Square Error (RMSE) with Inclusion of Weights: 2.9155
Code Explanation − In this example, we have used a third vector containing weights that we want to include in the root mean square error. In the "rmse" function, we have used this weight vector "W" by specifying the "Weight" option.
Conclusion
In conclusion, the root mean square error is a value that represents the difference between actual values and estimated values. We can calculate the root mean square error using manual method or built-in function in MATLAB.
The built-in function used to calculate the root mean square value in MATLAB is "rmse()". This function takes the arrays of predicted values and actual values as input, and then gives a single value which is the root mean square error between these two arrays. In this tutorial, I explained the different methods of calculating the root mean square error (RMSE) using MATLAB with the help of examples.