Exercise: endmembers and the geometry of mixtures
Before this unit: Least-squares unmixing (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:
- draw the cone and the simplex of a few spectra, and find the NNLS solution of a pixel outside the cone,
- compute barycentric abundances in a simplex,
- decide the number of components from singular values,
- find endmembers by the largest-simplex rule, and explain when vertex finding and NMF fail.
Do the pen-and-paper part first, then the two demos.
Background
| Idea | What it means |
|---|---|
| Cone | all non-negative mixtures M\mathbf a, \mathbf a \ge 0 |
| Simplex | normalised pixels (divided by their total): mixtures with \sum a_k = 1; corners = normalised spectra |
| NNLS | the closest point of the cone; components behind the face it lands on get zero |
| Fully constrained | the closest point of the simplex (\mathbf a \ge 0, \sum a_k = 1) |
| Number of components | singular values of the pixel matrix that stand out above the noise floor |
| Vertex finding | pure pixels = corners: most extreme pixel, project out, repeat; N-FINDR: largest simplex |
| NMF | Y \approx WH, W, H \ge 0; no pure pixels needed, but not unique |
By hand
B1. Two channels, two spectra \mathbf m_1 = (1, 0.2) and \mathbf m_2 = (0.3, 1), and a dim noisy pixel \mathbf y = (0.1, 0.9). (a) Draw the two rays and the cone. Is \mathbf y inside? (Compare the angles of the three vectors.) (b) Solve M\mathbf a = \mathbf y (least squares is exact here: two equations, two unknowns). Which abundance is negative? (c) Find the NNLS solution: project \mathbf y onto the nearer ray, a_2 = (\mathbf y \cdot \mathbf m_2)/(\mathbf m_2 \cdot \mathbf m_2). How large is the residual? Compare with projecting onto the other ray.
B2. In the plane of normalised pixels, three endmembers sit at \mathbf e_1 = (0, 0), \mathbf e_2 = (4, 0), \mathbf e_3 = (0, 3). (a) Find the fractions (a_1, a_2, a_3), summing to one, of the pixel at (1, 1). (b) Do the same for the pixel at (3, 1.5). What do you conclude about this pixel?
B3. N-FINDR by hand. Six normalised pixels in 2D: P1 (0.5, 0.2), P2 (3.6, 0.4), P3 (1.5, 1.2), P4 (0.3, 2.8), P5 (2.2, 1.9), P6 (1.0, 0.9). (a) Plot them. Which three would you guess are the endmembers? (b) Compute the area of the triangles P1–P2–P4, P1–P2–P5 and P2–P4–P6 (area = \tfrac12|(x_2 - x_1)(y_3 - y_1) - (x_3 - x_1)(y_2 - y_1)|). Which is largest?
B4. The singular values of a pixel matrix are 41.2,\ 12.5,\ 6.1,\ 0.42,\ 0.40,\ 0.39,\ 0.37. How many components? What could make you choose one more?
B5. A colleague runs PCA on a lambda stack and reports the first three principal axes as “the spectra of the three dyes”. Give two reasons why that cannot be right.
B6. Two dyes, three channels. All pixels are 70/30, 50/50 or 30/70 mixtures of the two dyes; no pixel is pure. Sketch the normalised cloud on the segment between the two spectra. What does vertex finding return? Give two different pairs of “spectra” that explain all pixels with non-negative abundances.
Math questions
B7. (math) Write the KKT conditions for NNLS and check them for your solution of B1(c). Why does the solution of B1(b) violate them?
B8. (math) One multiplicative update of NMF. Y = \begin{pmatrix} 3 & 1 \\ 1 & 3 \end{pmatrix}, W = \begin{pmatrix} 1 & 0.5 \\ 0.5 & 1 \end{pmatrix} (kept fixed), H = \begin{pmatrix} 1 & 1 \\ 1 & 1 \end{pmatrix}. Compute W^\top Y, W^\top WH and the updated H \leftarrow H \odot (W^\top Y) \oslash (W^\top WH). Compare \|Y - WH\|_F^2 before and after.
B9. (math) Show that Y = \begin{pmatrix} 3 & 1 \\ 1 & 3 \end{pmatrix} has two exact non-negative factorisations, W_1 = I, H_1 = Y and W_2 = \begin{pmatrix} 0.75 & 0.25 \\ 0.25 & 0.75 \end{pmatrix}, H_2 = 4I. Find the matrix R with W_2 = W_1R^{-1}, H_2 = RH_1. Draw both pairs of “spectra” (columns of W) as rays together with the two pixels (columns of Y).
B10. (math) Derive the sum-to-one solution \hat{\mathbf a} = \hat{\mathbf a}_\text{LS} + G^{-1}\mathbf 1\,(1 - \mathbf 1^\top\hat{\mathbf a}_\text{LS})/(\mathbf 1^\top G^{-1}\mathbf 1), G = M^\top M, with a Lagrange multiplier.
B1. (a) Angles: \mathbf m_1 at 11.3°, \mathbf m_2 at 73.3°, \mathbf y at 83.7°: outside the cone, beyond the \mathbf m_2 ray. (b) \det M = 1 - 0.06 = 0.94; a_1 = (0.1 - 0.3 \cdot 0.9)/0.94 = -0.18, a_2 = (-0.2 \cdot 0.1 + 0.9)/0.94 = 0.94. Abundance 1 is negative. (c) \mathbf y \cdot \mathbf m_2 = 0.03 + 0.9 = 0.93, \mathbf m_2 \cdot \mathbf m_2 = 1.09, a_2 = 0.853, a_1 = 0. Fit (0.256, 0.853), residual (-0.156, 0.047), length 0.163. Projecting onto \mathbf m_1 instead: a_1 = 0.269, residual length 0.863, much worse. NNLS: (0, 0.853).
B2. (a) a_2 \cdot 4 = 1 and a_3 \cdot 3 = 1 (the vertices sit on the axes), so a_2 = 0.25, a_3 = 0.333, a_1 = 1 - 0.25 - 0.333 = 0.417. (b) a_2 = 0.75, a_3 = 0.5, a_1 = -0.25: a negative fraction. The pixel lies outside the triangle (beyond the edge \mathbf e_2\mathbf e_3): noise, a wrong endmember, or a fourth component.
B3. (a) P1, P2 and P4 look like the corners; P3, P5, P6 inside or near the edges. (b) P1–P2–P4: \tfrac12|3.1 \cdot 2.6 - (-0.2) \cdot 0.2| = 4.05. P1–P2–P5: \tfrac12|3.1 \cdot 1.7 - 1.7 \cdot 0.2| = 2.465. P2–P4–P6: \tfrac12|(-3.3)(0.5) - (-2.6)(2.4)| = 2.295. Checking all 20 triangles confirms P1–P2–P4 (area 4.05) is the largest: those are the N-FINDR endmembers.
B4. Three: the first three stand out, then a drop by a factor of 15 to a flat floor (0.42–0.37), the noise. You might choose a fourth if the residual map with three components shows structure (a component that is dim or present in few pixels can hide just above the floor; in the demo the dim autofluorescence gives a fourth value only about twice the floor).
B5. Principal axes are orthogonal; dye spectra overlap. Principal axes have negative entries (all but possibly the first); spectra are non-negative. (Also: the first axis of uncentred data points at the mean colour, a mixture.)
B6. All normalised pixels lie on the segment between the two normalised spectra, at the 70/30, 50/50 and 30/70 positions: the extreme points are the 70/30 and 30/70 mixtures, so vertex finding returns those mixtures as “spectra”. Two valid pairs: the true spectra, or the 70/30 and 30/70 mixtures themselves; any pair of points on the line at or beyond the outermost pixels (and still non-negative) explains every pixel with non-negative abundances.
B7. KKT: \hat a_k \ge 0, w_k \le 0, \hat a_k w_k = 0 with \mathbf w = M^\top(\mathbf y - M\hat{\mathbf a}). For (0, 0.853): \mathbf w = (-0.147, 0.000), all conditions hold. The solution of B1(b) has \hat a_1 = -0.18 < 0: infeasible.
B8. W^\top Y = \begin{pmatrix} 3.5 & 2.5 \\ 2.5 & 3.5 \end{pmatrix}, W^\top W = \begin{pmatrix} 1.25 & 1 \\ 1 & 1.25 \end{pmatrix}, W^\top WH = \begin{pmatrix} 2.25 & 2.25 \\ 2.25 & 2.25 \end{pmatrix}. New H = \begin{pmatrix} 1.556 & 1.111 \\ 1.111 & 1.556 \end{pmatrix}. The error drops from 5.00 to 3.16.
B9. W_1H_1 = Y trivially; W_2H_2 = 4\begin{pmatrix} 0.75 & 0.25 \\ 0.25 & 0.75 \end{pmatrix} = Y. R = H_2H_1^{-1} = 4Y^{-1} = \frac{4}{8}\begin{pmatrix} 3 & -1 \\ -1 & 3 \end{pmatrix} = \begin{pmatrix} 1.5 & -0.5 \\ -0.5 & 1.5 \end{pmatrix}, and indeed W_1R^{-1} = R^{-1} = \tfrac14 Y = W_2. R has negative entries, yet both factorisations are non-negative. Picture: the pixels are the rays through (3, 1) and (1, 3). Pair 1 uses the axes (1, 0), (0, 1): a wide cone containing the pixels. Pair 2 uses the pixels’ own directions: the narrowest cone. Every cone between these two fits exactly.
B10. Minimise \tfrac12\|\mathbf y - M\mathbf a\|^2 + \mu(\mathbf 1^\top\mathbf a - 1). Gradient: G\mathbf a - M^\top\mathbf y + \mu\mathbf 1 = 0, so \mathbf a = \hat{\mathbf a}_\text{LS} - \mu G^{-1}\mathbf 1. The constraint gives \mathbf 1^\top\hat{\mathbf a}_\text{LS} - \mu\,\mathbf 1^\top G^{-1}\mathbf 1 = 1, \mu = (\mathbf 1^\top\hat{\mathbf a}_\text{LS} - 1)/(\mathbf 1^\top G^{-1}\mathbf 1). Substituting gives the formula.
Explore: the cloud, the cone and the simplex
A 3-channel image of the day’s cell with three components (nucleus, mitochondria, membrane) and its pixels as a point cloud in channel space (drag to rotate; the buttons give standard views). The arrows are the three spectra; the grey faces are the cone they span. normalise divides each pixel by its total: the cone becomes the triangle. Right: the same normalised pixels in a flat triangle whose corners are the three channels; the coloured circles are the endmembers, which you can drag. Click a pixel in the image: its least-squares (red), NNLS (orange) and fully constrained (green) solutions appear as points and as bars; the readout lists them with their residuals. If your browser has no WebGL2, the triangle on the right still works.
C1. Preset noise-free. Rotate the cloud. Where are the nucleus, the mitochondria and the membrane pixels? Is any point outside the cone?
C2. Preset noise: LS goes negative. A dim pixel is selected. Which abundance is negative in LS, and what do NNLS and the fully constrained solution give instead? Compare the residuals.
C3. Preset normalise. Where do the dim pixels end up in the triangle, and why are they so spread out? Lower photons and watch.
C4. Preset a wrong endmember: the membrane spectrum is moved inward. Where are the membrane pixels relative to the triangle? What do the three methods report for the selected membrane pixel, and which readout tells you that something is wrong?
C5. Preset an unmodelled component. Describe the cloud. Can any choice of three endmembers contain it?
C1. Three arms along the three arrows: nucleus pixels along the nucleus spectrum, the thin mitochondria and membrane structures along theirs; pixels on the boundaries between structures lie between the arms (on the faces), dark pixels near the origin. Without noise no point is outside the cone.
C2. LS fits the pixel exactly (three channels, three components: residual 0) but one abundance is negative. NNLS sets it to zero and moves the fit onto a face of the cone (small residual instead of a negative amount); the fully constrained solution also sums to the pixel’s total. The bars show the negative red bar replaced by zero.
C3. Dim pixels have few photons, so their channel fractions are very noisy: in the triangle they spread far from the corners and edges, many outside the simplex. Fewer photons spread them more. Bright pixels sit tightly near the corners and edges.
C4. Real membrane pixels now lie outside the triangle, beyond the moved corner. LS gives the selected membrane pixel a negative abundance for another component; NNLS and the fully constrained solution return non-negative abundances that look plausible, but their residual is large. The constraint hides the error; the residual reveals it.
C5. A fourth corner: the granules’ pixels form an arm towards a direction outside the triangle of the three spectra (near the edge between nucleus and mitochondria, but beyond it). No triangle with three of the true spectra contains it; any three endmembers that contain all pixels must include a fourth direction or misplace the others. One more component is needed.
Explore: finding the spectra
The same cell recorded with 16 channels and Poisson noise, now treating the spectra as unknown. Top left: the singular values of the pixel matrix (log scale; blue = the first K). Bottom left: the bright pixels normalised to sum one, projected onto their two main principal directions, with the true spectra (circles, dashed triangle) and the found ones (squares). Right: true (dashed) and found (solid) spectra; for NMF the error curve (divided by the error of the true spectra); the found abundance maps. Methods: PCA axes, vertex finding (step: one vertex per click), NMF (step = 10 iterations; run; seed). pure pixels off gives every pixel 30 % of the other components. The readout gives the singular values, the relative fit error of the found spectra (and of the true ones), and the angles between found and true spectra.
M1. Preset count the components (K = 4). Read the singular values. Is the fourth component clearly above the floor? Now set K = 3 (the autofluorescence disappears from the sample): what changes?
M2. Still on PCA axes: describe the “spectra” PCA finds. Why are they not spectra?
M3. Preset pure pixels. Step through vertex finding. Where are the found corners in the cloud, and how far (in degrees) are the found spectra from the true ones?
M4. Preset no pure pixels. Where is the cloud now relative to the true triangle? How far are the vertex-finding spectra from the truth?
M5. Preset NMF from two seeds (seed 1, 400 iterations). Note the error and the angles. Set the seed to 2 and run 400 iterations. Compare. Then switch pure pixels on and repeat both seeds.
M6. Preset three spectra known. Look at the found fourth spectrum and the dashed true autofluorescence. Where does it differ, and why can NMF not fix that?
M1. \sigma_i/\sigma_1 = 1,\ 0.653,\ 0.505,\ 0.122,\ 0.070,\ 0.066: the fourth (the dim autofluorescence) stands out, but only about twice the floor. With K = 3: 1,\ 0.779,\ 0.623,\ 0.068,\ 0.063,\ 0.060: three clear values, then the floor.
M2. The PCA “spectra” have positive and negative lobes and are orthogonal to each other; the abundance maps (scores) have negative regions (red). They describe the directions of spread of the cloud, not its corners: real spectra are non-negative and overlap.
M3. Each step picks one corner of the cloud; the squares land on the true circles. Angles to the true spectra: 4.5°, 3.8° and 2.6°. Relative fit error 0.118, against 0.120 with the true spectra.
M4. The cloud forms three clusters well inside the true triangle (every pixel is at least 30 % mixed). Vertex finding picks the extreme pixels, which are mixtures: angles 10.7°, 6.9° and 9.9°. The found spectra are too broad (each contains some of the others). Yet the fit error is the same as with the true spectra (0.153 for both): the cloud lies inside both cones, so the fit cannot tell you that the spectra are wrong.
M5. Without pure pixels: seed 1 gives \|Y - WH\|^2 = 3.195 and angles 4.5°, 3.2°, 9.1°; seed 2 gives 3.201 and 10.2°, 7.4°, 8.9°. The true spectra (with NNLS abundances) give 3.199: all three explain the data equally well, the answers differ. With pure pixels both seeds give the same result, 2.1°, 1.7°, 0.4°, error 3.140: the corners pin the cone down.
M6. The three known spectra are kept exactly (0°); the fourth is about 28° from the true autofluorescence. It follows the broad shape but has dips where the known dyes peak: the true broad spectrum minus a bit of each known spectrum is still non-negative, and the abundances of the known dyes absorb the difference. Both explain the data, so the data cannot decide: knowing some spectra reduces the non-uniqueness but does not remove it for a broad spectrum.
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/endmembers/exercise.rs. (C1, C2, … on this page are the questions of the cloud demo, so the code tasks are R1, R2, …) Spectra are the columns of M; in the pixel matrix Y each pixel is a column.
R1. nnls(m, y): non-negative least squares for a few components, by trying every guess of which components are not zero:
best = all zero
for every subset of the components:
a = least squares with only those columns, the others 0
if all of a ≥ 0 and ‖y − M a‖ is smaller than best's: best = a
With K components that is 2^K small problems: fine for 3 or 4 (the active-set method of the lecture visits only a few of them). The checks are B1(c) and B7.
R2. fractions(p, e): the barycentric coordinates of a point in a triangle (solve a_1(\mathbf e_1 - \mathbf e_0) + a_2(\mathbf e_2 - \mathbf e_0) = \mathbf p - \mathbf e_0, then a_0 = 1 - a_1 - a_2). The check is B2.
R3. singular_values(y): the square roots of the eigenvalues of YY^\top (no centring). The checks: a small matrix with known values, and pixels made of two spectra give two values above 0.
R4. find_vertices(pixels, k): vertex finding:
directions = empty
repeat k times:
for every pixel: rest = pixel minus its parts along the directions
pick the pixel with the longest rest
add rest / length(rest) to the directions
The check: among mixtures of three spectra, it picks the three pure pixels.
R5. update_h(y, w, h) and update_w(y, w, h): one multiplicative NMF update each, H \leftarrow H \odot (W^\top Y) \oslash (W^\top W H) and W \leftarrow W \odot (YH^\top) \oslash (WHH^\top). The checks are B8, and that alternating the two never raises the error.
Then cargo run -p unmixing -- endmembers and open the pictures in out/endmembers/. The program records a synthetic cell (nucleus, mitochondria, membrane, a dim diffuse autofluorescence) in 16 channels with photon noise and prints the singular values of all its pixels. Then, with the first three components only, it normalises the bright pixels to sum 1, removes the noise outside the three-dimensional signal space, and runs vertex finding, with pure pixels and without (every pixel 30 % mixed). vertices.png shows the pixels in the triangle of the true spectra (open circles) and the found corners (orange). Without pure pixels it also runs NMF (400 iterations) from two seeds. Last, it unmixes a dim (40-photon) version with least squares and with NNLS (mito-ls-vs-nnls.png: the mitochondria maps, 0 = grey).
R6. How many components do the singular values show? Is the fourth one clearly above the floor?
R7. How far (in degrees) are the vertex-finding spectra from the true ones, with and without pure pixels? Where are the found corners in vertices.png in each case?
R8. Compare the relative errors of NMF from the two seeds and of the true spectra, and the angles. What do you conclude? What share of the least-squares abundances is negative at 40 photons, and what does the NNLS map look like where there are no mitochondria?
The model solution is unmixing/src/endmembers/solution.rs in the download.
R6. Divided by the largest: 1.000, 0.815, 0.548, 0.080, then 0.064, 0.059, 0.057. Three values stand out, then the fourth (the dim autofluorescence) at 0.080, only a little above a flat floor of about 0.06. Four components, but the fourth is easy to miss (B4).
R7. With pure pixels: 2.1°, 3.7° and 2.4°. The cloud sits mostly at the three corners (the structures do not touch, so there are few mixtures), and the found corners lie on the true ones, slightly outside because of the noise that remains. Without pure pixels: 20.8°, 19.3° and 19.5°. The cloud is three clusters well inside the triangle, and the found corners are their most extreme pixels, mixtures themselves: vertex finding undershoots (B6).
R8. The true spectra (with NNLS abundances) give a relative error of 0.01831; NMF gives 0.01828 from both seeds. All three fit the data equally well, yet the NMF spectra are 13° to 18° from the true ones and differ between the seeds: without pure pixels the factorisation is not unique, and the fit cannot tell which answer is right (B9). At 40 photons least squares makes 26.8 % of the abundances negative. NNLS makes none: outside the mitochondria its map is 0 or a small positive value, the clipped noise (the positive bias of the slide “Sum to one, and the price of constraints”).
Where this leads
The same geometry works for brightfield stains: in optical density the pixels of an H&E slide fill a flat wedge spanned by two stain vectors, and Macenko’s method finds them as the two extreme directions. The next unit, stain separation, does exactly that. The simplex returns on the FLIM day: on the phasor plot (day 50) mixtures of lifetimes lie in a triangle, and the fractions are barycentric coordinates.