Exercise: energy and probability
Before this unit: Day 27: Graph cuts.
See also: Day 3: Bit depth and noise.
Goal
By the end of this exercise you should be able to:
- compute the Ising energy of a small lattice, and its Gibbs probability e^{-E/T}/Z,
- compute the partition function Z of a 2 \times 2 lattice by grouping states by energy,
- predict what temperature, a field and a negative coupling do to the probabilities,
- translate an Ising energy into the segmentation energy of the graph-cut day,
- (math) compute Z of a ring with the transfer matrix.
Do the pen-and-paper part first, then the interactive part. Questions marked (math) are for those who want the derivations; the concept questions do not depend on them.
Background
| Word | Meaning |
|---|---|
| Spin s_p | +1 or -1 at pixel p |
| Ising energy | E(\mathbf s) = -J\sum_{(p,q)} s_p s_q - h\sum_p s_p, the first sum over neighbour pairs |
| Coupling J | J > 0: agreeing neighbours lower the energy; J < 0: disagreeing ones do |
| Field h | h > 0 favours +1 |
| Gibbs distribution | p(\mathbf s) = e^{-E(\mathbf s)/T}/Z |
| Partition function | Z = \sum_{\mathbf s} e^{-E(\mathbf s)/T}, over all 2^n states |
| Temperature T | small: only the lowest energies matter; large: all states nearly equally likely |
| Energy level | all states with the same energy; its probability = number of states \times\, e^{-E/T}/Z |
| Magnetisation | m = \frac1n\sum_p s_p, between -1 and +1 |
Unless stated otherwise: J = 1, h = 0, free boundary (no wrap-around), 4-neighbours.
By hand
B1. The 2 \times 2 lattice has 4 neighbour pairs and 16 states. (a) Group the 16 states by energy: how many have E = -4, E = 0 and E = +4? Draw one of each. (b) Write Z as a function of T and evaluate it at T = 1 and T = 4. (c) What is the probability of the energy level E = -4 at T = 1 and at T = 4? Of one particular aligned state? (d) At T = 4, which energy level is most probable? Which single state is most probable? Explain the difference.
B2. Same 2 \times 2 lattice at T = 1, now with a field h = 0.5. (a) Compute the energies of “all +1” and “all -1”. (b) By what factor is “all +1” more likely than “all -1”? Answer without computing Z. (c) With Z = 427.20, give both probabilities. Where has the probability of “all -1” gone?
B3. Temperature. For the 2 \times 2 lattice, describe without computing what happens to p(\text{all } +1) as T \to 0 and as T \to \infty. What do the two limits correspond to in image analysis?
B4. Negative coupling. Take J = -1, h = 0 on the 2 \times 2 lattice at T = 1. Which states are now most probable, and with what total probability? (Use B1: what happens to the energy of each state when J changes sign?)
B5. From spins to labels. A row of 4 pixels has the Ising energy E = -J(s_1s_2 + s_2s_3 + s_3s_4) - h(s_1 + s_2 + s_3 + s_4), with J = 1, h = 0.5. (a) Compute E for \mathbf s = (+, +, -, -) and for (+, -, +, -). (b) With labels L_p = (s_p + 1)/2, write the energy as \sum_p D_p(L_p) + \lambda\sum[L_p \ne L_q] + \text{const}. What are \lambda, D_p(1) - D_p(0) and the constant? (c) Check (b) on the two states of (a).
B6. How many states? How many states has a 4 \times 4 lattice, a 16 \times 16 lattice, a 256 \times 256 image? If a computer evaluates 10^9 energies per second, how long does Z take for the 4 \times 4 and for the 16 \times 16 lattice?
B1. (a) Two aligned states (all pairs agree): E = -4. Two checkerboards (all pairs disagree): E = +4. The other 12 states have two agreeing and two disagreeing pairs: E = 0. (One -1 among three +1: the -1 disagrees with its two neighbours and the other two pairs agree; two -1 side by side: the two pairs across the middle disagree.) (b) Z = 2e^{4/T} + 12 + 2e^{-4/T}. T = 1: 109.196 + 12 + 0.037 = 121.23. T = 4: 5.437 + 12 + 0.736 = 18.17. (c) Level E = -4: 109.20/121.23 = 0.901 at T = 1; 5.437/18.17 = 0.299 at T = 4. One aligned state: half of that, 0.450 and 0.150. (d) At T = 4 the level E = 0 has probability 12/18.17 = 0.660: most probable. But each of its 12 states has only 1/18.17 = 0.055, less than an aligned state (0.150). A single low-energy state is always the most probable state; a level with many states can still win because of their number.
B2. (a) All +1: E = -4 - 4 \cdot 0.5 = -6. All -1: E = -4 + 2 = -2. (b) e^{-(-6) + (-2)} = e^{4} = 54.6. (c) p(\text{all } +1) = e^6/427.20 = 0.944, p(\text{all } -1) = e^2/427.20 = 0.017. Without the field both had 0.450; the field moved almost all the probability of “all -1” to “all +1”. The field breaks the tie between the two ground states.
B3. As T \to 0 the two ground states share all the probability: p(\text{all } +1) \to \tfrac12 (and with any small h > 0 it goes to 1). As T \to \infty every state gets \tfrac1{16}. The first limit is energy minimisation (graph cuts find the ground state); the second is “the prior means nothing”: every image equally plausible.
B4. With J = -1 every energy changes sign: the two checkerboards now have E = -4, the aligned states +4. The 12 others stay at 0. So Z is the same, 121.23, and the two checkerboards carry 0.901 of the probability together. An antiferromagnet prefers disagreeing neighbours.
B5. (a) (+, +, -, -): pairs +1 - 1 + 1 = 1, field sum 0: E = -1. (+, -, +, -): pairs -3, field 0: E = +3. (b) \lambda = 2J = 2; D_p(1) - D_p(0) = -2h = -1 (take D_p(1) = -1, D_p(0) = 0); constant = \sum_p h - 3J = 2 - 3 = -1. So E = -(\text{number of object pixels}) + 2 \cdot (\text{number of boundaries}) - 1. (c) (+, +, -, -): 2 object pixels, 1 boundary: -2 + 2 - 1 = -1. (+, -, +, -): 2 object pixels, 3 boundaries: -2 + 6 - 1 = 3. Both agree with (a).
B6. 2^{16} = 65\,536 (well under a millisecond), 2^{256} \approx 1.2 \cdot 10^{77} (about 10^{68} seconds, far longer than the age of the universe, 4 \cdot 10^{17} s), 2^{65536} \approx 10^{19728}. Exact Z is out of the question for any real image.
Math questions
B7. (math) Show that adding a constant c to every energy leaves the Gibbs distribution unchanged, and that p(\mathbf a)/p(\mathbf b) depends only on E_a - E_b. Show that for two lattices that do not interact, E = E_1 + E_2, the Gibbs distribution factorises and Z = Z_1Z_2.
B8. (math) Derive the general translation of B5: with s_p = 2L_p - 1 and m neighbour pairs, show that E_{\text{Ising}} = \sum_p(-2h_p)L_p + 2J\sum_{(p,q)}[L_p \ne L_q] + \sum_p h_p - mJ.
B9. (math) Transfer matrix. For a ring of n spins with h = 0, show that Z = \operatorname{tr}(M^n) with M = \begin{bmatrix} e^{J/T} & e^{-J/T} \\ e^{-J/T} & e^{J/T}\end{bmatrix}, find the eigenvalues of M, and evaluate Z for n = 3, J = 1, T = 1. Check by listing the 8 states. What fraction of Z comes from the larger eigenvalue for n = 3, n = 10 and n = 100?
B7. e^{-(E + c)/T}/\sum e^{-(E' + c)/T} = e^{-c/T}e^{-E/T}/(e^{-c/T}\sum e^{-E'/T}): the factor cancels. p(\mathbf a)/p(\mathbf b) = e^{-E_a/T}/e^{-E_b/T} = e^{-(E_a - E_b)/T}; Z cancels. With E = E_1(\mathbf s_1) + E_2(\mathbf s_2): e^{-E/T} = e^{-E_1/T}e^{-E_2/T} and Z = \sum_{\mathbf s_1}\sum_{\mathbf s_2}e^{-E_1/T}e^{-E_2/T} = Z_1Z_2, so p(\mathbf s_1, \mathbf s_2) = p_1(\mathbf s_1)\,p_2(\mathbf s_2).
B8. s_ps_q = 1 if L_p = L_q and -1 otherwise, so s_ps_q = 1 - 2[L_p \ne L_q] and -Js_ps_q = 2J[L_p \ne L_q] - J; summed over m pairs: 2J\sum[L_p \ne L_q] - mJ. And -h_ps_p = -h_p(2L_p - 1) = -2h_pL_p + h_p. Adding gives the result.
B9. e^{-E/T} = \prod_i e^{Js_is_{i+1}/T} = \prod_i M_{s_is_{i+1}}; summing over s_1, \dots, s_n is the matrix product \sum M_{s_1s_2}M_{s_2s_3}\cdots M_{s_ns_1} = \operatorname{tr}(M^n). M has eigenvectors (1, 1) and (1, -1) with eigenvalues e^{J/T} \pm e^{-J/T} = 2\cosh(J/T),\ 2\sinh(J/T), and \operatorname{tr}(M^n) = \mu_1^n + \mu_2^n. n = 3: 3.0862^3 + 2.3504^3 = 29.39 + 12.98 = 42.38. Listing: 2 aligned states with E = -3 and 6 states with one spin different (E = +1): 2e^3 + 6e^{-1} = 40.17 + 2.21 = 42.38. Fraction from \mu_1^n: 0.694 (n = 3), 0.938 (n = 10), 1 - 1.5 \cdot 10^{-12} (n = 100): for long chains only the largest eigenvalue matters.
Explore
The demo lists every state of a 2 \times 2 (16 states) or 3 \times 3 (512 states) lattice. Click a spin to flip it (orange +1, blue -1). Bonds are green where the pair lowers the energy and red where it raises it; dashed stubs are the wrap-around pairs of the periodic boundary (the 3 \times 3 torus has 18 pairs, the free 3 \times 3 lattice 12). The readout gives E (split into pairs and field), e^{-E/T}, Z and the probability of the state shown. The bars are the probabilities of the energy levels (number of states above each bar; orange: the level of the current state), or of the magnetisation m.
E1. Preset 2×2 by hand. Check B1: read Z and p of the aligned state at T = 1. Flip one spin: what are E and p now? Move T to 4 and compare the bars with B1(c, d).
E2. Choose 3 \times 3, periodic, T = 2. How many energy levels are there, and how many states does the lowest one have? What is the probability of the two ground states together? Why does the level E = -2 have so many states and so little probability?
E3. Preset T → 0 and then T → ∞. Describe the two bar charts. In the second, why are the bars nearly proportional to the number of states?
E4. Preset a field breaks the tie (T = 1.5, h = 0.3, magnetisation tab). Read p(m = 1) and p(m = -1). Which is larger: p(m = -1) or p(m = 7/9)? Explain.
E5. Preset J < 0: checkerboards win (free boundary). Read the probability of the lowest level. Now switch to periodic. What is the lowest energy now, and how many states have it? Why can a 3 \times 3 torus not be a perfect checkerboard?
E1. Z = 121.2, p = 0.450 for all +1. One flipped spin: E = 0, p = 0.00825 = 1/Z. At T = 4: Z = 18.17; the bars are 0.299 (E = -4), 0.660 (E = 0), 0.040 (E = 4), as in B1.
E2. Six levels: E = -18, -10, -6, -2, 2, 6 with 2, 18, 48, 198, 144, 102 states; Z = 20\,438. The two ground states carry 0.793 together (0.396 each). The level E = -2 has 198 states, but each one has weight e^{1} against e^{9} for a ground state: 198e^{1} = 538 against 2e^{9} = 16\,206, so it gets only 0.026.
E3. At T = 0.3 the two ground states carry all the probability (1.000); every other level is invisible. At T = 100 every state has almost the same weight, so a level’s probability is its share of the 512 states: E = -2 (198 states) 0.394, E = 2 (144) 0.275, E = 6 (102) 0.187, and the ground states only 0.005. Counting states, not energy, decides.
E4. p(m = 1) = 0.941 and p(m = -1) = 0.026. p(m = 7/9) = 0.027 is (slightly) larger than p(m = -1): “all -1” is a single state with the lowest pair energy, while m = 7/9 has 9 states (one spin down), each with 8 more units of pair energy but a better field term. At this temperature the 9 states together just beat the lone state on the wrong side.
E5. Free 3 \times 3 (J = -1, T = 1): the two checkerboards have E = -12 (all 12 pairs disagree) and carry 0.890. Periodic: the lowest energy is E = -6, shared by 102 states (0.974 together). Each row and column of the torus is a ring of 3 spins: around an odd ring at least one pair must agree. So no state can make all 18 pairs disagree (this is called frustration); many states share the best compromise.
Code
Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in ising/src/ising/exercise.rs. A state is a small image of spins, +1 or -1; state_from_index (given) turns a number 0 \dots 2^n - 1 into a state, so that a loop over the numbers visits every state once.
C1. energy(s, J, h, periodic): the Ising energy. Count every pair of neighbours once, by pairing each pixel with its right neighbour and the one below it:
pairs = 0, field = 0
for every pixel (x, y):
field = field + s(x, y)
if there is a pixel to the right (or periodic: wrap to column 0):
pairs = pairs + s(x, y) · s(x+1, y)
if there is a pixel below (or periodic: wrap to row 0):
pairs = pairs + s(x, y) · s(x, y+1)
E = −J · pairs − h · field
With periodic, wrap only along sides of at least 3 pixels (on a side of 2 the wrapped pair would be the same pair twice). The checks are B1(a), B2(a), B5(a) and the 18 pairs of the 3 \times 3 torus.
C2. energy_levels(width, height, J, h, periodic): list all 2^n states, and count how many have each energy:
levels = empty list of (energy, count)
for index = 0 .. 2^n − 1:
E = energy(state number index)
if E is already in levels: add 1 to its count
else: add (E, 1)
sort levels by energy
The checks are B1(a) (2, 12 and 2 states) and the six levels of the 3 \times 3 torus in E2.
C3. level_probabilities(levels, T): Z = \sum (\text{count}) \cdot e^{-E/T} over the levels, and each level’s probability \text{count} \cdot e^{-E/T}/Z. The checks are B1(b–d), B2(c) (Z = 427.20) and E2.
C4. ring_partition_function(n, J, h, T): Z of a ring of n spins with the transfer matrix (math slide of the lecture):
M = [ e^((J+h)/T) e^(−J/T) ]
[ e^(−J/T) e^((J−h)/T) ]
P = identity
repeat n times: P = P · M
Z = P[1][1] + P[2][2] # the trace
The checks are B9 (Z = 42.38 for n = 3) and a comparison with listing all 1024 states of a ring of 10 (a 10 \times 1 image with periodic).
Then cargo run -p ising -- ising and open the picture in out/ising/. The program prints the 2 \times 2 lattice at several temperatures, the levels of the 3 \times 3 torus, how long listing takes, and Z of rings by both methods.
C5. The program times the listing for the 4 \times 4 lattice and for a 5 \times 4 lattice. At that speed per state, how long would a 6 \times 6 lattice take? A 16 \times 16 lattice?
C6. Compare the two columns for the rings. How does the time of each method grow with n? Look at states-3x3.png (all 512 states of the torus, lowest energy first, row by row): what are the first two tiles, and what do the tiles in the last rows have in common?
The model solution is ising/src/ising/solution.rs in the download.
C5. In our run listing took about 350 ns per state (2^{16} states in 0.02 s, 2^{20} in 0.37 s). A 6 \times 6 lattice has 2^{36} \approx 6.9 \cdot 10^{10} states: about 2.4 \cdot 10^4 s, close to 7 hours. A 16 \times 16 lattice: 2^{256} \cdot 3.5 \cdot 10^{-7}\text{ s} \approx 4 \cdot 10^{70} s. Every extra pixel doubles the time (B6).
C6. Both give the same Z (up to rounding in the last digits for n = 18). The transfer matrix takes a fraction of a microsecond for every n: n products of 2 \times 2 matrices. Listing doubles with every extra spin: in our run 4 ms for n = 14 and 150 ms for n = 18. The first two tiles of states-3x3.png are all orange and all blue, the two ground states. The tiles in the last rows are speckled: almost every neighbour pair disagrees, as far as an odd torus allows (no state of the 3 \times 3 torus is a perfect checkerboard).
Where this leads
We cannot compute Z for an image, so we cannot list probabilities. The next unit shows how to draw samples from the Gibbs distribution one pixel at a time, without Z, and what the samples look like on a large lattice at different temperatures: domains, a phase transition, and the danger of cooling too fast.