Sampling, temperature and the phase transition

Image analysis

About this lecture

Last unit: the Gibbs distribution p(\mathbf s) \propto e^{-E(\mathbf s)/T}, and why its normaliser Z is out of reach for an image. Today: we draw samples anyway, one pixel at a time.

  • The local rule: a pixel given its neighbours is a coin flip with a known bias.
  • Two samplers: Gibbs (redraw the pixel) and Metropolis (propose a flip, maybe accept).
  • Why they work: a random walk whose long-run distribution is the Gibbs distribution.
  • What samples of a large lattice look like: the phase transition at T_c \approx 2.27.
  • Simulated annealing: sample while cooling, to find low energies.

Slides titled The math give the formulas and proofs; the ideas are complete without them.

A sample is the best picture of a distribution

Each panel is one random state of a 128 \times 128 Ising lattice (J = 1, h = 0), drawn by the methods of this unit (500 sweeps from random spins).

  • We cannot list 2^{16384} probabilities, but a few typical states tell us what the model believes.
  • Cold: large regions that agree. Hot: noise. In between, at T_c: clusters of every size.

One pixel at a time: no Z needed

Change only pixel p. Its two possible values give two states that differ in s_p alone; their energy difference involves only p and its neighbours.

  • The neighbours “vote”: the local field u_p = J\cdot(\text{sum of the 4 neighbouring spins}) + h.
  • Given its neighbours, the pixel is +1 with probability p(s_p = +1 \mid \text{neighbours}) = \frac{1}{1 + e^{-2u_p/T}} a logistic curve in the vote.
  • Example: neighbours (+, +, +, -), J = 1, h = 0: u_p = 2. At T = 2: 1/(1 + e^{-2}) = 0.88. At T = 1: 0.98; at T = 4: 0.73.
  • Markov property: given its neighbours, a pixel does not care about the rest of the image. Hence “Markov random field”.

Left: a pixel marked with a question mark and its four neighbours, three +1 (orange) and one −1 (blue); their vote is u = 1 + 1 + 1 − 1 = 2. Right: the probability that the pixel is +1 against the vote u, a logistic curve for T = 1, 2 and 4, steeper at low T; at u = 2 it is 0.98, 0.88 and 0.73.

The math: the local conditional

Write \mathbf s^+ and \mathbf s^- for the state with s_p = +1 and s_p = -1, everything else fixed. Every term of E that does not contain s_p is the same in both, so

E(\mathbf s^-) - E(\mathbf s^+) = 2J\sum_{q \sim p}s_q + 2h = 2u_p.

The ratio of probabilities needs no Z:

\frac{p(\mathbf s^+)}{p(\mathbf s^-)} = e^{2u_p/T} \quad\Longrightarrow\quad p(s_p = +1 \mid \text{rest}) = \frac{e^{2u_p/T}}{e^{2u_p/T} + 1} = \frac{1}{1 + e^{-2u_p/T}}.

It depends on the rest of the image only through the neighbours (via u_p): the Markov property. Flipping p changes the energy by \Delta E = 2s_pu_p.

Two samplers

Visit the pixels one after the other (a sweep visits each once).

  • Gibbs sampler (heat bath; Geman & Geman, 1984): draw the pixel anew from its local conditional, i.e. set +1 with probability 1/(1 + e^{-2u_p/T}).
  • Metropolis (Metropolis et al., 1953): propose to flip the pixel. Compute \Delta E:
    • \Delta E \le 0 (downhill): always accept;
    • \Delta E > 0 (uphill): accept with probability e^{-\Delta E/T}.

Uphill moves are what make it a sampler and not a minimiser. A flip with \Delta E = 4 is accepted with probability e^{-2} = 0.14 at T = 2, but only e^{-8} = 0.0003 at T = 0.5.

Probability that a proposed flip happens against its energy change ΔE from −8 to 8. Metropolis: 1 for every downhill move, then falling as e^(−ΔE/T): 0.14 at ΔE = 4 for T = 2, and almost immediately to zero for T = 0.5 (0.0003 at ΔE = 4). The Gibbs sampler at T = 2 is a smooth logistic curve through 0.5 at ΔE = 0.

The two samplers as pseudocode

