1

I am currently using the Statsmodels library for forecasting. I am trying to run triple exponential smoothing with seasonality, trend, and a smoothing factor, but keep getting errors. Here is a sample pandas series with the frequency already set:

2018-01-01 25
2018-02-01 30
2018-03-01 40
2018-04-01 38
2018-05-01 33
2018-06-01 36
2018-07-01 34
2018-08-01 35
2018-09-01 37
2018-10-01 41
2018-11-01 36
2018-12-01 32
2019-01-01 31
2019-02-01 29
2019-03-01 28
2019-04-01 29
2019-05-01 30 
Freq: MS, dtype:float64

Here is my code:

import pandas as pd
from statsmodels.tsa.holtwinters import ExponentialSmoothing, SimpleExpSmoothing

def triple_expo(input_data_set, output_list1):
    data = input_data_set
    model1 = ExponentialSmoothing(data, trend='add', seasonal='mul', damped=False, seasonal_periods=12, freq='M').fit(smoothing_level=0.1, smoothing_slope=0.1, smoothing_seasonal=0.1, optimized=False)
    fcast1 = model1.forecast(12)
    fcast_list1 = list(fcast1)
    output_list1.append(fcast_list1)

for product in unique_products:
    product_slice = sorted_product_df["Product"] == product
    unique_slice = sorted_product_df[product_slice]
    amount = unique_slice["Amount"].tolist()
    index = unique_slice["Date"].tolist()
    unique_series = pd.Series(amount, index)
    unique_series.index = pd.DatetimeIndex(unique_series.index, freq=pd.infer_freq(unique_series.index))
    triple_expo(unique_series, triple_out_one)

I originally did not have a frequency argument at all because I was following the example on the statsmodels website which is here: http://www.statsmodels.org/stable/examples/notebooks/generated/exponential_smoothing.html

They did not pass a frequency argument at all as they inferred it using the DateTimeIndex in pandas. When I have no frequency argument, my error is "operands could not be broadcast together with shapes <5,><12,>". I have 17 months of data and pandas recognizes it as 'Ms'. The 5 and the 12 is referring to the 17. I then pass freq='M' like I have in the code sample, and I get "The given frequency argument is incompatible with the given index". I then try setting it to everything from len(data) to len(data)-1 and always get errors. I tried the len(data)-1 because I was originally referencing this stack post: seasonal_decompose: operands could not be broadcast together with shapes on a series

In that post, he said it would work if you set the frequency to one less than the length of the data set. It does not work for me though. Any help would be appreciated. Thanks!

EDIT: The comment below suggested I include more than one year's worth of data and it worked when I did that.

  • I am running into the same problem with the same configuration as yours (I have 18 points / months of data). I haven't checked into the math, but I believe the seasonal component can only be assessed with several periods, which is not our case (we only have 1 full seasonal period, not several). So I guess we have to abandon this approach to estimate the seasonal effect with such short times series ¯\_(ツ)_/¯ – Tanguy Aug 13 '19 at 17:54
  • That was helpful. I threw data at it from 2016 to present and it at least ran. I am still very frustrated that I always get the divide by zero error whenever I run double or triple expo(which uses the same ExponentialSmoothing in the background). "RunTimeWarning:divide by zero encountered in log". – pythondatanoob2019 Aug 15 '19 at 17:56
  • Have you tried tweaking other values with the smoothing parameters (start with the slope and the level parameters in the double ES)? – Tanguy Aug 15 '19 at 21:51
  • My double expo function I created runs the double expo forecast with 9 different variations(pretty much every combination between .1 and .3) as well as running the ex post forecast for the 6 months prior for each. https://pastebin.com/RtdnvNyM The code is in the pastebin. Are you thinking that it is just throwing that error on one specific model? I was reading the statsmodels github and it seems to be a known error that was supposed to be resolved. I didn't have this error until I upgraded to the new 10.1 version of statsmodels recently. – pythondatanoob2019 Aug 16 '19 at 12:55

0 Answers0