Exercise: nearest neighbours and empty space
Before this unit: Point patterns and complete spatial randomness (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 nearest-neighbour distances and \hat G(r) for a small pattern by hand, and compare with the CSR curve,
- compute \hat F(r) from test locations and say how G and F react to clustering and regularity,
- apply the border rule and predict the sign of the edge bias,
- (math) derive G(r) = 1 - e^{-\rho\pi r^2}, show J \equiv 1 under CSR, and compute the part of a disc cut off by a border.
Background
| Idea | What it means |
|---|---|
| G(r) | share of points whose nearest neighbour is within r; CSR: 1 - e^{-\rho\pi r^2} |
| F(r) | share of test locations whose nearest point is within r; CSR: the same curve |
| J(r) | (1 - G)/(1 - F): 1 for CSR, < 1 clustered, > 1 regular |
| Clark–Evans | R = \bar d/(0.5/\sqrt\rho) |
| Border rule | estimate at r only from points (locations) at least r from the border |
By hand
B1. Six points in a 10 × 10 µm window: A = (2, 2), B = (3, 2), C = (6, 3), D = (6, 6), E = (2, 7), F = (9, 8). (a) Find each point’s nearest neighbour and its distance. (b) Compute \hat G(r) for r = 1, 2, 3, 4 (no correction). (c) The intensity is ρ = 0.06 µm⁻². Compute the CSR value 1 - e^{-\rho\pi r^2} at r = 1, 2, 3 and compare. Compute Clark–Evans R.
B2. Cells with ρ = 0.01 µm⁻². (a) What is G(5\,\mu m) under CSR? (b) An observed \hat G(5) = 0.80. Which way does the pattern depart from CSR at this distance? (c) What is the mean nearest-neighbour distance under CSR?
B3. Five points P_1 = (1, 1), P_2 = (4, 2), P_3 = (8, 2), P_4 = (5, 6), P_5 = (2, 8) and eight test locations (2, 2), (6, 2), (9, 5), (5, 4), (3, 5), (7, 8), (1, 5), (9, 9) in a 10 × 10 window. (a) For each test location, the distance to the nearest point. (b) \hat F(r) for r = 1, 2, 3, and the CSR values (ρ = 0.05).
B4. For the six points of B1, which ones does the border rule keep at r = 1, 2, 3? Compute the border-corrected \hat G(r) at these radii and compare with B1(b).
B5. Predict the sign of the bias of an uncorrected \hat G for a pattern in a 20 × 200 µm strip of tissue, and explain why it is much larger than in a 200 × 200 µm square. What share of the strip’s area lies within 5 µm of its border? Of the square’s?
B6. A pattern has J(r) = 0.3 at r = 4 µm. A second pattern has J(4) = 1.2. Describe each in words.
B1. (a) A–B 1.00, B–A 1.00, C–D 3.00, D–C 3.00, E–D 4.12, F–D 3.61. (b) \hat G(1) = 2/6 = 0.33, \hat G(2) = 0.33, \hat G(3) = 4/6 = 0.67, \hat G(4) = 5/6 = 0.83. (c) CSR: 0.17, 0.53, 0.82 at r = 1, 2, 3. At 1 µm Ĝ is above CSR (the close pair A–B), at 2 and 3 µm below (the other points are spread out). Mean NN distance 2.62, expected 0.5/\sqrt{0.06} = 2.04: R = 1.28, on the regular side, but with 6 points nothing is conclusive (and these are uncorrected).
B2. (a) 1 - e^{-0.01 \cdot \pi \cdot 25} = 1 - e^{-0.785} = 0.54. (b) 0.80 > 0.54: more points have a close neighbour than chance gives: clustered at this distance. (c) 0.5/\sqrt{0.01} = 5 µm.
B3. (a) 1.41 (to P_1), 2.00 (P_2), 3.16 (P_3), 2.00 (P_4), 2.24 (P_4), 2.83 (P_4), 3.16 (P_5), 5.00 (P_4). (b) \hat F(1) = 0, \hat F(2) = 3/8 = 0.375, \hat F(3) = 5/8 = 0.625. CSR: 0.15, 0.47, 0.76. F lies a little below CSR: slightly larger gaps than chance, but 8 locations and 5 points make this a toy.
B4. Border distances: A 2, B 2, C 3, D 4, E 2, F 1. At r = 1 all six are kept: Ĝ = 2/6 = 0.33. At r = 2: A, B, C, D, E (F is 1 from the border): Ĝ = 2/5 = 0.40. At r = 3: only C and D: Ĝ = 2/2 = 1. The estimate jumps around because so few points remain: unbiased but noisy, and not monotone.
B5. Biased down (distances too long, the pattern looks regular). In the strip almost every point is close to a long edge: within 5 µm of the border lies 1 - (10 \cdot 190)/4000 = 52.5 % of the strip’s area, against 1 - 190^2/200^2 = 9.75 % of the square’s. At r = 10 µm the whole strip is within r of its border, so the border rule keeps no point at all.
B6. J = 0.3 < 1: points have close neighbours much more often than empty locations have a close point: clustered at 4 µm. J = 1.2 > 1: slightly regular at 4 µm (points keep their distance).
Math questions
B7. (math) Derive G(r) = 1 - e^{-\rho\pi r^2} for a Poisson process (state what Slivnyak’s theorem gives you), the density of the nearest-neighbour distance and its mean.
B8. (math) Show that F(r) = G(r) for a Poisson process and hence J \equiv 1. Why is J > 1 for r < h in a hard-core process?
B9. (math) A point lies at distance b from a straight border; its disc has radius r > b. Show that the area of the disc outside the border is r^2\arccos(b/r) - b\sqrt{r^2 - b^2}. Evaluate for r = 10, b = 5 and give the share of the disc that is lost.
B7. Slivnyak: conditioning a Poisson process on a point at \mathbf x leaves the other points a Poisson process of the same intensity. So P(D > r) = P(\text{no other point in } b(\mathbf x, r)) = e^{-\rho\pi r^2} and G(r) = 1 - e^{-\rho\pi r^2}. Density \frac{d}{dr}G = 2\pi\rho r\,e^{-\rho\pi r^2}. Mean \int_0^\infty e^{-\rho\pi r^2}dr = \frac12\sqrt{\pi/(\rho\pi)} = 1/(2\sqrt\rho).
B8. From a fixed location \mathbf u the count in b(\mathbf u, r) is Poisson(\rho\pi r^2) by definition, so 1 - F(r) = e^{-\rho\pi r^2} = 1 - G(r) and J = 1. In a hard-core process no point has a neighbour closer than h, so G(r) = 0 for r < h and J(r) = 1/(1 - F(r)) > 1 as soon as F(r) > 0.
B9. The cut-off part is a circular segment: the sector of angle 2\theta with \cos\theta = b/r minus the triangle with base 2\sqrt{r^2 - b^2} and height b: r^2\theta - b\sqrt{r^2 - b^2}. For r = 10, b = 5: θ = 60° = 1.047 rad, area 100 \cdot 1.047 - 5 \cdot 8.66 = 61.4 µm², which is 19.6 % of the disc (314.2 µm²).
Explore
The pattern comes from the same models and seeds as the first unit’s demo. Tab G: each point with a line to its nearest neighbour (orange when shorter than r). Tab F: a grid of test locations over the distance map, linked to their nearest point. Tab J. Choose the correction (none or border, with the guard zone shaded) and the window (square, 60 µm crop, a thin tissue band, a tissue mask). Click a point to see its distances. Average of 100 CSR patterns plots the mean curves of random patterns with the same number of points in the same window.
E1. Preset CSR: G = F = theory. Read Ĝ, F̂ and Ĵ at r = 6 µm and the CSR value. Move r: do Ĝ and F̂ stay close to the CSR curve?
E2. Preset clusters: G up, F down. Read Ĝ(6), F̂(6), Ĵ(6) and Clark–Evans R. Switch between the G and F tabs and explain the two pictures.
E3. Preset regular: the opposite. Up to which r is Ĝ zero, and why? What is Ĵ(6)?
E4. Preset tight pairs. Read Ĝ just below and just above 3 µm. Now raise the pair distance to 8 µm. What happens to Ĝ, and what would you need to tell whether the pairs themselves are random?
E5. Preset small window: no correction with the average of 100 CSR patterns ticked. Compare the mean uncorrected curve with the theory at r = 10. Then border correction: how many of the 14 points are kept at r = 10? Is the mean border-corrected curve closer?
E6. Preset thin tissue band. What share of the points is within r = 8 µm of the border? Compare the mean uncorrected Ĝ(8) with the theory. Tick border: what happens at r = 15?
E1. 150 points, ρ = 3.75 per 1000 µm². At 6 µm: CSR 0.346, Ĝ 0.375, F̂ 0.345, Ĵ 0.95. Both curves follow the CSR curve within a few hundredths over the whole range; Clark–Evans R = 1.06.
E2. 229 points: Ĝ(6) = 0.940 and F̂(6) = 0.234 against CSR 0.477; Ĵ = 0.08; R = 0.40. G: almost every point has a neighbour in its own cluster within a few µm. F: most test locations lie in the empty space between clusters, far from any point.
E3. Ĝ = 0 up to the hard core of 10 µm: no two points are closer. F̂(6) = 0.410 > CSR 0.346 (no large gaps), Ĝ(6) = 0, so Ĵ(6) = 1/(1 − 0.41) = 1.70.
E4. 149 points: Ĝ is near 0 below 3 µm and jumps to almost 1 just above it (pairs whose partner fell outside the window are the exceptions). With 8 µm pairs the jump moves to 8 µm. G cannot say anything about distances beyond the pair distance; whether the pairs are random needs a function that counts all neighbours (K, next unit).
E5. 14 points in 60 × 60 µm. Mean uncorrected Ĝ(10) = 0.61 against the theory 0.71: biased low, the pattern looks regular. Border: only 4 of the 14 points are at least 10 µm from the border; the mean border-corrected Ĝ(10) = 0.67, much closer (and still slightly low because so few points remain).
E6. 57 points in a band of 5730 µm²: 13 are at least 8 µm from the border, so 77 % are within 8 µm. Mean uncorrected Ĝ(8) = 0.81 against 0.87. With the border rule at r = 15 µm (the band is 30 µm wide) no point is kept: the estimate is undefined. In thin tissue the border rule fails; pair-based corrections (next unit) still work.
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/nn_functions/exercise.rs. Points are pairs (x, y) in µm, the window is [0, a] \times [0, b]. Pattern generators, the distance, the distance to the border and a grid of test locations are given.
C1. nn_distances(points): for every point, the distance to the nearest other point (try all of them). The check is B1(a).
C2. g_hat(points, a, b, r, border): \hat G(r), with or without the border rule:
kept = 0, hits = 0
for every point i:
if border and (distance from i to the border of W) < r: skip i
kept = kept + 1
if d_i ≤ r: hits = hits + 1 # d_i from C1
G = hits / kept # undefined if kept = 0
The checks are B1(b) and B4.
C3. f_hat(points, tests, a, b, r, border): \hat F(r), the same with the test locations in place of the points, and the distance from each test location to its nearest point. The check is B3(b).
C4. j_hat(points, a, b, r, step): \hat J = (1 - \hat G)/(1 - \hat F), both with the border rule, F from test locations on a grid with spacing step. The check: J > 1 for a hard core, J < 1 for clusters, J \approx 1 for CSR.
Then cargo run -p spatialstats -- nn-functions. It prints \hat G, \hat F, \hat J and the CSR curve at r = 2 to 10 µm for four patterns in 200 \times 200 µm (CSR, clusters, a hard core of 10 µm, pairs 3 µm apart), with the Clark–Evans ratio. Then it averages \hat G(10) over 1000 random patterns of 14 points in a 60 \times 60 µm window, without and with the border rule.
C5. For each pattern, how do \hat G and \hat F lie relative to the CSR column, and what does \hat J do? What can G say about the pairs beyond 3 µm?
C6. Compare the two means of the small window with the theory and with E5. Which way is the uncorrected estimate biased, and why?
C7. Count the cost: C1 compares every point with every other point. How many distances is that for 150 points, and for 15 000? And for F with a 2 µm grid in 200 \times 200 µm?
The model solution is spatialstats/src/nn_functions/solution.rs in the download.
C5. CSR (150 points, Clark–Evans 0.95): \hat G and \hat F stay within a few hundredths of the CSR column up to 8 µm (at 10 µm: 0.736 and 0.679 against 0.692), and \hat J lies between 0.82 and 1.02. Clusters (223 points, R = 0.54): \hat G far above (0.643 at 4 µm against 0.244), \hat F below (0.178), \hat J falls from 0.73 to 0.02. Hard core (R = 1.53): \hat G = 0 up to 10 µm, \hat F above the CSR column (0.410 at 6 µm against 0.346), \hat J rises from 1.05 to 6.2. Pairs (R = 0.36): \hat G jumps from 0.027 at 2 µm to 1 at 4 µm and stays there; from then on it cannot say anything, while \hat F is a little below CSR and \hat J is 0. Whether the pairs themselves are random needs all neighbours (the next unit).
C6. Theory 0.705; no correction 0.629, border rule 0.668, as in the demo (0.71, 0.61, 0.67). Without a correction a point near the border misses its neighbours outside the window: its nearest-neighbour distance is too long, and \hat G is too low (the pattern looks regular). The border rule removes that bias but keeps few points; it is still a little low here.
C7. 150 \cdot 149 = 22\,350 distances; for 15 000 points about 2.25 \cdot 10^8, ten thousand times more for a hundred times more points. F with a 2 µm grid: 100 \cdot 100 = 10\,000 test locations times 150 points, 1.5 million distances per radius. Real software sorts the points into cells first (or uses one distance transform for F), so that only nearby points are compared.
Where this leads
G sees only the nearest neighbour. The next unit counts all neighbours within r (Ripley’s K), and its derivative, the pair correlation, which tells at which distances there are more or fewer pairs than chance.