repeat for every sweep:
    for every pixel p:
        u = J · (sum of the 4 neighbours of p) + h        # the local field
        Gibbs:       s_p = +1 if random() < 1/(1 + e^(−2u/T)), else −1
        Metropolis:  ΔE = 2 · s_p · u
                     if ΔE ≤ 0 or random() < e^(−ΔE/T):  s_p = −s_p
  • Each update reads only the four neighbours: one sweep costs a few operations per pixel.
  • random() is a uniform number between 0 and 1. The image is changed in place: later pixels see the new values.

Try it: sampling the 3 \times 3 lattice

Why does this work? A random walk on states

The sampler moves from state to state at random: a Markov chain. Two states, a (energy 0) and b (energy 1), T = 1, Metropolis:

  • From a: propose b, uphill by 1: go with probability e^{-1} = 0.37, else stay.
  • From b: propose a, downhill: always go.

P = \begin{bmatrix} 0.63 & 0.37 \\ 1 & 0 \end{bmatrix} \quad (\text{row} = \text{from}, \text{column} = \text{to})

  • Run long: the fraction of time in a and b settles at \boldsymbol\pi = (0.73, 0.27), whatever the start.
  • That is exactly the Gibbs distribution: 1/(1 + e^{-1}) = 0.73.
  • Balance: in the long run, traffic a \to b (0.73 \cdot 0.37 = 0.27) equals traffic b \to a (0.27 \cdot 1).

Top: two states a (energy 0) and b (energy 1) drawn as circles; arrows a to b with probability 0.37, a to a 0.63, b to a 1. Bottom: the probability of being in a after each step, starting in a (orange) or in b (blue); both oscillate and settle at 0.73 within about five steps.

The math: stationary distribution and detailed balance

A Markov chain with transition matrix P (P_{ab} = probability to go from a to b; rows sum to 1) moves a distribution \boldsymbol\pi (a row vector) to \boldsymbol\pi P. A stationary distribution satisfies

\boldsymbol\pi P = \boldsymbol\pi: \quad \boldsymbol\pi \text{ is a left eigenvector of } P \text{ with eigenvalue } 1.

If every state can reach every other (and the chain is not periodic), it is unique and the chain converges to it from any start.

Detailed balance: \pi_aP_{ab} = \pi_bP_{ba} for all pairs. Summing over a: \sum_a\pi_aP_{ab} = \pi_b\sum_aP_{ba} = \pi_b, so detailed balance implies stationarity.

Metropolis with a symmetric proposal and E_b > E_a: P_{ab} = q\,e^{-(E_b - E_a)/T}, P_{ba} = q. Then

\pi_aP_{ab} = \frac{e^{-E_a/T}}{Z}\,q\,e^{-(E_b - E_a)/T} = \frac{e^{-E_b/T}}{Z}\,q = \pi_bP_{ba}.

The Gibbs sampler satisfies it too (it is a Metropolis–Hastings move that is always accepted). Note: Z never had to be computed.

In practice: burn-in and mixing

  • Burn-in: the chain starts somewhere arbitrary (the noisy image, random spins). The first sweeps are not yet samples of the Gibbs distribution: discard them.
  • Correlated samples: consecutive states differ in a few pixels. 1000 sweeps are worth far fewer than 1000 independent samples.
  • Mixing: at low T the chain can stay in one region of states for a very long time. On the 3 \times 3 lattice at T = 1 it never leaves “mostly +1” in 100 000 steps, although “mostly -1” is as probable.
  • No convergence proofs here: in practice, run longer, start from several states, and check that the answers agree.
Left: energy per spin of a 128 × 128 lattice at T = 2.5 against sweeps, from random spins (starting at 0) and from all +1 (starting at −2); both reach the same fluctuating level near −1.1 within about 40 sweeps, shaded as burn-in. Right: the magnetisation of the 3 × 3 lattice at T = 1 (Metropolis, from all +1) over 100 000 single-site steps: it stays at +1 apart from two brief dips and never visits negative values.

The phase transition

On a large lattice (here 256 \times 256, periodic, h = 0) the samples change character at the critical temperature T_c = 2/\ln(1 + \sqrt2) \approx 2.27 (for J = 1; Onsager, 1944):

  • Below T_c: domains grow and one sign takes over the lattice: a magnetisation |m| well above 0 (0.91 at T = 2).
  • Above T_c: noise with small clusters; m \approx 0.
  • At T_c: clusters of all sizes, from single pixels to the whole lattice.

The model has no preferred sign, but below T_c the lattice picks one: spontaneous symmetry breaking. A tiny field h decides which.

