# 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.96083754,"math_prob":0.86364573,"size":2234,"snap":"2019-43-2019-47","text_gpt3_token_len":462,"char_repetition_ratio":0.10224215,"word_repetition_ratio":0.0,"special_character_ratio":0.20993733,"punctuation_ratio":0.100238666,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9601751,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-11-20T16:34:44Z\",\"WARC-Record-ID\":\"<urn:uuid:87ee99d7-8d0a-4d93-b33f-11c99b10dad6>\",\"Content-Length\":\"150248\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a42ddaf2-09a7-4e21-8216-d27d40886b47>\",\"WARC-Concurrent-To\":\"<urn:uuid:f4344ef6-747b-4d44-b894-eccb36a6cc12>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://stats.stackexchange.com/questions/10378/using-holt-winters-for-forecasting-in-python/10422\",\"WARC-Payload-Digest\":\"sha1:N33SFYMECD4U5DRSEGRSSJRMDRPS6TF5\",\"WARC-Block-Digest\":\"sha1:4DC357FWNFIXV5PAK2JTYULJBW5UOHOE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-47/CC-MAIN-2019-47_segments_1573496670597.74_warc_CC-MAIN-20191120162215-20191120190215-00008.warc.gz\"}"}