Exercise: point patterns and complete spatial randomness

Goal

By the end of this exercise you should be able to:

  • describe a set of detected objects as a point pattern (positions, window, marks) and predict how detection errors change it,
  • compute quadrat counts, the variance-to-mean ratio and the χ² statistic by hand, and read them,
  • use the Poisson model of complete spatial randomness for expected counts and empty regions,
  • explain why the verdict depends on the quadrat size,
  • (math) show conditional uniformity of the Poisson process and compute the moments of a cluster process.

Do the pen-and-paper part first, then the demo.

Background

Idea What it means
Point pattern positions \mathbf x_i + window W + optional marks
Intensity ρ expected points per µm²; estimate \hat\rho = n/|W|
CSR (homogeneous Poisson) N(A) \sim \text{Poisson}(\rho|A|), independent for disjoint regions; given n: uniform positions
Empty region P(N(A) = 0) = e^{-\rho|A|}
VMR s^2/\bar n of quadrat counts: > 1 clustered, ≈ 1 random, < 1 regular
Quadrat test X^2 = (m - 1)\,\text{VMR} \approx \chi^2_{m-1}; upper tail: clustered, lower tail: regular
χ² critical values upper 5 %: 25.0 for 15 df, 36.4 for 24 df

By hand

B1. A pattern of 20 cells in a square window is divided into 4 × 4 quadrats. The counts are

c1 c2 c3 c4
r1 0 3 1 0
r2 2 0 0 4
r3 1 0 5 0
r4 0 2 0 2
  1. Compute the mean, the variance (divide by m - 1) and the VMR. What does the VMR suggest?
  2. How many empty quadrats are there? How many would you expect under CSR with this mean?
  3. Another pattern of 20 cells has 4 quadrats with 2 cells and 12 with 1. Compute its VMR. Which archetype is it?

B2. Cells with intensity ρ = 0.02 µm⁻² (CSR) in a 50 × 50 µm window. (a) How many cells do you expect in the window? (b) What is the probability that a given 5 × 5 µm square is empty? A 10 × 10 µm square? (c) A colleague finds an empty 10 × 10 µm square and concludes the cells avoid that spot. Comment.

B3. For each situation, predict whether quadrat counts (of a size of a few cell diameters) give VMR > 1 or < 1, and why: (a) dividing cells whose daughters stay together; (b) cells that keep a minimum distance from each other; (c) cells that are denser near a blood vessel, without interacting; (d) fluorescent spots detected in a widefield image where close spots merge.

B4. A cluster pattern has tight clusters of about 8 µm diameter, 10 cells each, scattered at random. Predict the VMR for quadrats of (a) 1 µm, (b) 30 µm, (c) 150 µm (in a large window). Sketch VMR against quadrat size.

B5. Why can a pattern of cells be regular at one scale and clustered at another? Give a biological example, and say what one VMR value would report.

B1. (a) Sum 20, m = 16, mean \bar n = 1.25. Squared deviations sum to 39, so the variance is 39/15 = 2.6 and VMR = 2.6/1.25 = 2.08: more variable than Poisson, suggesting clustering at this scale. (b) 8 empty quadrats; under CSR 16 e^{-1.25} = 4.6. Too many empty quadrats is the same message. (c) Mean 1.25, deviations: 4 × 0.75² + 12 × 0.25² = 3.0, variance 3.0/15 = 0.2, VMR = 0.16: much more even than chance, regular.

B2. (a) \rho|W| = 0.02 \cdot 2500 = 50 cells. (b) e^{-0.02 \cdot 25} = e^{-0.5} = 0.61; e^{-0.02 \cdot 100} = e^{-2} = 0.14. (c) With 25 non-overlapping 10 × 10 squares in the window, about 25 \cdot 0.14 \approx 3.4 empty ones are expected by chance. One empty square is not evidence of avoidance; the question needs a test that looks at the whole pattern.

