0

I have the following code to perform an out-of-sample assessment of a time series. The idea is to perform a recursive and rolling method to calculate MAPE and MSPE.

The code is as follows:

long = len(y)

n_estimation = 83
real = y[(n_estimation):len(y)]

n_forecasting = long - n_estimation
horizontes = 2

predicc = np.zeros((horizontes,n_forecasting))
MSFE = np.zeros((horizontes, 1))
MAPE = np.zeros((horizontes, 1))

for Periods_ahead in range(horizontes):
    for i in range(0,n_forecasting):
        aux_y = y[0:(n_estimation - Periods_ahead + i)]
        model = SARIMAX(endog = aux_y, order = (1,1,0), seasonal_order = (1,1,0,4))
        model_fit=model.fit(disp=0)
        y_pred = fit.forecast(Periods_ahead + 1)
        predicc[Periods_ahead][i] = y_pred[0][Periods_ahead]
       
    error = np.array(real) - predicc[Periods_ahead]
    MSFE[Periods_ahead] = np.mean(error**2)
    MAPE[Periods_ahead] = np.mean(np.abs(error/np.array(real))) * 100

df_pred = pd.DataFrame({"V1":predicc[0], "V2":predicc[1]})


print("MSFE",MSFE)
print("MAPE %",MAPE)

I am getting the following error, most likely related to using a newer version of SARIMAX.

ValueError                                Traceback (most recent call last)
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pandas\core\indexes\range.py:392, in RangeIndex.get_loc(self, key, method, tolerance)
    391 try:
--> 392     return self._range.index(new_key)
    393 except ValueError as err:

ValueError: 0 is not in range

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
c:\Users\dianaf\OneDrive - Microsoft\Documents\GitHub\big_data_operations\Homework2.ipynb Cell 36 in <cell line: 13>()
     17 model_fit=model.fit(disp=0)
     18 y_pred = fit.forecast(Periods_ahead + 1)
---> 19 predicc[Periods_ahead][i] = y_pred[0][Periods_ahead]

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pandas\core\series.py:982, in Series.__getitem__(self, key)
    979     return self._values[key]
    981 elif key_is_scalar:
--> 982     return self._get_value(key)
    984 if is_hashable(key):
    985     # Otherwise index.get_value will raise InvalidIndexError
    986     try:
    987         # For labels that don't resolve as scalars like tuples and frozensets
...
--> 394         raise KeyError(key) from err
    395 self._check_indexing_error(key)
    396 raise KeyError(key)

KeyError: 0

Any idea how to fix it without downgrading to previous versions of statsmodel?

Thank you!

diana
  • 3
  • 1

0 Answers0