I've been having a frustrating issue with the ExponentialSmoothing module from statsmodels.

My data is a pandas series with 74 weekly data points that looks like this:

2017-12-31    6069
2018-01-07    8143
2018-01-14    6740
2018-01-21    6433
2018-01-28    6631
2018-02-04    6308
2018-02-11    5536
2018-02-18    6025
2018-02-25    5171
...           ...

When I call the following functions:

model = ExponentialSmoothing(data, trend='add',damped=True,seasonal='mul',seasonal_periods=52)
model_fit = model.fit()

I get:

Traceback (most recent call last):
  File ".\smoothingjuly.py", line 24, in <module>
    model_fit = model.fit()
  File "C:\Users\lhughes\AppData\Local\Programs\Python\Python37\lib\site-packages\statsmodels\tsa\holtwinters.py", line 641, in fit
    l0, b0, s0 = self.initial_values()
  File "C:\Users\lhughes\AppData\Local\Programs\Python\Python37\lib\site-packages\statsmodels\tsa\holtwinters.py", line 773, in initial_values
    b0 = ((lead - lag) / m).mean()
ValueError: operands could not be broadcast together with shapes (22,) (52,)

Why is this? It works if I decrease the number of seasonal periods, but that makes my model useless. Is 74 data points not enough for the model? If so what is the minimum?



The error is raised from lead - lag; in initial_values, these are set as y[m:2m] and y[:m] respectively, where m is the seasonality length (52 in your case). So lead is only getting 22 values, and hence the size mismatch complaint.

So yes, for this to work directly, you'd need at least 104 datapoints. https://www.statsmodels.org/dev/_modules/statsmodels/tsa/holtwinters.html#ExponentialSmoothing.initial_values

But, you can bypass this bit of the code by passing your own initial_slope to fit. I might suggest mimicking statsmodels's approach, but limiting the year-over-year slopes to the range you have available: (y[52:74] - y[:22]).mean() / 52 https://www.statsmodels.org/dev/generated/statsmodels.tsa.holtwinters.ExponentialSmoothing.fit.html#statsmodels.tsa.holtwinters.ExponentialSmoothing.fit

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.