Exercise: simulation envelopes and global tests

Goal

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

  • compute a Monte Carlo p-value from simulated values,
  • explain why a pointwise envelope read over many r is a multiple test, and run a MAD test by hand,
  • design the comparison of conditions with images and experiments,
  • (math) show that Monte Carlo ranks are uniform, bound the global error of many pointwise tests, and justify the n^2 weights of a pooled K.

Background

Idea What it means
Monte Carlo p-value (1 + \#\{T_k \text{ at least as extreme as } T_0\})/(1 + s)
Pointwise envelope min and max of s simulated curves at each r; level 2/(s+1) per r
MAD statistic T = \max_{r}|\hat L(r) - r| over a range fixed in advance
Global envelope 0 \pm T_\text{crit}, T_\text{crit} the \lfloor 0.05(s+1)\rfloor-th largest simulated T
Conditions one summary per image, mean per experiment, test across experiments

By hand

B1. 19 CSR simulations give these values of L(5) - 5 (µm):

−0.9, 0.3, −0.2, 1.1, 0.6, −0.5, 0.0, 0.8, −1.3, 0.4, −0.1, 0.9, 1.4, −0.7, 0.2, 0.5, −0.4, 1.0, 0.1

  1. The observed value is 1.2. Give its rank among the 20 values and the one-sided p-value (clustering), and the two-sided p-value.
  2. What would the one-sided p-value be for an observed 1.6? Why can it not be smaller with 19 simulations?

B2. (a) What is the level of a two-sided min/max envelope at one r with 39 simulations? With 19? With 99? (b) A curve lies outside a 39-simulation envelope at one of 25 radii. Why is this not p = 0.05? (c) If the 25 tests were independent, how often would a random pattern leave the band somewhere? Why is the real figure (37 %) lower?

B3. Ten simulated curves of L - r (µm) at five radii, and the observed curve:

sim r₁ r₂ r₃ r₄ r₅
1 0.4 −0.3 0.6 0.9 −0.8
2 −0.5 0.2 −0.4 0.3 1.1
3 0.1 0.7 0.2 −0.6 −0.4
4 0.8 0.1 −0.9 0.5 0.6
5 −0.2 −0.6 0.3 1.2 0.2
6 0.3 0.4 0.1 −0.3 −1.0
7 −0.7 0.2 0.5 0.4 0.3
8 0.2 −0.9 0.8 −0.2 0.7
9 0.6 0.3 −0.2 0.1 −0.5
10 −0.1 0.5 0.4 −0.8 0.9
obs 0.5 1.0 1.3 0.9 0.6
  1. At which radii does the observed curve leave the pointwise min/max envelope?
  2. Compute the MAD statistic T of each curve and of the observed one. Rank and p-value?
  3. What is the smallest p-value possible with 10 simulations, and what do you conclude?

B4. Design: three independent experiments, two conditions (control, treated), five images per condition and experiment, about 200 cells per image. You want to know whether the treatment makes cells cluster at about 10 µm. (a) What is n? (b) What do you compute per image? (c) Which test? (d) A colleague pools all 15 treated images into one pattern of 3000 cells and runs a MAD test against CSR. What does a tiny p-value tell you about the treatment?

B5. Predict how the rate of false alarms of a pointwise envelope changes when the r range is doubled (from 0–25 to 0–50 µm, same step), and how the MAD test’s rate changes.

B1. (a) Only 1.4 is larger, so the observed is 2nd from the top among 20: one-sided p = (1 + 1)/20 = 0.10. Lower side: 18 simulations are ≤ 1.2, p = 19/20; two-sided p = 2 · 0.10 = 0.20. Not significant. (b) Larger than all 19: p = 1/20 = 0.05, the smallest possible with 19 simulations.

B2. (a) 2/40 = 0.05; 2/20 = 0.10; 2/100 = 0.02. (b) Each of the 25 radii is a chance to stray; one escape among 25 is expected fairly often under CSR. (c) Independent: 1 - 0.95^{25} = 0.72. Neighbouring radii share most pairs (K is cumulative), so the tests are strongly correlated and the curve tends to stay in or out over a stretch of r: 37 %.

B3. (a) Envelope maxima per radius: 0.8, 0.7, 0.8, 1.2, 1.1 (minima −0.7, −0.9, −0.9, −0.8, −1.0). The observed 1.0 > 0.7 at r₂ and 1.3 > 0.8 at r₃: it leaves the band at two radii. (b) T of the simulations: 0.9, 1.1, 0.7, 0.9, 1.2, 1.0, 0.7, 0.9, 0.6, 0.9; observed T = 1.3, the largest of the 11: rank 1 from the top, p = 1/11 = 0.09. (c) 1/11 = 0.09. Not significant at 5 % although the curve “escapes” twice: with so few simulations the test cannot reach 5 %; use at least 19 (better 39 or 99).

B4. (a) n = 3 (experiments) per condition. (b) One summary per image, e.g. L(10) - 10 with an edge correction (or the area under L - r over a pre-chosen range), then the mean of the five images per experiment and condition. (c) A paired t-test (or its non-parametric equivalent) on the 3 differences treated − control; with 3 pairs, report the effect and its confidence interval, not only p. (d) Only that the pooled treated pattern is not CSR. It says nothing about the treatment (control may be just as clustered), and it treats 3000 cells as independent when the experiment has n = 3; pooling 15 images into one window also invents neighbours across image borders.

B5. Pointwise: more radii, more chances to stray: the rate rises (in the lecture’s simulations from about 37 % to 46 % for 25 → 50 radii). MAD: stays at about 5 %; only its power changes.

Math questions

B6. (math) Let T_0, T_1, \dots, T_s be exchangeable and almost surely distinct. Show that P(T_0 \text{ is among the } k \text{ largest}) = k/(s+1). Why does this make the MAD test exact even though the distribution of T is unknown?

B7. (math) Let A_1, \dots, A_m be the events “the curve leaves the band at radius r_j”, each with probability α. Show that \alpha \le P(\cup_j A_j) \le \min(1, m\alpha), and that P(\cup A_j) = 1 - (1 - \alpha)^m for independent events. Where does the real value (37 % at α = 0.05, m = 25) lie?

B8. (math) Images k = 1, \dots, m with n_k points and pair counts S_k(r) = \sum_{i \ne j} w_{ij}[d_{ij} \le r], so that \hat K_k = |W| S_k/(n_k(n_k - 1)). Show that the ratio estimator |W|\sum_k S_k / \sum_k n_k(n_k - 1) is a weighted mean of the \hat K_k with weights n_k(n_k - 1) \approx n_k^2. What weight share does an image of 200 points get next to one of 50?

B6. Exchangeability means every permutation of (T_0, \dots, T_s) has the same distribution, so each of the s + 1 values is equally likely to hold any given rank: P(\text{rank of } T_0 = j) = 1/(s+1) for each j, and summing over the top k ranks gives k/(s+1). The argument uses only the symmetry between observed and simulated values under H_0, not the distribution of T, so the level is exact for any statistic, including the maximum over a range of r.

B7. P(\cup A_j) \ge P(A_1) = \alpha; the union bound gives \le \sum P(A_j) = m\alpha. Independent: P(\text{none}) = (1 - \alpha)^m. With α = 0.05, m = 25: between 0.05 and 1 - 0.95^{25} = 0.72 (the union bound gives 1.25, i.e. no information). 37 % lies in between: the events are positively dependent.

B8. \frac{|W|\sum_k S_k}{\sum_k n_k(n_k-1)} = \sum_k \frac{n_k(n_k-1)}{\sum_l n_l(n_l-1)}\cdot\frac{|W| S_k}{n_k(n_k-1)} = \sum_k w_k\hat K_k with w_k \propto n_k(n_k - 1) \approx n_k^2. 200 against 50 points: 200^2/(200^2 + 50^2) = 0.94 of the weight.

Explore

Tab envelopes: the observed L - r (or g) of a pattern from the shared source, the pointwise envelope of s CSR simulations (same n, same window), the global (MAD) envelope, and the r range (two sliders). Tab how often is CSR rejected? runs 200 random patterns live. Tab conditions: two conditions × 4 experiments × images, one L(10) - 10 per image, with the t-test on images as n and on experiments as n.

E1. Preset CSR: inside the band. Read the MAD statistic, its rank and p. Press new seed ten times and count how often the curve leaves the pointwise band somewhere, and how often the MAD p is ≤ 0.05.

E2. Preset pointwise trap. At which radii does the random pattern escape? What does the MAD test say? Tick the global envelope.

E3. Preset 199 simulations. Why does the curve no longer leave the pointwise band? Has the global test changed?

E4. Preset weak clusters: escapes at small r. Read T, rank and p. Then preset choose the r range first (20–50 µm): what happens, and what is the lesson?

E5. Preset hard core: below the band. Where is the curve, and what is T?

E6. Tab how often is CSR rejected? Press run 200 CSR patterns. Compare the three dots with the precomputed lines at 25 radii.

E7. Preset images as n: tiny p. Read both p-values. Press new experiment ten times and count how often each is below 0.05. Then set the dish-to-dish sd to 0: what changes?

E1. Seed 4 (150 points): no escape, T = 1.14, rank 14 of 40, p = 0.68. Over new seeds (CSR, 50 radii, 39 simulations) about every second pattern leaves the pointwise band somewhere (15 of the seeds 1–30 do), while the MAD p is rarely ≤ 0.05 (about 1 in 20).

E2. It leaves the band at 6 of 50 radii (8–12 µm and 29 µm). MAD: T = 1.56 µm, rank 31 of 40, p = 0.25: no evidence against CSR. The global envelope (±2.61 µm) contains the whole curve.

E3. With 199 simulations the min/max band is a 1 % band per r (2/200), so it is wider: no escape. The global test is unchanged in level (5 %): T = 1.56, p = 0.26.

E4. 168 points: T = 5.68 µm, rank 40 of 40, p = 0.025 (the smallest possible with 39 simulations); the curve is above the pointwise band from 1 to 33 µm. With r restricted to 20–50 µm: T = 1.79, p = 0.15: the clustering lives at small r and the chosen range misses it. The range must come from the question (e.g. cell-to-cell distances), decided before looking; trying ranges until one works is not allowed either way.

E5. L − r = −r below the hard core of 7 µm (no neighbours at all), well below the band; T = 7.00 µm, p = 0.025.

E6. The live run (200 patterns, 25 radii) gives rates close to the precomputed lines: pointwise with 19 simulations about 60 %, with 39 about 37 %, the MAD test about 5 % (with 200 patterns each rate is uncertain by a few percent).

E7. Seed 1: images as n p = 0.008, experiments as n p = 0.18, although there is no treatment effect. Over 40 seeds the images-as-n test is “significant” about 30 % of the time, the experiments-as-n test about 10 % (4 vs 4 with a Welch test is rough). With dish-to-dish sd 0, images within a dish are independent replicates of the same condition, and both tests come close to 5 %.

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/envelopes/exercise.rs. The pattern generators and l_curve (the curve L(r) - r with the translation correction at a list of radii, from the previous unit) are given.

C1. mc_p_value(observed, simulated, larger): the one-sided Monte Carlo p-value,

p = (1 + number of simulated values ≥ observed) / (1 + number of simulations)
                                     # ≤ instead of ≥ for the other side

The check is B1.

C2. pointwise_envelope(curves): at every radius the smallest and the largest simulated value; escapes(observed, lower, upper): the radii where the observed curve lies outside. The check is B3(a).

C3. mad_test(observed, simulated):

T(curve) = largest |curve(r)| over the radii
T0 = T(observed),  T1 .. Ts = T(each simulated curve)
p = (1 + number of Tk ≥ T0) / (1 + s)

The check is B3(b).

C4. false_alarm_rates(n, a, b, radii, s, patterns, rng): how often is CSR rejected?

repeat for every one of the patterns:
    observed = L − r of n uniform points
    simulated = L − r of s further patterns of n uniform points
    pointwise rejects if the observed curve escapes the envelope at some r
    MAD rejects if its p ≤ 0.05
return the share of patterns each test rejected

The check: for random patterns the pointwise band rejects far more often than the MAD test.

Then cargo run -p spatialstats -- envelopes. For three patterns in 200 \times 200 µm (CSR, weak clusters, a hard core of 7 µm) it prints where L - r leaves the pointwise band of 39 simulations (r = 1 to 50 µm), the MAD test over 1–50 µm and over 20–50 µm, and the half-width of the global envelope. Then it repeats C4 for 200 random patterns of 100 points at 25 radii, with 19 and with 39 simulations (this takes a few seconds).

C5. The CSR pattern leaves the pointwise band. Is that evidence against CSR? What do the MAD test and the global envelope say?

C6. For the weak clusters and the hard core, compare the MAD test over 1–50 µm with the test over 20–50 µm. What is the lesson?

C7. Compare the rejection rates with the slide “How often is CSR rejected?”. Why are 4 % and 6 % both consistent with a test that rejects 5 % of random patterns?

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

C5. It leaves the band at 10–15 µm, but that is the pointwise trap: 50 radii are 50 chances to stray. The MAD test gives T = 1.99 µm, p = 0.10: no evidence against CSR. The global envelope is \pm 2.39 µm and contains the whole curve.

C6. Weak clusters (158 points): above the band at 2–13 µm; MAD over 1–50 µm: T = 2.70, p = 0.025, the smallest possible with 39 simulations. Over 20–50 µm only: T = 0.73, p = 0.80. The hard core: T = 7.00 (L - r = -r below 7 µm), p = 0.025; over 20–50 µm p = 0.15. The effects live at small r, and a range chosen without them misses them. The range must come from the biological question and be fixed before looking at the data.

C7. The program gives 58 % (pointwise, 19 simulations), 36 % (39 simulations) and 4 % and 6 % for the MAD test; the slide has 59 %, 37 % and 5 % from 1000 patterns. With 200 patterns a rate of 5 % is uncertain by about \sqrt{0.05 \cdot 0.95 / 200} = 1.5 percentage points, so 4 % and 6 % are within the chance variation.

Where this leads

So far every pattern had one kind of point and a constant intensity. The next unit adds marks (two cell types) and an uneven density, and with them two new null models: independence and random labelling.