Tutorials › Generalized Linear Mixed Models (GLMM) › Gibbs Sampling for Conjugate Variance Components

Bayesian GLMM and Prior Specification for Variance Components · Tutorial 269 of 698

Gibbs Sampling for Conjugate Variance Components

Derive conjugate Gibbs updates for Gaussian random effects and variance components, and work through a full numerical iteration.

Advanced 12 min read

What You'll Learn

  • Derive the conditional normal distribution of a Gaussian random intercept.
  • Update residual and random-effect variances under inverse-gamma priors.
  • Carry out a complete Gibbs sweep with explicitly calculated conditional distributions.
  • Explain why each full-conditional update preserves the joint posterior.
  • Check the assumptions required for conjugate updates and recognize common implementation errors.
  • Distinguish direct Gibbs draws from Metropolis-Hastings updates.

Why Gibbs Updates Can Be Direct

In a Gaussian hierarchical model, random effects and variance components are linked: the observed responses inform the random effects, and the random effects inform their variance. Gibbs sampling uses this structure by drawing one parameter block at a time from its full conditional distribution—the distribution of that block given the data and the current values of all other blocks. When the model and priors are conjugate, these conditional distributions have standard forms, so each block can be sampled directly rather than accepted or rejected.

The calculations below use a random-intercept model. The same logic applies to other Gaussian random-effect structures when the covariance form and priors support the required conditional distributions. Conjugacy is a property of a specified likelihood-prior pairing; it is not a general guarantee for every GLMM or every prior.

Definition: A Gibbs update replaces a parameter block by a draw from its full conditional distribution given the data and the current values of all other blocks. A Gibbs sweep is an ordered sequence of such updates, usually updating every block once. The resulting sequence of joint parameter values is a Markov chain whose intended stationary distribution is the joint posterior.

Model and Conjugate Full Conditionals

Suppose there are \(J\) groups and \(n_j\) Gaussian responses in group \(j\). Use the model

$$ y_{ij}=\mu+b_j+\epsilon_{ij},\qquad b_j\mid\tau^2\sim N(0,\tau^2),\qquad \epsilon_{ij}\mid\sigma^2\sim N(0,\sigma^2). $$

Conditional on the parameters, the errors are independent across observations, and the group effects are independent across groups. The mean \(\mu\) is assigned a normal prior, \(\mu\sim N(m_0,V_0)\). Independently, assign inverse-gamma priors to the two variances. We use the shape-rate convention

$$ v\sim\operatorname{IG}(a,b) \quad\Longleftrightarrow\quad p(v)\propto v^{-(a+1)}\exp(-b/v),\qquad v>0, $$

where \(a>0\) and \(b>0\). Thus, larger \(b\) changes the scale of the prior on the variance; \(b\) is not a rate multiplying \(v\) in an exponential density. Assume proper priors and a posterior that is proper.

To derive the update for \(b_j\), retain the likelihood terms from group \(j\) and multiply by its normal prior. Completing the square in \(b_j\) gives a normal conditional distribution:

$$ b_j\mid\mu,\sigma^2,\tau^2,\mathbf y \sim N\left( \frac{\sum_{i=1}^{n_j}(y_{ij}-\mu)/\sigma^2} {n_j/\sigma^2+1/\tau^2}, \frac{1}{n_j/\sigma^2+1/\tau^2} \right). $$

The conditional precision is the sum of the data precision \(n_j/\sigma^2\) and prior precision \(1/\tau^2\). The conditional mean is a precision-weighted compromise between the group's observed deviations from \(\mu\) and the prior mean zero.

Given the random effects and variances, the conditional distribution of \(\mu\) is also normal. Its precision adds the prior precision to the observation precision:

$$ \mu\mid\mathbf b,\sigma^2,\mathbf y \sim N\left( \frac{m_0/V_0+\sum_{j=1}^{J}\sum_{i=1}^{n_j}(y_{ij}-b_j)/\sigma^2} {1/V_0+\sum_{j=1}^{J}n_j/\sigma^2}, \frac{1}{1/V_0+\sum_{j=1}^{J}n_j/\sigma^2} \right). $$

Write \(N=\sum_j n_j\) for the total number of observations and define the residual sum of squares \(SSE=\sum_j\sum_i(y_{ij}-\mu-b_j)^2\). Define \(SSB=\sum_j b_j^2\), the sum of squared group effects. Multiplication of each Gaussian likelihood contribution by the corresponding inverse-gamma prior yields

$$ \sigma^2\mid\mu,\mathbf b,\mathbf y \sim\operatorname{IG}\left(a_\sigma+\frac{N}{2},\ b_\sigma+\frac{SSE}{2}\right), \qquad \tau^2\mid\mathbf b \sim\operatorname{IG}\left(a_\tau+\frac{J}{2},\ b_\tau+\frac{SSB}{2}\right). $$

Each observation contributes one Gaussian residual term to the update for \(\sigma^2\); each random effect contributes one normal term to the update for \(\tau^2\). Consequently, the shape increments are \(N/2\) and \(J/2\), respectively—not the number of groups for the residual variance or the number of observations for the random-effect variance.

