Python Code on Holt-Winters Forecasting
By: Etqad Khan
Tags: Data Science, Artificial Intelligence, Forecasting, Machine Learning, Analytics Vidhya

Fitting the Data with Holt-Winters Exponential Smoothing

Now we shall fit this data on Single, Double, and Triple Exponential Smoothing respectively, and will see how it performs.

Before starting with the models, we shall first define the weight coefficient Alpha and the Time Period. We also set the DateTime frequency to a monthly level.

# Set the frequency of the date time index as Monthly start as indicated by the data

airline.index.freq = ‘MS’ # Set the value of Alpha and define m (Time Period)

m = 12

alpha = 1/(2*m)

Now, we will fit the data on the Single Exponential Smoothing,

Single HWES

airline[‘HWES1’] = SimpleExpSmoothing(airline[‘Thousands of Passengers’]).fit(smoothing_level=alpha,optimized=False,use_brute=True).fittedvalues airline[[‘Thousands of Passengers’,’HWES1']].plot(title=’Holt Winters Single Exponential Smoothing’);

As expected, it didn’t fit quite well, and rightfully so, because if we remember, Single ES doesn’t work for data with Trends and Seasonality.

Double HWES

So, we’d fit the data on Double ES, on both Additive and Multiplicative Trend,

airline[‘HWES2_ADD’] = ExponentialSmoothing(airline[‘Thousands of Passengers’],trend=’add’).fit().fittedvalues airline[‘HWES2_MUL’] = ExponentialSmoothing(airline[‘Thousands of Passengers’],trend=’mul’).fit().fittedvalues airline[[‘Thousands of Passengers’,’HWES2_ADD’,’HWES2_MUL’]].plot(title=’Holt Winters Double Exponential Smoothing: Additive and Multiplicative Trend’);

Well, this looks a little better, but since we know there is Seasonality, we shall move into Triple ES and look at how it fits.

Triple HWES

airline[‘HWES3_ADD’] = ExponentialSmoothing(airline[‘Thousands of Passengers’],trend=’add’,seasonal=’add’,seasonal_periods=12).fit().

fittedvalues airline[‘HWES3_MUL’] = ExponentialSmoothing(airline[‘Thousands of Passengers’],trend=’mul’,seasonal=’mul’,seasonal_periods=12).fit().

fittedvalues airline[[‘Thousands of Passengers’,’HWES3_ADD’,’HWES3_MUL’]].plot(title=’Holt Winters Triple Exponential Smoothing: Additive and Multiplicative Seasonality’);

This looks promising! Here we have plotted for both Additive and Multiplicative Seasonality.

Forecasting with Holt-Winters Exponential Smoothing (Triple ES)

Let’s try and forecast sequences, let us start by dividing the dataset into Train and Test Set. We have taken 120 data points as Train set and the last 24 data points as Test Set.

forecast_data = pd.read_csv(‘airline_passengers.csv’,index_col=’Month’,

parse_dates=True)

forecast_data.index.freq = ‘MS’

# Split into train and test set

train_airline = forecast_data[:120]

test_airline = forecast_data[120:]

What are we even waiting for, Let’s fit and forecast,

fitted_model = ExponentialSmoothing(train_airline[‘Thousands of Passengers’],trend=’mul’,seasonal=’mul’,seasonal_periods=12).fit()

test_predictions = fitted_model.forecast(24) train_airline[‘Thousands of Passengers’].plot(legend=True,label=’TRAIN’)

test_airline[‘Thousands of Passengers’].plot(legend=True,label=’TEST’,figsize=(6,4))

test_predictions.plot(legend=True,label=’PREDICTION’) plt.title(‘Train, Test and Predicted Test using Holt Winters’)

As we close in on the forecasted segment, it seems that the model has done a decent job overall.