简体   繁体   English

Statsmodels ARIMA - 使用预测()和预测()的不同结果

[英]Statsmodels ARIMA - Different results using predict() and forecast()

I use ( Statsmodels ) ARIMA in order to predict values from a series:我使用( Statsmodels )ARIMA 来预测一系列值:

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

I thought that I would get the same results from these two methods, but instead I get this:我以为我会从这两种方法中得到相同的结果,但我得到了这个: 在此处输入图像描述

I would like to know whether to use predict() or forecast() .我想知道是使用predict()还是 predict( forecast()

From the chart it looks as if you are doing out-of sample preictions with forecast() , bit in-sample predictions with predict.从图表上看,您好像在使用 predict forecast()进行样本外预测,使用 predict 进行样本内预测。 Based on the nature of the ARIMA equations, out-of-sample forecasts tend to converge to the sample mean for long forecasting periods.基于 ARIMA 方程的性质,对于较长的预测周期,样本外预测往往会收敛到样本均值。

In order to find out how forecast() and predict() work for different scenarios, I compared various models in the ARIMA_results class systematically.为了了解 predict( forecast()predict()如何在不同场景下工作,我系统地比较了ARIMA_results类中的各种模型。 Feel free to reproduce the comparison with statsmodels_arima_comparison.py in this repository .随意在此存储库中重现与statsmodels_arima_comparison.py的比较。 I looked into each combinations of order=(p,d,q) , only restricting p, d, q to 0 or 1. For instance, a simple autoregressive model can be obtained with order=(1,0,0) .我研究了order=(p,d,q)的每种组合,仅将p, d, q限制为 0 或 1。例如,可以使用order=(1,0,0)获得简单的自回归模型。 In a nutshell, I looked into three options, using the following (stationary) time series :简而言之,我使用以下(固定)时间序列研究了三个选项:

A. Iterative in-sample forecasting form a history. A. 迭代样本内预测形成历史。 The history was formed of the first 80 % of a time series, and a test set was formed by the last 20 %.历史由时间序列的前 80% 组成,而测试集由最后 20% 组成。 Then I forecasted the first point of the test set, added the true value to the history, forecasted the second point etc. This shall give an evaluation of the model predictive quality.然后我预测了测试集的第一个点,将真实值添加到历史中,预测了第二个点等。这将对模型的预测质量进行评估。

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. Next, I looked into out-of-sample forecasting by iteratively predicting the next point of the test series, and appending this prediction to the history. 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)

C. I used the forecast(step=n) parameter and the predict(start, end) parameters in order to do internal multi-step forecasting with these methods. C. 我使用了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)

It turned out that:事实证明:

A. Forecast and predict yield identical results for AR, but different results for ARMA: test time series chart A. 预测和预测 AR 的结果相同,但 ARMA 的结果不同:测试时间序列图

B. Forecast and predict yield different results for both AR and ARMA: test time series chart B. 预测和预测对 AR 和 ARMA 产生不同的结果:测试时间序列图

C. Forecast and predict yield identical results for AR, but different results for ARMA: test time series chart C. 预测和预测 AR 的结果相同,但 ARMA 的结果不同:测试时间序列图

Further, comparing the seemingly identical approaches in B. and C. I found subtle but visible differences in the results.此外,比较 B. 和 C. 中看似相同的方法,我发现结果存在细微但明显的差异。

I suggest that the differences result mainly from the fact that "prediction is done in the levels of the original endogenous variable" in forecast() and predict() produces prediction of differences in levels ( compare the API reference ).我认为差异主要是由于 predict() 中的“预测是在原始内生变量的水平上完成的”,而predict() forecast()产生水平差异的预测( 比较 API 参考)。

Further, given I trust more the internal functionality of the statsmodels functions than my simple iterative forecasting loop (this is subjective), I would recommend going with forecast(step) or predict(start, end) .此外,鉴于我更信任 statsmodels 函数的内部功能而不是简单的迭代预测循环(这是主观的),我建议使用forecast(step)predict(start, end)

Continuing noteven2degrees's reply, I submitted a pull request to correct in method B from history_f.append(yhat_p) to history_p.append(yhat_p) .继续 noteven2degrees 的回复,我提交了一个拉取请求以更正方法 B 从history_f.append(yhat_p)history_p.append(yhat_p)

Also, as noteven2degrees suggested, unlike forecast() , the predict() requires an argument typ='levels' to output a forecast, instead of a differenced forecast.此外,正如 noteven2degrees 建议的那样,与 predict( forecast()不同, predict()需要一个参数typ='levels'来输出预测,而不是差异预测。

After the above two changes, method B produces the same result as method C, while method C takes far less time, which is reasonable.经过上述两次改动后,方法B产生的结果与方法C相同,而方法C所用的时间要少得多,这是合理的。 And both converges to a trend, as I think is related to the stationarity of the model itself.两者都收敛到一个趋势,因为我认为这与模型本身的平稳性有关。

No matter in which method, forecast() and predict() produce the same result with whatever configurations of p,d,q .无论采用哪种方法,对于 p,d,q 的任何配置, predict( forecast()predict()都会产生相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM