拟合时间序列模型

Python 中的 ARIMA 模型

James Fulton

Climate informatics researcher

创建模型

from statsmodels.tsa.arima.model import ARIMA
# This is an ARMA(p,q) model
model = ARIMA(timeseries, order=(p,0,q))
Python 中的 ARIMA 模型

创建 AR 和 MA 模型

ar_model = ARIMA(timeseries, order=(p,0,0))
ma_model = ARIMA(timeseries, order=(0,0,q))
Python 中的 ARIMA 模型

拟合模型与摘要

model = ARIMA(timeseries, order=(2,0,1))

results = model.fit()
print(results.summary())
Python 中的 ARIMA 模型

拟合摘要

                             SARIMAX Results                                                  
 ==============================================================================
 Dep. Variable:                      y   No. Observations:                 1000
 Model:                     ARMA(2, 1)   Log Likelihood                 148.580
 Date:                Thu, 25 Apr 2022   AIC                           -287.159
 Time:                        22:57:00   BIC                           -262.621
 Sample:                             0   HQIC                          -277.833
 Covariance Type:                    opg                                       
 ==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
 ------------------------------------------------------------------------------
 const         -0.0017      0.012     -0.147      0.883      -0.025       0.021
 ar.L1.y        0.5253      0.054      9.807      0.000       0.420       0.630
 ar.L2.y       -0.2909      0.042     -6.850      0.000      -0.374      -0.208
 ma.L1.y        0.3679      0.052      7.100      0.000       0.266       0.469
Python 中的 ARIMA 模型

拟合摘要

                            SARIMAX Results                           
==============================================================================
Dep. Variable:                      y   No. Observations:                 1000
Model:                     ARMA(2, 1)   Log Likelihood                 148.580
Date:                Thu, 25 Apr 2022   AIC                           -287.159
Time:                        22:57:00   BIC                           -262.621
Sample:                             0   HQIC                          -277.833
Covariance Type:                    opg                             
Python 中的 ARIMA 模型

拟合摘要

 ==============================================================================
                  coef    std err          z      P>|z|      [0.025      0.975]
 ------------------------------------------------------------------------------
 const         -0.0017      0.012     -0.147      0.883      -0.025       0.021
 ar.L1.y        0.5253      0.054      9.807      0.000       0.420       0.630
 ar.L2.y       -0.2909      0.042     -6.850      0.000      -0.374      -0.208
 ma.L1.y        0.3679      0.052      7.100      0.000       0.266       0.469

sigma2 1.6306 0.339 6.938 0.000 0.583 1.943
Python 中的 ARIMA 模型

ARMAX 简介

  • 外生 ARMA
  • 同时使用外部变量与时间序列

  • ARMAX = ARMA + 线性回归

Python 中的 ARIMA 模型

ARMAX 方程

ARMA(1,1) 模型:$$y_t = a_1 y_{t-1} + m_1 \epsilon_{t-1} + \epsilon_t$$

ARMAX(1,1) 模型:$$y_t = x_1 z_t + a_1 y_{t-1} + m_1 \epsilon_{t-1} + \epsilon_t$$

Python 中的 ARIMA 模型

ARMAX 示例

Python 中的 ARIMA 模型

ARMAX 示例

Python 中的 ARIMA 模型

拟合 ARMAX

# Instantiate the model
model = ARIMA(df['productivity'], order=(2,0,1), exog=df['hours_sleep'])

# Fit the model results = model.fit()
Python 中的 ARIMA 模型

ARMAX 摘要

 ==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
 -----------------------------------------------------------------------------
 const         -0.1936      0.092     -2.098      0.041      -0.375      -0.013
 x1             0.1131      0.013      8.602      0.000       0.087       0.139
 ar.L1.y        0.1917      0.252      0.760      0.450      -0.302       0.686
 ar.L2.y       -0.3740      0.121     -3.079      0.003      -0.612      -0.136
 ma.L1.y       -0.0740      0.259     -0.286      0.776      -0.581       0.433
Python 中的 ARIMA 模型

Passons à la pratique !

Python 中的 ARIMA 模型

Preparing Video For Download...