Sampling from the noncentered MVN distribution

Univariate normal distribution

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 = "")

Multivariate normal distribution

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.

Examples

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)

Priors

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\)).

Examples

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

Hierarchical regression with correlated intercepts and slopes

See

Data

dat <- read.table("../../04-HierarchicalModels/FP/exer/NicenboimEEG.txt",
                  header = TRUE)
lattice::xyplot(n400 ~ cloze, dat, groups = subj,
                type = c("g", "p", "r"), ylim = c(-25, 25))

dat$ccloze <- dat$cloze - mean(dat$cloze)  # center

datlist <- c(dat,
  nsubj = unique(dat$subj) |> length(),
      N = nrow(dat)
)

Model

s <- "
data {
  int<lower=1> N;
  int<lower=1> nsubj;
  array[N] int<lower=1, upper=nsubj> subj;  // subj id
  row_vector[N] ccloze;
  vector[N] n400;
}
parameters {
  vector[2]               mu;     // group intercept and slope
  vector<lower=0>[2]      tau;    // subj sd's
  real<lower=0>           sigma;  // error sd
  cholesky_factor_corr[2] L_Rho;  // Cholesky factor of correlation matrix
  matrix[2, nsubj]        z;      // by-subj standardized intercepts/slopes
}
transformed parameters {
  matrix[2, nsubj] beta =         // by-subj intercepts and slopes
    rep_matrix(mu, nsubj) + diag_pre_multiply(tau, L_Rho) * z;
}
model {
            mu ~ normal(0, 10);
           tau ~ normal(0, 30);
         sigma ~ normal(0, 50);
         L_Rho ~ lkj_corr_cholesky(2);
  to_vector(z) ~ std_normal();
          n400 ~ normal(beta[1, subj] + beta[2, subj] .* ccloze, sigma);
}
"
m <- stan(model_code = s, data = datlist)
print(m, pars = c("mu", "tau", "sigma", "L_Rho[2,1]",
                  "beta[1,1]", "beta[2,1]", "beta[1,2]", "beta[2,2]"), 
      probs = c(.025, .975))
## Inference for Stan model: anon_model.
## 4 chains, each with iter=2000; warmup=1000; thin=1; 
## post-warmup draws per chain=1000, total post-warmup draws=4000.
## 
##             mean se_mean   sd  2.5% 97.5% n_eff Rhat
## mu[1]       3.65    0.01 0.43  2.81  4.46  2046    1
## mu[2]       2.32    0.01 0.63  1.10  3.55  5431    1
## tau[1]      2.20    0.01 0.38  1.54  3.01  1612    1
## tau[2]      1.60    0.03 0.91  0.09  3.43   940    1
## sigma      11.62    0.00 0.15 11.33 11.92  7121    1
## L_Rho[2,1]  0.17    0.01 0.34 -0.57  0.79  4649    1
## beta[1,1]   4.78    0.01 1.08  2.70  6.91  6458    1
## beta[2,1]   1.57    0.03 1.65 -2.25  4.40  2943    1
## beta[1,2]   2.87    0.01 1.19  0.47  5.20  7690    1
## beta[2,2]   1.32    0.03 1.71 -2.84  4.10  3120    1
## 
## Samples were drawn using NUTS(diag_e) at Mon Dec 16 11:47:34 2024.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at 
## convergence, Rhat=1).