HOWTO · Python

Auto ARIMA in Python

In this article, we will learn about Auto ARIMA in Python and how it works. The Auto Arima() Function of Python is used for the identification of optimum parameters of the fitted ARIMA model. Auto ARIMA function can be imported from Python library named pmdarima.'

In this article, we will learn about Auto ARIMA in Python and how it works.

Auto ARIMA in Python

The auto_arima() function from the pmdarima library assists in determining the ARIMA model’s optimum parameters and provides a fitted ARIMA model as a result.

Take note that this package was once called "Pyramid" before being renamed "pmdarima". Ensure that the "pmdarima" package is being installed.

If you don’t have the package, install it by running the below command in the terminal.

pip install pmdarima

Use the following command to test see whether the package was created successfully.

from pmdarima.arima import auto_arima

Use the auto_arima() Function in Python

In the following code, the data.csv is a CSV file containing the data and is used for Auto ARIMA. The output will be dataframe with a value with order=(P,D,Q) in index p and q.

Code Example:

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())

Output:

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

Conclusion

The ARIMA model extensively estimates the stock performance over the next several days. The auto_arima() function of Python is used to identify the optimum parameters of the fitted ARIMA model. For related information on dealing with common issues in Python, consider reading about how to fix the ImportError: Missing Required Dependencies Numpy or learn more about importing modules with How to Fix Python ImportError: No Module Named.

The auto_arima() function can be imported from the Python library named pmdarima.