Skip to contents

This function computes the mean absolute percentage error.

Usage

mean_absolute_percentage_error(
  true,
  pred,
  weights = NULL,
  multi = c("average", "sum", "raw"),
  eps = 1e-10,
  rweights = NULL
)

Arguments

true

a vector (or a matrix) of observed values. If a matrix is provided, a multi-response is assumed

pred

a vector (or a matrix) of predicted values

weights

observation weights

multi

what to do when response has multiple output values

average

errors of multiple outputs are averaged to get a single value for each observation

sum

errors of multiple outputs are summed up to get a single value for each observation

raw

returns a vector containing one error for each output

eps

an arbitrary positive small number, used to correct the MAPE formula and avoid the division by zero

rweights

response weights

Value

the mean absolute percentage error, a positive double or a vector of positive double values, one for each response, if response has multiple output values and multi = "raw"

Details

The mean absolute percentage error (MAPE) is a measure of errors based on relative absolute values. The MAPE estimated over n observations is defined as

$$MAPE(y,\hat{y}) = \frac{1}{n}\sum_{i=1}^{n} \frac{\lvert y_{i} - \hat{y}_{i} \rvert}{\max{\lvert y_{i} \rvert, \epsilon}}$$

where \(y_{i}\) is the true value of the \(i\)-th sample, \(\hat{y}_{i}\) is the predicted value, and \(\epsilon\) is an arbitrary positive small number used to avoid a division by zero.

If observation weights are provided, then the weighted mean absolute percentage error is computed as

$$wMAPE(w,y,\hat{y}) = \frac{1}{\sum_{i=1}^{n} w_{i}}\sum_{i=1}^{n} w_{i} * \frac{\lvert y_{i} - \hat{y}_{i} \rvert}{\max{\lvert y_{i} \rvert, \epsilon}}$$

where \(w_{i}\) is the weighting factor assigned to the \(i\)-th observation. The best possible score is zero.

Author

Alessandro Barberis