B3. (a) VMR > 1: daughters stay together, so cells come in groups. (b) VMR < 1: counts are more even than Poisson. (c) VMR > 1 for quadrats large enough to feel the gradient: dense quadrats near the vessel, sparse ones far away (the first-order effect). (d) VMR < 1: merging removes close pairs and makes the pattern more even.

B4. (a) 1 µm: almost every quadrat holds 0 or 1 cell, so VMR ≈ 1. (b) 30 µm: quadrats hold whole clusters or none: VMR far above 1, approaching 1 + 10 = 11 for quadrats much larger than a cluster (math question B7). (c) 150 µm: still about 1 + μ = 11 for a Thomas-type process with random parents (the counts are sums of whole clusters). The curve rises from 1 and levels off; it would come back down only if the clusters themselves were spread regularly.

B5. Cells have a size, so neighbouring cells cannot be closer than about one diameter (regular at the cell scale), but they can still form colonies (clustered at the colony scale); for example epithelial islands or tumour nests. One VMR reports one scale only: with small quadrats “regular”, with colony-sized quadrats “clustered”.

Math questions

B6. (math) Let W = A \cup B with A, B disjoint, and N(A), N(B) independent Poisson with means \rho|A|, \rho|B|. Show that P(N(A) = k \mid N(W) = n) = \binom nk p^k (1 - p)^{n - k} with p = |A|/|W|. Why does this justify simulating CSR with n uniform points?

B7. (math) For B1(a), compute X^2 and decide at 5 % (15 df, critical value 25.0). Then show that for a Thomas process (parent intensity κ, Poisson(μ) offspring per parent), the count in a quadrat Q much larger than the clusters has \text{VMR} \approx 1 + \mu. With κ = 0.0005 µm⁻² and μ = 8: the intensity, the expected count in a 25 × 25 µm quadrat and the large-quadrat VMR.

B8. (math) Write X^2 = \sum_k (n_k - \bar n)^2/\bar n in terms of the VMR. Why does the lower tail of \chi^2_{m-1} test for regularity?

B6. P(N(A) = k, N(W) = n) = P(N(A) = k)\,P(N(B) = n - k) = e^{-\rho|A|}\frac{(\rho|A|)^k}{k!}\,e^{-\rho|B|}\frac{(\rho|B|)^{n-k}}{(n-k)!}, and P(N(W) = n) = e^{-\rho|W|}\frac{(\rho|W|)^n}{n!}. Dividing, the exponentials and ρ cancel: \binom nk \left(\frac{|A|}{|W|}\right)^k\left(\frac{|B|}{|W|}\right)^{n-k}. Each of the n points falls into A independently with probability |A|/|W|, for every A: exactly what n independent uniform points do.

B7. X^2 = 39/1.25 = 31.2 > 25.0: reject CSR towards clustering (p = 0.008). Thomas: with J parents in Q, J \sim \text{Poisson}(\kappa|Q|), and N(Q) = \sum_{j=1}^J M_j when whole clusters fall in Q. Then E[N] = E[J]\,\mu = \kappa|Q|\mu and, by the law of total variance, \text{Var}[N] = E[J]\,\text{Var}[M] + \text{Var}[J]\,\mu^2 = \kappa|Q|(\mu + \mu^2), so VMR = 1 + μ. Numbers: ρ = κμ = 0.004 µm⁻², expected count 0.004 \cdot 625 = 2.5, VMR → 9.

B8. X^2 = (m - 1)\,s^2/\bar n = (m - 1)\,\text{VMR}. A regular pattern has counts that are more even than Poisson, so s^2 and with it X^2 is unusually small: the lower tail. (B1(c): X^2 = 15 \cdot 0.16 = 2.4, P(\chi^2_{15} \le 2.4) = 0.0001.)

Explore

