Tutorials › Bayesian Clinical Trials › Gibbs Sampling From Conjugate Full Conditionals

Posterior Computation · Tutorial 123 of 696

Gibbs Sampling From Conjugate Full Conditionals

Learn to derive and run a two-parameter Gibbs sampler for a normal clinical-trial outcome, and understand why its alternating conditional updates preserve the joint posterior.

Intermediate 11 min read

What You'll Learn

  • Specify a normal likelihood with a Normal–Inverse-Gamma joint prior for an unknown mean and variance.
  • Derive the full conditional distribution for each parameter from the joint posterior kernel.
  • Carry out a numerical Gibbs iteration using hypothetical trial data.
  • Explain why an exact update from either full conditional preserves the joint posterior.
  • Recognize dependence between parameters, common implementation errors, and when direct posterior sampling may be preferable.

Why Cycle Through Conditional Distributions?

A trial model may have a simple likelihood but parameters that are dependent in the posterior. Gibbs sampling addresses this by repeatedly drawing each parameter from its distribution conditional on the current values of the others. The key requirement is that these full conditional distributions can be sampled from. Each conditional update leaves the joint posterior unchanged, so a complete cycle also preserves it.

We develop the method for a continuous outcome in one trial arm. This normal model is deliberately conjugate: its joint posterior and full conditionals can both be derived exactly. That makes it possible to check every step of the sampler. In this example direct draws from the joint posterior are also available, so Gibbs sampling is a teaching device rather than a computational necessity. In more complicated models, conditionals may remain tractable even when a direct joint draw is inconvenient.

Model and assumptions: Let \(x_1,\ldots,x_n\) be continuous outcomes, such as change in a clinical score measured in points, from one trial arm. Conditional on the unknown mean \(\theta\) and variance \(\sigma^2\), assume the outcomes are independent and identically distributed as \(X_i\sim N(\theta,\sigma^2)\). Specify the joint prior hierarchically as \(p(\theta,\sigma^2)=p(\theta\mid\sigma^2)p(\sigma^2)\), where \(\theta\mid\sigma^2\sim N(m_0,\sigma^2/\kappa_0)\) and \(\sigma^2\sim\operatorname{InvGamma}(a_0,b_0)\). Here \(\kappa_0,a_0,b_0>0\). The parameters \(\theta\) and \(\sigma^2\) are not asserted to be independent: the prior distribution of \(\theta\) conditional on \(\sigma^2\) depends on \(\sigma^2\).

We use the inverse-gamma shape-and-scale convention \(p(v)=b^a v^{-a-1}\exp(-b/v)/\Gamma(a)\) for \(v>0\), when \(v\sim\operatorname{InvGamma}(a,b)\). Thus \(b\) appears in the numerator of the exponent as \(-b/v\). The prior is proper under the stated positive hyperparameters. The normal likelihood and this prior give a proper posterior.

Deriving the Two Full Conditionals

Write \(\bar{x}\) for the sample mean and \(\sum_i(x_i-\bar{x})^2\) for the within-sample sum of squares. The joint posterior density is proportional to likelihood times joint prior. Keeping only terms that depend on the parameters gives

$$ p(\theta,\sigma^2\mid x) \propto (\sigma^2)^{-\{a_0+1+(n+1)/2\}} \exp\left[ -\frac{b_0+\tfrac12\sum_{i=1}^n(x_i-\theta)^2+ \tfrac12\kappa_0(\theta-m_0)^2}{\sigma^2} \right]. $$

For the first full conditional, hold \(\sigma^2\) fixed and collect the quadratic terms in \(\theta\). Completing the square gives a normal distribution. For the second, hold \(\theta\) fixed; the power of \(\sigma^2\) and the exponent have the inverse-gamma form. These are the distributions used for the Gibbs updates.

Full conditional distributions: With \(x=(x_1,\ldots,x_n)\),
$$ \theta\mid\sigma^2,x\sim N\left( \frac{\kappa_0m_0+n\bar{x}}{\kappa_0+n}, \frac{\sigma^2}{\kappa_0+n} \right), $$
$$ \sigma^2\mid\theta,x\sim \operatorname{InvGamma}\left( a_0+\frac{n+1}{2}, b_0+\frac12\sum_{i=1}^n(x_i-\theta)^2+ \frac12\kappa_0(\theta-m_0)^2 \right). $$

