AR Process

AR models are also known as autoregressive models. As the name autoregressive implies, it refers to a stochastic process in which the output of the model at time t depends on its own output before time t.
import statsmodels.api as sm
import numpy as np
import matplotlib.pyplot as plt
import japanize_matplotlib

Create data for AR process

Prepare functions to generate data

def create_ARdata(phis=[0.1], N=500, init=1, c=1, sigma=0.3):
    """Generating AR process data"""
    print(f"==AR({len(phis)}), #data={N}==")
    data = np.zeros(N)
    data[0] = init + np.random.normal(0, sigma)

    for t in range(2, N):
        res = c + np.random.normal(0, sigma)
        for j, phi_j in enumerate(phis):
            res += phi_j * data[t - j - 1]
        data[t] = res
    return data

φ < 1

plt.figure(figsize=(12, 6))
phis = [0.1]
ar1_1 = create_ARdata(phis=phis)
plt.plot(ar1_1)
plt.show()
==AR(1), #data=500==

png

φ = 1

plt.figure(figsize=(12, 6))
phis = [1]
ar1_2 = create_ARdata(phis=phis)
plt.plot(ar1_2)
plt.show()
==AR(1), #data=500==

png

φ > 1

plt.figure(figsize=(12, 6))
phis = [1.04]
ar1_2 = create_ARdata(phis=phis)
plt.plot(ar1_2)
plt.show()
==AR(1), #data=500==

png

AR(2)

plt.figure(figsize=(12, 6))
phis = [0.1, 0.3]
ar2_1 = create_ARdata(phis=phis, N=100)
plt.plot(ar2_1)
plt.show()
==AR(2), #data=100==

png

plt.figure(figsize=(12, 6))
phis = [0.1, -1.11]
ar2_1 = create_ARdata(phis=phis)
plt.plot(ar2_1)
plt.show()
==AR(2), #data=500==

png

Estimate the autoregressive (AR) model

from statsmodels.tsa.ar_model import AutoReg

res = AutoReg(ar1_1, lags=1).fit()

out = "AIC: {0:0.3f}, HQIC: {1:0.3f}, BIC: {2:0.3f}"
print(out.format(res.aic, res.hqic, res.bic))
AIC: 231.486, HQIC: 236.445, BIC: 244.124
print(res.params)
print(res.sigma2)
res.summary()
[1.03832755 0.07236388]
0.09199676371696269
AutoReg Model Results
Dep. Variable:yNo. Observations:500
Model:AutoReg(1)Log Likelihood-112.743
Method:Conditional MLES.D. of innovations0.303
Date:Sat, 13 Aug 2022AIC231.486
Time:01:55:17BIC244.124
Sample:1HQIC236.445
500
coefstd errzP>|z|[0.0250.975]
const1.03830.05220.0590.0000.9371.140
y.L10.07240.0451.6210.105-0.0150.160
Roots
RealImaginaryModulusFrequency
AR.113.8190+0.0000j13.81900.0000

Comments

(Comments will appear after approval)