Seeded patterns in a 200 × 200 µm window from the models of the lecture (choose a model, its sliders, a detection artefact and a seed). Tab quadrats: the grid with counts, the histogram of counts against the Poisson distribution of the same mean, the χ² test and the VMR against the quadrat side. Tab intensity: the kernel estimate with a bandwidth slider. Tab which is random?: three patterns, guess the random one. Tab from an image: the points rendered as spots and detected again.

E1. Preset CSR: variance = mean. Read the mean, the variance, VMR and both p-values. Press new seed five times: how much does the VMR wander for a truly random pattern?

E2. Presets clusters: VMR > 1 and regular: VMR < 1. Compare the histograms of counts with the Poisson dots: where do the observed counts differ?

E3. Preset quadrat size changes the verdict (colonies with a hard core). Read the VMR at 40 quadrats per side (5 µm), then move to 10 per side (20 µm). Explain both results. Look at the VMR-against-size plot.

E4. Presets merged spots look regular and split nuclei look clustered. The true positions are random in both. What do the quadrats say? Then open the tab from an image and move the PSF σ from 1 to 4 µm: how do the number of detections and the Clark–Evans R change?

E5. Preset gradient: clustered by quadrats. Read the verdict with 4 quadrats per side, then with 20. Then preset intensity bandwidth: which bandwidth shows the gradient best?

E6. Tab which is random? Make a guess on five trios (new trio). How often were you right? Which pattern do you tend to mistake for the random one?

E1. 150 points in 64 quadrats of 25 µm: mean 2.34, variance 2.32, VMR 0.99, p clustered 0.49, p regular 0.51. Over new seeds the VMR typically stays between about 0.7 and 1.4 (its standard deviation under CSR is about \sqrt{2/63} = 0.18).

E2. Clusters (229 points, 25 µm quadrats): VMR 6.51; far too many empty quadrats and a long tail of quadrats with 10–20 points. Regular (hard core 12 µm): VMR 0.25; almost all quadrats hold 1–3 points, the histogram is much narrower than the Poisson dots.

E3. 5 µm quadrats: VMR 0.90, p regular 0.002: at the scale of single cells the hard core (6 µm) makes the pattern regular. 20 µm quadrats: VMR 2.97, p clustered < 0.001: whole colonies fall into some quadrats. The plot rises from below 1 to above 12 at 100 µm. Both verdicts are right, at their scale.

E4. Merged (spots within 8 µm merged): 110 points instead of 150, VMR 0.65, p regular 0.003. Split (30 %): 198 points, VMR 1.29, p clustered 0.03. From an image: at PSF σ = 1.5 µm 131 of 150 spots are detected (Clark–Evans R 1.20); at 3 µm 107 (R 1.34, VMR 0.53). The wider the PSF, the more close pairs merge and the more regular the detected pattern looks.

E5. 4 × 4 quadrats of 50 µm: VMR 2.97, p clustered < 0.001. 20 × 20 quadrats of 10 µm: VMR 0.95, not significant: small quadrats do not feel a gradient that changes over 200 µm. Intensity: at σ ≈ 15–30 µm the profile follows the true ramp; at 3–5 µm it is a bump per point; at 60 µm it flattens.

E6. Most people pick the regular pattern as “random”: it has no clumps and no gaps. A truly random pattern has both by chance.

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/point_patterns/exercise.rs. A point is a pair (x, y) in µm, the window the rectangle [0, a] \times [0, b]. Uniform points (csr), Poisson numbers (poisson), a hard-core pattern, the normal distribution function Φ and a picture of a pattern are given.

C1. quadrat_counts(points, a, b, k): the counts in k \times k equal quadrats:

counts = k × k zeros
for every point (x, y):
    column = floor(x / a · k),  row = floor(y / b · k)      # at most k − 1
    counts[row][column] = counts[row][column] + 1

C2. dispersion(counts): the mean \bar n, the variance s^2 (divided by m - 1), the \text{VMR} = s^2/\bar n and X^2 = \sum_k (n_k - \bar n)^2/\bar n. The checks for C1 and C2 are your answers to B1 and B7, and X^2 of B1(c) (B8).

