Exercise: least-squares unmixing
Before this unit: Bleed-through and compensation (previous unit); Day 46: Colour.
See also: Day 3: Bit depth and noise; Day 16: Measurements.
Goal
By the end of this exercise you should be able to:
- solve a small unmixing problem with more channels than components by the normal equations, and check that the residual is orthogonal to the spectra,
- predict from the spectra how much noise unmixing amplifies (the condition number), and choose filters,
- use the residual map to detect a missing component,
- explain when weighted least squares helps.
Do the pen-and-paper part first, then the demo.
Background
| Idea | What it means |
|---|---|
| Model | \mathbf y \approx M\mathbf a: N channels, K < N components, spectra as columns of M |
| Least squares | minimise \|\mathbf y - M\mathbf a\|^2: the closest mixture |
| Normal equations | M^\top M\,\hat{\mathbf a} = M^\top\mathbf y; pseudo-inverse M^+ = (M^\top M)^{-1}M^\top, \hat{\mathbf a} = M^+\mathbf y |
| Residual | \mathbf r = \mathbf y - M\hat{\mathbf a}, orthogonal to every column: M^\top\mathbf r = \mathbf 0 |
| Condition number | \kappa = \sigma_\text{max}/\sigma_\text{min} (singular values of M = square roots of the eigenvalues of M^\top M) |
| Noise of the estimate | \operatorname{Cov}(\hat{\mathbf a}) = \sigma_n^2 (M^\top M)^{-1} for independent channel noise \sigma_n |
| Weighted LS | \hat{\mathbf a} = (M^\top W M)^{-1}M^\top W\mathbf y, W = \operatorname{diag}(1/\text{expected count}) |
By hand
B1. Three channels, two dyes: M = \begin{pmatrix} 1 & 0.2 \\ 0.5 & 0.5 \\ 0.1 & 1 \end{pmatrix} and a measured pixel \mathbf y = (1.3, 0.9, 1.2). (a) Compute M^\top M and M^\top\mathbf y. (b) Solve the 2 \times 2 system for \hat{\mathbf a}. (c) Compute the fit M\hat{\mathbf a} and the residual \mathbf r. Check that \mathbf r is orthogonal to both columns. (d) The pixel was made from \mathbf a = (1, 1). What would the noise-free pixel have been? How large is the residual compared with the noise you can infer?
B2. Two pairs of spectra on three channels: near-parallel \mathbf m_1 = (1, 0.9, 0.5), \mathbf m_2 = (0.9, 1, 0.5); well separated \mathbf m_1 = (1, 0.1, 0.5), \mathbf m_2 = (0.1, 1, 0.5). (a) For each pair compute M^\top M and its eigenvalues. (A 2 \times 2 matrix \begin{pmatrix} p & q \\ q & p \end{pmatrix} has eigenvalues p \pm q with eigenvectors (1, 1) and (1, -1).) (b) The singular values of M are the square roots of these eigenvalues. Compute \kappa for each pair. (c) Channel 1 reads one unit too high. How much does each abundance move? (The first column of M^+ is (5.23, -4.77) for the near-parallel pair and (0.88, -0.23) for the separated pair; check one entry.) (d) Which combination of the two abundances is well determined for the near-parallel pair, and which is not?
B3. Two dyes are sampled in six bins: dye 1 = (0.1, 0.6, 1.0, 0.6, 0.3, 0.1), dye 2 = (0, 0.1, 0.4, 0.9, 1.0, 0.5). You can build two channels from the bins in three ways: A: bin 2 and bin 5; B: bin 3 and bin 4; C: bins 1–3 and bins 4–6. (a) Write M for each set (channel value = sum over its bins). (b) Which set has the smallest bleed-through between the channels? Which collects the most photons? (c) The condition numbers are 2.08 (A), 3.22 (B) and 2.34 (C), and the noise on the abundances, per unit channel noise, is (1.76, 1.18), (1.49, 1.77) and (0.68, 0.55). Which set would you choose, and why is \kappa alone not the whole story?
B4. Four detectors, two dyes, with reference spectra \mathbf m_1 = (1, 0.45, 0.10, 0.02) and \mathbf m_2 = (0.02, 0.30, 1, 0.40). A pixel records \mathbf y = (104, 58, 61, 24). (a) Why does M^{-1} not exist? (b) Compute M^\top M and M^\top\mathbf y, and solve for \hat{\mathbf a} (two decimals). (c) The pixel was made from (100, 50) plus small errors. Compare.
B5. An unmixed image looks clean, but the residual map is at about 1.4 times the noise level in the cytoplasm and at 1.0 in the nucleus. Give two possible causes and how you would distinguish them.
B6. A photon-starved pixel has expected counts of 4 in one channel and 400 in another. What is the standard deviation of each count? Which channel should weigh more in the fit, and by what factor?
Math questions
B7. (math) Derive the normal equations by setting the gradient of \|\mathbf y - M\mathbf a\|^2 to zero. Then derive them a second time from the requirement that the residual be orthogonal to every column.
B8. (math) Show that P = MM^+ is symmetric and idempotent (P^2 = P), and that (I - P)M = 0. What do these say about a pixel that is an exact mixture?
B9. (math) Show that \operatorname{Cov}(\hat{\mathbf a}) = \sigma_n^2 (M^\top M)^{-1} when the channel noise is independent with variance \sigma_n^2. For the near-parallel pair of B2, compute the standard deviations of \hat a_1 + \hat a_2 and \hat a_1 - \hat a_2 in units of \sigma_n.
B10. (math) Write the log-likelihood of \mathbf a for independent Gaussian channels with variances v_c, and show that its maximum is the weighted least-squares solution with W = \operatorname{diag}(1/v_c).
B1. (a) M^\top M = \begin{pmatrix} 1 + 0.25 + 0.01 & 0.2 + 0.25 + 0.1 \\ 0.55 & 0.04 + 0.25 + 1 \end{pmatrix} = \begin{pmatrix} 1.26 & 0.55 \\ 0.55 & 1.29 \end{pmatrix}, M^\top\mathbf y = (1.3 + 0.45 + 0.12,\ 0.26 + 0.45 + 1.2) = (1.87, 1.91). (b) \det = 1.26 \cdot 1.29 - 0.55^2 = 1.3229; \hat a_1 = (1.29 \cdot 1.87 - 0.55 \cdot 1.91)/1.3229 = 1.029, \hat a_2 = (1.26 \cdot 1.91 - 0.55 \cdot 1.87)/1.3229 = 1.042. (c) M\hat{\mathbf a} = (1.238, 1.036, 1.145), \mathbf r = (0.062, -0.136, 0.055). \mathbf m_1 \cdot \mathbf r = 0.062 - 0.068 + 0.0055 = 0.000 and \mathbf m_2 \cdot \mathbf r = 0.0124 - 0.068 + 0.055 = 0.000 (up to rounding). (d) M(1, 1)^\top = (1.2, 1.0, 1.1): the noise was (0.1, -0.1, 0.1). The residual (|\mathbf r| = 0.16) is the part of that noise perpendicular to the plane; the part within the plane moved the estimate from (1, 1) to (1.03, 1.04).
B2. (a) Near-parallel: M^\top M = \begin{pmatrix} 2.06 & 2.05 \\ 2.05 & 2.06 \end{pmatrix}, eigenvalues 4.11 (along (1, 1)) and 0.01 (along (1, -1)). Separated: \begin{pmatrix} 1.26 & 0.45 \\ 0.45 & 1.26 \end{pmatrix}, eigenvalues 1.71 and 0.81. (b) Near-parallel: \sigma = 2.03 and 0.10, \kappa = 20.3. Separated: \sigma = 1.31 and 0.90, \kappa = 1.45. (c) Near-parallel: \hat{\mathbf a} moves by (5.23, -4.77): a unit error becomes errors of about 5 in opposite directions. Separated: (0.88, -0.23). Check, near-parallel: M^+ = (M^\top M)^{-1}M^\top, first column = (M^\top M)^{-1}(1, 0.9)^\top = \frac{1}{0.0411}(2.06 - 1.845,\ -2.05 + 1.854) = (5.23, -4.77). (d) The sum \hat a_1 + \hat a_2 is well determined (large eigenvalue along (1, 1)); the difference is not (eigenvalue 0.01 along (1, -1)). The two errors in (c) nearly cancel in the sum.
B3. (a) A: \begin{pmatrix} 0.6 & 0.1 \\ 0.3 & 1.0 \end{pmatrix}; B: \begin{pmatrix} 1.0 & 0.4 \\ 0.6 & 0.9 \end{pmatrix}; C: \begin{pmatrix} 1.7 & 0.5 \\ 1.0 & 2.4 \end{pmatrix} (rows = channels, columns = dyes). (b) A has the smallest bleed-through relative to the dyes’ own signals: dye 1 into channel 2 0.3/0.6 = 50 %, dye 2 into channel 1 0.1/1.0 = 10 % (B: 60 % and 44 %; C: 59 % and 21 %). C collects by far the most photons (2.7 and 2.9 per unit dye, against 0.9/1.1 for A and 1.6/1.3 for B). (c) C: its \kappa (2.34) is almost as good as A’s, and because it collects about three times more light its absolute noise gain is the smallest (0.68 and 0.55). \kappa measures only the shape of M (how unequal its stretches are), not its size: a matrix twice as large has the same \kappa but half the noise on \hat{\mathbf a}. For a choice of filters, compare the noise on the abundances you care about.
B4. (a) M is 4 \times 2, not square. (b) M^\top M = \begin{pmatrix} 1.2129 & 0.263 \\ 0.263 & 1.2504 \end{pmatrix}, M^\top\mathbf y = (136.68, 90.08), \hat{\mathbf a} = (101.71, 50.65). (c) Close to (100, 50): the four detectors average the errors (3, -2, 1, 2) that were added to the noise-free pixel (101, 60, 60, 22). The residual is (1.28, -2.96, 0.18, 1.71).
B5. (1) A missing component in the cytoplasm (autofluorescence, an unexpected dye): its spectrum cannot be made from the reference spectra. (2) A wrong reference spectrum for a component that lives in the cytoplasm (for example a dye whose spectrum shifted there). Look at the residual spectrum of the cytoplasm pixels: a broad, smooth shape that also appears in an unstained control points to autofluorescence; a residual shaped like the difference of two similar curves (positive on one flank, negative on the other) points to a shifted reference.
B6. Poisson: sd = \sqrt{\text{mean}}, so 2 and 20 counts. The dim channel’s absolute error is ten times smaller: weight 1/4 against 1/400, a factor 100 (the squared ratio of the standard deviations).
B7. E(\mathbf a) = \mathbf y^\top\mathbf y - 2\mathbf a^\top M^\top\mathbf y + \mathbf a^\top M^\top M\mathbf a, \nabla E = -2M^\top\mathbf y + 2M^\top M\mathbf a = 0, so M^\top M\hat{\mathbf a} = M^\top\mathbf y. Orthogonality: \mathbf m_k^\top(\mathbf y - M\hat{\mathbf a}) = 0 for every k is M^\top(\mathbf y - M\hat{\mathbf a}) = \mathbf 0, the same equations. (The closest point of a subspace is the one whose difference is perpendicular to it.)
B8. P^\top = M\big((M^\top M)^{-1}\big)^\top M^\top = P since M^\top M is symmetric. P^2 = P as in quiz Q8. (I - P)M = M - M(M^\top M)^{-1}M^\top M = M - M = 0. For an exact mixture \mathbf y = M\mathbf a: P\mathbf y = \mathbf y (fit = pixel), residual (I - P)\mathbf y = 0, and M^+\mathbf y = \mathbf a exactly.
B9. \hat{\mathbf a} = M^+\mathbf y is linear in \mathbf y, so \operatorname{Cov}(\hat{\mathbf a}) = M^+\operatorname{Cov}(\mathbf y)M^{+\top} = \sigma_n^2 (M^\top M)^{-1}M^\top M(M^\top M)^{-1} = \sigma_n^2(M^\top M)^{-1}. For the near-parallel pair, (M^\top M)^{-1} has eigenvalues 1/4.11 along (1, 1) and 1/0.01 along (1, -1). \operatorname{Var}(\hat a_1 + \hat a_2) = \sigma_n^2 (1, 1)(M^\top M)^{-1}(1, 1)^\top = \sigma_n^2 \cdot 2/4.11, sd = 0.70\,\sigma_n; \operatorname{Var}(\hat a_1 - \hat a_2) = \sigma_n^2 \cdot 2/0.01, sd = 14.1\,\sigma_n.
B10. \log L(\mathbf a) = -\tfrac12\sum_c (y_c - (M\mathbf a)_c)^2/v_c + \text{const} = -\tfrac12(\mathbf y - M\mathbf a)^\top W(\mathbf y - M\mathbf a) + \text{const}. Its gradient M^\top W(\mathbf y - M\mathbf a) vanishes at M^\top WM\hat{\mathbf a} = M^\top W\mathbf y, the WLS solution; the Hessian -M^\top WM is negative definite, so it is a maximum.
Explore: unmix a lambda stack
A synthetic cell (seeded, 128 \times 128) with four components: nucleus (blue), mitochondria (green), membrane (magenta) and a diffuse autofluorescence with a broad spectrum (orange). It is recorded as a lambda stack of 3–32 contiguous channels between 480 and 700 nm, with Poisson noise; photons is the expected total count of a pixel with abundance 1. Top: the true and the unmixed pseudocolour merge, and the residual (in units of the expected photon noise: about 1 = noise only). Bottom: the four abundance maps (red = negative), or the true ones. Right: the four spectra over the channel bins (dashed = not in M); below, the clicked pixel: dots = measured, black line = fit, coloured bands = each component’s contribution. Methods: one channel per dye (each dye read in its brightest channel, as if there were no bleed-through), least squares, weighted LS. The readout gives \kappa(M), the noise amplification per component compared with the same dye measured alone, the RMSE of each map against the truth, the share of negative abundances and the mean residual.
L1. Preset one channel per dye. Which map is worst, and why? Look at the mitochondria map: what else do you see in it?
L2. Preset least squares removes it. Compare the RMSEs with L1. Click a nucleus pixel and a mitochondrion: how do the coloured bands explain the measured dots?
L3. Preset two dyes 6 nm apart. What happens to the nucleus and mitochondria maps, the RMSE and the noise amplification? Which maps are unaffected?
L4. With the dyes still 6 nm apart, set 8 channels, then 32 (preset 32 channels: no rescue). How much does the noise amplification change? Then move the peaks back to 45 nm apart.
L5. Preset forgot the autofluorescence. Where does the residual light up? What happens to the other maps? Then add it as a component.
L6. Set 3 channels with all four components in M. What does the demo say, and why?
L7. Preset photon-starved: WLS vs LS (20 photons). Compare the RMSEs of weighted and ordinary least squares, and the share of negative abundances.
L1. The autofluorescence map (RMSE 0.30): its brightest channel is also bright for the other dyes, so it shows the whole cell. The mitochondria map (RMSE 0.069) contains the nucleus too: dye 1’s light in the mitochondria dye’s channel. This is bleed-through again, now with four dyes.
L2. RMSEs 0.017, 0.023, 0.024 and 0.062 (against 0.035, 0.069, 0.058, 0.298): the maps are clean. In a nucleus pixel the blue band carries almost everything and the black fit follows the dots; in a mitochondrion the green band dominates, with some orange autofluorescence underneath. The dots scatter around the black line: that scatter is the photon noise, the residual.
L3. The nucleus and mitochondria maps become grainy: RMSE 0.071 and 0.074 (were 0.017 and 0.023), noise amplification 4.9 and 5.1 (was 1.3 and 1.7), \kappa = 10.2. Membrane and autofluorescence are hardly affected (noise × 1.5 and 1.7): only the two similar spectra trade off against each other.
L4. Noise amplification for nucleus and mitochondria: 5.7 and 5.9 with 8 channels, 4.9 and 5.1 with 16, 4.7 and 4.9 with 32; the RMSE barely moves (0.069/0.073 at 32 channels). Finer sampling helps a little; the photons are the same. With 45 nm separation the amplification drops back to 1.3 and 1.7.
L5. The residual rises in the cytoplasm (1.37 times the noise on average, against 1.04 in the nucleus): the autofluorescence spectrum cannot be built from the three narrow dyes. Its light leaks into the other maps (mitochondria RMSE 0.054, membrane 0.057). With the autofluorescence added, the residual is about 1 everywhere and the RMSEs return to 0.017–0.062.
L6. “3 channels < 4 components: no unique solution”. Three equations cannot determine four unknowns: M^\top M is singular, and infinitely many mixtures fit exactly. You need at least as many channels as components (and in practice more, for noise).
L7. Ordinary LS: RMSE 0.077, 0.105, 0.108, 0.271; weighted LS: 0.072, 0.095, 0.100, 0.244, a modest gain (6–10 %). About 20 % of the abundance values are negative with either method: the constraints of the next unit address that.
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 unmixing/src/lsunmix/exercise.rs. A lambda stack is a list of images, one per channel; M is stored row by row (rows = channels, columns = components).
C1. unmix_pixel(m, y): least squares by the normal equations:
G[i][j] = Σ_c M[c][i] · M[c][j] # MᵀM, K × K
b[i] = Σ_c M[c][i] · y[c] # Mᵀy
solve G a = b
The checks are B1(b) and B4(b).
C2. pseudo_inverse(m) and unmix_image(stack, m): column c of M^+ is the unmixing of the pixel that is 1 in channel c and 0 elsewhere; then every abundance map is a weighted sum of the channels:
M⁺ = unmix every unit vector e_c # once
for every component k, every pixel:
a_k = Σ_c M⁺[k][c] · y_c
The check is B2(c) (the first columns of M^+) and M^+M = I.
C3. residual(m, y, a) and residual_map(stack, m, abundances): \mathbf r = \mathbf y - M\mathbf a, and per pixel the size of the residual in units of the expected photon noise, \sqrt{\tfrac{N}{N-K}\sum_c r_c^2 / \sum_c \text{fit}_c} (Poisson: the variance equals the expected count). The check is B1(c): \mathbf r is orthogonal to both columns.
C4. condition_number(m): \kappa = \sqrt{\lambda_\text{max}/\lambda_\text{min}} from the eigenvalues of M^\top M. The check is B2(b).
C5. unmix_weighted(m, y, w): weighted least squares. Scale row c of M and y_c by \sqrt{w_c} and solve ordinary least squares: the normal equations of the scaled problem are M^\top W M\,\mathbf a = M^\top W\mathbf y.
Then cargo run -p unmixing -- lsunmix and open the pictures in out/lsunmix/. The program makes a synthetic cell with four components (nucleus blue, mitochondria green, membrane magenta, autofluorescence orange; truth.png) and records 16 channels between 480 and 700 nm with photon noise, 400 photons for a pixel of abundance 1. It unmixes one channel per dye (one-channel.png) and by least squares (least-squares.png), and prints the RMSE of each map against the truth. It draws the residual map with three spectra (the autofluorescence left out) and with all four (residual-3-vs-4.png, 0 black, 2 white). Then it moves the mitochondria dye from 55 nm to 6 nm after the nucleus dye (similar.png), and compares ordinary and weighted least squares at 20 photons.
C6. Compare the RMSEs of one channel per dye and least squares. Which map is worst with one channel per dye, and why? What share of the least-squares abundances is negative?
C7. Where does the residual light up with three spectra, and how large is it there? What happens to the mitochondria map?
C8. Compare κ and the RMSEs for the two gaps. Why is κ of all four spectra the same for both, while κ of the nucleus and mitochondria spectra alone changes so much? How much does weighting help at 20 photons?
The model solution is unmixing/src/lsunmix/solution.rs in the download.
C6. One channel per dye: RMSE 0.057, 0.079, 0.073 and 0.470; least squares: 0.035, 0.036, 0.034 and 0.096. The autofluorescence map is by far the worst with one channel per dye: its brightest channel also collects the light of the three narrow dyes, so the map shows the whole cell (yellowish in one-channel.png). Least squares gives each channel’s light to the right component. 22.4 % of all abundance values are negative: wherever a component is absent, its estimate is noise around zero, half of it below.
C7. With three spectra the residual is 1.37 times the noise in the cytoplasm and 0.89 in the nucleus; with four it is 0.96 and 0.87. In the picture the cytoplasm is bright on the left and as dark as the nucleus on the right. The broad autofluorescence cannot be made from the three narrow spectra. Its light goes into the other maps: the mitochondria RMSE rises from 0.036 to 0.079. (The value stays a little below 1 even with the right model, because the channels’ noise is not equal; the formula is a rough scale.)
C8. 55 nm apart: RMSE 0.036 and 0.036; 6 nm apart: 0.075 and 0.079, about twice the noise. κ of all four spectra is 9.1 both times: it is set by the broad autofluorescence spectrum, which overlaps every other one, and \kappa measures the worst direction only. For the nucleus and mitochondria spectra alone κ goes from 1.1 to 8.5: those two maps trade off against each other. At 20 photons weighted least squares lowers the RMSEs from 0.155, 0.158, 0.152, 0.427 to 0.117, 0.121, 0.119, 0.304, about a quarter: in a dim pixel the channels with a few counts are much less noisy than the bright ones, and weighting trusts them more.
Where this leads
Least squares lets the abundances go negative and needs the spectra in advance. The next unit, endmembers, adds the constraints (non-negative, sum to one) with their geometry, cones and simplices, and asks how to find the spectra when no reference is available. The same least squares, with time bins instead of wavelength bins, gives the amplitudes in the global analysis of fluorescence decays (day 50).