0

I want to create two random variables $X \sim N(0,1)$ and $Y \sim N(0,1)$ that satisfy $E(X,Y)=0.5 $

That is I want to create $Z=(X,Y)^\top $ with a joint bivariate normal distribution

$ Z \sim N\left( \left(\begin{array}{c} 0\\ 0 \end{array}\right) , \left(\begin{array}{cc} 1 & 0.5\\ 0.5 & 1 \end{array}\right) \right) $.

How do I code this in R?

Thanks.

user45370
  • 1
  • 1
  • This question appears to be off-topic give that it is mostly about R code. – Andy May 11 '14 at 07:47
  • What does $E(X,Y)$ mean? Did you mean $E(X \cdot Y)$? – QuantIbex May 11 '14 at 09:02
  • 1
    This [answer](http://stats.stackexchange.com/a/64988/27403) describes how this can be done using a Cholesky decomposition of the covariance matrix. Of course, you could use `mvrnorm` from [MASS](http://stat.ethz.ch/R-manual/R-patched/library/MASS/html/mvrnorm.html) but it is less fun. – QuantIbex May 11 '14 at 09:08
  • Duplicate. See [here](http://stats.stackexchange.com/questions/38856/how-to-generate-correlated-random-numbers-given-means-variances-and-degree-of) or [here](http://stats.stackexchange.com/questions/24257/how-to-simulate-multivariate-outcomes-in-r) – Glen_b May 11 '14 at 09:25

1 Answers1

1

I suppose you are looking for the mvtnorm package:

> library(mvtnorm)
> sigma <- matrix(c(1, 0.5, 0.5, 1), nrow = 2)
> x <- rmvnorm(5000, mean = c(0,0), sigma = sigma, method = "chol")
> colMeans(x)
[1] 0.02096549 0.03626787
> var(x)
          [,1]      [,2]
[1,] 1.0061570 0.4920715
[2,] 0.4920715 1.0087832
Sergio
  • 5,628
  • 2
  • 11
  • 27