An Ising model gives each pair of neighbouring spins the weight eJ if they agree and e−J if they differ (4.75). The Gibbs sampler below updates the sites of one checkerboard colour at a time. Raise J through the critical value Jc = ½ log(1 + √2) ≈ 0.4407 (4.79) and large clusters appear; the mean |m| follows Onsager's exact curve. A K-state Potts model (4.84) has its transition at log(1 + √K) (4.85). The second mode uses the Ising model as a prior for a noisy picture and infers the clean pixels by mean-field updates (Figure 4.27).
Sampling starts from the chosen start and runs 200 sweeps when a preset is chosen; Run continues. Spins are ±1 for K = 2 and the energy is −J∑xixj − h∑xi; for K ≥ 3 it is −J∑δ(xi, xj). The exact 4 × 4 numbers come from enumerating all 65,536 states. The denoising image is a 64 × 64 shape with Gaussian noise of standard deviation 2.
code/ch04_graphical_models.py (ising_gibbs, potts_gibbs, ising_exact_small, mean_field_ising_denoise, Figures 4.17, 4.18, 4.27, 4.S2).