- Thread starter consuli
- Start date
- Tags autocorrelation

\( X_t = \phi X_{t-1} + \epsilon_t \)

Then

1. First you decide the initial value, say \( X_0 \)

2. Generate \( \epsilon_1 \sim \mathcal{N}(0, \sigma^2) \) (or any other distribution you want)

3. Obtain \( X_1= \phi X_0 + \epsilon_1 \)

4. Repeat step 2-3 for \( X_2, X_3, X_4, \ldots \) until you obtain enough for your time series.

in your original post you don't say that the defined correlation needs to be an explicit parameter.

you never specified if you only want to control the lag 1 autocorrelation or more/less than that

You could also just use the multivariate normal distribution and give it the covariance matrix you want. Note that you do need to keep that matrix positive definite. It could be tricky to guarantee that criteria if you get too crazy with trying to specify weird lag-1, lag-2, ..., lag-10 correlations.

so the first step is to generate the residulas here. for this i will sample from a bivariate normal distribution and i'm guessing it would be a auto-correlated process with a lag of 1 and the auto-correlation would be 0.5. the key is basically to intercalate the two columns of correlated random vectors so you end up with only 1 vector with double the length you initially specified.

so something like this:

Code:

```
library(MASS)
mu <- c(0,0)
S <- matrix(c(1,.5,.5,1),2,2)
N <- 500
datum <- mvrnorm(N,mu,S)
e <- c(rbind(datum[,1], datum[,2]))
```

Code:

```
x <- 1*rnorm(1000)
y <- 1 + x +e
mod1 <- lm(y~x)
```

Code:

```
mod1 <- lm(y~x)
durbinWatsonTest(mod1)
> durbinWatsonTest(mod1)
lag Autocorrelation D-W Statistic p-value
1 0.2655003 1.467445 0
Alternative hypothesis: rho != 0
```

i think the issue is just to make sure you can change between-column dependencies to become between-row dependencies...right? wrong? maybe?

Code:

```
j <- acf(e)
j
```

Code:

```
[COLOR=#000000] 0 1 2 3 4 5 6 7 8 9 10 11 1.000 0.198 -0.014 0.014 -0.031 -0.005 -0.046 -0.055 -0.015 0.030 -0.005 -0.049 12 13 14 15 16 17 18 19 20 21 22 23 -0.029 -0.020 -0.067 0.000 0.014 -0.029 0.013 -0.013 0.014 0.024 -0.047 0.007 24 25 26 27 28 29 30 -0.006 0.042 0.031 0.008 0.012 0.032 0.066
[/COLOR]
```

weird... :/

You could also just use the multivariate normal distribution and give it the covariance matrix you want.

Dason, did that mean the following?

Code:

```
n=10000
x= matrix(ncol= 5, nrow= n)
x1= rnorm(n)
x[ , 1]= x1
x[ , 2]= c(0, x1[ 1:(n-1)])
x[ , 3]= c(rep(0, 2), x1[1:(n-2)])
x[ , 4]= c(rep(0, 3), x1[1:(n-3)])
x[ , 5]= c(rep(0, 4), x1[1:(n-4)])
head(x)
covm= as.matrix(read.table( text= "
1.00 0.50 0.35 0.15 0.10
0.50 1.00 0.25 0.12 0.05
0.35 0.25 1.00 0.08 0.03
0.15 0.12 0.08 1.00 0.01
0.10 0.05 0.03 0.01 1.00
", stringsAsFactors= F
))
x= x %*% chol(covm)
cor(x)
acf(x[ , 1], plot= F)
[/FONT][/COLOR][COLOR=#000000][FONT=Times New Roman][COLOR=#000000][FONT=Times New Roman]
```

Does not work!

> acf(x[ , 1], plot= F)

Autocorrelations of series ‘x[, 1]’, by lag

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

1.000 0.000 0.003 0.003 -0.024 0.008 0.005 -0.013 -0.005 -0.017 0.004 -0.006 -0.017 0.009 -0.006 0.002

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

0.010 0.013 -0.015 0.015 0.005 0.001 -0.013 0.001 0.004 -0.008 -0.004 0.021 -0.024 -0.001 -0.005 -0.012

32 33 34 35 36 37 38 39 40

-0.003 -0.003 -0.010 0.008 0.011 0.021 0.008 0.006 0.010

>

[/FONT][/COLOR]

Code:

```
n=10000
x= matrix(ncol= 5, nrow= n)
x1= rnorm(n)
x[ , 1]= x1
x[ , 2]= c(0, x1[ 1:(n-1)])
x[ , 3]= c(rep(0, 2), x1[1:(n-2)])
x[ , 4]= c(rep(0, 3), x1[1:(n-3)])
x[ , 5]= c(rep(0, 4), x1[1:(n-4)])
colnames(x)= c("x1", "x2", "x3", "x4", "x5")
head(x)
covm= as.matrix(read.table( text= "
1.00 0.50 0.35 0.15 0.00
0.50 1.00 0.00 0.00 0.00
0.35 0.00 1.00 0.00 0.00
0.15 0.00 0.00 1.00 0.00
0.00 0.00 0.00 0.00 1.00
", stringsAsFactors= F
))
x= x %*% chol(covm)
colnames(x)= c("x1", "x2", "x3", "x4", "x5")
head(x)
cor(x)
acf(x[ , 1], lag.max= 5, plot= F)
acf(x[ , 2], lag.max= 5, plot= F)
acf(x[ , 3], lag.max= 5, plot= F)
acf(x[ , 4], lag.max= 5, plot= F)
acf(x[ , 5], lag.max= 5, plot= F)
```

http://www.mapleprimes.com/questions/40278-Generating-A-Sequence-Of-Autocorrelated

The maple example translated to R generates a defined autocorrelation.

Code:

```
n= 1000
p= 0.5
x_tm0= rnorm(n)
x_tm1= c(0, x_tm0[1:(n-1)])
# x_tm1
b= rbinom(n, 1, p)
# b_tm1
x1= b* x_tm1+ (1-b)* x_tm0
acf(x1, lag.max= 5, plot= F)
```

So I thought about mixing x and x(t-1) together.

Code:

```
x2= p* x_tm1+ (1-p)* x_tm0
acf(x1, lag.max= 5, plot= F)
```

It does exactly what you asked for and is probably the easiest to interpret what is going on and easy to code.

It does exactly what you asked for and is probably the easiest to interpret what is going on and easy to code.

An AR(1) process offers a direct map to the lag-1 correlation as I've said many times before.

Last edited: