开始使用免费开始使用

使用 AR 模型进行预测

除了在上一个练习中估计模型参数之外,您还可以使用 statsmodels 进行样本内与样本外预测。样本内是指在每个时间点,用截至该点的数据来预测下一个数据点;样本外是指预测未来任意数量的数据点。您可以使用函数 plot_predict() 来绘制预测值。您需要提供预测的起始位置和结束位置,结束位置可以超出数据集末尾任意多个数据点。

对于 DataFrame simulated_data_1 中的模拟数据($\small \phi=0.9$),请绘制样本外预测以及这些预测的置信区间。

本练习是课程的一部分

Python 中的时间序列分析

查看课程

练习说明

  • 导入类 ARIMA,并导入函数 plot_predict
  • 使用 DataFrame simulated_data_1 中的模拟数据和模型的 (p,d,q) 次数(此处为 AR(1)),创建一个名为 modARIMA 类实例:order=(1,0,0)
  • 使用 .fit() 方法拟合模型 mod,并将结果保存到名为 res 的结果对象中
  • 从第 950 个数据点开始绘制样本内数据
  • 使用 plot_predict() 函数绘制数据的样本外预测及其置信区间,从数据结束处第 1000 个点开始,直到第 1010 个点结束

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Import the ARIMA and plot_predict from statsmodels
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.graphics.tsaplots import plot_predict

# Forecast the first AR(1) model
mod = ARIMA(___, order=___)
res = mod.fit()

# Plot the data and the forecast
fig, ax = plt.subplots()
simulated_data_1.loc[950:].plot(ax=ax)
plot_predict(res, start=___, end=___, ax=ax)
plt.show()
编辑并运行代码