Bayesian Monte Carlo and MCMCLesson 73 of 76 · 10 min

Update one conditional at a time

Implement a Gibbs sampler using the latest value of each coordinate.

Gibbs sampling updates components by drawing from their conditional distributions given the other components. Exact conditional draws require no Metropolis rejection step. The resulting sequence is still a Markov chain and its successive states are usually dependent.

For a zero-mean bivariate normal with unit marginal variances and correlation rho, X given Y=yY=y is normal with mean ρy\rho y and variance 1-rho squared. The conditional for Y is analogous. First update X from the current Y, then update Y from the newly drawn X.

The code records a state after both coordinate updates, called a sweep here. Strong correlation can slow movement along a narrow joint distribution even though every conditional draw is accepted. The known means and covariance supply checks; they do not make correlated draws independent. Lag-one correlation compares neighbouring retained states: the recorded sequence with a copy shifted by one row. A large value warns that rows supply less independent information.

Use the newest coordinate
  1. Current state
    (xt,yt)(x_t,y_t)
  2. Update the first conditional
    xt+1∼p(x∣yt)x_{t+1}\sim p(x\mid y_t)
  3. Then the second
    yt+1∼p(y∣xt+1)y_{t+1}\sim p(y\mid x_{t+1})
Reference illustration. The Gaussian Gibbs sampler uses the latest first coordinate in its second update. Strong correlation can make movement slow, so retained rows are not independent samples.

In the second update of a sweep, should Y condition on the old X or the newly drawn X?

Write a prediction (optional)

Seeded teaching simulation

Select Run Python below with the code unchanged.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

Published example output

This result comes from the original Python. Your runs appear above. Build environment ↗

Kept means; target [0,0]: [-0.    -0.004]
Kept covariance; target diagonal 1 and off-diagonal rho:
[[0.974 0.822]
 [0.822 0.968]]
Lag-1 correlation of X: 0.715

Read the first result

Conditioning uses the latest state. The lag correlation warns against treating the kept row count as an independent sample count.

Try an edit

Replace rho = .85 with rho = .98. Select Run Python again. Compare Lag-1 correlation of X and the off-diagonal values in Kept covariance with the first run. What does stronger correlation between neighbouring rows imply about the information they add?

Back to the Python editor

Scope. These exact normal conditionals are tractable. More complex targets may need other conditional samplers and stronger diagnostics.

Try a fresh case

Every Gibbs update is accepted. What can still make the sampler inefficient?

Choose an answer
Need a hint?
Hint 1

Inspect the lag correlation.

Hint 2

Compare how quickly coordinates move across the full target range.

Your notes