Home > Software design >  How to fit Holt Winter’s model and forecast future outcomes in Python?
How to fit Holt Winter’s model and forecast future outcomes in Python?

Time:10-19

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?

CodePudding user response:

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

CodePudding user response:

You could also should try ARIMA model it usually gives the better performance and this code makes combinations of different ARIMA parameters (AR, autoregressive parameter; I, differencing parameter; and MA, moving average parameter; - p,d,q respectively) and finds the best combination of them by lowering the Akaike information criteria (AIK) which penalizes the maximum likelihood with number of parameters (i.e. finds the best likelihood, with the smallest number of parameters):

  from statsmodels.tsa.arima_model import ARIMA
import itertools
# Grid Search
p = d = q = range(0,3) # p, d, and q can be either 0, 1, or 2
pdq = list(itertools.product(p,d,q)) # gets all possible combinations of p, d, and q
combs = {} # stores aic and order pairs
aics = [] # stores aics
# Grid Search continued

for combination in pdq:
        try:
            model = ARIMA(train_data['Sales'], order=combination) # create all possible models
            model = model.fit()
            combs.update({model.aic : combination}) # store combinations
            aics.append(model.aic)
        except:
            continue
    best_aic = min(aics)
  • Related