Search code examples
pythonmachine-learningtime-seriesstatsmodels

How to fit Holt Winter’s model and forecast future outcomes in Python?


I've a dataset with 4 years of sales and trying to forecast sales for next five years. I've split the dataset into 36 months as training-set and 12 months as test-set. I have chosen Holt Winter’s method and written following code to test the model.

from statsmodels.tsa.api import ExponentialSmoothing

holt_winter = ExponentialSmoothing(np.asarray(train_data['Sales']), seasonal_periods=12, trend='add', seasonal='add')

hw_fit = holt_winter.fit()

hw_forecast = hw_fit.forecast(len(test_data))

plt.figure(figsize=(16,8))

plt.plot(train_data.index, train_data['Sales'], "b.-", label='Train Data')
plt.plot(test_data.index, test_data['Sales'], "ro-", label='Original Test Data')
plt.plot(test_data.index, hw_forecast, "gx-", label='Holt_Winter Forecast Data')
plt.ylabel('Score', fontsize=16)
plt.xlabel('Time', fontsize=16)
plt.legend(loc='best')
plt.title('Holt Winters Forecast', fontsize=20)
plt.show()

It seems the code is working fine, and probably correctly predicting outcome of test data set. However, I'm struggling to figure out how to code if I want predict sales for the next five year?


Solution

  • hw_fit.predict(start, end)
    

    will make prediction from step start to step end, with step 0 being the first value of the training data.
    forecast makes out-of-sample predictions. So these two are equivalent:

    hw_fit.forecast(steps)
    hw_fit.predict(len(train_data), len(train_data)+steps-1)
    

    So, since your model was trained with a monthly step, if you want to forecast n months after the training data, you can call the methods above with steps=n