Create and SimulateΒΆ
The definition of an ARMA model is:
where is the lag operator, a -dimensional vector of observed output variables, a -dimensional vector of white noise and a -dimensional vector of input variables. Since and are matrices in the lag shift operator, we have is a tensor to define auto-regression, is a tensor to moving-average and is a tensor to account for the input variables.
We create a simple ARMA model for a two dimensional output vector with matrices:
In order to set this matrix we just write the entries left to right, up to down into an array and define the shape of this array in a second array:
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
from pydse.arma import ARMA
AR = (np.array([1, .5, .3, 0, .2, .1, 0, .2, .05, 1, .5, .3]), np.array([3, 2, 2]))
MA = (np.array([1, .2, 0, .1, 0, 0, 1, .3]), np.array([2, 2, 2]))
arma = ARMA(A=AR, B=MA, rand_state=0)
Note that we set the random state to seed 0 to get the same results. Then by simulating we get:
sim_data = arma.simulate(sampleT=100)
sim_index = pd.date_range('1/1/2011', periods=sim_data.shape[0], freq='d')
df = pd.DataFrame(data=sim_data, index=sim_index)
df.plot()
(Source code, png, hires.png, pdf)
Let’s create a simpler ARMA model with scalar output variable.
AR = (np.array([1, .5, .3]), np.array([3, 1, 1]))
MA = (np.array([1, .2]), np.array([2, 1, 1]))
arma = ARMA(A=AR, B=MA, rand_state=0)
Quite often you wanna check the autocorrelation function and partial autocorrelation function:
from statsmodels.graphics.tsaplots import plot_pacf, plot_acf
sim_data = arma.simulate(sampleT=3000)
sim_index = pd.date_range('1/1/2011', periods=sim_data.shape[0], freq='d')
df = pd.DataFrame(data=sim_data, index=sim_index)
plot_acf(df[0], lags=10)
plot_pacf(df[0], lags=10)
plt.show()
Find a good introduction to ARMA on the Decision 411 course page.