Simulating Data

There are often times you will be required to write complicated code to estimate some model. For example, you might need to write a program that estimates a model using General Method of Moments (GMM) or Simulated Method of Moments (SMM). As complexity increases, the chance of making a mistake increases exponentially. So how do you know that your program is estimating correctly or if there is a bug in your code? One way to find out is by simulating a bunch of artificial data where you know what all of the true parameters are, then have your code estimate the parameters of the simulated data. Comparing your estimated values to the actual values will give you a pretty good idea (but not certainty) if your code is working correctly.

  • In this section we will simulate a random walk, a random walk with drift, an AR(1) process, an AR(2) process and an MA(1) process.

  • To generate these stochastic paths we will need to make use of Matlab’s random number generater “randn()”. This function produces a psuedorandom scalar drawn from a standard normal distribution. For more information type “help randn” in the Matlab Command Window.

  • Another helpful function when you are testing code using a random number generator is “rng()”. This function changes between different random number generators and sets the seed for the generator. You could place a piece of code like rng(123456) above a certain section of code and it would reproduce the same set of random numbers every time you run the code.

  • Throughout this section we will assume the random variable \(\epsilon_{t} \sim iid N(0,\sigma^2)\)

Gaussian Random Walk

  • A Guassian random walk can be thought of as the sum of a series of random variables that are \(iid N(0,\sigma^2)\).

  • To generate a random walk path, first choose how many time steps you want the path to be. Next, create a vector for storing the path, choose an initial value and a value for \(\sigma\). Then, write a for loop where \(y_{t} = y_{t-1} + \epsilon_{t}\).

  • For example, your code could look like this:
_images/RW.png

Gaussian Random Walk with Drift

  • A Gaussian random walk with drift is the same as a random walk except at each time step the drift rate \(\mu\) is added to the path.

  • The setup is the same as above except you need to choose a drfit rate \(\mu\) and add this term into your for loop so that \(y_{t} = \mu + y_{t-1} + \epsilon_{t}\)

  • For example, your code could look like this:
_images/RWD.png

AR(1) Process

  • An AR(1) process is similar to a random walk except the effect that the past has on the future values of the time path decreases with time. We can write an AR(1) as \(y_{t} = \phi y_{t-1} + \epsilon_{t}\)

  • The code will look similar to the random walk except we need to choose a persistence term \(|\phi| < 1\) so that its stable and stationary.

  • We can also add a constant to the process \(y_{t} = c + \phi y_{t-1} + \epsilon_{t}\)

  • For example, your code could look like this:
_images/AR1.png

AR(2) Process

  • An AR(2) process is similar to the AR(1) except there are now two persistence terms associated with the two lagged values of Y. We can write an AR(2) as \(y_{t} = \phi_{1} y_{t-1} + \phi_{2} y_{t-2} + \epsilon_{t}\)

  • We need to choose these values such that the process is stable and stationary. One way to do this is to write the process in matrix notation:
\[\begin{split}\begin{bmatrix} y_{t} \\ y_{t-1} \end{bmatrix} = \begin{bmatrix} \phi_{1} & \phi_{2} \\ 1 & 0 \end{bmatrix} \begin{bmatrix} y_{t-1} \\ y_{t-2} \end{bmatrix} + \begin{bmatrix} 1 \\ 0 \end{bmatrix} \epsilon_{t}\end{split}\]
  • And find the eigenvalues of the matrix:
\[\begin{split}\begin{bmatrix} \phi_{1} & \phi_{2} \\ 1 & 0 \end{bmatrix}\end{split}\]
  • By solving:
\[\begin{split}\begin{vmatrix} \phi_{1}-\lambda & \phi_{2} \\ 1 & -\lambda \end{vmatrix} = 0\end{split}\]
  • Choosing \(\phi_{1},\phi_{2}\) such that \(|\lambda_{i}|<1\) for \(i=1,2\) will guarantee a stable and stationary process

  • For example, your code could look like this:
_images/AR2.png

MA(1)

  • The current value of an MA(1) process depends on a current shock and the persistence of the previous shock. We can write an MA(1) process as \(y_{t} = \mu + \epsilon_{t} + \theta\epsilon_{t-1}\).

  • The trick to simulating an MA(1) process is to generate and save all of the shock values before you generate Y. We can do this using randn(time,1) to generate shocks for every period.

  • Now the processs is straight forward and your code might look like:
_images/MA1.png