Exercise: is it more than chance?
Before this unit: Colocalisation of two channels (previous unit); Day 46: Colour.
See also: Day 4: Image formation; Day 14: Labelling; Day 16: Measurements; Day 23: FFT registration; Day 47: Spectral unmixing.
Goal
By the end of this exercise you should be able to:
- read a shift curve: peak position, width, and the part that survives large shifts,
- compute a permutation p-value and say which block size makes a randomisation test honest,
- say what a significant test does not show (bleed-through, shared background, resolution),
- compute the PDM signs and the ICQ, and compare an ICQ with shuffled data,
- read a Costes trace and pick the threshold,
- (math) relate Pearson’s r to the PDM, derive the width of the shift curve and a sub-pixel peak.
Do the pen-and-paper part first, then the demo.
Background
| Idea | What it means |
|---|---|
| Shift curve | r of A against B shifted by \Delta x, for \Delta x = -20 \dots 20 px |
| Block randomisation | cut B into b \times b blocks inside the region, shuffle them, recompute r; repeat N times |
| Permutation p-value | p = (1 + \#\{r^* \ge r_\text{obs}\}) / (N + 1) |
| PDM | (A_i - \bar A)(B_i - \bar B) per pixel: + both up or both down, − one up, one down |
| ICQ | share of pixels with PDM > 0, minus 0.5 (between −0.5 and 0.5) |
| Costes trace | r of the pixels below the thresholds (A \le t_A or B \le t_B) against t_A; stop at the first r \le 0 |
By hand
B1. An image gives r_\text{obs} = 0.12. Nineteen block-shuffled copies of B give −0.08, 0.03, −0.11, 0.05, 0.14, −0.02, 0.07, −0.05, 0.01, −0.09, 0.10, −0.03, 0.04, −0.06, 0.02, −0.01, 0.08, −0.04, 0.00. (a) Compute the permutation p-value. (b) What is the smallest p-value 19 shuffles can give? (c) How many shuffles do you need to be able to reach p \le 0.01?
B2. The B1 channels of the last unit: A = \begin{bmatrix} 0 & 2 & 8 \\ 1 & 6 & 9 \\ 0 & 1 & 3 \end{bmatrix}, \qquad B = \begin{bmatrix} 1 & 1 & 6 \\ 0 & 5 & 8 \\ 1 & 2 & 0 \end{bmatrix}, \qquad \bar A = 3.33,\ \bar B = 2.67. (a) Write down the sign of (A - \bar A), of (B - \bar B) and of the PDM for every pixel. (b) Compute the ICQ. (c) Sum the PDM values (you need them, not just the signs) and check that the sum is \sum (A - \bar A)(B - \bar B) = 74 from the last unit. (d) The ICQ is at its maximum while r = 0.916. What does the ICQ ignore?
B3. A shift curve, sampled at \Delta x = -3 \dots 3 px:
| \Delta x | −3 | −2 | −1 | 0 | 1 | 2 | 3 |
|---|---|---|---|---|---|---|---|
| r | 0.30 | 0.52 | 0.78 | 0.90 | 0.84 | 0.62 | 0.38 |
At \Delta x = \pm 20 px the curve levels off at 0.25. (a) At which integer shift is the peak, and on which side of it is the true peak? (b) How much of the correlation comes from large-scale structure, and how much from small structures? (c) Estimate the half width of the peak above the 0.25 level.
B4. Spots blurred to an FWHM of 3 pixels, in a cell of 6000 pixels; the channels are unrelated. You run a randomisation test with 1-px blocks and one with 8-px blocks. Predict which null distribution is wider, roughly by how much, and which test gives honest p-values.
B5. Unrelated structures, but 30 % of dye A leaks into channel B. The demo gives r = 0.23, and none of 200 block shuffles reaches it. (a) What is the p-value? (b) Is the test wrong? (c) What would you do before testing?
B6. The last steps of a Costes search (perfect colocalisation, background offset 10, noise σ = 4):
| t_A | 32.4 | 30.1 | 27.7 | 25.4 | 23.1 | 20.8 | 18.5 | 16.2 | 13.9 |
|---|---|---|---|---|---|---|---|---|---|
| t_B | 32.4 | 30.1 | 27.7 | 25.4 | 23.1 | 20.8 | 18.5 | 16.1 | 13.8 |
| r below | 0.677 | 0.635 | 0.586 | 0.508 | 0.407 | 0.314 | 0.214 | 0.101 | −0.026 |
- Which thresholds does Costes’ method choose? (b) Where are they relative to the background (offset 10, noise σ = 4)? (c) Why does r below fall as t_A falls?
Math questions
B7. (math) Show that r = \sum_i \text{PDM}_i / (n\,s_A s_B), with s_A^2 = \frac1n\sum (A_i - \bar A)^2 and likewise s_B. Why can ICQ and r disagree in sign?
B8. (math) Show that with N shuffles the permutation p-value p = (1 + \#\{r^* \ge r_\text{obs}\})/(N + 1) satisfies P(p \le k/(N + 1)) = k/(N + 1) under the null when all N + 1 values are distinct and exchangeable. What is the smallest reachable p?
B9. (math) For two 2D Gaussian spots g_\sigma at distance \delta, show that \int g_\sigma(\mathbf x)\,g_\sigma(\mathbf x - \boldsymbol\delta)\,d\mathbf x \,\big/ \int g_\sigma^2 = e^{-\delta^2/4\sigma^2}, and that the half width at half height is 2\sigma\sqrt{\ln 2} \approx 0.71 FWHM.
B10. (math) Fit the parabola r(x) = a x^2 + b x + c through (-1, r_-), (0, r_0), (1, r_+) and show that its vertex is at x^* = (r_- - r_+)/\big(2(r_- - 2r_0 + r_+)\big). Apply it to B3.
B1. (a) Only 0.14 is ≥ 0.12: p = (1 + 1)/(19 + 1) = \mathbf{0.10}. (b) 1/20 = 0.05, when no shuffle reaches the observed value. (c) 1/(N + 1) \le 0.01 needs N \ge 99.
B2. (a) A - \bar A: − − + / − + + / − − −; B - \bar B: − − + / − + + / − − −. Same sign in every pixel, so every PDM is positive. (b) ICQ = 9/9 - 0.5 = \mathbf{0.5}, the maximum. (c) PDM values (row by row): 5.56, 2.22, 15.56, 6.22, 6.22, 30.22, 5.56, 1.56, 0.89; sum 74.0. (d) The ICQ counts only signs: it ignores how strongly the channels vary together (the 9 and 8 contribute 30.22, the 3 and 0 contribute 0.89, both count as one positive pixel).
B3. (a) At 0 (r = 0.90); its right neighbour (0.84) is larger than its left (0.78), so the true peak lies slightly to the right (B10: +0.17 px). (b) About 0.25 survives large shifts: large-scale structure (the cell outline, a shared background). The peak rises 0.65 above it: small structures that really overlap. (c) Half height above the level: 0.25 + 0.65/2 = 0.575. On the right the curve crosses 0.575 between 1 and 2 px (linear interpolation: at 2.2 px), on the left between −1 and −2 px (at −1.8 px): half width about 2.0 px (the peak is slightly off-centre, B10).
B4. The 1-px shuffle makes the pixels independent: sd about 1/\sqrt{6000} \approx 0.013. The 8-px blocks keep the spots intact: the null is roughly w = 3 times wider (sd about 0.04; n_\text{eff} \approx 6000/9). Only the block test is honest: with 1-px blocks, unrelated channels are declared “significant” far too often. (Demo, blur FWHM 4.7 px: 0.013 against 0.064.)
B5. (a) p = 1/201 = \mathbf{0.005}, the smallest possible with 200 shuffles. (b) No: the leaked light really is at A’s positions in channel B, so the overlap in the images is more than chance. The conclusion “the proteins colocalise” is wrong, but that is not what the test tests. (c) Measure the leakage with a single-stained control and compensate (or image the channels one after the other); then test (unit 4).
B6. (a) The first step with r below \le 0: t_A = 13.9, t_B = 13.8. (b) Just above the background: offset 10 plus about one noise sd (4). Everything below is (mostly) background noise, which is uncorrelated between the channels. (c) As the thresholds fall, more and more of the bright, colocalised pixels move from “below” to “above”, and the pixels left below are increasingly pure noise.
B7. r = \dfrac{\sum (A_i - \bar A)(B_i - \bar B)}{\sqrt{\sum (A_i - \bar A)^2 \sum (B_i - \bar B)^2}} = \dfrac{\sum \text{PDM}_i}{\sqrt{n s_A^2 \cdot n s_B^2}} = \dfrac{\sum \text{PDM}_i}{n s_A s_B}. The sign of r is the sign of the sum of the PDM values, the ICQ that of the count of positive ones: many small positive values (background pixels below both means) and a few large negative ones (bright A where B is dark) give ICQ > 0 but r < 0.
B8. Under the null the observed image is one of N + 1 exchangeable arrangements, so its rank among the N + 1 values is uniform on 1, \dots, N + 1. p \le k/(N + 1) means that at most k - 1 shuffles are ≥ the observed value, i.e. the observed value is among the top k: probability k/(N + 1). The smallest p is 1/(N + 1) (observed value the largest of all).
B9. With g_\sigma(\mathbf x) \propto e^{-|\mathbf x|^2/2\sigma^2}: |\mathbf x|^2 + |\mathbf x - \boldsymbol\delta|^2 = 2|\mathbf x - \boldsymbol\delta/2|^2 + \delta^2/2, so the product is e^{-\delta^2/4\sigma^2}\, e^{-|\mathbf x - \boldsymbol\delta/2|^2/\sigma^2}, and its integral is e^{-\delta^2/4\sigma^2} times the integral for \delta = 0, which is \int g_\sigma^2. Half height: e^{-\delta^2/4\sigma^2} = \tfrac12 \iff \delta = 2\sigma\sqrt{\ln 2} = 1.665\,\sigma; with FWHM = 2\sqrt{2\ln 2}\,\sigma = 2.355\,\sigma this is 0.707 FWHM.
B10. r(-1) = a - b + c, r(0) = c, r(1) = a + b + c, so b = (r_+ - r_-)/2 and a = (r_- - 2r_0 + r_+)/2. The vertex is at x^* = -b/(2a) = (r_- - r_+)/\big(2(r_- - 2r_0 + r_+)\big). B3: (0.78 - 0.84)/\big(2(0.78 - 1.80 + 0.84)\big) = -0.06/(-0.36) = \mathbf{+0.17} px.
Explore
The demo uses the two-cell scene of the last unit’s demo. Left: the merge (or, on the PDM tab, the PDM image: red positive, blue negative, white 0). Middle: one tab per question: the shift curve (r against a shift of B in x, solid, and in y, dashed; the shaded band is the blur FWHM), randomisation (histogram of r for block-shuffled B, with the 1-px shuffle in grey; press shuffle), PDM / ICQ (the ICQ of block-shuffled B), Costes trace. Sliders: shared fraction f, structure size, blur, a sub-pixel shift of B, bleed-through, a background shared by both channels, noise; region inside the cells or the whole image. All shuffles use fixed seeds (200 each).
E1. Preset colocalised: narrow peak at 0 (shift tab). Read r at 0, the half width, and r at Δx = 1, 2 and 3 px (readout), and at 6 px (from the curve). Compare with the Gaussian prediction e^{-\delta^2/4\sigma^2} for σ = 2.15 px. Then set the structure size to 2, and separately the blur to 3: how does the half width change?
E2. Preset a shifted channel: peak at +2.4 px. Read r at 0, the peak position and the peak value. Is the colocalisation worse, or only the alignment?
E3. Preset large-scale correlation: broad hump (whole image, diffuse signal in both cells, unrelated structures). Read r at 0 and at ±20 px. Switch the region to inside cells: what happens to the curve, and why?
E4. Preset pixel shuffle: everything is significant, then blocks the size of the blur: honest null. Read the observed r, the sd of the null, the number of shuffles ≥ observed and p for both. Then try blocks of 2, 4 and 16 px.
E5. Preset bleed-through passes the test. Read r and p. Set the bleed-through to 0 and shuffle again. What does the test test?
E6. Presets independent: ICQ is not 0 and segregated: ICQ < 0 (PDM tab). For each, read r, the ICQ, the sign-test p and the ICQ of shuffled B. Where in the PDM image are the negative pixels? Then switch to colocalised and back to the PDM tab.
E7. Presets weak correlation: Costes stops high and shared background: Costes walks to the bottom (Costes tab). Read where the search stops, M1, M2 and the share of pixels above both thresholds. Which of the two failures would you notice from M1 and M2 alone?
E1. r(0) = 0.988, half width 3.7 px. r = 0.940 (1 px), 0.808 (2 px), 0.628 (3 px), about 0.16 (6 px); the Gaussian prediction gives 0.947, 0.805, 0.615 and 0.143 (times 0.988): the structures act like Gaussians of σ ≈ 2.15 px (radius about 1.9 px, blurred by σ = 1). Size 2: half width 6.9 px; blur 3: 6.0 px. The peak is as wide as the structures and the blur together.
E2. r(0) = 0.743, peak at 2.39 px with r = 0.981. The colocalisation is intact (0.98 at the right shift); only the registration is off. A single r at zero shift would have reported 0.74 and blamed the biology.
E3. Whole image: r = 0.51 at 0 and 0.33 at ±20 px: no narrow peak, a broad hump from the two clusters (cells bright in both channels, background dark in both). Inside the cells: r = -0.08 at 0, 0.08 and 0.06 at ±20 px: flat. The correlation was the cell outline, not structures.
E4. r_\text{obs} = 0.047 in both. 1-px blocks: null sd 0.013, 0 of 200 ≥ observed, p = 0.005: “significant”. 8-px blocks: sd 0.064, 44 of 200, p = 0.224. 2 px: sd 0.024, p = 0.020; 4 px: 0.045, p = 0.174; 16 px: 0.078, p = 0.313. The null widens as the blocks keep more of each channel’s texture; from about the structure size on the verdict no longer changes.
E5. r = 0.322, 0 of 200 shuffles reach it, p = 0.005. With bleed-through 0: r = -0.078, p = 0.935. The test asks whether the overlap in these two images is more than chance, and with leakage it is; it cannot know that the overlap is A’s own light.
E6. Independent: r = -0.078, ICQ = 0.060 (3345 of 5978 positive), sign-test p \approx 3 \cdot 10^{-20}, shuffled ICQ 0.100 ± 0.021: the positive ICQ comes from the dark background pixels (below both means), and the sign test is wrong; against the shuffles, 0.06 is if anything low. Segregated: r = -0.291, ICQ = −0.038, sign-test p \approx 6 \cdot 10^{-9}, shuffled ICQ 0.115 ± 0.019. The negative pixels sit at the structures: bright A where B is below its mean, and the other way round. Colocalised: ICQ = 0.473, deep red at the spots.
E7. Weak: the trace reaches 0 at t_A = 99.1, t_B = 48.0 after 57 steps; M1 = 0.30, M2 = 0.11, 143 of 5978 pixels (2 %) above both. Shared background (whole image): the trace stays near 0.28 and the search runs to t_A = 15.7, t_B = 19.4; M1 = 0.92, M2 = 0.97, 12294 of 16384 pixels (75 %) above both. The second looks like excellent colocalisation from M1 and M2 alone: it is the dangerous one. The first at least looks weak.
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 colocalisation/src/coloc_null/exercise.rs. Pearson’s r over a mask is given (it is C1 of the last unit).
C1. shift_curve(a, b, mask, max_shift): r of A against B shifted sideways, for every shift:
for dx = −max_shift … max_shift:
B_shifted(x, y) = B(x + dx, y) # outside the image: nearest edge pixel
curve[dx] = r(A, B_shifted) over the mask
The check makes B a copy of A moved 2 pixels to the right: the curve must peak at +2 with r = 1.
C2. subpixel_peak(r_minus, r0, r_plus): the vertex of the parabola through the three values around the peak, (r_- - r_+)/\big(2(r_- - 2r_0 + r_+)\big). The check is B10 (+0.17 px for the curve of B3).
C3. block_shuffle(b, mask, block, rng): cut B into squares, shuffle the squares inside the region:
corners = top-left corners of the block × block squares whose centre is in the mask
order = 0, 1, …, n−1; shuffle it:
for i = n−1 down to 1: swap order[i] with order[random number in 0 … i]
for k = 0 … n−1:
copy the square at corners[order[k]] of B to the square at corners[k] of the result
The checks: the result has exactly the pixel values of B, squares outside the cells stay, and every square of the result is a whole square of B.
C4. permutation_p(r_obs, null): (1 + \#\{r^* \ge r_\text{obs}\})/(N + 1). The check is B1.
C5. pdm(a, b, mask) and icq(a, b, mask): the image (A - \bar A)(B - \bar B) inside the mask, and the share of its positive pixels minus 0.5. The checks are the PDM values and the ICQ of B2.
Then cargo run -p colocalisation -- coloc-null and open the pictures in out/coloc-null/. It uses the synthetic two-cell images of the last unit’s program (structure radius 1.3–2.5 px, blur σ = 1 px unless stated). It prints three shift curves, block randomisations of an image with independent structures (blur σ = 2 px) for blocks of 1 to 16 px, and the ICQ of a colocalised and an independent image next to the ICQ of 200 block-shuffled copies. Pictures: the independent image and two shuffled versions (shuffled-1px.png, shuffled-8px.png), and the PDM images (white positive, black negative).
C6. Read the three shift curves. What are the half width of the colocalised peak, the peak position of the shifted image, and what survives at ±10 px in the third? Compare with E1–E3.
C7. For the independent image, how does the sd of the null distribution change with the block size, and which block sizes call the image “significant” at the 5 % level? Which answer is right, and how can you tell from the pictures? What does the test say about the image with 40 % bleed-through?
C8. Compare each observed ICQ with the ICQ of the shuffled copies. Why is the shuffled ICQ not 0, and which of the two images shows more than chance?
The model solution is colocalisation/src/coloc_null/solution.rs in the download.
C6. Colocalised (inside the cells): r(0) = 0.932, a narrow peak with half width 2.9 px, falling to 0.03 at ±10 px. Shifted by 2.4 px: r(0) = 0.584, but the peak is at 2.37 px with r = 0.922: the structures still overlap, only the registration is off (E2). Independent structures with a diffuse signal in both cells, over the whole image: r(0) = 0.656 and still 0.47–0.49 at ±10 px: a broad hump from the cells being bright in both channels, no peak from structures (E3).
C7. The observed r is 0.052. The null sd grows with the block size: 0.011 (1 px), 0.020 (2 px), 0.038 (4 px), 0.047 (8 px), 0.054 (16 px). With 1- and 2-px blocks none of the 200 shuffles reaches 0.052, p = 0.005: “significant”. With 4 px p = 0.075, with 8 and 16 px p = 0.18 and 0.19. The structures are independent by construction, so the large blocks are right. In shuffled-1px.png the magenta channel has become fine-grained noise without any structures: that is not “the same image without the link”; with 8-px blocks the blurred spots survive, cut into squares. With 40 % bleed-through r = 0.457 and p = 0.005: the overlap in the images is real, although the structures are independent (the slide “What the test does not protect against”).
C8. Colocalised: ICQ = 0.430 against 0.199 ± 0.011 for the shuffles: far more than chance. Independent: ICQ = 0.176 against 0.192 ± 0.011: not more than chance, rather a little less. The shuffled ICQ is about 0.2, not 0, because most pixels in the cells are background, below both means, and so have a positive PDM. In pdm-independent.png the spots have dark (negative) rims and only a few white (positive) ones where two spots happen to overlap.
Where this leads
The tests of this unit say whether the overlap in the images is more than chance. The next unit, objects instead of pixels, asks which structures overlap and how far apart they are, with its own chance level. Unit 4, what the microscope does, removes the overlap that the optics add (bleed-through, background, chromatic shift). A p-value per image is not a comparison between treatments: for that, coefficients per cell and per experiment go through the statistics of the evaluation day.