无法使用 HoltWinters 的 statsmodel 获得适当的预测

Unable to get appropriate prediction using statsmodel for HoltWinters

我正在尝试使用一些随机数据对 HoltWinters 进行试验。但是,使用统计模型 api 我无法预测下一个 X 数据点。

这是我的示例代码。我无法理解 predict API 以及 startend 的含义。

import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing


data = np.linspace(start=15, stop=25, num=100)
noise = np.random.uniform(0, 1, 100)

data = data + noise

split = int(len(data)*0.7)
data_train = data[0:split]
data_test = data[-(len(data) - split):]

model = ExponentialSmoothing(data_train)
model_fit = model.fit()

# make prediction
pred = model_fit.predict(split+1, len(data))

test_index = [i for i in range(split, len(data))]

plt.plot(data_train, label='Train')
plt.plot(test_index, data_test, label='Test')
plt.plot(test_index, pred, label='Prediction')
plt.legend(loc='best')
plt.show()

我得到一个奇怪的预测图,我相信这与我对 predict API.

的理解有关

您选择的指数平滑模型不包含趋势,因此它预测的是最佳水平,并且给出水平线预测。

如果你这样做:

model = ExponentialSmoothing(data_train, trend='add')

然后你会得到一个趋势,它可能看起来更像你期望的那样。

例如:

# Simulate some data
np.random.seed(12346)
dta = pd.Series(np.arange(100) + np.sin(np.arange(100)) * 5 + np.random.normal(scale=4, size=100))

# Perform exponention smoothing, no trend
mod1 = sm.tsa.ExponentialSmoothing(dta)
res1 = mod1.fit()
fcast1 = res1.forecast(30)

plt.plot(dta)
plt.plot(fcast1, label='Model without trend')

# Perform exponention smoothing, with a trend
mod2 = sm.tsa.ExponentialSmoothing(dta, trend='add')
res2 = mod2.fit()
fcast2 = res2.forecast(30)

plt.plot(fcast2, label='Model with trend')
plt.legend(loc='lower right')

给出以下内容: