rmse

Root-mean-square error between arrays

Description

example

E = rmse(F,A) returns the root-mean-square error (RMSE) between the forecast (predicted) array F and the actual (observed) array A.

• F and A must either be the same size or have sizes that are compatible.

• If F and A are vectors of the same size, then E is a scalar.

• If F-A is a matrix, then E is a row vector containing the RMSE for each column.

• If F and A are multidimensional arrays, then E contains the RMSE computed along the first array dimension of size greater than 1, with elements treated as vectors. The size of E in this dimension is 1, while the sizes of all other dimensions are the same as in F-A.

E = rmse(F,A,"all") returns the RMSE of all elements in F and A.

example

E = rmse(F,A,dim) operates along the dimension dim. For example, if F and A are matrices, then rmse(F,A,2) operates on the elements in each row and returns a column vector containing the RMSE of each row.

example

E = rmse(F,A,vecdim) operates along the dimensions specified in the vector vecdim. For example, if F and A are matrices, then rmse(F,A,[1 2]) operates on all the elements in F and A because every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

example

E = rmse(___,nanflag) specifies whether to include or omit NaN values in the calculation. Specify nanflag after all other input arguments from the previous syntaxes. For example, rmse(F,A,"includenan") includes the NaN values in the calculation, while rmse(F,A,"omitnan") ignores them.

example

E = rmse(___,Weight=W) specifies a weighting scheme W and returns the weighted RMSE. If W is a vector, its length must equal the length of the operating dimension. If W is a matrix or multidimensional array, it must have the same dimensions as F, A, or F-A. You cannot specify a weighting scheme if you specify vecdim or "all".

Examples

collapse all

Create two column vectors of forecast (predicted) data and one column vector of actual (observed) data.

F1 = [1; 10; 9];
F2 = [2; 5; 10];
A = [1; 9; 10];

Compute the RMSE between each forecast and the actual data.

E1 = rmse(F1,A)
E1 = 0.8165
E2 = rmse(F2,A)
E2 = 2.3805

Alternatively, create a matrix containing both forecasts and compute the RMSE between each forecast and the actual data in one command.

F = [F1 F2]
F = 3×2

1     2
10     5
9    10

E = rmse(F,A)
E = 1×2

0.8165    2.3805

The first element of E is the RMSE between the first forecast column and the actual data. The second element of E is the RMSE between the second forecast column and the actual data.

Create a matrix of forecast data and a matrix of actual data.

F = [17 19; 1 6; 16 15];
A = [17 25; 3 4; 16 13];

Compute the RMSE between the forecast and the actual data across each row by specifying the operating dimension as 2. The smallest RMSE corresponds to the RMSE between the third rows of the forecast data and actual data.

E = rmse(F,A,2)
E = 3×1

4.2426
2.0000
1.4142

Create a 3-D array with pages containing forecast data and a matrix of actual data.

F(:,:,1) = [2 4; -2 1];
F(:,:,2) = [4 4; 8 -3];
A = [6 7; 1 4];

Compute the RMSE between the predicted data in each page of the forecast array and the actual data matrix by specifying a vector of operating dimensions 1 and 2.

E = rmse(F,A,[1 2])
E =
E(:,:,1) =

3.2787

E(:,:,2) =

5.2678

The first page of E contains the RMSE between the first page of F and the matrix A. The second page of E contains the RMSE between the second page of F and the matrix A.

Create a row vector of forecast data and a row vector of actual data containing a missing value.

F = [1 6 10 5];
A = [2 6 NaN 3];

Compute the RMSE between the forecast and the actual data.

E = rmse(F,A)
E = NaN

Because NaN values are included in the RMSE calculation by default, the result is NaN. Ignore the missing value in the input data by specifying "omitnan". Now, the function computes the RMSE for only the first, second, and fourth columns of the input data.

Eomit = rmse(F,A,"omitnan")
Eomit = 1.2910

Create a forecast column vector and an actual column vector.

F = [2; 10; 13];
A = [1; 9; 10];

Compute the RMSE between the forecast and actual data according to a weighting scheme specified by W.

