1

I've seen attempts to answer this particular question before, but I'm either not understanding something (often a priori correct), or I'm missing a better way.

Essentially, what I want to do is draw n random samples from (say) a normal distribution, with specific mu and sigma, where there is a serial autocorrelation out to some max lag.

The two approaches I've seen used are either to (i) use mvrnorm in MASS, or (ii) filter the distribution.

The former appeals because you can explicitly specify the var-covar matrix, but the examples I've seen blow up when you want long time-series. For example, from How to generate series of pseudorandom autocorrelated numbers

 tmp.r <- matrix(0.2, n, n)
 tmp.r <- tmp.r^abs(row(tmp.r)-col(tmp.r))
 tmp.r[1:5, 1:5]
       [,1]  [,2] [,3]  [,4]   [,5]
[1,] 1.0000 0.200 0.04 0.008 0.0016
[2,] 0.2000 1.000 0.20 0.040 0.0080
[3,] 0.0400 0.200 1.00 0.200 0.0400
[4,] 0.0080 0.040 0.20 1.000 0.2000
[5,] 0.0016 0.008 0.04 0.200 1.0000

library(MASS) x <- mvrnorm(1, rep(0,n), tmp.r) acf(x, plot=FALSE, lag.max=5)

Autocorrelations of series ‘x’, by lag

 0      1      2      3      4      5 

1.000 0.246 0.065 0.056 0.032 -0.013

Does the trick, except (1) it assumes N(0,1), which I don't typically want, and (2) the bigger issue, for n large (say, you want 10,000 autocorrelated random numbers), this will blow your machine up in creating tmp.r in second line of the code (as a 10K x 10K matrix would be expected to do).

So, perhaps using a filter approach. Something like

 x <- filter(rnorm(10000), filter=rep(1,3), circular=TRUE)

Doesn't require specifying a huge VC matrix, but...I haven't been able to figure out how to specify the autocorrelation I want (no doubt because I don't entirely understand how filtering works in this context).

In essence, 10000 random numbers, with a given mean and variance, and specific lagged correlation structure is what I'm after.

Pointers to the obvious? Thanks in advance...

  • you can use arima.sim function to create autocorelated data with specified AR coefficients. You will need to figure out how to go from covariance matrix to AR coefs. If you look into the arima.sim code, you will see that it uses the filter function, but with the recursive=TRUE parameter – rep_ho May 18 '23 at 17:35
  • Does this question help? https://stats.stackexchange.com/questions/610341/how-to-generate-a-standard-normal-distributed-time-series-with-a-given-acf/611037#611037 – David Veitch May 18 '23 at 18:23

0 Answers0