自回归移动平均 (ARMA):人工数据¶
[1]:
%matplotlib inline
[2]:
import numpy as np
import pandas as pd
from statsmodels.graphics.tsaplots import plot_predict
from statsmodels.tsa.arima_process import arma_generate_sample
from statsmodels.tsa.arima.model import ARIMA
np.random.seed(12345)
从 ARMA 过程生成一些数据
[3]:
arparams = np.array([0.75, -0.25])
maparams = np.array([0.65, 0.35])
arma_generate 函数的约定要求我们为 AR 和 MA 参数的零滞后指定 1,并且 AR 参数需要取反。
[4]:
arparams = np.r_[1, -arparams]
maparams = np.r_[1, maparams]
nobs = 250
y = arma_generate_sample(arparams, maparams, nobs)
现在,我们可以选择添加一些日期信息。在本例中,我们将使用 pandas 时间序列。
[5]:
dates = pd.date_range("1980-1-1", freq="M", periods=nobs)
y = pd.Series(y, index=dates)
arma_mod = ARIMA(y, order=(2, 0, 2), trend="n")
arma_res = arma_mod.fit()
/tmp/ipykernel_4466/3761908329.py:1: FutureWarning: 'M' is deprecated and will be removed in a future version, please use 'ME' instead.
dates = pd.date_range("1980-1-1", freq="M", periods=nobs)
[6]:
print(arma_res.summary())
SARIMAX Results
==============================================================================
Dep. Variable: y No. Observations: 250
Model: ARIMA(2, 0, 2) Log Likelihood -353.445
Date: Thu, 03 Oct 2024 AIC 716.891
Time: 15:48:55 BIC 734.498
Sample: 01-31-1980 HQIC 723.977
- 10-31-2000
Covariance Type: opg
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
ar.L1 0.7905 0.142 5.566 0.000 0.512 1.069
ar.L2 -0.2314 0.124 -1.859 0.063 -0.475 0.013
ma.L1 0.7007 0.131 5.344 0.000 0.444 0.958
ma.L2 0.4061 0.097 4.177 0.000 0.216 0.597
sigma2 0.9801 0.093 10.514 0.000 0.797 1.163
===================================================================================
Ljung-Box (L1) (Q): 0.00 Jarque-Bera (JB): 0.29
Prob(Q): 0.96 Prob(JB): 0.86
Heteroskedasticity (H): 0.92 Skew: 0.02
Prob(H) (two-sided): 0.69 Kurtosis: 2.84
===================================================================================
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
[7]:
y.tail()
[7]:
2000-06-30 0.173211
2000-07-31 -0.048325
2000-08-31 -0.415804
2000-09-30 0.338725
2000-10-31 0.360838
Freq: ME, dtype: float64
[8]:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10, 8))
fig = plot_predict(arma_res, start="1999-06-30", end="2001-05-31", ax=ax)
legend = ax.legend(loc="upper left")
最后更新:2024 年 10 月 3 日