SPS
SPS

Reputation: 475

Predict with Triple Exponential Smoothing using ExponentialSmoothing of statsmodels in python

I'm working on the forecast of some time series and I need to compare different methods using python. Practically I need to generate some predictions using the Triple Exponential Smoothing, and I'm using this library and the relative functions like this. My time series has this format, as pd.Series object:

    Date Close
2016-04-11 01:17:04    -10.523793
2016-04-11 07:25:13     -5.352295
2016-04-11 22:40:11     92.556003
2016-04-13 05:06:31     -1.769866
2016-04-13 05:17:50     -2.330789
2016-04-14 08:43:09     17.636638
2016-04-17 21:15:12     -0.454655
2016-04-19 06:10:04     -0.026375
2016-04-19 06:10:04     -0.175647
...

I wrote these lines in python:

from statsmodels.tsa.holtwinters import ExponentialSmoothing
from matplotlib import pyplot as plt
import numpy as np 
import pandas as pd
train_size = int(len(myTimeSeries) * 0.66)
train, test = myTimeSeries[1:train_size], myTimeSeries[train_size:]

model = ExponentialSmoothing(train)
model_fit = model.fit()
dict=model.params
params=np.array(list(dict.items()))
dates=test.index.astype(str)
pred = model.predict(params,start=dates[2], end=dates[-1])
plt.plot(train, label='Train')
plt.plot(test, label='Test')
plt.plot(pred, label='Holt-Winters')
plt.legend(loc='best')

I had a problem with the function model.predict so I added the param values as required, getting them from the class model, after its fit. I'm not sure if I did well but I've not found so much around. Moreover I got a problem in setting the start date (and maybe also the end). It returns this: KeyError: 'Thestartargument could not be matched to a location related to the index of the data.' As I found here, I also moved the start date of the prediction to the third value, that is the index [2] of the test dataset. I got the same if I set [0],[1], etc... What's wrong with that?

As you can see, myTimeSeries has not a fixed frequency set, but the collection of the value is random. I've found different tutorials, like this, this other one, or this about theory, but they are not in the same conditions: I don't know any news (trend, seasonal frequency, etc..) about my dataset. I've not found any violated hypotheses for this: warn me if I'm wrong. I used to consider my theory guide, what I've found here. Moreover, this post covers a similar problem but not exactly the same.

Upvotes: 1

Views: 5546

Answers (1)

cfulton
cfulton

Reputation: 3195

I think maybe you just want:

pred = model_fit.forecast(len(test))

You can't use dates here for specifying the forecast period because your date index doesn't have a frequency associated with it, so the best you can do is just indicate the number of forecasts that you want.

Upvotes: 3

Related Questions