Search code examples
pythontime-seriessignal-processingstatsmodelssmoothing

Why does exponential smoothing in statsmodels return identical values for a time series forecast?


I am using the following code to get simple exponential smoothing in statsmodels.

years = [1979,1980,1981,1982,1983,1984,1985,1986,1987,1988]
mylist = [3.508046180009842, 64.08556070923805, 15.407086104154587, 0, 3.8572598099708557, 11.009202659130096, 5.324577212333679, 4.33474725484848, 4.024865210056305, 5.065838277339935]

import pandas as pd
from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing
myinput = pd.Series(mylist, years)
fit_model = SimpleExpSmoothing(myinput).fit()
smoothed = fit_model.fittedvalues
print(smoothed.tolist())

The result I got was quite surprising. i.e. I get the same value for every year.

[11.661719028226004, 11.661719028226004, 11.661719028226004, 11.661719028226004, 11.661719028226004, 11.661719028226004, 11.661719028226004, 11.661719028226004, 11.661719028226004, 11.661719028226004]

I am wondering why I get the same value for every year. Please let me know how to fix this?

I am happy to provide more details if needed.


Solution

  • This error is raised if the index is not of type DatetimeIndex or RangeIndex.

    In your case the list is simply converted to a "normal" Int64Index.

    I tried several things, e.g. converting converting the index to a RangeIndex

    myinput = pd.Series(mylist, range(1979, 1989))
    

    or a DatetimeIndex

    years_index = [datetime(_, 1, 1) for _ in years] 
    myinput = pd.Series(mylist, years_index)
    

    but then there is another exception.

    I think the solution to your problem is to supply the keyword argument smoothing_level to the fit like

    fit_model = SimpleExpSmoothing(myinput).fit(smoothing_level=0.2)
    

    Then the returned numbers are not identical. I did not check the results, but most of the code for plotting can be found in the statsmodel tutorial.

    The default value seems to be smoothing_level=None, but I am not sure why the fit function is not working out of the box.

    The docs only state

    The smoothing_level value of the simple exponential smoothing, if the value is set then this value will be used as the value.

    This is the description of the simple exponential smoothing method as mentioned in the docs if you are interested in how the smoothing level is defined.