How do I calculate SMAPE using Python?

SMAPE (Symmetric Mean Absolute Percentage Error) is a commonly used metric for measuring the accuracy of forecasting models. It takes into account both the magnitude and direction of errors, making it a more robust measure compared to other error metrics. To calculate SMAPE using Python, one can follow these steps:

1. Import the necessary libraries such as numpy and pandas.
2. Load the actual and forecasted values into separate arrays or dataframes.
3. Calculate the absolute difference between the actual and forecasted values.
4. Calculate the sum of absolute differences.
5. Calculate the mean of actual and forecasted values.
6. Multiply the sum of absolute differences by 2 and divide it by the sum of actual and forecasted values.
7. Multiply the result by 100 to get the SMAPE value.
8. Print or return the SMAPE value.

By following these steps and using appropriate syntax, one can easily calculate SMAPE using Python and evaluate the accuracy of their forecasting models.

Calculate SMAPE in Python


The symmetric mean absolute percentage error (SMAPE) is used to measure the predictive accuracy of models. It is calculated as:

SMAPE = (1/n) * Σ(|forecast – actual| / ((|actual| + |forecast|)/2) * 100

where:

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

This tutorial explains how to calculate SMAPE in Python.

How to Calculate SMAPE in Python

There is no built-in Python function to calculate SMAPE, but we can create a simple function to do so:

import numpy as npdef smape(a, f):
    return 1/len(a) * np.sum(2 * np.abs(f-a) / (np.abs(a) + np.abs(f))*100)

We can then use this function to calculate the SMAPE for two arrays: one that contains the actual data values and one that contains the forecasted data values.

#define arrays of actual and forecasted data values
actual = np.array([12, 13, 14, 15, 15,22, 27])
forecast = np.array([11, 13, 14, 14, 15, 16, 18])

#calculate SMAPE
smape(actual, forecast)

12.45302

From the results we can see that the symmetric mean absolute percentage error for this model is 12.45302%.

Additional Resources

Wikipedia Entry for SMAPE
Rob J. Hyndman’s thoughts on SMAPE
How to Calculate MAPE in Python
How to Calculate MAPE in R
How to Calculate MAPE in Excel

x