Why a Full-Conditional Update Preserves the Posterior

The key justification is conditional resampling. Let \(\theta_k\) be the block being updated and let \(\theta_{-k}\) denote all remaining blocks. If the current joint state has posterior density \(\pi(\theta_k,\theta_{-k}\mid\mathbf y)\), the new value \(\theta_k'\) is drawn from \(\pi(\theta_k'\mid\theta_{-k},\mathbf y)\), holding \(\theta_{-k}\) fixed. The density of the new joint state is therefore

$$ \int \pi(\theta_k,\theta_{-k}\mid\mathbf y)\, \pi(\theta_k'\mid\theta_{-k},\mathbf y)\,d\theta_k = \pi(\theta_{-k}\mid\mathbf y)\, \pi(\theta_k'\mid\theta_{-k},\mathbf y) = \pi(\theta_k',\theta_{-k}\mid\mathbf y). $$

The integral is over the old value of the updated block, while the other blocks remain fixed. The resulting density is the target posterior density at the new value. Thus each exact full-conditional update leaves the posterior invariant, and a sequence of such updates also leaves it invariant. Invariance alone does not say that a finite run has reached the posterior; that requires adequate exploration of the posterior, a separate practical concern.

Worked Example: Deriving a Group-Effect Conditional

Suppose one hypothetical group has three responses \(4,6,5\), with current \(\mu=4\), \(\sigma^2=4\), and \(\tau^2=1\). Its conditional precision is \(3/4+1=1.75\), so its conditional variance is \(1/1.75=4/7\). The sum of response deviations from \(\mu\) is \((4-4)+(6-4)+(5-4)=3\). Therefore its conditional mean is

$$ \frac{(3/4)}{3/4+1} \left(\frac{3}{3}\right) =\frac{3}{7}. $$

Equivalently, using the full-conditional formula, the numerator is \(3/4\) and the denominator is \(7/4\), giving \((3/4)/(7/4)=3/7\). Thus \(b_j\mid\cdots\sim N(3/7,4/7)\). The mean is pulled toward zero by the prior: the unshrunk group mean deviation is \(3/3=1\), whereas the conditional mean is about \(0.4286\).

A Complete Numerical Gibbs Sweep

A sweep requires a specified starting state, a specified update order, and draws from the resulting conditional distributions. The next example uses two hypothetical groups with responses \(2,4\) and \(5,7\). Let \(\mu\sim N(0,100)\), and let both variance priors be \(\operatorname{IG}(2,1)\). Start at \(\mu=4\), \(\sigma^2=1\), and \(\tau^2=4\). Update \(b_1,b_2,\mu,\sigma^2,\tau^2\) in that order.

Worked Example: One Complete Gibbs Iteration

1. Update the group effects. Each group has two observations. The conditional variance for either effect is \(1/(2/1+1/4)=1/2.25=4/9\), with standard deviation \(2/3\). The group 1 conditional mean is \(\{(2-4)+(4-4)\}/2.25=-2/2.25=-8/9\). The group 2 conditional mean is \(\{(5-4)+(7-4)\}/2.25=4/2.25=16/9\). For illustration, suppose the two normal draws use standardized values \(0.5\) and \(-1\). Then

$$ b_1=-\frac{8}{9}+\frac{2}{3}(0.5)=-\frac{5}{9}\approx-0.5556,\qquad b_2=\frac{16}{9}+\frac{2}{3}(-1)=\frac{10}{9}\approx1.1111. $$

2. Update the grand mean. The prior precision is \(1/100=0.01\), and the data precision is \(4/1=4\). The sum of responses is \(18\); subtracting the effect contribution \(2b_1+2b_2=10/9\) leaves \(152/9\). Hence the conditional variance and mean are

$$ V_\mu=\frac{1}{4.01}\approx0.24938,\qquad m_\mu=\frac{152/9}{4.01}\approx4.2117. $$

If the standardized normal draw is \(-0.2\), then \(\mu=4.2117-0.2\sqrt{0.24938}\approx4.1118\).

3. Update the residual variance. Using \(\mu=4.1118\), \(b_1=-5/9\), and \(b_2=10/9\), the fitted group means are approximately \(3.5563\) and \(5.2229\). Thus

$$ SSE=(2-3.5563)^2+(4-3.5563)^2 +(5-5.2229)^2+(7-5.2229)^2 \approx5.8265. $$

There are four observations, so the conditional is \(\sigma^2\mid\cdots\sim\operatorname{IG}(2+4/2,\ 1+5.8265/2) =\operatorname{IG}(4,3.9133)\). Suppose a draw from this conditional is \(1.20\).

4. Update the random-effect variance. Here \(SSB=(-5/9)^2+(10/9)^2=125/81\approx1.5432\). There are two groups, so

$$ \tau^2\mid\cdots\sim\operatorname{IG}\left(2+\frac{2}{2},\ 1+\frac{125/81}{2}\right) =\operatorname{IG}(3,1.7716). $$

Suppose this conditional draw is \(0.80\). The sweep ends at approximately \((\mu,b_1,b_2,\sigma^2,\tau^2)=(4.1118,-0.5556,1.1111,1.20,0.80)\). The variance draws are illustrative random outcomes, not estimates obtained by taking the conditional means. The next sweep would use this new state.

Worked Example: Updating a Variance Component

Suppose three hypothetical random effects currently equal \(0.2,-0.4,0.6\), and their common variance has prior \(\operatorname{IG}(3,2)\). The squared-effect sum is \(0.2^2+(-0.4)^2+0.6^2=0.04+0.16+0.36=0.56\). Therefore the conditional variance distribution is

$$ \tau^2\mid\mathbf b\sim \operatorname{IG}\left(3+\frac{3}{2},\ 2+\frac{0.56}{2}\right) =\operatorname{IG}(4.5,2.28). $$

The shape increment is \(3/2\) because the likelihood contains three normally distributed effects. Confusing the variance with its standard deviation, or using \(0.56\) rather than \(0.56/2\) in the inverse-gamma rate, would give a different and incorrect conditional distribution.

Assumptions, Diagnostics, and Alternatives

These direct updates depend on the stated model. The response likelihood must be Gaussian with common residual variance \(\sigma^2\); conditional on that variance, the errors must be independent. The random effects must be Gaussian with the stated variance structure, and the normal prior on \(\mu\) and inverse-gamma priors must have the specified parameterizations. If there are known offsets or predictors, they can be included in the linear predictor and the corresponding residuals, but the conditional formulas must then use those terms consistently.

Conjugacy can fail when the likelihood is not Gaussian, when random effects have a nonnormal prior, or when covariance parameters are assigned priors that do not yield standard full conditionals. An inverse-gamma prior is also not automatically a sensible prior merely because it is convenient; its implications should be considered on the variance scale and in the context of the application.

Useful implementation checks include verifying that all sampled variances are positive, that the residual and effect sums of squares use the current values specified by the update order, and that the shape increments count observations or effects correctly. Compare conditional formulas against simple cases, such as one group or zero residual sum of squares. In a run, inspect sampled values and trace behavior for signs of sticking, extreme sensitivity to starting values, or poor movement. Correct full conditionals do not by themselves ensure that a chain explores the posterior well.

Unlike a Metropolis-Hastings update, a Gibbs update from an exact full conditional needs no proposal distribution and has no accept-reject step. That advantage is available only where the full conditional can be sampled directly. When it cannot, other sampling methods may be needed. Neither direct sampling nor posterior invariance guarantees fast mixing, especially when effects and their variance components are strongly dependent.

Common Mistakes

  • Integrating the wrong value in the invariance argument. The old value of the updated block is integrated out while the other blocks are held fixed. Integrating the other blocks instead gives a marginal density, not the joint density at the newly updated state.
  • Using the wrong inverse-gamma convention. Under the shape-rate convention here, the kernel is \(\exp(-b/v)\). A formula from a source using a scale or a different parameterization must be translated before use.
  • Using the wrong sample-size count. The residual variance shape increment is half the number of observations; the random-effect variance shape increment is half the number of effects.
  • Treating a conditional draw as a point estimate. A Gibbs update is a random draw from a conditional distribution. Its value need not equal that distribution's mean or mode.
Key takeaway: In a Gaussian hierarchical model with compatible normal and inverse-gamma priors, Gaussian effects and variance components have direct full-conditional updates. Derive each update from its likelihood and prior, keep the conditioning values and inverse-gamma convention explicit, and assess chain behavior separately from the algebra.

Check Your Understanding

Answer each question, then compare with the brief solutions below.

  1. If a variance conditional has distribution \(\operatorname{IG}(a,b)\), what kernel defines it under the convention used here?
  2. There are five observations and a prior \(\operatorname{IG}(2,3)\) on \(\sigma^2\). If the current residual sum of squares is \(8\), find its full conditional.
  3. Four normal random effects share a variance with prior \(\operatorname{IG}(1,0.5)\). Their squared-effect sum is \(6\). Find the conditional distribution of that variance.
  4. In the posterior-invariance calculation, which value is integrated out, and which parameter blocks are held fixed?
  5. Give one reason a direct conjugate Gibbs update may not be available, and explain what that means for the update.

Solutions. (1) Its density is proportional to \(v^{-(a+1)}\exp(-b/v)\) for \(v>0\). (2) The shape is \(2+5/2=4.5\), and the rate is \(3+8/2=7\), so \(\sigma^2\mid\cdots\sim\operatorname{IG}(4.5,7)\). (3) The shape is \(1+4/2=3\), and the rate is \(0.5+6/2=3.5\), giving \(\operatorname{IG}(3,3.5)\). (4) Integrate the old value of the updated block; hold the other blocks fixed. (5) A non-Gaussian likelihood, for example, may produce a full conditional with no standard directly samplable form. A different sampling method may then be required; the usual direct conjugate Gibbs draw is unavailable.

Clinical Biostats