2

I have three variables A, B, and C with a time-series of returns. I would to run a random sampling using mvrnorm from package MASS, to generate 30 values for A, B and C.

I would like to be able to repeat this operation 10000 times, each time generating 30 values for A, B, C.

I execute one iteration using:

sim.ret = mvrnorm(n = 30, mu = mu, Sigma = sigma)

How can I run this function 10,000 times? I then use the result of each iteration to do some computations.

Any help would be appreciated! Thanks.

Mayou
  • 8,498
  • 16
  • 59
  • 98
  • Not a duplicate at all. My question is regarding a random generation of a whole matrix, rather than a vector. I am looking for getting a result as a list of matrices. None of this is addressed in the links you posted. – Mayou Oct 16 '13 at 17:48
  • Oups.. You are right, my bad! – Mayou Oct 16 '13 at 17:53

1 Answers1

2

Use replicate():

N = 10000
results = replicate(N, mvrnorm(n = 30, mu = mu, Sigma = sigma))
print(head(results, 10))
Fernando
  • 7,785
  • 6
  • 49
  • 81
  • Thanks! Would `results` be a list? – Mayou Oct 16 '13 at 17:29
  • On the simplest case the result is a matrix, where each column is a replicate. – Fernando Oct 16 '13 at 17:30
  • Well, `mvrnorm(n = 30, mu = mu, Sigma = sigma))` is a matrix with 3 columns and 30 rows. But when I apply your suggestion, I do not get a bunch of matrices, I get a vector instead.. – Mayou Oct 16 '13 at 17:47
  • No, you probably getting a *list* or *array* back, where each element is a replicate (you can loop through lists using *lapply* for example). – Fernando Oct 16 '13 at 17:51
  • weird.. I am not getting a list.. – Mayou Oct 16 '13 at 17:52
  • So it's an *array* object. – Fernando Oct 16 '13 at 17:52
  • @Mariam try `simplify = TRUE` in `replicate`. You should get a `matrix`. – Simon O'Hanlon Oct 16 '13 at 17:54
  • But this would mess up the structure of the result. I explicitely need to return a list of matrices, not an array. – Mayou Oct 16 '13 at 17:54
  • 1
    that's almost the same structure...and arrays are faster i guess. – Fernando Oct 16 '13 at 17:55
  • @SimonO101 I would get a matrix that way, but not structured as I want. So for each iteration, I need to get a matrix with nc = 3 and nr = 30. For 10,000 iterations, I expect a list with 10000 elements, each element is a matrix with nc = 3 and nr = 30. – Mayou Oct 16 '13 at 17:56
  • 1
    @Mariam so `simplify = FALSE` then!!! Look at the documentation, that would have cleared it up instantly. The default for `simplify` is `"array"`. – Simon O'Hanlon Oct 16 '13 at 17:57
  • @SimonO101 Perfect! I can accept your suggestion if you post it as an answer!! Thanks again – Mayou Oct 16 '13 at 17:59
  • 1
    @Mariam if you want to `apply`some function to each element of your list you would use `lapply` to do this to an array, you can use `apply` on the appropriate dimension, in your case the 3rd dimension, so `apply( results , 3 , myfunction )` will work nicely. Add my suggestion as an edit to this answer. It doesn't warrant a whole new answer. – Simon O'Hanlon Oct 16 '13 at 17:59
  • @Fernando +1 for `replicate()`. SimomO101 adjusted it nicely for my purpose! – Mayou Oct 16 '13 at 18:01
  • 2
    Thanks. The R help can be 'hard' to read sometimes, but it's worth a try. – Fernando Oct 16 '13 at 18:03