1
$\begingroup$

I am trying to prove via some Monte Carlo simulation that the variance of a random walk equals $t*σ^2$ in R. I am running the following code, 180 times, to find the variance of 180 different random walks with the same error standard deviation.

Although I can see in graphs that the variance is increasing, the variance itself is very far away than the expected. The theory would say that expected variance for n = 1000, would be $t*σ^2 = 1000 * 0.5^2 = 250$ but in reality I get a much lower variance. Running 180 simulations, I get a mean of ~40 for the variance, instead of 250. Even the max variance in 180 iterations, rarely goes above 200.

Can somebody let me know what I am missing here? If I run an AR model (e.g. adding $0.8*y[t-1]$) the variance is approaching the expected from the formula. But not for the random walk.

n <- 1000
cumulative_var <- 0

for (h in 1:180) {
  
  et <- rnorm(n, mean = 0 , sd = 0.5)

  yt <- et[1]
  vars <- 0 


    for (i in 2:n){
      yt[i] <- yt[i-1] + et[i]
      vars[i] <- var(yt)
  
                  }
  
  cumulative_var[h] <- var(yt)
  
}

hist(cumulative_var)
mean(cumulative_var)

enter image description here

$\endgroup$

1 Answer 1

2
$\begingroup$

$n\sigma^2$ is the variance of the final value, i.e. $y_n$, however you calculate the variance based on the historical values of $y_t$, that's one reason you get lower values.

If you change your code to the following, you'll obtain values closer to $250$, estimated over $180$ realisations of $y_t$. Of course, to build an histogram of variances, you'll need an extra outer loop.

cumulative_var[h] <- yt[n]
$\endgroup$
1
  • 1
    $\begingroup$ I was totally brainfreezed, that is correct.I changed the code as below, and now I get the correct answer not the prettiest code and takes some time to run,in case somebody wants to use it, but it works): n <- 1000 cumulative_var <- 0 cumulative_yt <- 0 for (x in 1:100){ for (h in 1:100) { et <- rnorm(n, mean = 0 , sd = 0.5) yt <- et[1] vars <- 0 for (i in 2:n){ yt[i] <- yt[i-1] + et[i] } } cumulative_var[x] <- var(cumulative_yt) } plot(cumulative_var) mean(cumulative_var) $\endgroup$
    – Arg
    Commented Jan 13, 2021 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.