Let $X \sim N(\mu, \sigma^2)$, and $Y \sim Bernoulli(p)$, with $Cov(X, Y) \neq 0$.
Is there an algorithm for generating samples from $X$ and $Y$ given we specify the covariance?
Let $X \sim N(\mu, \sigma^2)$, and $Y \sim Bernoulli(p)$, with $Cov(X, Y) \neq 0$.
Is there an algorithm for generating samples from $X$ and $Y$ given we specify the covariance?
Without losing generality we assume $\mu=0$ and $\sigma=1$.
\begin{eqnarray} Y|X &\sim & Ber(\Phi(a-X)) \\ X &\sim & N(0,1) \end{eqnarray} where $\Phi$ is cumulative distribution function of normal.
By this we have $E\left(\Phi\left( \frac{a-bX}{c}\right)\right)=\Phi\left( \frac{a}{c\sqrt{1+\frac{b^2}{c^2}}}\right)$ and you can calculate $E(XY)$ and the covariance.
Now generate $x_i$ from $N(0,1)$ and then generate $y_i$ from $Ber(\Phi(a-x_i))$. You can choose $a$ according the value of covariance.
R code, use correlation instead covariance
n <- 10000
a <- seq(-3, 3, len = 10) ## -2<a<2
cor_1 <- c()
for (i in 1:length(a)) {
x <- rnorm(n, 0, 1)
y <- rbinom(n, size = 1, prob = pnorm(a[i] - x))
cor_1[i] <- round(cor(x, y), 2)
}
> data.frame(a, cor_1)
a cor_1
1 -3.0000000 -0.23
2 -2.3333333 -0.32
3 -1.6666667 -0.44
4 -1.0000000 -0.52
5 -0.3333333 -0.56
6 0.3333333 -0.57
7 1.0000000 -0.52
8 1.6666667 -0.42
9 2.3333333 -0.32
10 3.0000000 -0.23
R code, use correlation instead covariance
n <- 10000
a <- seq(-3, 3, len = 10) ## -2<a<2
cor_1 <- c()
for (i in 1:length(a)) {
x <- rnorm(n, 0, 1)
y <- rbinom(n, size = 1, prob = pnorm(a[i] + x)) ## here is the change
cor_1[i] <- round(cor(x, y), 2)
}
> data.frame(a, cor_1)
a cor_1
1 -3.0000000 0.23
2 -2.3333333 0.32
3 -1.6666667 0.44
4 -1.0000000 0.52
5 -0.3333333 0.56
6 0.3333333 0.57
7 1.0000000 0.52
8 1.6666667 0.42
9 2.3333333 0.32
10 3.0000000 0.23