Let \(X \sim N(\mu, \sigma^2)\) and \[ Z = \frac{X - \mu}{\sigma}. \] Then \(Z \sim N(0, 1)\) and \(X = \mu + \sigma Z\).
Instead of sampling from a centered normal distribution \(N(\mu, \sigma^2)\), one samples from a standard normal \(N(0, 1)\) multiplying by \(\sigma\) and then adding \(\mu\). In the context of Bayesian hierarchical modeling, this idea is called noncentering.
par(mfrow = 1:2, mai = c(.6, .6, .1, .1), mgp = c(2, .7, 0))
rnorm(2000, mean = 400, sd = 50) |>
hist(main = "")
(400 + 50 * rnorm(2000, mean = 0, sd = 1)) |>
hist(main = "")
To apply the idea of noncentering to the multivariate case, consider the three equivalent distribution statements \[ \begin{aligned} \begin{pmatrix} X_{1i} \\ X_{2i} \end{pmatrix} &\sim \text{MVN} \left(\begin{pmatrix} \mu_1 \\ \mu_2 \end{pmatrix}, \Sigma = \begin{pmatrix} \sigma^2_1 & \varrho \sigma_1 \sigma_2 \\ \varrho \sigma_1 \sigma_2 & \sigma^2_2 \end{pmatrix}\right) \\ \begin{pmatrix} X_{1i} \\ X_{2i} \end{pmatrix} &= \begin{pmatrix} \mu_1 \\ \mu_2 \end{pmatrix} + L \cdot \begin{pmatrix} Z_{1i} \\ Z_{2i} \end{pmatrix}, \quad \begin{pmatrix} Z_{1i} \\ Z_{2i} \end{pmatrix} \sim \text{MVN} \left(\begin{pmatrix} 0 \\ 0 \end{pmatrix}, \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}\right) \\ \begin{pmatrix} X_{1i} \\ X_{2i} \end{pmatrix} &= \begin{pmatrix} \mu_1 \\ \mu_2 \end{pmatrix} + L \cdot \begin{pmatrix} Z_{1i} \\ Z_{2i} \end{pmatrix}, \quad Z_{1i} \sim N(0, 1), \quad Z_{2i} \sim N(0, 1), \end{aligned} \] where \[ L = \begin{pmatrix} \sigma_1 & 0 \\ \varrho \sigma_2 & \sqrt{1 - \varrho^2} \sigma_2 \end{pmatrix} \] is the Cholesky factor (“matrix square root”) of the covariance matrix \(\Sigma\) such that \(\Sigma = LL'\). The final equivalence uses the fact that it is more efficient to repeatedly sample from univariate normal distributions than from a single standard MVN distribution.
par(mfrow = c(1, 3), mai = c(.6, .6, .1, .1), mgp = c(2, .7, 0))
mu <- c(200, 500)
S <- matrix(c(30^2, .7*30*60, .7*30*60, 60^2), 2, 2)
(L <- chol(S) |> t()) # Cholesky factor
## [,1] [,2]
## [1,] 30 0.00000
## [2,] 42 42.84857
## Centered MVN
MASS::mvrnorm(1000, mu, S) |>
plot(xlim = c(50, 350), ylim = c(250, 750), col = "darkblue")
abline(v = mu[1], h = mu[2], lty = 2)
## Standard MVN
t(mu + L %*% t(MASS::mvrnorm(1000, c(0, 0), diag(2)))) |>
plot(xlim = c(50, 350), ylim = c(250, 750), col = "darkblue")
abline(v = mu[1], h = mu[2], lty = 2)
## Independent univariate N
t(mu + L %*% rbind(rnorm(1000), rnorm(1000))) |>
plot(xlim = c(50, 350), ylim = c(250, 750), col = "darkblue")
abline(v = mu[1], h = mu[2], lty = 2)
In order to make it easier to specify realistic priors, it may help to factorize both the covariance matrix and its Cholesky factor, \[ \begin{aligned} \Sigma &= D_\sigma \mathrm{P} D_\sigma = \begin{pmatrix} \sigma_1 & 0 \\ 0 & \sigma_2 \end{pmatrix} \begin{pmatrix} 1 & \varrho \\ \varrho & 1 \end{pmatrix} \begin{pmatrix} \sigma_1 & 0 \\ 0 & \sigma_2 \end{pmatrix}, \\ L &= D_\sigma L_\mathrm{P} = \begin{pmatrix} \sigma_1 & 0 \\ 0 & \sigma_2 \end{pmatrix} \begin{pmatrix} 1 & 0 \\ \varrho & \sqrt{1 - \varrho^2} \end{pmatrix}. \end{aligned} \] The Stan user guide recommends an LKJ prior for the Cholesky factor (and some positive prior distribution, like a half-Cauchy, for \(\sigma\)).
Rho <- matrix(.7, 2, 2) # correlation matrix
diag(Rho) <- 1
(L_Rho <- chol(Rho) |> t())
## [,1] [,2]
## [1,] 1.0 0.0000000
## [2,] 0.7 0.7141428
diag(c(30, 60)) %*% L_Rho # factorized Cholesky factor
## [,1] [,2]
## [1,] 30 0.00000
## [2,] 42 42.84857
s <- "
functions {
matrix lkj_rng(int K, real eta) {
return lkj_corr_rng(K, eta);
}
matrix lkj_chol_rng(int K, real eta) {
return lkj_corr_cholesky_rng(K, eta);
}
}
"
expose_stan_functions(stanc(model_code = s))
lkj_rng(5, 2) |> round(2) # sample Rho
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.00 -0.46 0.10 0.05 -0.21
## [2,] -0.46 1.00 0.03 -0.04 0.72
## [3,] 0.10 0.03 1.00 0.81 -0.09
## [4,] 0.05 -0.04 0.81 1.00 -0.02
## [5,] -0.21 0.72 -0.09 -0.02 1.00
lkj_chol_rng(5, 2) |> round(2) # sample L_Rho
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.00 0.00 0.00 0.00 0.00
## [2,] -0.46 0.89 0.00 0.00 0.00
## [3,] 0.10 0.09 0.99 0.00 0.00
## [4,] 0.05 -0.02 0.82 0.57 0.00
## [5,] -0.21 0.71 -0.13 0.20 0.63