How can you simulate correlated beta distributed variables?
This question has attracted a lot of views, suggesting (future) readers might still be interested in the titular question. I would like to add an approach, with some examples demonstrating that it works. A general description for how to implement it, as well as an implementation in R, is included at the end.
The approach I will be highlighting here, is by using the probability integral transform, which states that samples from a known, continuous probability distribution can be converted to a uniformly distributed sample between $0$ and $1$, using that original distribution's inverse cumulative distribution function. The resulting (correlated) probabilities can then be used as input for the beta distribution's quantile function.
This method roughly preserves rank correlation between both variables. Linear correlation is not preserved, but this is not a reasonable demand, as for many choices of $\alpha$ and $\beta$, even perfect correlation does not yield a linear relationship between both variables.
Equal parameters for both distributions
Below are the results for various values of $\rho$, used as input to create two beta distributed variables $x$ and $y$ with the same shape parameters:
$$\begin{aligned}\\x &\sim\mathsf{Beta}(5, 1) \\ \\ y &\sim\mathsf{Beta}(5, 1) \\\\ \end{aligned}$$
Different parameters for both distributions
Below are the results for various values of $\rho$, used as input to create two beta distributed variables $x$ and $y$ with a different set of shape parameters:
$$\begin{aligned}\\x &\sim\mathsf{Beta}(1, 6) \\ \\ y &\sim\mathsf{Beta}(3, 3) \\\\ \end{aligned}$$
(Click on the images to see the full-resolution version.)
How to implement this approach
The process works as follows:
- Draw samples from a bivariate normal distribution $$\begin{aligned}\begin{pmatrix}a \\ b\end{pmatrix} &\sim \mathcal{N}\!\left(\mathbf{0}, \mathbf{\Sigma} \right), \\\\ \mathbf{\Sigma} &= \begin{pmatrix} 1 & \rho \\ \rho & 1 \end{pmatrix}\end{aligned}$$ for some given correlation $\rho$ (e.g., using
MASS::mvrnorm
).
- Convert these samples to probabilities, using the inverse cumulative distribution function (
pnorm
).$^\dagger$
- Use the resulting (correlated) probabilities to compute quantiles of the target distribution (
qbeta
).$^\ddagger$
This works because simulating from a multivariate normal distribution allows us to directly specify a covariance matrix, and the probability integral transform allows us to convert those samples to (correlated) probabilities. We then simply ask: "Up till what point does the target distribution equal this probability?"
Implementation in R
require("MASS")
require("sfsmisc")
set.seed(2024)
n <- 1e4
alpha <- c(1, 6)
beta <- c(3, 3)
rho <- 0.99
Sigma <- matrix(c(1, rho,
rho, 1), nrow = 2, byrow = TRUE)
XY <- mvrnorm(n, c(0, 0), Sigma)
PIT <- pnorm(XY)
x <- qbeta(PIT[, 1], alpha[1], beta[1])
y <- qbeta(PIT[, 2], alpha[2], beta[2])
plot(y ~ x, col = densCols(y, x), pch = 16, bty = "n", axes = FALSE,
main = bquote(rho == .(rho)), cex.main = 4)
eaxis(1)
eaxis(2)
$^\dagger$: This part is the probability integral transform.
$^\ddagger$: This step can be easily adapted for other distributions than beta.
Y1
orY2
from being outside $(0,1)$ so they cannot be beta distributed? $\endgroup$