I hope this helps you . Consider the data below, , basic solution

No, it can't be vectorized. Write a for loop.

code :

```
v <- matrix(0, n, 2)
for (i in 1:n) {
sig11 <- sigma1[i]
sig21 <- rho * sqrt(sig11)
Sigma <- matrix(c(sig11, sig21, sig21, 1), 2)
v[i, ] <- mvrnorm(1, c(0,0), Sigma)
}
```

```
sig11 ^ 2 rho * sig11 * sig22
rho * sig11 * sig22 sig22 ^ 2
```

```
sig11 0
rho * sig22 sqrt(1 - rho ^ 2) * sig22
```

```
# mu1, mu2, sig11, sig22, rho can be
## length-n vectors
## scalars
## vectors than can be recycled to be length-n
birnorm <- function (n, mu1, mu2, sig11, sig22, rho) {
x1 <- rnorm(n)
x2 <- rnorm(n)
z1 <- sig11 * x1 + mu1
z2 <- rho * sig22 * x1 + sqrt(1 - rho ^ 2) * sig22 * x2 + mu2
cbind(z1, z2)
}
```

```
v <- birnorm(3, 0, 0, sqrt(sigma1), 1, 0.4)
```

```
mvrnorm2 <- function (n, mu, Sigma) {
sig11 <- sqrt(Sigma[1])
sig22 <- sqrt(Sigma[4])
rho <- Sigma[2] / (sig11 * sig22)
birnorm(n, mu[1], mu[2], sig11, sig22, rho)
}
```

```
mu <- c(0,0)
Sigma <- matrix(c(1,0.5,0.5,1),2)
library(microbenchmark)
microbenchmark(mvrnorm(1000, mu, Sigma), mvrnorm2(1000, mu, Sigma))
```