Simulating the Autoregressive Process with ArmaProcess

The Autoregressive process is a fundamental principle in time series analysis and prediction. Essentially, it suggests that events within a time series are interrelated in a linear fashion with their preceding/lagged occurrences. Consequently, it is possible to forecast future events by modeling past data.

This example simulates the AR(2) Process

Importing necessary packages

from statsmodels.tsa.arima_process import ArmaProcess
import numpy as np
import pandas as pd

import matplotlib.pyplot as plt

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

Simulating AR(p) with ArmaProcess

To simulate the AR(2) process, create a numpy array with the constant and two coefficients (p=2). Because the ArmaProcess simulates both AR and MA, we only set coefficients for AR.

ma = np.array([1])
ar = np.array([1, -.63, .25, .41])
ar
array([ 1.  , -0.63,  0.25,  0.41])

Run and Visualize simulation

ar2_process = ArmaProcess(ar=ar, ma=ma).generate_sample(nsample=136)
fig = plt.figure(figsize=(10,4))
plt.plot(ar2_process, '-o')
plt.title('AR Process Simulation')
plt.tight_layout()
Simulating Autoregressive process with ArmaProcess

ACF and PACF Plots

fig, ax = plt.subplots(2, 1,figsize=(12,8))

plot_acf(ar2_process, lags=20, auto_ylims=True, ax=ax[0])
plot_pacf(ar2_process, lags=20, method='ywm', auto_ylims=True, ax=ax[1])
plt.show()
Time Series Plot for Moving Average Process Simulation