The \(n+1\) in the second shape parameter matters. The likelihood contributes \(n/2\) to the power of \((\sigma^2)^{-1}\), and the conditional normal prior \(p(\theta\mid\sigma^2)\) contributes another \(1/2\). Omitting the latter produces an incorrect full conditional.

Worked Example: Deriving the Conditional Parameters

Suppose, hypothetically, that five patients in one trial arm have outcome changes \(4,6,5,7,3\) points. Use \(m_0=0\) points, \(\kappa_0=1\), \(a_0=2\), and \(b_0=2\) point-squared. The observations are modeled as independent normal outcomes conditional on \(\theta,\sigma^2\), and the prior is the proper hierarchical prior specified above.

The sample mean is \(\bar{x}=(4+6+5+7+3)/5=5\) points, and the sum of squared deviations from the sample mean is \(1+1+0+4+4=10\) point-squared. The conditional mean for \(\theta\) is \((1\times0+5\times5)/(1+5)=25/6=4.167\) points, and its conditional variance is \(\sigma^2/6\). Therefore, \(\theta\mid\sigma^2,x\sim N(4.167,\sigma^2/6)\), with the variance expressed in point-squared.

The conditional shape for \(\sigma^2\) is \(2+(5+1)/2=5\). Its scale depends on the current \(\theta\), as it must for a full conditional. For example, at \(\theta=4\) points, the residual sum of squares is \(0^2+2^2+1^2+3^2+(-1)^2=15\) point-squared, and the prior quadratic term is \((4-0)^2=16\) point-squared. Thus the inverse-gamma scale is \(2+15/2+16/2=17.5\) point-squared, giving \(\sigma^2\mid\theta=4,x\sim\operatorname{InvGamma}(5,17.5)\).

A Gibbs Cycle and Why It Preserves the Posterior

Choose starting values \(\theta^{(0)}\) and \((\sigma^2)^{(0)}>0\). In each iteration, draw \(\theta\) conditional on the current variance, then draw the variance conditional on the newly updated mean. Repeat the cycle. Each draw must be from the stated full conditional, not from a marginal distribution or from a conditional based on stale values.

1
Update the mean.
Draw \(\theta^{(t+1)}\) from \(p(\theta\mid(\sigma^2)^{(t)},x)\), using the normal full conditional.
2
Update the variance.
Draw \((\sigma^2)^{(t+1)}\) from \(p(\sigma^2\mid\theta^{(t+1)},x)\), using the inverse-gamma full conditional.
3
Retain the pair.
The updated pair \((\theta^{(t+1)},(\sigma^2)^{(t+1)})\) is one draw in the chain. Continue cycling to obtain further draws.

The preservation result follows directly from conditional probability. Suppose a pair \((\theta,\sigma^2)\) currently has the joint posterior distribution. Updating only \(\theta\) by drawing from \(p(\theta\mid\sigma^2,x)\) gives the joint density of the updated pair as

$$ p(\sigma^2\mid x)p(\theta_{\mathrm{new}}\mid\sigma^2,x) =p(\theta_{\mathrm{new}},\sigma^2\mid x). $$

The variance marginal is unchanged, and conditional on that variance the new mean has exactly the posterior conditional distribution. The updated pair therefore still has the joint posterior distribution. The same argument applies when updating \(\sigma^2\) conditional on \(\theta\). Since composing two updates that each preserve a distribution also preserves it, a full Gibbs cycle preserves the joint posterior. This establishes invariance; it does not by itself establish that a chain from every starting point has converged after a chosen finite number of iterations. Also, a systematic mean-then-variance cycle need not itself satisfy detailed balance, even though it preserves the target distribution.

Worked Example: One Numerical Gibbs Iteration

Continue with the hypothetical data and prior above. Start at \((\sigma^2)^{(0)}=20\) point-squared. For illustration, suppose the random-number generator produces a standard normal variate \(z=0.40\) for the mean update. The conditional mean is \(4.167\) points and the conditional standard deviation is \(\sqrt{20/6}=1.826\) points. Hence \(\theta^{(1)}=4.167+1.826(0.40)=4.897\) points.

For the variance update, the residual sum of squares at \(\theta=4.897\) can be computed as \(\sum_i(x_i-\theta)^2=10+5(4.897-5)^2\approx10.053\) point-squared. The added prior quadratic term is \((4.897-0)^2\approx23.981\) point-squared. The inverse-gamma scale is therefore \(2+\tfrac12(10.053)+\tfrac12(23.981)=19.017\) point-squared, and the shape remains \(5\).

