Here is the general approach you can follow to generate two correlated random variables. Let's suppose, X and Y are two random variable, such that:
$$X \sim N(\mu_1, \sigma_1^2)$$
$$Y \sim N(\mu_2, \sigma_2^2)$$
and $$cor(X,Y)=\rho$$
Now consider: $y=bx + e_i$, where $x$ $(=\frac{X-\mu_1}{\sigma_1}$) and $y$ $(=\frac{Y-\mu_2}{\sigma_2}$) both follow standard normal distribution , such that $cor(x,y)=\rho. $ For standard normal variate, $b= \rho$. So we have:
$$y=\rho x + e_i$$
Now, here is the algorithm, you can follow:
1) Generate $n$ standard normal variate for $x$.
2) Since, $e_i \sim N(0, 1-\rho^2)$. So generates $n$ normal variate as $e_i$ from normal distribution with mean 0 and variance $1-\rho^2$.
3) Get $y=\rho x + e_i$.
4) Convert your standard normal numbers back to Normal (remember correlation is independent of change of origin and scale)
R code for simulating correlated GBM:
corGBM <- function(n, r, t=1/365, plot=TRUE) {
#n is number of samples
#r is correlation
#t is tick step
x <- rnorm(n, mean=0, sd= 1)
se <- sqrt(1 - r^2) #standard deviation of error
e <- rnorm(n, mean=0, sd=se)
y <- r*x + e
X <- cumsum(x* sqrt(t))
Y <- cumsum(y* sqrt(t))
Max <- max(c(X,Y))
Min <- min(c(X,Y))
if(plot) {
plot(X, type="l", ylim=c(Min, Max))
lines(Y, col="blue")
}
return(cor(x,y))
}
#sample result
corGBM(10000,.85)
[1] 0.8523341
