How to generate series of pseudorandom autocorrelated numbers

Say I am Ok with the numbers getting drawn from a standard normal distribution, but I also want the autocorrelation of the series at lag 1 to be a specific number. How can I generate such a series of numbers?

• To generate correlated random variables, you need joint distributions. – Xi'an Nov 24 '15 at 19:29
• Are you looking to specify the sample autocorrelation or the population autocorrelation? – Glen_b -Reinstate Monica Nov 24 '15 at 21:52

There are a couple of options.

If you have a tool that generates multivariate normal from a given correlation/covariance matrix (e.g. the mvrnorm function in the MASS package for R) then you can create your correlation matrix to represent an autocorrelation of $\rho$ by creating a matrix with 1's on the diagonal, $\rho$ in the positions 1 away from the diagonal, $\rho^2$ for those elements that are 2 away from the diagonal, $\rho^3$ for those 3 away from the diagonal, etc.

The other option is to loop and use the idea of an AR model. Start by generating a random value with mean 0 and SD 1, then generate the second value from a normal distribution with mean equal to $\rho$ times the previous value and SD 1, then the 3rd value comes from a normal with mean $\rho$ times the 2nd value and SD 1, etc.

edit

Here is an example of the first method using R:

> n <- 500
> 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
>

And here is the second method:

> n <- 500
> x <- numeric(n)
> x[1] <- rnorm(1)
> for( i in 2:n ) {
+   x[i] <- rnorm(1, 0.2*x[i-1], 1)
+ }
> acf(x, plot=FALSE, lag.max=5)

Autocorrelations of series ‘x’, by lag

0      1      2      3      4      5
1.000  0.224  0.055 -0.033 -0.004  0.047
• Thank you very much, I will try the first idea. Just to make sure I understand you correctly, can you please show me an example of 3x3 matrix, with autocorrelation 0.2? I want to see exactly what "positions 1 away from the diagonal" means. – The Baron Nov 24 '15 at 19:48
• @TheBaron, I added an R example above so you can see part of the correlation matrix (or the whole thing if you run the code yourself). – Greg Snow Nov 24 '15 at 20:21
• In addition to filtering white noise and using the Cholesky factorization of the autocovariance matrix there are also spectral methods (see e.g. the work of Shinozuka and Jan) that can be useful for generating long sequences or multidimensional Markov random fields. – Brian Borchers Nov 24 '15 at 21:25