AR 모형
AR(p) 모형은 이전 시점의 값이 현재 시점의 값에 영향을 준다고 가정하는 모형입니다. p는 영향을 주는 시점의 수를 나타냅니다.
$$ y_t = \phi_1 y_{t-1} + \cdots + \phi_p y_{t-p} + a_{t} $$
직전 t-1 시점의 오차가 영향을 주면 AR(1) 모형, 직전 t-1과 그 이전 t-2의 오차가 영향을 주면 AR(2) 모형, 등으로 구분합니다.
다음은 AR(1) 모형의 시뮬레이션입니다. 직전의 값에 0.8을 곱하고 오차를 더하면 현재의 값이 됩니다. 이 모형의 경우 양의 자기 상관을 가지게 됩니다.
import numpy as np
n = 100
e = np.random.normal(size=n)
sim = np.zeros(n)
for i in range(1, n):
sim[i] = 0.8 * sim[i-1] + e[i]
import matplotlib.pyplot as plt
plt.plot(sim)
[<matplotlib.lines.Line2D at 0x1546f5e5ea0>]
직전의 값에 -0.8을 곱하는 경우입니다. 이 모형은 음의 자기상관을 가지게 됩니다.
sim = np.zeros(n)
for i in range(1, n):
sim[i] = -0.8 * sim[i-1] + e[i]
plt.plot(sim)
[<matplotlib.lines.Line2D at 0x15472900910>]
AR(p) 모형의 ACF는 점진적으로 감소하는 형태를 띕니다.
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
plot_acf(sim);
대신 PACF는 p를 지나면 0으로 감소하는 형태를 띕니다. (MA모형은 ACF가 이런 형태를 띕니다.)
plot_pacf(sim);
SARIMAX 함수로 추정할 때 order는 (p, d, q) 순입니다. 따라서 AR(1) 모형은 order=(1, 0, 0)으로 설정합니다. ar.L1 계수가 -0.8에 가까운 것을 볼 수 있습니다.
from statsmodels.tsa.api import SARIMAX
ar1 = SARIMAX(sim, order=(1, 0, 0)).fit()
ar1.summary()
| Dep. Variable: | y | No. Observations: | 100 |
|---|---|---|---|
| Model: | SARIMAX(1, 0, 0) | Log Likelihood | -134.831 |
| Date: | Sun, 18 Jun 2023 | AIC | 273.661 |
| Time: | 19:04:21 | BIC | 278.872 |
| Sample: | 0 | HQIC | 275.770 |
| - 100 | |||
| Covariance Type: | opg |
| coef | std err | z | P>|z| | [0.025 | 0.975] | |
|---|---|---|---|---|---|---|
| ar.L1 | -0.7879 | 0.069 | -11.465 | 0.000 | -0.923 | -0.653 |
| sigma2 | 0.8599 | 0.132 | 6.532 | 0.000 | 0.602 | 1.118 |
| Ljung-Box (L1) (Q): | 0.44 | Jarque-Bera (JB): | 0.33 |
|---|---|---|---|
| Prob(Q): | 0.51 | Prob(JB): | 0.85 |
| Heteroskedasticity (H): | 0.82 | Skew: | -0.01 |
| Prob(H) (two-sided): | 0.56 | Kurtosis: | 2.72 |
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
맥주 데이터를 AR 모형으로 분석합니다.
import pandas as pd
df = pd.read_excel('beer.xlsx')
y = df.production
y.plot()
<Axes: >
AR(1)
ar1 = SARIMAX(y, order=(1, 0, 0)).fit()
ar1.summary()
| Dep. Variable: | production | No. Observations: | 218 |
|---|---|---|---|
| Model: | SARIMAX(1, 0, 0) | Log Likelihood | -1229.071 |
| Date: | Sun, 18 Jun 2023 | AIC | 2462.142 |
| Time: | 19:04:48 | BIC | 2468.911 |
| Sample: | 0 | HQIC | 2464.876 |
| - 218 | |||
| Covariance Type: | opg |
| coef | std err | z | P>|z| | [0.025 | 0.975] | |
|---|---|---|---|---|---|---|
| ar.L1 | 0.9860 | 0.011 | 86.017 | 0.000 | 0.963 | 1.008 |
| sigma2 | 4545.1184 | 574.696 | 7.909 | 0.000 | 3418.736 | 5671.501 |
| Ljung-Box (L1) (Q): | 11.12 | Jarque-Bera (JB): | 11.84 |
|---|---|---|---|
| Prob(Q): | 0.00 | Prob(JB): | 0.00 |
| Heteroskedasticity (H): | 1.18 | Skew: | 0.44 |
| Prob(H) (two-sided): | 0.47 | Kurtosis: | 2.28 |
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
y.plot()
ar1.predict(0, 250).plot()
<Axes: >
AR(1)과 절편
ar1c = SARIMAX(y, order=(1, 0, 0), trend='c').fit()
ar1c.summary()
| Dep. Variable: | production | No. Observations: | 218 |
|---|---|---|---|
| Model: | SARIMAX(1, 0, 0) | Log Likelihood | -1210.093 |
| Date: | Sun, 18 Jun 2023 | AIC | 2426.187 |
| Time: | 19:05:11 | BIC | 2436.340 |
| Sample: | 0 | HQIC | 2430.288 |
| - 218 | |||
| Covariance Type: | opg |
| coef | std err | z | P>|z| | [0.025 | 0.975] | |
|---|---|---|---|---|---|---|
| intercept | 131.5954 | 25.200 | 5.222 | 0.000 | 82.205 | 180.986 |
| ar.L1 | 0.6822 | 0.063 | 10.908 | 0.000 | 0.560 | 0.805 |
| sigma2 | 3845.3125 | 494.840 | 7.771 | 0.000 | 2875.443 | 4815.181 |
| Ljung-Box (L1) (Q): | 0.43 | Jarque-Bera (JB): | 22.64 |
|---|---|---|---|
| Prob(Q): | 0.51 | Prob(JB): | 0.00 |
| Heteroskedasticity (H): | 0.83 | Skew: | 0.78 |
| Prob(H) (two-sided): | 0.43 | Kurtosis: | 2.83 |
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
y.plot()
ar1c.predict(0, 250).plot()
<Axes: >
AR(2)와 절편
ar2 = SARIMAX(y, order=(2, 0, 0), trend='c').fit()
ar2.summary()
| Dep. Variable: | production | No. Observations: | 218 |
|---|---|---|---|
| Model: | SARIMAX(2, 0, 0) | Log Likelihood | -1209.547 |
| Date: | Sun, 18 Jun 2023 | AIC | 2427.094 |
| Time: | 19:05:57 | BIC | 2440.632 |
| Sample: | 0 | HQIC | 2432.562 |
| - 218 | |||
| Covariance Type: | opg |
| coef | std err | z | P>|z| | [0.025 | 0.975] | |
|---|---|---|---|---|---|---|
| intercept | 126.5603 | 28.948 | 4.372 | 0.000 | 69.823 | 183.298 |
| ar.L1 | 0.6295 | 0.091 | 6.881 | 0.000 | 0.450 | 0.809 |
| ar.L2 | 0.0650 | 0.098 | 0.662 | 0.508 | -0.128 | 0.257 |
| sigma2 | 3854.9323 | 525.738 | 7.332 | 0.000 | 2824.505 | 4885.360 |
| Ljung-Box (L1) (Q): | 0.20 | Jarque-Bera (JB): | 24.83 |
|---|---|---|---|
| Prob(Q): | 0.66 | Prob(JB): | 0.00 |
| Heteroskedasticity (H): | 0.84 | Skew: | 0.82 |
| Prob(H) (two-sided): | 0.45 | Kurtosis: | 2.85 |
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
y.plot()
ar2.predict(0, 250).plot()
<Axes: >