Part 1

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

# TODO
# import numpy

Generate a \(4D\) 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}\)

Hint: see here

Using a for loop, generate the remaining 3 realizations of \(\mathbf{z}\)

# TODO
#for ....

As discussed in a lecture (see again here) there is a linear relationship between \(\mathbf{z}\) and \(\boldsymbol \varepsilon\), In particular, there is a \((T+1)\times(T+1)\) matrix \(\boldsymbol A\) such that

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

Build the \(4x4\) matrix \(\boldsymbol A\), and confirm that the equality \(\mathbf{z} = \boldsymbol A \boldsymbol\varepsilon\) holds

# TODO
#A =

#np.testing.assert_array_almost_equal(z, A @ eps)

An alternative (and perhaps simpler) way to represent the linear relationship between \(\mathbf{z}\) and \(\boldsymbol\varepsilon\), is with a matrix \(\boldsymbol B\), such that

\[\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}\]

Using pen and paper, confirm for yourself that this relationship indeed holds. Then, build the matrix \(\boldsymbol B\) and check programmatically if it is invertible (the matrix rank is equal to the number of columns/rows, i.e. it is full rank)

Hint: check the documentation of Numpy for a function that returns the rank of a matrix. Use an assertion to confirm that \(\boldsymbol B\) is full rank.

# TODO
#B =

# assert full rank...

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

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