Tutorials › Stochastic Processes › Metropolis-Within-Gibbs Updates

Markov Chain Monte Carlo · Tutorial 924 of 994

Metropolis-Within-Gibbs Updates

Build a Metropolis-within-Gibbs sweep for a factorized target, calculate its updates, and prove that the combined transition leaves the target invariant.

Advanced 11 min read

What You'll Learn

  • Factor a joint target and identify which conditional distribution each update requires.
  • Calculate a Metropolis-Hastings acceptance probability for one selected parameter.
  • Derive exact Gibbs draws for the remaining parameters.
  • Prove that each update preserves the joint target and that their ordered composition does too.
  • Check proposal rows, transition probabilities, and conditional invariance using exact arithmetic.

Combining Metropolis-Hastings and Gibbs Updates

In a multivariate model, one parameter may have a convenient full conditional distribution while another does not. Metropolis-within-Gibbs handles this by using an exact Gibbs draw for the convenient parameter and a Metropolis-Hastings (MH) update for the difficult one. The resulting sweep is still a Markov transition on the full parameter vector. To justify it, we will show that each update preserves the specified joint target; then their ordered composition preserves it as well.

Consider a finite product state space \(S=\mathcal X\times\mathcal Y\times\mathcal Z\). A state is \((x,y,z)\). Suppose the target probability mass function has the factorization \(\pi(x,y,z)=Z^{-1}\phi(x,y)\psi(y,z)\), where all factor values are strictly positive and \(Z\) is the sum of the unnormalized weights over all states. Strict positivity ensures that each conditional distribution used below is defined. We use a fixed systematic sweep: update \(x\) by MH conditional on the current \(y,z\), draw \(y\) exactly conditional on the updated \(x\) and current \(z\), and then draw \(z\) exactly conditional on the updated \(x,y\).

Definition: A Metropolis-within-Gibbs sweep updates coordinates of a joint state in a specified order. Some coordinates are proposed and accepted or rejected by an MH step conditional on the other current coordinates; other coordinates are drawn directly from their full conditional distributions. A probability distribution \(\pi\) is invariant for a transition kernel \(K\) if \(\pi K=\pi\), meaning that a state with distribution \(\pi\) still has distribution \(\pi\) after one transition.

