# Using Holt-Winters for forecasting in Python

[I first posted this question to Stack Overflow here but didn't get any replies, so I thought I'd try over here. Apologies if reposting isn't allowed.]

I've been trying to use this implementation of the Holt-Winters algorithm for time series forecasting in Python but have run into a roadblock... basically, for some series of (positive) inputs, it sometimes forecasts negative numbers, which should clearly not be the case. Even if the forecasts are not negative, they are sometimes wildly inaccurate - orders of magnitude higher/lower than they should be. Giving the algorithm more periods of data to work with does not appear to help, and in fact often makes the forecast worse.

The data I'm using has the following characteristics, which might be problems:

• Very frequently sampled (one data point every 15 minutes, as opposed to monthly data as the example uses) - but from what I've read, the Holt-Winters algorithm shouldn't have a problem with that. Perhaps that indicates a problem with the implementation?

• Has multiple periodicities - there are daily peaks (i.e. every 96 data points) as well as a weekly cycle of weekend data being significantly lower than weekday data - for example weekdays can peak around 4000 but weekends peak at 1000 - but even when I only give it weekday data, I run into the negative-number problem.

Is there something I'm missing with either the implementation or my usage of the Holt-Winters algorithm in general? I'm not a statistician so I'm using the 'default' values of alpha, beta, and gamma indicated in the link above - is that likely to be the problem? What is a better way to calculate these values?

Or ... is there a better algorithm to use here than Holt-Winters? Ultimately I just want to create sensible forecasts from historical data here. I've tried single- and double-exponential smoothing but (as far as I understand) neither support periodicity in data.

I have also looked into using the R forecast package instead through rpy2 - would that give me better results? I imagine I would still have to calculate the parameters and so on, so it would only be a good idea if my current problem lies in the implementation of the algorithm...?

Any help/input would be greatly appreciated!

I think the R forecast package you mentioned is a better fit for this problem than just using Holt-Winters. The two functions you are interested in are ets() and auto.arima(). ets() will fit an exponential smoothing model, including Holt-Winters and several other methods. It will choose parameters (alpha, beta, and gama) for a variety of models and then return the one with the lowest AIC (or BIC if you prefer). auto.arima() works similarly.

However, as IrishStat pointed out, these kinds of models may not be appropriate for your analysis. In that case, try calculating some covariates, such as dummy variables for weekends, holidays, and their interactions. Once you've specified covariates that make sense, use auto.arima() to find a ARMAX model, and then forecast() to make predictions. You will probably end up with something much better than a simple Holt-Winters model in python with default parameters.

You should also note that both ets() and auto.arima can fit seasonal models, but you need to format your data as a seasonal time series. Let me know if you need any help with that.

• How might I go about calculating covariates?
– Wern
May 9, 2011 at 5:41
• @Wern: It sounds like you need AT LEAST 2 sets of covariates: a set of dummy variables for hour-of-day, and a variable representing weekend vs. weekday. In other words, you need a matrix of the following 0/1 variables: H1,H2,H3,H4...H23,Weekday, where H1=1 if hour==1, and is 0 otherwise. H2=2 if hour==2, and is 0 otherwise, etc. Weekday= 1 on weekdays and 0 on weekends. Additionally, you might include: an interaction between weekday and hour, day of week, month of year, holiday, and other interactions. Let me know if you need help doing this in R. Start with hour and weekday dummies.
– Zach
May 9, 2011 at 13:41
• @Wern: After re-reading your question, it sounds like you have 15-minute data, so rather than calculating H1-H23, you should calculate I1-I95, where "interval" counts the 15-minute intervals since the beginning of the day. Also, be sure to omit 1 variable from this set of dummies, e.g. H24 or I96. What kind of data are you looking at? Web traffic? Electricity load?
– Zach
May 9, 2011 at 13:44
• Hmm, I think I know what you mean. I'll try it out and let you know - thanks! The data is web traffic.
– Wern
May 9, 2011 at 23:31
• @Wern if you like my answer (or IrishStat's) feel free to accept one by clicking the green check mark.
– Zach
May 10, 2011 at 13:04

The problem might be that Holt-Winters is a specific model form and may not be applicable to your data. The HW Model assumes among other things the following. a) one and only one trend b) no level shifts in the data i.e. no intercept changes 3) that seasonal parameters do not vary over time 4) no outliers 5) no autoregressive structure or adaptive model structure 6)model errors that have constant variance And of course 7) that the history causes the future i.e. no incorporation of price/promotions.events etc as helping variables

From your description it appears to me that a mixed-frequency approach might be needed. I have seen time series problems where the hour-of-the-day effects and the day-of-the-week effects have significant interaction terms. You are trying to force your data into an inadequate i.e. not-generalized enough structure. Estimating parameters and choosing from a small set of models does not replace Model Identification. You might want to read a piece on the different approaches to Automatic Modeling at www.autobox.com/pdfs/catchword.pdf . In terms of a more general approach I would suggest that you consider an ARMAX model otherwise known as a Transfer Function which relaxes the afore-mentioned assumptions.

• Hmm you have some very good points ... my data also contains a lot of outliers and is subject to significant variation on events like Christmas or New Year. But what do you mean by 'autoregressive or adaptive model structure'? I'll give ARMAX a shot though, thanks!
– Wern
May 6, 2011 at 4:54
• @Wern An adaptive/autoregressive component exists when the "predictors" includel either memory (previous values) of the endogenous (Y) series or exogenous series OR lagged values of the error term. It is important to identify and model , with possible lead,contemporaneous and lag effects around EACH of the Holidays. Furthermore there may be assignable cause to specific days-of-the-month , dependent on the day-of-the-week. For example the 5th of the month may be important GIVEN that it was not a weekend. We have been able to successfully model/automatically identify this structure. May 6, 2011 at 10:41