Part 2

In this part you will simulate a sample of T = 100 observations from a stationary AR(1) process with \(\alpha=0.8\), \(\sigma^2 = 1\)

# TODO
# import numpy

Generate a \(T\) vector of innovations using gen = np.random.default_rng(100)

# TODO
#eps = ...

Using \(\varepsilon_0\), generate \(z_0\) from the stationary distribution of \(\mathbf{z}\). Then, using a for loop, generate the remaining 99 realizations of \(\mathbf{z}\)

# TODO
#for ....

It would be nice if we could more easily build matrices with simple structure like that of \(\boldsymbol B\) without having the specify each element individually.

\[\begin{split} \underset{\boldsymbol B}{\underbrace{\left[ \begin{array}{cccccccc} \sqrt{1-\alpha^2} & 0 & 0 & 0 & \cdots & 0 & 0 & 0\\ -\alpha & 1 & 0 & 0 & \cdots & 0 & 0 & 0\\ 0 & -\alpha & 1 & 0 & \cdots & 0 & 0 & 0\\ \vdots & \vdots & \vdots & \vdots & \cdots & \vdots & \vdots & \vdots\\ 0 & 0 & 0 & 0 & \cdots & 0 & -\alpha & 1 \end{array}\right]}} \underset{\boldsymbol z}{\underbrace{ \left[ \begin{array}{c} z_{0}\\ z_{1}\\ z_{2}\\ \vdots\\ z_{T} \end{array} \right] }} = \underset{\mathbf{\varepsilon}}{\underbrace{\left[ \begin{array}{c} \varepsilon_{0}\\ \varepsilon_{1}\\ \varepsilon_{2}\\ \vdots\\ \varepsilon_{T} \end{array} \right] }} \end{split}\]

Fortunately, we can. Ignoring the the (0,0) element for a moment, the rest of \(\boldsymbol B\) can be created using np.eye(), np.diag() and np.tile() functions. After that, the (0,0) element can be added manually. Check these functions’ documentation and create the \(T \times T\) matrix \(\boldsymbol B\).

# TODO
#B =

Note

There are other ways to quickly build such matrices. If you are interested, check the documentation of the toeplitz() function from the scipy.linalg package

Verify that the equality \(\mathbf{z} = \boldsymbol B^{-1} \boldsymbol\varepsilon\) holds

# TODO
#np.testing.assert_array_almost_equal(...)

Using \(\boldsymbol B\), compute the first T theoretical autocovariances of \(z_t\)

#TODO
#ar1_acov1 = ...
#assert len(ar1_acov1) == T

Using statsmodels, define an AR(1) object with the same parameters as given above, and compute the T theoretical autocovariances

#TODO
#from statsmodels....
#ar1_process = ...
#ar1_acov2 = ar1_process....

Confirm that ar1_acov1 and ar1_acov2 are the same (or numerically very close)

#TODO