C3. thomas(kappa, mu, sigma, a, b, rng): a cluster pattern, as on the slide “The math: the cluster process”:

enlarge the window by 4σ on every side
number of parents = Poisson(κ · area of the enlarged window)
for every parent, uniform in the enlarged window:
    for each of Poisson(μ) offspring:
        offspring = parent + σ · (normal, normal)
        keep it if it lies inside the window

The check is B7: with \kappa = 0.0005, \mu = 8, \sigma = 4 µm the intensity is about 0.004 µm⁻², and large quadrats have VMR near 1 + \mu = 9.

C4. intensity_map(points, a, b, sigma): the kernel estimate on a grid of 1 µm pixels, with the edge correction:

for every pixel centre (x, y):
    s = sum over the points of  exp(−d² / 2σ²) / (2πσ²)        # d = distance to the point
    e = [Φ((a − x)/σ) − Φ(−x/σ)] · [Φ((b − y)/σ) − Φ(−y/σ)]    # share of a kernel inside W
    ρ(x, y) = s / e

The check: points on a 5 µm grid give 0.04 µm⁻² everywhere, also in the corners, where a map without e would give a quarter of that.

Then cargo run -p spatialstats -- point-patterns. It simulates five patterns in a 200 \times 200 µm window: CSR, clusters (Thomas), a hard core of 12 µm, a gradient (more points to the right), and the CSR pattern with points closer than 8 µm merged (as close spots merge in a detection). It prints the VMR for quadrats of 5 to 50 µm, the χ² statistic for 8 \times 8 quadrats, and the intensity of the gradient at three bandwidths. Pictures: out/point-patterns/patterns.png and intensity.png.

C5. Read the VMR table. Which patterns are clustered and which regular, and at which quadrat sizes? Why does the CSR pattern get 0.47 with 50 µm quadrats?

C6. The χ² distribution with 63 degrees of freedom has its upper 5 % point at 82.5 and its lower 5 % point at 45.7. What do the three X^2 values say?

C7. Compare the three intensity maps and the printed column means with the true intensity. Which bandwidth would you choose?

The model solution is spatialstats/src/point_patterns/solution.rs in the download.

C5. CSR (150 points): VMR 1.11, 1.15, 1.02, 0.92 for quadrats of 5 to 25 µm. Clusters (151 points): 1.37 at 5 µm, rising to 3.73 at 20 µm and 5.99 at 50 µm, as quadrats begin to hold whole clusters. Hard core: 0.91 at 5 µm (most quadrats hold 0 or 1 point, so the core hardly shows), down to 0.15 at 50 µm. The gradient (135 points) looks random in small quadrats (1.01, 1.07) and clustered in large ones (4.31 at 50 µm). The merged pattern (110 of 150 points left) is regular at every size (0.93 to 0.37): detection made a random pattern regular. With 50 µm quadrats there are only 16 counts, so the VMR of a random pattern scatters a lot; 0.47 is a chance value, not regularity.

C6. CSR: X^2 = 58.2, between 45.7 and 82.5: no evidence against CSR. Clusters: 256.3, far above 82.5: clustered. Hard core: 16.4, far below 45.7: regular (the lower tail, B8).

C7. The true intensity at x = 10, 100, 190 µm is 0.0004, 0.0037, 0.0071 µm⁻². With \sigma = 3 µm the map is a bump per point (column means 0.0000, 0.0039, 0.0063, but very uneven along each column). With \sigma = 15 µm it follows the ramp (0.0002, 0.0035, 0.0069) with some blotches. With \sigma = 60 µm it flattens the ramp (0.0015, 0.0034, 0.0053). A bandwidth of about 15 µm, small compared with the 200 µm over which the density changes, but large compared with the spacing of the points.

Where this leads

Quadrats answer “clustered or not” at one scale. The next unit looks at distances instead: from each point to its nearest neighbour (G), from empty locations to the nearest point (F), and what the border of the window does to them.