Equivalently, if the generator produces \(G=0.25\) from a gamma distribution with shape \(5\) and rate \(1\), then \(1/G\) has an inverse-gamma distribution with shape \(5\) and scale \(1\). The corresponding variance draw is \((\sigma^2)^{(1)}=19.017/G=76.07\) point-squared. This is one possible illustrative update, not a fixed output: subsequent values depend on the random draws. Both conditional updates use the latest available value, and the variance draw is positive as required.

Dependence, Checks, and Alternatives

Worked Example: Why Separate Marginal Draws Are Not Gibbs Updates

For the five observations, the conditional mean of \(\theta\) is \(4.167\) points at every fixed variance, but its conditional variance is \(\sigma^2/6\). At \(\sigma^2=9\) point-squared the conditional standard deviation is \(\sqrt{9/6}=1.225\) points; at \(\sigma^2=36\) point-squared it is \(\sqrt{36/6}=2.449\) points. Thus the distribution of the mean changes with the variance, even though its conditional mean does not.

Drawing \(\theta\) and \(\sigma^2\) independently from their marginal posterior distributions would discard this dependence and generally would not produce draws from the joint posterior. A Gibbs update instead uses the current variance in the normal draw, then the current mean in the inverse-gamma draw. This conditional dependence is precisely why the cycle can represent the joint posterior rather than just its separate margins.

Several implementation checks follow from the derivation:

  • Check the inverse-gamma shape. It is \(a_0+(n+1)/2\) for this conditional variance update; the extra half comes from \(p(\theta\mid\sigma^2)\).
  • Use the updated mean. In the stated scan order, calculate the variance conditional on \(\theta^{(t+1)}\), not \(\theta^{(t)}\).
  • Check units and support. The mean and its standard deviation are in outcome units; the variance and inverse-gamma scale are in squared outcome units. The variance draws must be positive.
  • Check that the model matches the data. Conditional independence and a common normal distribution are substantive assumptions. Strong skewness, outliers, or important differences in variability may make this model inadequate. A correctly implemented sampler does not repair a misspecified likelihood.

A common mistake is to say that both parameters are drawn independently because each is updated separately. They are not: each draw is conditional on the current value of the other parameter, and the prior itself is specified by \(p(\theta\mid\sigma^2)p(\sigma^2)\), not by a product of independent marginal priors. Another mistake is to interpret invariance as a guarantee that a short run is representative. Invariance identifies the target distribution of the transition; assessing whether a finite simulated chain adequately explores that target requires careful computation and diagnostics.

Compared with a direct draw from the Normal–Inverse-Gamma posterior, Gibbs sampling is less efficient for this simple example: the conjugate joint posterior can be sampled directly, avoiding serial dependence between successive states. Gibbs becomes useful when direct joint simulation is difficult but the full conditionals are available. Compared with Metropolis–Hastings, an exact Gibbs update needs no proposal distribution or acceptance decision. If a full conditional cannot be sampled directly, a different update strategy is needed; the conditional step is then no longer an exact Gibbs draw. The method’s limitations are therefore practical as well as modeling-related: conditionals must be derived correctly, dependence can make movement through the joint posterior slow, and a preserved target does not guarantee rapid exploration.

Key takeaway: Derive each full conditional from the joint posterior, update one parameter conditional on the latest values of the others, and remember that the proof of posterior invariance concerns the transition rule—not the adequacy of the model or the convergence of a finite run.

Check Your Understanding

Answer each question, then compare with the solutions below.

  1. Why does the conditional shape for \(\sigma^2\) contain \(a_0+(n+1)/2\), rather than \(a_0+n/2\)?
  2. For \(n=8\), \(a_0=1.5\), and a normal conditional prior for the mean, what is the shape parameter of \(\sigma^2\mid\theta,x\)?
  3. In a mean-then-variance scan, which value of the mean is used in the variance update?
  4. Does posterior invariance alone prove that a finite chain has converged? Explain briefly.

Solutions. (1) The likelihood contributes \(n/2\) to the inverse-gamma shape, while the conditional normal prior for \(\theta\mid\sigma^2\) contributes another \(1/2\); together with the prior shape this gives \(a_0+(n+1)/2\). (2) The shape is \(1.5+(8+1)/2=6\). (3) Use the newly drawn value \(\theta^{(t+1)}\), because the variance update conditions on the latest mean. (4) No. Invariance says that a chain already distributed according to the posterior remains so after an update; it does not show that a finite chain from a particular starting point has reached or adequately explored that distribution.

Clinical Biostats