Exercise: Ripley’s K, the L function and the pair correlation
Before this unit: Nearest neighbours and empty space: G, F and J (previous unit); Day 16: Measurements.
See also: Day 20: Point detection; Day 38: Evaluation and statistics; Day 48: Colocalisation.
Goal
By the end of this exercise you should be able to:
- compute \hat K(r) and L(r) - r for a small pattern by hand and compare with CSR,
- read a pair-correlation curve: cluster size, hard-core distance, two scales,
- compute a translation edge weight and predict the bias of an uncorrected K,
- (math) derive K = \pi r^2 for CSR, the Thomas K function, and why the translation weight removes the edge bias.
Background
| Idea | What it means |
|---|---|
| K(r) | mean number of further points within r, divided by ρ; CSR: \pi r^2 |
| \hat K(r) | \frac{|W|}{n(n-1)}\sum_{i\ne j} w_{ij}[d_{ij} \le r] (ordered pairs; w = 1 without correction) |
| L(r) - r | \sqrt{K/\pi} - r: 0 random, > 0 clustered, < 0 regular |
| g(r) | K'/(2\pi r): pairs at distance r relative to CSR |
| Translation weight | |W|/|W \cap (W + \mathbf x_j - \mathbf x_i)|; rectangle: ab/((a - |\Delta x|)(b - |\Delta y|)) |
| Thomas | K = \pi r^2 + (1 - e^{-r^2/4\sigma^2})/\kappa |
By hand
B1. Five points in a 10 × 10 µm window: P_1 = (2, 2), P_2 = (3, 2), P_3 = (4, 4), P_4 = (7, 5), P_5 = (8, 7). Their pairwise distances:
| P_2 | P_3 | P_4 | P_5 | |
|---|---|---|---|---|
| P_1 | 1.00 | 2.83 | 5.83 | 7.81 |
| P_2 | 2.24 | 5.00 | 7.07 | |
| P_3 | 3.16 | 5.00 | ||
| P_4 | 2.24 |
- Count the ordered pairs closer than r for r = 1, 2, 3, 4.
- Compute \hat K(r) (no correction) and compare with \pi r^2.
- Compute L(r) - r at these radii. What would you conclude, and how much would you trust it?
B2. Cells with ρ = 0.01 µm⁻², observed \hat K(10\,\mu m) = 500 µm². (a) How many neighbours within 10 µm does CSR give a typical cell? How many does the observed K give? (b) Compute L(10) - 10. Clustered or regular?
B3. A pair-correlation curve has these values:
| r (µm) | 2 | 4 | 6 | 8 | 10 | 14 | 18 | 22 | 30 |
|---|---|---|---|---|---|---|---|---|---|
| g | 0 | 0 | 0.1 | 3.1 | 2.6 | 1.9 | 1.4 | 1.05 | 1.0 |
Describe the pattern: minimum distance, the scale of clustering, the behaviour beyond.
B4. A window of 100 × 50 µm. A pair has \Delta x = 20, \Delta y = 10 µm. Compute its translation weight. What would it be in a 1000 × 500 µm window?
B5. A jittered square lattice with spacing 20 µm (small jitter). Predict L(r) - r at r = 10, 25 and 30 µm. (Use ρ = 1/400 µm⁻² and count the neighbours of a lattice point.)
B6. For 100 random points in a square window, the uncorrected \hat K is about 8 % too low at a tenth of the side and 20 % too low at a quarter. In L - r terms at r = 50 µm, how large is that error? Would you mistake it for regularity?
B1. (a) Ordered pairs (each pair counts twice): r = 1: 2 (P1–P2); r = 2: 2; r = 3: 8 (adding P1–P3, P2–P3, P4–P5); r = 4: 10 (adding P3–P4). (b) |W|/(n(n-1)) = 100/20 = 5: \hat K = 10, 10, 40, 50 against \pi r^2 = 3.1, 12.6, 28.3, 50.3. (c) L - r = 0.78, −0.22, 0.57, −0.01. Slight excess at 1 µm (one close pair), otherwise near CSR. With 5 points and no edge correction this means nothing; unit 4 shows how to judge it.
B2. (a) CSR: \rho\pi r^2 = 0.01 \cdot 314 = 3.1 neighbours. Observed: \rho K = 0.01 \cdot 500 = 5.0. (b) \sqrt{500/\pi} - 10 = 12.6 - 10 = 2.6 µm > 0: clustered at 10 µm.
B3. No pairs below about 6 µm: a hard core (objects of about 6–7 µm diameter). g well above 1 from 8 to about 20 µm, falling to 1 near 22 µm: clusters of about 20 µm diameter. Random beyond. Like colonies of cells that keep a minimum distance.
B4. 5000/((100 - 20)(50 - 10)) = 5000/3200 = 1.56. In the large window: 500\,000/(980 \cdot 490) = 1.04: in a large window the correction hardly matters at this distance.
B5. r = 10: no neighbours closer than about 20 µm, so K ≈ 0 and L - r ≈ -10 µm. r = 25: the 4 nearest lattice neighbours at 20 µm: K = 4/\rho = 1600, L = \sqrt{1600/\pi} = 22.6, L - r = -2.4. r = 30: also the 4 diagonal neighbours at 28.3 µm: K = 3200, L = 31.9, L - r = +1.9. L − r oscillates around 0 as the lattice shells are passed: regular.
B6. L = \sqrt{0.8\,\pi r^2/\pi} = 0.894\,r, so at 50 µm L - r ≈ -5.3 µm. Yes: without a correction a random pattern looks clearly regular at large r.
Math questions
B7. (math) Using Slivnyak’s theorem, show that K(r) = \pi r^2 for a Poisson process and that g \equiv 1. Why does \hat K use n(n-1)/|W|^2 as an estimate of \rho^2?
B8. (math) Derive the Thomas K function. Two offspring of one parent are displaced by independent N(0, \sigma^2 I) vectors: what is the distribution of their distance? With κ = 0.0005 µm⁻², σ = 4 µm compute K(10), L(10) - 10 and g(0).
B9. (math) Show that the translation-corrected estimator of the pair sum is unbiased for a stationary process: E\left[\sum_{i \ne j}\frac{[\mathbf x_i - \mathbf x_j \in B]}{|W \cap W_{\mathbf x_j - \mathbf x_i}|}\right] = \rho^2\int_B g(\mathbf v)\,d\mathbf v. (Use the second-order product density \rho^2 g(\mathbf u - \mathbf w).)
B7. Given a point at \mathbf x, the others are Poisson(ρ), so the expected count within r is \rho\pi r^2 = \rho K(r). K' = 2\pi r, g = 1. Under CSR conditioned on n, E[n(n-1)] = (\rho|W|)^2 for a Poisson count (E[N(N-1)] = \lambda^2 for N \sim Poisson(λ)), so n(n-1)/|W|^2 is unbiased for \rho^2; n^2 would be biased by n.
B8. The difference is N(0, 2\sigma^2 I), so its length is Rayleigh with P(D \le r) = 1 - e^{-r^2/4\sigma^2}. A typical point has the CSR neighbours of the other clusters, \rho\pi r^2, plus on average \mu siblings (Poisson offspring: given a point, its number of siblings is again Poisson(μ)), of which a share P(D \le r) is within r. Dividing by \rho = \kappa\mu: K = \pi r^2 + (1 - e^{-r^2/4\sigma^2})/\kappa. Numbers: K(10) = 314.2 + (1 - e^{-1.5625})/0.0005 = 314.2 + 1580.7 = 1895 µm²; L(10) - 10 = \sqrt{1895/\pi} - 10 = 14.6 µm; g(0) = 1 + 1/(4\pi \cdot 0.0005 \cdot 16) = 10.9.
B9. E\sum_{i\ne j} h(\mathbf x_i, \mathbf x_j) = \int_W\int_W h(\mathbf u, \mathbf w)\,\rho^2 g(\mathbf w - \mathbf u)\,d\mathbf u\,d\mathbf w. Substitute \mathbf v = \mathbf w - \mathbf u: the integrand is [\mathbf v \in B]\,\rho^2 g(\mathbf v)/|W \cap W_{\mathbf v}|, and for fixed \mathbf v the set of \mathbf u with both \mathbf u and \mathbf u + \mathbf v in W has area |W \cap W_{-\mathbf v}| = |W \cap W_{\mathbf v}|. It cancels, leaving \rho^2\int_B g(\mathbf v)\,d\mathbf v. With B the disc of radius r this is \rho^2 K(r); multiplying by |W| and dividing by n(n-1)/|W| gives \hat K.
Explore
The pattern comes from the shared source (choose a model and seed), or you build it yourself (tick edit). The red probe point carries a disc of radius r (what K counts, orange) and a ring r ± h (what g counts, green); click another point to move the probe. Tabs: K, L − r, g and the bias of the corrections (50 CSR patterns in the same window). Choose the edge correction below the plot.
E1. Preset CSR: L − r ≈ 0. Move r from 5 to 40 µm. How far from 0 does L - r wander? Click a few points: how much does a single point’s count vary around ρπr²?
E2. Preset Thomas: g falls to 1 at the cluster size. Where does ĝ return to 1? Compare with 4σ. Why does ĝ dip below the formula at the smallest r?
E3. Preset K remembers small clusters. Read L - r at 5 and at 30 µm and compare with ĝ at the same distances.
E4. Preset colonies with a hard core: two scales. Read the hard-core distance and the colony size off ĝ. Then look at the L − r tab: where does it cross 0?
E5. Preset no correction: looks regular. Read \hat K(30) and L - r at 30 µm, then switch to translation and isotropic. Open the bias of corrections tab and compare the four corrections at 40 µm.
E6. Preset build your own pattern. Add points to make (a) a tight cluster in an otherwise random pattern, (b) a ring of points around the probe at 15 µm. Predict and then check the K and g curves.
E1. For the default seed L - r stays within 2 µm of 0 (−0.51 at 10, −0.80 at 20, −1.71 at 30 µm). Single points’ counts vary a lot: with ρπr² ≈ 1.2 at r = 10, counts of 0 to 4 are common. K averages over all points, which is why it is stable.
E2. ĝ(8) = 3.74 and ĝ is about 1 from 16 µm on = 4σ. Near r = 0 the kernel of half-width h reaches below 0, where there are no pairs, so ĝ is pulled down (kernel bias at the origin).
E3. L - r = 13 µm at 5 µm and still 2.7 µm at 30 µm. ĝ is very high below 4 µm and close to 1 (it fluctuates around it) from about 6 µm. K keeps the close pairs in every disc; g only looks at the ring.
E4. ĝ = 0 below 6 µm (the hard core), a peak above 4 just beyond, values above 1 up to about 20 µm (the colony size), 1 beyond. L - r = −5 at 5 µm (no neighbours), crosses 0 near 6–7 µm and is +6 at 10 µm, +9 at 20 µm.
E5. No correction: \hat K(30) = 2209 against \pi r^2 = 2827, L - r = -3.5. Translation: 2515, L - r = -1.7 (this seed is slightly regular by chance). In the bias tab the uncorrected curve falls steadily (roughly −16 % at 40 µm), the others stay within a few percent of 0.
E6. (a) A tight cluster adds many short pairs: L - r and g jump up at small r; K stays raised at large r. (b) A ring at 15 µm around the probe: the probe’s count curve (thin red steps) jumps at 15 µm; g gets a bump at 15 µm, but only a small one, because only one point’s neighbours changed.
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 spatialstats/src/ripley_k/exercise.rs. Points are pairs (x, y) in µm, the window is the rectangle [0, a] \times [0, b]; the pattern generators of the previous units are given.
C1. translation_weight(dx, dy, a, b): ab / ((a - |\Delta x|)(b - |\Delta y|)). The check is B4.
C2. k_hat(points, a, b, r, translation) and l_minus_r(k, r):
s = 0
for every point i:
for every point j ≠ i:
if distance(i, j) ≤ r:
s = s + w_ij # translation weight, or 1 without a correction
K = |W| / (n (n − 1)) · s
L − r = sqrt(K / π) − r
The checks are B1(b) and (c), and that with the translation weight the mean \hat K(25) of 20 random patterns in 100 \times 100 µm is close to \pi r^2, while without it is clearly lower.
C3. pair_correlation(points, a, b, r, h): the kernel estimate of g, counting pairs whose distance lies within h of r:
s = 0
for every ordered pair i ≠ j with |r − d_ij| ≤ h:
t = (r − d_ij) / h
s = s + w_ij · 3/(4h) · (1 − t²) / (2π d_ij)
g = |W| / (n (n − 1)) · s
The checks: g \approx 1 for CSR, g = 0 below a hard core, g \gg 1 at short distances in a cluster pattern.
Then cargo run -p spatialstats -- ripley-k. It prints L - r and g at r = 2 to 30 µm for four patterns in 200 \times 200 µm: CSR, clusters (Thomas, σ = 4 µm), a hard core of 8 µm, and colonies of cells that keep 6 µm apart (colony radius 12 µm). Then the mean error of \hat K over 200 random patterns, without and with the translation correction.
C4. Read the cluster size and the hard-core distance off g for each pattern. Compare L - r and g of the clusters at 20 µm: which one still remembers the clusters?
C5. Compare the bias without a correction with the slide “How large is the bias?”.
C6. \hat K at one r looks at all n(n-1) ordered pairs. How many is that for 150 points, and how could you get \hat K at all eight radii from one pass over the pairs?
The model solution is spatialstats/src/ripley_k/solution.rs in the download.
C4. CSR: L - r stays within 0.6 µm of 0 and g between 0.85 and 1.27. Clusters (287 points): g = 3.9 at 2 µm, falling through 1 between 10 and 15 µm, about 4\sigma = 16 µm, the cluster diameter; beyond it fluctuates a little below 1 (0.7). Hard core: g = 0 up to 6 µm, 0.68 at 8 µm, 1.20 at 10 µm (the packing bump), then 1; L - r = -r up to 8 µm: no neighbours at all. Colonies (131 cells): g = 0 below 6 µm (the cells’ distance), about 5 from 6 to 8 µm, above 1 up to about 20–30 µm (the colony size), 1.16 at 30. At 20 µm the clusters have g = 0.70 but L - r = +2.7: K counts every pair closer than 20 µm, including the close pairs inside the clusters, so L remembers them; g only looks at the ring at 20 µm.
C5. Without a correction: −4.3 % at 10 µm, −7.8 % at 20 µm, −20.0 % at 50 µm, as on the slide (−8 % and −21 %). With translation weights: within 1 %.
C6. 150 \cdot 149 = 22\,350 ordered pairs (half that many distinct pairs, each counted twice). Compute every distance once, and add the pair’s weight to every radius r \ge d_{ij} (or sort the distances and take running sums): one pass gives the whole curve.
Where this leads
Every curve so far was compared with the CSR formula by eye. The next unit asks how far a curve can stray from it by chance: simulation envelopes, and why reading a band at many r at once is a trap.