W = [0.5; 0.25; 0.25];
E = rmse(F,A,Weight=W)
E = 1.7321

Input Arguments

collapse all

Forecast or predicted array, specified as a vector, matrix, or multidimensional array.

Inputs F and A must either be the same size or have sizes that are compatible. For example, F is an m-by-n matrix and A is a 1-by-n row vector. For more information, see Compatible Array Sizes for Basic Operations.

Data Types: single | double
Complex Number Support: Yes

Actual or observed array, specified as a vector, matrix, or multidimensional array.

Inputs F and A must either be the same size or have sizes that are compatible. For example, F is an m-by-n matrix and A is a 1-by-n row vector. For more information, see Compatible Array Sizes for Basic Operations.

Data Types: single | double
Complex Number Support: Yes

Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension of size greater than 1.

The size of E in the operating dimension is 1. All other dimensions of E have the same size as the result of F-A.

For example, consider four forecasts in a 3-by-4 matrix, F, and actual data in a 3-by-1 column vector, A:

• rmse(F,A,1) computes the RMSE of the elements in each column and returns a 1-by-4 row vector.

The size of E in the operating dimension is 1. The difference of F and A is a 3-by-4 matrix. The size of E in the nonoperating dimension is the same as the second dimension of F-A, which is 4. The overall size of E becomes 1-by-4.

• rmse(F,A,2) computes the RMSE of the elements in each row and returns a 3-by-1 column vector.

The size of E in the operating dimension is 1. The difference of F and A is a 3-by-4 matrix. The size of E in the nonoperating dimension is the same as the first dimension of F-A, which is 3. The overall size of E becomes 3-by-1.

Vector of dimensions to operate along, specified as a vector of positive integers. Each element represents a dimension of the input arrays. The size of E in the operating dimensions is 1. All other dimensions of E have the same size as the result of F-A.

For example, consider forecasts in a 2-by-3-by-3 array, F, and actual data in a 1-by-3 row vector, A:

• rmse(F,A,[1 2]) computes the RMSE over each page of F and returns a 1-by-1-by-3 array.

The size of E in the operating dimensions is 1. The difference of F and A is a 2-by-3-by-3 array. The size of E in the nonoperating dimension is the same as the third dimension of F-A, which is 3. The overall size of E becomes 1-by-1-by-3.

NaN condition, specified as one of these values:

• "includenan" — Include NaN values when computing the RMSE. If any elements in F or A are NaN, then the result is NaN.

• "omitnan" — Ignore all NaN values in the input. If all elements in F, A, or W are NaN, then the result is NaN.

Weighting scheme, specified as a vector, matrix, or multidimensional array. The elements of W must be nonnegative.

If W is a vector, it must have the same length as the operating dimension. If W is a matrix or multidimensional array, it must have the same dimensions as F, A, or F-A.

You cannot specify this argument if you specify vecdim or "all".

Data Types: single | double

Output Arguments

collapse all

Root-mean-square error, returned as a scalar, vector, matrix, or multidimensional array. The default size of E is as follows.

• If F and A are vectors of the same size, then E is a scalar.

• If F-A is a matrix, then E is a row vector containing the RMSE for each column.

• If F and A are multidimensional arrays, then E contains the RMSE computed along the first array dimension of size greater than 1, with elements treated as vectors. The size of E in this dimension is 1, while the sizes of all other dimensions are the same as in F-A.

collapse all

Root-Mean-Square Error

For a forecast array F and actual array A made up of n scalar observations, the root-mean-square error is defined as

$E=\sqrt{\frac{1}{n}\sum _{i=1}^{n}{|{A}_{i}-{F}_{i}|}^{2}}$

with the summation performed along the specified dimension.

Weighted Root-Mean-Square Error

For a forecast array F and actual array A made up of n scalar observations and weighting scheme W, the weighted root-mean-square error is defined as

${E}_{W}=\sqrt{\frac{\sum _{i=1}^{n}{W}_{i}{|{A}_{i}-{F}_{i}|}^{2}}{\sum _{i=1}^{n}{W}_{i}}}$

with the summation performed along the specified dimension.

Version History

Introduced in R2022b