Magnetisation |m| against temperature from 1 to 4. Onsager's exact curve for the infinite lattice stays near 1 at low T, drops steeply to 0 at T_c = 2.27 and is 0 above. Dots from a 64 × 64 simulation (the demo's T-scan) follow it below T_c (0.912 at T = 2, exact 0.911) and stay small but above zero above T_c.

Try it: a large lattice

The math: Onsager’s results

For the infinite square lattice with h = 0 (Onsager 1944; Yang 1952):

T_c = \frac{2J}{\ln(1 + \sqrt2)} \approx 2.269\,J, \qquad |m| = \Big(1 - \sinh(2J/T)^{-4}\Big)^{1/8} \text{ for } T < T_c, \quad 0 \text{ above}.

Energy per spin at T_c: -\sqrt2\,J.

No transition in 1D. For a chain, the transfer matrix of unit 1 gives the free energy per spin -T\ln\mu_1 with \mu_1 = 2\cosh(J/T) (for h = 0): a smooth function of T for every T > 0. A single disagreeing pair cuts a chain into two domains at a finite energy cost 2J, so at any T > 0 such walls appear with density about e^{-2J/T} and destroy long-range order. In 2D a wall around a domain costs energy proportional to its length, which wins against the entropy of the wall below T_c.

Critical slowing down

Near T_c local updates become slow: big clusters must be dismantled one pixel at a time.

Start a 128 \times 128 lattice with all spins +1 and count the sweeps until |m| < 0.1 (the true value above T_c is 0):

Magnetisation |m| against sweeps on a log axis, from all +1, for T = 4, 3, 2.5, 2.3 and 2.27. The sweeps until |m| first drops below 0.1: 2, 6, 29, 1580 and 3245. At T = 2.3 and 2.27 the magnetisation hovers between 0.5 and 0.8 for hundreds of sweeps before it falls.
  • For measurements near T_c, physicists use cluster algorithms that flip whole clusters at once (Swendsen & Wang, 1987); named only here.
  • For images, the lesson is general: a sampler with local moves can be very slow when the distribution has large-scale structure.

Simulated annealing

To find a low-energy state (Kirkpatrick et al., 1983; Geman & Geman, 1984): sample while lowering T.

  • At high T the chain moves freely between regions; as T drops, it settles into deeper and deeper valleys of the energy.
  • Cool slowly enough and it ends in a ground state.
  • Quench (drop T at once): domains freeze where they are. A domain wall that is flat across the whole periodic lattice cannot move by single flips without raising the energy: a local minimum.
  • In the demo (128 \times 128): quench to T = 0.5 from random spins: two stripes remain (m = 0.06 after 1000 sweeps) however long we wait. Cooling from 3.5 to 0.5 over 300 sweeps: one domain (m = -1), a ground state.

Two 128 × 128 lattices after 1000 sweeps from the same random spins. Left, quenched to T = 0.5: a vertical orange stripe between blue stripes, m = 0.06, energy per spin −1.97. Right, cooled from 3.5 to 0.5 over 300 sweeps: all blue, m = −1, energy per spin −2, the ground state.

The math: cooling schedules

  • Logarithmic: T_k = c/\ln(k + 2). Geman & Geman (1984) proved that for c large enough the chain ends in a ground state with probability 1. Impractically slow: halving T needs squaring k.
  • Geometric: T_k = T_0\,r^k with r slightly below 1 (e.g. from T_0 to T_1 in K sweeps: r = (T_1/T_0)^{1/K}). No guarantee; what people use.
  • At each temperature the chain should be close to its stationary distribution before T drops further. Near T_c that takes longest (critical slowing down): cool slowest there.

Summary

  • Given its neighbours, a pixel is +1 with probability 1/(1 + e^{-2u_p/T}): a logistic function of the neighbours’ vote. No Z needed.
  • Gibbs: redraw each pixel from this rule. Metropolis: propose a flip; accept downhill always, uphill with e^{-\Delta E/T}.
  • Both are Markov chains whose long-run distribution is the Gibbs distribution (detailed balance). Discard the burn-in; watch out for slow mixing.
  • The 2D Ising model has a phase transition at T_c \approx 2.27: ordered domains below, noise above, all scales at T_c, where local samplers slow down.
  • Simulated annealing cools while sampling; a quench freezes into a local minimum.