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 is normal with mean 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.
- Current state
- Update the first conditional
- Then the second
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.
A saved draft is open. To run the published example, open Tools and select Restore example. Recover draft brings your code back.
Execution error
Technical error details
Your result
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?
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?
Need a hint?
Hint 1
Inspect the lag correlation.
Hint 2
Compare how quickly coordinates move across the full target range.