Let \(q(x'\mid x)\) be the proposal probability for the \(x\)-update. For fixed \(y\), the target conditional mass of \(x\) is proportional to \(\phi(x,y)\): the factor \(\psi(y,z)\) does not depend on \(x\), so it cancels in the conditional distribution. The MH acceptance probability for a proposal from \(x\) to \(x'\) is therefore \(\alpha_y(x,x')=\min\{1,\phi(x',y)q(x\mid x')/[\phi(x,y)q(x'\mid x)]\}\) whenever the proposal probabilities in the ratio are positive. A proposal with \(q(x'\mid x)=0\) is never made; if \(q(x\mid x')=0\) while \(q(x'\mid x)>0\), the acceptance probability is zero. Rejection leaves \(x\) unchanged. The \(y\)- and \(z\)-updates draw, respectively, from masses proportional to \(\phi(x,y)\psi(y,z)\) and \(\psi(y,z)\).

Worked Example: Calculating the Conditional MH Update

Use \(\mathcal X=\{0,1,2\}\). Let the proposal have \(q(1\mid0)=1\), \(q(0\mid1)=\tfrac13\), \(q(2\mid1)=\tfrac23\), and \(q(1\mid2)=1\), with all other proposal probabilities zero. Its matrix, with rows and columns ordered \(0,1,2\), is \(\begin{bmatrix}0&1&0\\ \tfrac13&0&\tfrac23\\0&1&0\end{bmatrix}\). The row sums are \(1\), \(\tfrac13+\tfrac23=1\), and \(1\), so this is a valid proposal matrix.

Take \(\phi(0,0)=4\), \(\phi(1,0)=2\), and \(\phi(2,0)=1\). At \(y=0\), a proposal from \(0\) to \(1\) has acceptance probability \(\min\{1,(2/4)(1/3)/1\}=\tfrac16\). For a proposal from \(1\) to \(0\), the probability is \(\min\{1,(4/2)1/(1/3)\}=1\). For a proposal from \(1\) to \(2\), it is \(\min\{1,(1/2)1/(2/3)\}=\tfrac34\). For a proposal from \(2\) to \(1\), it is \(\min\{1,(2/1)(2/3)/1\}=1\).

Including rejected proposals on the diagonal gives the conditional transition matrix \(R_0=\begin{bmatrix}\tfrac56&\tfrac16&0\\ \tfrac13&\tfrac16&\tfrac12\\0&1&0\end{bmatrix}\). Its row sums are \(\tfrac56+\tfrac16=1\), \(\tfrac13+\tfrac16+\tfrac12=1\), and \(1\). For example, from state \(1\), the proposal to \(0\) is accepted with probability \(1\), the proposal to \(2\) is accepted with probability \(\tfrac34\), and the remaining probability \(\tfrac12(1-\tfrac34)=\tfrac16\) is rejection. The proposal distribution is not itself the conditional target, so its reverse probability must be included in the acceptance ratio.

Exact Conditional Draws in the Same Sweep

For this factorization, the full conditional distributions needed for the Gibbs updates follow directly by holding the other coordinates fixed and normalizing over the coordinate being updated: \[ \mathbb P(Y=y\mid X=x,Z=z) =\frac{\phi(x,y)\psi(y,z)}{\sum_{\tilde y\in\mathcal Y}\phi(x,\tilde y)\psi(\tilde y,z)}, \qquad \mathbb P(Z=z\mid X=x,Y=y) =\frac{\psi(y,z)}{\sum_{\tilde z\in\mathcal Z}\psi(y,\tilde z)}. \]

Worked Example: Drawing the Gibbs Coordinates

Take \(\mathcal Y=\mathcal Z=\{0,1\}\), with factor values \(\phi(0,0)=4,\phi(0,1)=1;\ \phi(1,0)=2,\phi(1,1)=3;\ \phi(2,0)=1,\phi(2,1)=2\) and \(\psi(0,0)=3,\psi(0,1)=1;\ \psi(1,0)=1,\psi(1,1)=4\). All values are positive, so the conditional distributions are defined. For example, at \(x=1,z=1\), the unnormalized probabilities for \(y=0,1\) are \(2\cdot1=2\) and \(3\cdot4=12\). Their sum is \(14\), giving probabilities \(\tfrac17\) and \(\tfrac67\). At \(y=1\), the unnormalized probabilities for \(z=0,1\) are \(1\) and \(4\), giving conditional probabilities \(\tfrac15\) and \(\tfrac45\).

To see how these updates operate in order, start at \((0,0,1)\). The \(x\)-update uses \(R_0\), so its new \(x\) is \(0,1,2\) with probabilities \(\tfrac56,\tfrac16,0\). For \(z=1\), the conditional probability of \(y=1\) is \(\tfrac12\) when \(x=0\), since its two weights are \(4\) and \(4\); it is \(\tfrac67\) when \(x=1\), as computed above. Thus after the \(y\)-draw, the probability that \(y=1\) is \(\tfrac56\cdot\tfrac12+\tfrac16\cdot\tfrac67=\tfrac5{12}+\tfrac17=\tfrac{47}{84}\). The probability of \(y=0\) is \(1-\tfrac{47}{84}=\tfrac{37}{84}\).

The final draw has \(\mathbb P(Z=1\mid Y=0)=\tfrac14\) and \(\mathbb P(Z=1\mid Y=1)=\tfrac45\). Consequently, the probability of ending the sweep with \(z=1\) is \(\tfrac{37}{84}\cdot\tfrac14+\tfrac{47}{84}\cdot\tfrac45 =\tfrac{37}{336}+\tfrac{188}{420}=\tfrac{937}{1680}\). The complementary probability is \(\tfrac{743}{1680}\), and the two probabilities sum to \(1\). This marginal calculation does not claim that the entire sweep has distribution \(\pi\) from this particular starting state; invariance is a statement about starting with distribution \(\pi\).

Why the Ordered Sweep Preserves the Target

Theorem: Let \(S=\mathcal X\times\mathcal Y\times\mathcal Z\) be finite, let \(\phi,\psi\) be strictly positive, and let \(q\) be a row-stochastic proposal matrix on \(\mathcal X\). Construct \(K_x\) by the MH update using \(\alpha_y\), \(K_y\) by an exact draw from \(\mathbb P(Y\mid X,Z)\), and \(K_z\) by an exact draw from \(\mathbb P(Z\mid X,Y)\). Each update preserves \(\pi(x,y,z)=Z^{-1}\phi(x,y)\psi(y,z)\), and the systematic sweep \(K=K_xK_yK_z\) preserves \(\pi\).

Proof. First fix \(y\), and let \(A_y=\sum_x\phi(x,y)\). The conditional target for \(x\) is \(m_y(x)=\phi(x,y)/A_y\). For distinct \(x,x'\) with both proposal probabilities positive, the probability flow from \(x\) to \(x'\) under the MH transition is \[ m_y(x)q(x'\mid x)\alpha_y(x,x') =\frac{1}{A_y}\min\{\phi(x,y)q(x'\mid x),\phi(x',y)q(x\mid x')\}. \] The expression is unchanged when \(x\) and \(x'\) are interchanged. If either proposal probability is zero, both directed accepted flows are zero, so the equality still holds. Thus the accepted flows balance in both directions. Rejected proposals stay at their starting state and contribute only to diagonal flow. Summing the balanced flows over the starting state shows \(m_yR_y=m_y\), where \(R_y\) is the conditional MH transition matrix. For each fixed \(y,z\), the target conditional of \(x\) is \(m_y\), since \(\psi(y,z)\) cancels from its normalization. Therefore \(K_x\) preserves the full joint target.

Next, write \(\pi(x,z)=\sum_y\pi(x,y,z)\). Under \(K_y\), the probability of arriving at \((x,y',z)\) is \[ \sum_y\pi(x,y,z)\mathbb P(Y=y'\mid X=x,Z=z) =\pi(x,z)\mathbb P(Y=y'\mid X=x,Z=z) =\pi(x,y',z). \] The first equality uses that the conditional draw is the same for every old \(y\) when \(x,z\) are fixed. The second is the definition of the conditional probability. The same calculation for the \(z\)-update gives \(\sum_z\pi(x,y,z)\mathbb P(Z=z'\mid X=x,Y=y)=\pi(x,y,z')\). Thus \(K_y\) and \(K_z\) each preserve \(\pi\). Using row-vector convention, \(\pi K=\pi K_xK_yK_z=\pi\), because each factor in turn leaves \(\pi\) unchanged. \(\square\)

Worked Example: Verifying Conditional Invariance Exactly

For the factor values in the Gibbs example, sum the unnormalized joint weights first. At \(y=0\), summing over \(x\) gives \(4+2+1=7\), and summing over \(z\) gives \(3+1=4\), so the total weight at \(y=0\) is \(28\). At \(y=1\), the corresponding sums are \(1+3+2=6\) and \(1+4=5\), giving weight \(30\). Hence \(Z=28+30=58\). This also checks that the joint masses normalized by \(58\) sum to \(1\).

For the \(x\)-update at \(y=0\), the conditional target is \((\tfrac47,\tfrac27,\tfrac17)\), and the matrix \(R_0\) from the first example has row sums \(1\). Multiplication gives \[ (\tfrac47,\tfrac27,\tfrac17)R_0 =(\tfrac{10}{21}+\tfrac{2}{21},\ \tfrac{4}{42}+\tfrac{2}{42}+\tfrac17,\ \tfrac17) =(\tfrac47,\tfrac27,\tfrac17). \] At \(y=1\), the factor values give acceptance probability \(1\) for each proposed move: the four MH ratios, in the order \(0\to1,1\to0,1\to2,2\to1\), are \(1,1,1,1\). The transition matrix is \(R_1=\begin{bmatrix}0&1&0\\ \tfrac13&0&\tfrac23\\0&1&0\end{bmatrix}\). Its row sums are \(1\), \(\tfrac13+\tfrac23=1\), and \(1\). Direct multiplication gives \((\tfrac16,\tfrac36,\tfrac26)R_1=(\tfrac16,\tfrac16+\tfrac26,\tfrac36\cdot\tfrac23)=(\tfrac16,\tfrac36,\tfrac26)\). These two checks verify the conditional \(x\)-invariance at both values of \(y\). The theorem then establishes invariance of the full target under each update and their sweep.

What Invariance Does—and Does Not—Say

Invariance guarantees that the target is unchanged by one sweep when the chain starts with the target distribution. It does not say that one sweep transforms every starting state into a target draw, nor does it by itself establish that a chain will converge to the target. Questions about convergence require additional properties of the transition, considered separately.

Common mistake: omitting the reverse proposal probability. In the first example, using only the target ratio for \(0\to1\) would give the incorrect acceptance probability \(\tfrac12\). The correct value is \(\tfrac16\), because the reverse proposal probability is \(q(0\mid1)=\tfrac13\). With the incorrect value, the flow from \(0\) to \(1\) would be \(\tfrac47\cdot\tfrac12=\tfrac27\), while the reverse flow under the correct \(1\to0\) transition would be \(\tfrac27\cdot\tfrac13=\tfrac{2}{21}\). The flows do not balance. Including the reverse proposal gives the correct forward flow \(\tfrac47\cdot\tfrac16=\tfrac{2}{21}\), matching the reverse flow.

A second practical safeguard is to use the conditional target for the coordinate being updated, not the whole joint weight without simplification. In this example, the \(x\)-update conditions on \(y,z\), but \(\psi(y,z)\) cancels because it is constant as \(x\) varies. The \(y\)-update, in contrast, depends on both factors. Finally, update order is part of the definition of a sweep: the \(y\)-draw here uses the newly updated \(x\), and the \(z\)-draw uses the newly updated \(y\).

Check Your Understanding

Use the factorization and proposal in the examples above, and verify each probability calculation before drawing a conclusion.

  1. At \(y=0\), calculate the MH acceptance probability for a proposal from \(1\) to \(2\), including the reverse proposal probability.
  2. For \(x=0,z=1\), derive both probabilities in the full conditional distribution of \(y\).
  3. Explain why the factor \(\psi(y,z)\) cancels from the conditional target for \(x\), but not from the conditional target for \(y\).
  4. State why invariance of each component kernel implies invariance of the ordered sweep under the row-vector convention.
  5. Give one reason that invariance alone does not guarantee convergence from an arbitrary initial state.