估计与预测 AR 模型

Python 中的时间序列分析

Rob Reider

Adjunct Professor, NYU-Courant Consultant, Quantopian

估计 AR 模型

  • 从数据(模拟)估计参数

    from statsmodels.tsa.arima_model import ARMA
    mod = ARMA(data, order=(1,0))
    result = mod.fit()
    
  • ARMA 已弃用,改用 ARIMA

    from statsmodels.tsa.arima.model import ARIMA
    mod = ARIMA(data, order=(1,0,0))
    result = mod.fit()
    
  • 对 ARMA,order=(p,q)

  • 对 ARIMA,order=(p,d,q)
Python 中的时间序列分析

估计 AR 模型

  • 完整输出(真实值 $\large \mu=0$,$\large \phi=0.9$)
    print(result.summary())
    

Python 中的时间序列分析

估计 AR 模型

  • 仅显示 $\large \mu$ 和 $\large \phi$ 的估计(真实值 $\large \mu=0$,$\large \phi=0.9$)
    print(result.params)
    
array([-0.03605989,  0.90535667])
Python 中的时间序列分析

用 AR 模型进行预测

from statsmodels.graphics.tsaplots import plot_predict
fig, ax = plt.subplots()
data.plot(ax=ax)
plot_predict(result, start='2012-09-27', end='2012-10-06', alpha=0.05, ax=ax)
plt.show()
  • 函数 plot_predict() 的参数
    • 第一个参数为已拟合模型
    • alpha=None 可去除置信区间
    • ax=ax 可在同一坐标轴上绘制数据与预测

Python 中的时间序列分析

Passons à la pratique !

Python 中的时间序列分析

Preparing Video For Download...