Statsmodels ARIMA - 使用predict()和forecast()的不同结果

时间:2017-08-09 16:50:34

标签: python statsmodels arima

我会使用( Statsmodels )ARIMA来预测系列中的值:

plt.plot(ind, final_results.predict(start=0 ,end=26))
plt.plot(ind, forecast.values)
plt.show()

我认为我会从这两个图中得到相同的结果,但我得到了这个: enter image description here

我会知道问题是否是预测预测

2 个答案:

答案 0 :(得分:6)

从图表中可以看出,您使用forecast()进行了样本预测,使用预测进行样本内预测。根据ARIMA方程的性质,样本外预测往往会收敛到长预测期的样本均值。

为了了解forecast()predict()如何适用于不同的情景,我系统地比较了ARIMA_results课程中的各种模型。请随意重现与statsmodels_arima_comparison.py in this repository的比较。我查看了order=(p,d,q)的每个组合,仅将p, d, q限制为0或1.例如,可以使用order=(1,0,0)获取简单的自回归模型。 简而言之,我使用以下(stationary) time series

查看了三个选项

一个。迭代样本内预测形成了历史。历史由前80%的时间序列组成,测试集由最后的20%组成。然后我预测了测试集的第一点,在历史中添加了真实值,预测了第二点等。这将对模型预测质量进行评估。

for t in range(len(test)):
    model = ARIMA(history, order=order)
    model_fit = model.fit(disp=-1)
    yhat_f = model_fit.forecast()[0][0]
    yhat_p = model_fit.predict(start=len(history), end=len(history))[0]
    predictions_f.append(yhat_f)
    predictions_p.append(yhat_p)
    history.append(test[t])

B中。接下来,我通过迭代预测测试系列的下一个点来查看样本外预测,并将此预测附加到历史记录中。

for t in range(len(test)):
    model_f = ARIMA(history_f, order=order)
    model_p = ARIMA(history_p, order=order)
    model_fit_f = model_f.fit(disp=-1)
    model_fit_p = model_p.fit(disp=-1)
    yhat_f = model_fit_f.forecast()[0][0]
    yhat_p = model_fit_p.predict(start=len(history_p), end=len(history_p))[0]
    predictions_f.append(yhat_f)
    predictions_p.append(yhat_p)
    history_f.append(yhat_f)
    history_f.append(yhat_p)

℃。我使用了forecast(step=n)参数和predict(start, end)参数,以便使用这些方法进行内部多步预测。

model = ARIMA(history, order=order)
    model_fit = model.fit(disp=-1)
    predictions_f_ms = model_fit.forecast(steps=len(test))[0]
    predictions_p_ms = model_fit.predict(start=len(history), end=len(history)+len(test)-1)

事实证明:

一个。预测和预测AR的结果相同,但ARMA的结果不同:test time series chart

B中。预测和预测产生AR和ARMA的不同结果:test time series chart

℃。预测和预测AR的结果相同,但ARMA的结果不同:test time series chart

此外,比较B.和C中看似相同的方法。我发现结果中存在细微但明显的差异。

我认为差异的主要原因是预测是在原始内生变量的水平上进行的。在forecast()predict()中生成预测级别差异(compare the API reference)。

此外,鉴于我更信任statsmodels函数的内部功能而不是我的简单迭代预测循环(这是主观的),我建议使用forecast(step)predict(start, end)

答案 1 :(得分:0)

继续noteven2degrees的回复,我提交了一个拉动请求,以在方法B中从history_f.append(yhat_p)修正为history_p.append(yhat_p)

此外,正如noteven2degrees所建议的,与forecast()不同,predict()需要参数typ='levels'来输出预测,而不是差异预测。

在上述两个更改之后,方法B产生的结果与方法C相同,而方法C花费的时间要少得多,这是合理的。两者都趋于趋势,因为我认为与模型本身的平稳性有关。

无论采用哪种方法,forecast()predict()在p,d,q的任何配置下都产生相同的结果。