Python中的自动自回归模型

在这篇文章中,我们将学习Python中的Auto ARIMA以及它是如何工作的。

Python中的自动自回归模型

pmdarima 库中的auto_arima() 函数可以帮助确定 ARIMA 模型的最佳参数,并提供一个拟合的 ARIMA 模型作为结果。

请注意,这个包在改名为"pmdarima" 之前曾经叫做"Pyramid" 。确保正在安装"pmdarima" 包。

如果你没有这个包,在终端运行下面的命令来安装它。

pip install pmdarima

使用下面的命令来测试软件包是否被成功创建。

from pmdarima.arima import auto_arima

在Python中使用auto_arima() 函数

在下面的代码中,data.csv是一个包含数据的CSV文件,用于Auto ARIMA。输出将是数据框架,在索引pq 中有一个带有order=(P,D,Q) 的值。

代码示例:

import pmdarima as pm
import pandas as pd
df1 = pd.read_csv('data.csv', names=['value'], header=0)
model_1 = pm.auto_arima(df1.value, start_p=1, start_q=1,
                      test='adf',
                      max_p=3, max_q=3,
                      m=1,
                      d=None,
                      seasonal=False,
                      start_P=0,
                      D=0,
                      trace=True,
                      error_action='ignore',
                      suppress_warnings=True,
                      stepwise=True)
print(model_1.summary())

输出:

Performing stepwise search to minimize aic
 ARIMA(1,1,1)(0,0,0)[0] intercept   : AIC=1605.366, Time=0.09 sec
 ARIMA(0,1,0)(0,0,0)[0] intercept   : AIC=1660.860, Time=0.01 sec
 ARIMA(1,1,0)(0,0,0)[0] intercept   : AIC=1619.269, Time=0.04 sec
 ARIMA(0,1,1)(0,0,0)[0] intercept   : AIC=1604.209, Time=0.04 sec
 ARIMA(0,1,0)(0,0,0)[0]             : AIC=1658.968, Time=0.01 sec
 ARIMA(0,1,2)(0,0,0)[0] intercept   : AIC=1605.215, Time=0.08 sec
 ARIMA(1,1,2)(0,0,0)[0] intercept   : AIC=1606.845, Time=0.12 sec
 ARIMA(0,1,1)(0,0,0)[0]             : AIC=1603.295, Time=0.02 sec
 ARIMA(1,1,1)(0,0,0)[0]             : AIC=1604.373, Time=0.03 sec
 ARIMA(0,1,2)(0,0,0)[0]             : AIC=1604.196, Time=0.04 sec
 ARIMA(1,1,0)(0,0,0)[0]             : AIC=1617.588, Time=0.04 sec
 ARIMA(1,1,2)(0,0,0)[0]             : AIC=1605.883, Time=0.04 sec
Best model:  ARIMA(0,1,1)(0,0,0)[0]
Total fit time: 0.580 seconds
                               SARIMAX Results
==============================================================================
Dep. Variable:                      y   No. Observations:                  173
Model:               SARIMAX(0, 1, 1)   Log Likelihood                -799.648
Date:                Sat, 03 Sep 2022   AIC                           1603.295
Time:                        23:15:18   BIC                           1609.590
Sample:                             0   HQIC                          1605.849
                                - 173
Covariance Type:                  opg
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
ma.L1         -0.5856      0.056    -10.478      0.000      -0.695      -0.476
sigma2       637.6579     54.893     11.616      0.000     530.069     745.247
===================================================================================
Ljung-Box (L1) (Q):                   0.54   Jarque-Bera (JB):                24.81
Prob(Q):                              0.46   Prob(JB):                         0.00
Heteroskedasticity (H):               0.18   Skew:                             0.41
Prob(H) (two-sided):                  0.00   Kurtosis:                         4.67
===================================================================================
Process finished with exit code 0

结论

ARIMA模型广泛地估计了未来几天的股票表现。Python的auto_arima() 函数被用来确定拟合ARIMA模型的最佳参数。

auto_arima() 函数可以从名为pmdarima 的 Python 库中导入。