<p>Since you are stuck at just estimating <code>AR(1)</code> 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 <code>random walk</code> and  <code>random walk with drift</code>. Furthermore, the example code is in <code>Python</code>, however it should be self-explanatory and reproducible in <code>STATA</code>. </p>

<pre><code>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()
</code></pre>

<p><a href="https://i.sstatic.net/bh428.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/bh428.png" alt="AR(1) process simulation"></a></p>

CC BY-SA 3.0
2018-04-25T21:32:01.590Z
2018-04-25T21:32:01.590Z
An economist
<p>I wanted to use STATA for a kalman filter simulation and wanted to go about it the following way</p>

<pre><code>1. Generate iid error terms 
</code></pre>

<p>$$ 
{e_t}, {n_t}, {u_t} 
$$</p>

<pre><code>2. Generate three AR1 process like so
</code></pre>

<p>$$ 
             \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
$$</p>

<pre><code> 3. use Kalman filter to predict b_hat
</code></pre>

<p>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.</p>

<econometrics><stata><simulations>
AR 1 Process simulation
Lorien
# AR 1 Process simulation

(Asked by: username_0 on 2018-04-23)

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.

**Tags:** econometrics, stata, simulations


---

## 1 answers

### Answer by username_1 on 2018-04-25. Score: 0 | Accepted 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()


---

