Search code examples
pythonstatisticsstatsmodelsforecastingsmoothing

My MAPE (Mean Absolute Percentage Error) Function returns a number over 100 when I want a percentage based value


I have a data set of how much a Dollar is worth in Liras since 2002.

My task is to run Simple Exponential Smoothing on this data and calculate MAPE but my MAPE returns a value around 250(This changes if I change smoothing level).

I need a percentage based number which should be 0-100.

Here is my Python code

from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing, Holt
import numpy as np
import pandas as pd


def mean_absolute_percentage_error(y_true, y_pred):
    y_true, y_pred = np.array(y_true), np.array(y_pred)
    return np.mean(np.abs((y_true - y_pred) / y_true)) * 100


raw_data = pd.read_excel("arda.xlsx", sheet_name=0, parse_dates=['DATE'], index_col='ID')

data_open = raw_data['OPEN']
time_log = np.log(data_open)

ses = SimpleExpSmoothing(time_log).fit(smoothing_level=0.1)
ses1 = ses.forecast(len(time_log))

mape = mean_absolute_percentage_error(time_log, ses1)
print(mape)

And here is my output

249.08272600004295

Thanks a lot for any help!


Solution

  • "I need a percentage based number which should be 0-100."

    This isn't necessarily true. I checked your MAPE function and it is working as expected. If your prediction is 3.5x your actual, you will get 250% error. Plot your predictions vs your truth and I bet you will find that they are way different.

    import numpy as np
    
    def mean_absolute_percentage_error(y_true, y_pred):
        y_true, y_pred = np.array(y_true), np.array(y_pred)
        return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
    
    
    y_true = np.random.randn(100)
    y_pred = y_true * 3.5
    
    print(mean_absolute_percentage_error(y_true, y_pred))
    

    you should:

    from matplotlib import pyplot as plt
    
    plt.plot(y_true)
    plt.plot(y_pred)
    plt.legend(["true", "pred"])
    plt.show()