# AR 1 Process simulation

I wanted to use STATA for a kalman filter simulation and wanted to go about it the following way

1. Generate iid error terms


$${e_t}, {n_t}, {u_t}$$

2. Generate three AR1 process like so


$$\beta_t = a*b_{t-1} + u_t$$ $$x_t = c_0 + c_1*x_{t-1} + n_t$$ $$y_t = b_t*x_t + e_t$$

 3. use Kalman filter to predict b_hat


I have been able to use rnorm() to generate the iid error terms, but I am stuck at the second step. I know how to run a state space model to get the kalman filter estimates but AR1 has really tripped me up.

## 1 Answer

Since you are stuck at just estimating AR(1) models I can show you some example code for estimating the equations you gave. Keep in mind that I did not set slopes: $a$ and $c_1$ in the random walk and random walk with drift. Furthermore, the example code is in Python, however it should be self-explanatory and reproducible in STATA.

import numpy as np
import matplotlib.pyplot as plt

# Setting sample size
sample_size = 100

# Vectors with variables of interest
b = np.zeros(sample_size)
x = np.zeros(sample_size)
y = np.zeros(sample_size)

# IID random errors (normally distributed)
u = np.random.normal(size = sample_size)
n = np.random.normal(size = sample_size)
e = np.random.normal(size = sample_size)

# Drift
c_0 = 2

# Simulating each series
for t in range(sample_size):
b[t] = b[t-1] + u[t]
x[t] = c_0 + x[t-1] + n[t]
y[t] = b[t] * x[t] + e[t]

# Plotting the results
plt.rcParams["figure.figsize"] = [10,10]

plt.subplot(321)
plt.plot(b)
plt.title('Random walk: $b_t = b_{t-1} + u_t$')

plt.subplot(322)
plt.plot(x)
plt.title('Random walk with drifft: $x_t = c_0 + x_{t-1} + n_t$')

plt.subplot(323)
plt.plot(y)
plt.title('$y_t = b_t * x_t + e_t$')

plt.show() 