Solution (source code)

= Solution

For a target <probability density function> $\pi(\theta_1,\ldots,\theta_p)$, one systematic sweep of the <Gibbs sampler> updates coordinates successively:
$$
\theta_j^{(m+1)}\sim\pi\left(\theta_j\mid\theta_1^{(m+1)},\ldots,\theta_{j-1}^{(m+1)},\theta_{j+1}^{(m)},\ldots,\theta_p^{(m)}\right),\quad j=1,\ldots,p.
$$
Each update leaves the target invariant: keeping the other coordinates with their target marginal and replacing one coordinate by its <conditional distribution> reconstructs the target <joint probability density function>. Therefore composition of the coordinate updates also preserves $\pi$. It defines a <Markov chain> whose consecutive draws are generally dependent. Under irreducibility, aperiodicity and the usual recurrence conditions it converges to the target law from an arbitrary valid start; a stationary start gives exact target marginals immediately. Invariance alone does not prove these ergodicity conditions, and a systematic full sweep need not itself be reversible.