Exercise: sparsity and the ℓ1 penalty

Goal

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

  • tell sparse from compressible signals, pick a basis that makes a signal sparse, and count how many coefficients carry most of the energy,
  • shrink coefficients by hand with the ℓ2, ℓ1 and ℓ0 penalties and explain the difference,
  • find the ℓ1 and ℓ2 solutions of a two-coefficient problem by drawing,
  • explain λ_max, the lasso path and shrinkage bias,
  • (math) derive the lasso optimality conditions, λ_max and the MAP reading of ℓ1.

Background

Idea What it means
Sparse few nonzero coefficients in some basis
Compressible sorted magnitudes fall fast; the K largest carry almost all the energy
ℓ2 penalty (λ/2)x² every coefficient shrinks: c = w/(1 + λ) in an orthonormal basis
ℓ1 penalty λ|x| soft threshold at λ: small coefficients become exactly 0, the others lose λ
ℓ0 penalty (λ²/2) per nonzero hard threshold at λ: keep or kill
Lasso \min \tfrac12\|A\mathbf x - \mathbf y\|^2 + λ\|\mathbf x\|_1 with a general matrix A
λ_max \max_j\|\mathbf a_j^\top\mathbf y\|: above it the lasso solution is 0
Shrinkage bias ℓ1 pulls surviving coefficients towards 0; fix by refitting the support
TV ℓ1 of the differences of the image (analysis sparsity)

By hand

A1. Shrink w = (4,\ -0.5,\ 1.2,\ -2.5,\ 0.3,\ 3) with λ = 1 (orthonormal basis): (a) with ℓ2 (c = w/(1 + λ)), (b) with ℓ1 (soft threshold at 1), (c) with ℓ0 (hard threshold at 1). (d) Count the zeros in each. Which coefficient loses most with ℓ2, and with ℓ1? (e) The energy \sum w^2 is 33.0. What is the energy of the ℓ2 answer?

A2. Sorted coefficient magnitudes of a signal: 10, 6, 3, 2, 1, 0.5, 0.3, 0.2 (all others 0). (a) Is the signal sparse, compressible, or both? (b) How many coefficients are needed for 95 % of the energy? For 99 %?

A3. The budget picture. Two coefficients, round misfit contours, least-squares point (3, 1). (a) With the budget \|\mathbf x\|_2 \le 2 the answer is the point of the disc closest to (3, 1). Compute it. (b) Draw the ℓ1 ball |x_1| + |x_2| \le 2 and the circles around (3, 1). Which point of the diamond is closest? Check by computing the distance from (3, 1) to (2, 0) and to the nearest point of the edge x_1 + x_2 = 2. (c) For which budget would the ℓ1 answer have two nonzeros?

A4. A = I (2 × 2), \mathbf y = (3, 1). (a) What is λ_max? (b) Give the lasso solution for λ = 0.5, 1, 2, 3. (c) Give the ridge solution for λ = 1. Does ridge ever give an exact zero here?

A5. Match. A noisy signal with a few tall spikes and a smooth background is restored with four penalties. Match each description to ℓ2 (in the pixel basis), ℓ1 (pixel basis), ℓ0 (pixel basis) and TV: (i) the spikes keep their height, the noise between them is gone, but a few noise samples above the threshold survive at full height; (ii) everything is smaller, the noise is lower but still everywhere; (iii) the spikes are lower by the same amount, the noise between them is exactly 0; (iv) the result is piecewise constant: flat plateaus with jumps.

A6. Which basis (pixels, DCT/Fourier, Haar, smooth wavelets such as db4) makes each signal sparse? (a) A few isolated bright pixels on a dark background (spots). (b) A sum of three slow oscillations. (c) A cartoon: flat regions separated by sharp steps. (d) A smooth profile with two jumps.

A7. Counting supports. Minimising ℓ0 exactly means trying supports. (a) How many supports with 8 nonzeros are there among 256 unknowns? (b) At 10^9 least-squares fits per second, how long does trying all of them take?

A8. Four spikes of heights (3, −2.5, 2, 1.2) with noise σ = 0.3 are soft-thresholded at λ = 1 (pixel basis). The results at the spike positions are (2.01, −1.32, 0.82, 0.65); no other sample survives. (a) Explain why the four values are too small. (b) What does debiasing give here, and why does the debiased value at the last spike (1.65) differ from 1.2?

A1. (a) (2, −0.25, 0.6, −1.25, 0.15, 1.5). (b) (3, 0, 0.2, −1.5, 0, 2). (c) (4, 0, 1.2, −2.5, 0, 3). (d) ℓ2: 0 zeros; ℓ1: 2; ℓ0: 2. With ℓ2 the largest coefficient (4) loses most (2); with ℓ1 every surviving coefficient loses exactly 1, and the small ones (−0.5, 0.3) lose all they have. (e) 33.03/4 = 8.26: halving every coefficient divides the energy by 4.

A2. (a) Both: exactly sparse (8 nonzeros), and also compressible, because the sorted values fall fast. (b) Energies 100, 36, 9, 4, 1, 0.25, 0.09, 0.04; total 150.38. Cumulative: 66.5 %, 90.4 %, 96.4 %, 99.1 %. So 3 coefficients for 95 %, 4 for 99 %.

A3. (a) Along the line to the origin: 2 \cdot (3, 1)/\sqrt{10} = (1.90, 0.63). (b) The corner (2, 0), at distance \sqrt{1 + 1} = 1.41. On the edge x_1 + x_2 = 2 the closest point to (3, 1) is the foot of the perpendicular, (3, 1) - \tfrac{(3 + 1 - 2)}{2}(1, 1) = (2, 0): the corner itself. Any other point of the diamond is further. (c) The projection is the soft threshold of (3, 1) at θ with (3 - θ) + (1 - θ) = s, i.e. θ = (4 − s)/2; the second coordinate stays nonzero when θ < 1, i.e. for budgets s > 2 (and s < 4, beyond which (3, 1) itself is inside the ball).

A4. (a) λ_max = \max(|3|, |1|) = 3. (b) Soft threshold of (3, 1): λ = 0.5: (2.5, 0.5); λ = 1: (2, 0); λ = 2: (1, 0); λ = 3: (0, 0). (c) \mathbf y/(1 + λ) = (1.5, 0.5). Ridge gives x_j = y_j/(1 + λ), which is 0 only if y_j = 0: never here.

A5. (i) ℓ0 (keep or kill: survivors unchanged, including noise samples above the threshold). (ii) ℓ2 (uniform shrinkage, no zeros). (iii) ℓ1 (soft threshold). (iv) TV (sparse differences: piecewise constant).

A6. (a) Pixels. (b) DCT/Fourier. (c) Haar. (d) Smooth wavelets (db4): smooth parts give tiny details, each jump only a few large ones at every level.

A7. (a) \binom{256}{8} = 4.1 \cdot 10^{14}. (b) 4.1 \cdot 10^5 s, about 4.7 days. For 16 nonzeros among 1024 unknowns it would be about 10^{34} supports: hopeless.

A8. (a) Soft thresholding subtracts λ = 1 from every surviving magnitude (shrinkage bias). The values differ from “true − 1” by the noise at each spike. (b) Debiasing keeps the support and refits it by least squares; with A = I that means taking the noisy values themselves: (3.01, −2.32, 1.82, 1.65). The bias is gone, but each value still carries the noise of its sample (+0.45 at the last spike).

Math questions

A9. (math) Derive the lasso optimality conditions from the subgradient of f(\mathbf x) = \tfrac12\|A\mathbf x - \mathbf y\|^2 + λ\|\mathbf x\|_1, and show that \mathbf x^\star = 0 is optimal iff λ ≥ \|A^\top\mathbf y\|_\infty.

A10. (math) (a) Write the ridge solution in closed form. (b) For A = I, when is a component exactly 0? (c) Explain why, for data with continuous noise, ridge gives an exact zero with probability 0, while the lasso gives exact zeros with positive probability.

A11. (math) Gaussian noise of standard deviation σ and an independent Laplace prior p(x_j) = \frac{1}{2b}e^{-|x_j|/b}. Show that the MAP estimate is the lasso with λ = σ²/b. What is λ for σ = 10 grey levels and b = 5?

A12. (math) Show that for A = I the lasso optimality conditions give the soft threshold, and that the ℓ0 problem with cost μ per nonzero gives the hard threshold at \sqrt{2μ}.

A9. The smooth part has gradient A^\top(A\mathbf x - \mathbf y) = -A^\top\mathbf r. The subgradient of |x_j| is \{\mathrm{sign}(x_j)\} for x_j \ne 0 and [-1, 1] for x_j = 0. Optimality: 0 \in -\mathbf a_j^\top\mathbf r + λ\,\partial|x_j| for each j, i.e. \mathbf a_j^\top\mathbf r = λ\,\mathrm{sign}(x_j) if x_j \ne 0 and |\mathbf a_j^\top\mathbf r| \le λ if x_j = 0. At \mathbf x = 0, \mathbf r = \mathbf y and all components are 0, so the condition is |\mathbf a_j^\top\mathbf y| \le λ for all j: λ ≥ \max_j|\mathbf a_j^\top\mathbf y| = \|A^\top\mathbf y\|_\infty. The objective is convex, so these conditions are also sufficient.

A10. (a) Setting the gradient of \tfrac12\|A\mathbf x - \mathbf y\|^2 + \frac{λ}{2}\|\mathbf x\|^2 to 0: (A^\top A + λI)\mathbf x = A^\top\mathbf y, so \mathbf x = (A^\top A + λI)^{-1}A^\top\mathbf y. (b) x_j = y_j/(1 + λ): zero iff y_j = 0. (c) Each component of the ridge solution is a fixed linear combination \mathbf m_j^\top\mathbf y of the data; it is 0 only on the hyperplane \mathbf m_j^\top\mathbf y = 0, which has probability 0 under continuous noise. The lasso component is 0 on the whole set |\mathbf a_j^\top\mathbf r| \le λ, a region of positive volume (for A = I: the interval |y_j| \le λ).

A11. p(\mathbf x\mid\mathbf y) \propto p(\mathbf y\mid\mathbf x)p(\mathbf x) \propto e^{-\|A\mathbf x - \mathbf y\|^2/2σ^2}\prod_j e^{-|x_j|/b}. Taking −log: $|Ax - y|^2 + 1b|x|_1 + $ const. Multiplying by σ² does not move the minimiser: \tfrac12\|A\mathbf x - \mathbf y\|^2 + \frac{σ^2}{b}\|\mathbf x\|_1, the lasso with λ = σ²/b. For σ = 10 and b = 5: λ = 100/5 = 20 (grey levels).

A12. With A = I, \mathbf r = \mathbf y - \mathbf x. For x_j \ne 0: y_j - x_j = λ\,\mathrm{sign}(x_j), so x_j = y_j - λ\,\mathrm{sign}(y_j), consistent only if |y_j| > λ. For x_j = 0: |y_j| \le λ. Together: the soft threshold. ℓ0: compare c = 0 (cost \tfrac12 w^2) with the best nonzero c = w (cost μ); keep iff |w| > \sqrt{2μ}.

Explore

The coefficient plane of a two-unknown problem. The misfit ellipses come from Q = A^\top A = \begin{pmatrix}1 & ρ\\ ρ & 1\end{pmatrix} (ρ: the correlation of the two columns; ρ = 0 gives round contours) around the least-squares point, which you can drag. The penalty ball through the solution, the touching ellipse (black) and the path of the solution as λ grows (dashed or dotted) are drawn. Right: the scalar transfer curves at the current λ, or (tab “path”) the coefficients of a 6-unknown problem against λ for ℓ1 and ℓ2.

E1. Preset round contours: ℓ1 = soft threshold. Read the solution and λ_max. Set λ to 0.5 and to 1.5. Compare with A4: is every coordinate soft-thresholded on its own?

E2. Preset the corner wins (ρ = 0.3, λ = 2). Read the ℓ1 solution, then switch to ℓ2. Now lower λ to 1 with ℓ1: how many zeros? Which λ_max does the readout give, and why is it not 3 any more?

E3. Preset correlated columns: ℓ1 picks one (ρ = 0.9). Compare the ℓ1 and ℓ2 solutions. In which direction are the ellipses long, and why does that make the choice between the two coefficients “cheap” for the data?

E4. Round contours, least-squares point (3, 1). Compare ℓ0 and ℓ1 at λ = 1.5. Then lower λ to 0.9 with ℓ0: what happens to the second coordinate? Explain with the transfer curves.

E5. Preset p < 1: pointier, but two valleys. Read the ℓ½ solution and compare with ℓ1. Now drag the least-squares point slowly across the diagonal (from (2, 1.8) to (1.8, 2)). What happens to the ℓ½ solution, and what does this say about solving non-convex problems?

E6. Preset the path: one at a time. In the 6-coefficient problem, which coefficient enters first as λ falls from 4? How many are nonzero at λ = 2, 1 and 0.2 (move the λ slider, read the bottom line)? How many does ridge have?

E1. (2, 0), λ_max = 3. λ = 0.5: (2.5, 0.5); λ = 1.5: (1.5, 0). Yes: with round contours (A orthonormal) each coordinate is soft-thresholded on its own, exactly as in A4.

E2. ℓ1: (1.30, 0); ℓ2: (1.05, 0.53). At λ = 1, ℓ1 gives (2.23, 0.23): no zeros (the budget is now large enough to reach the edge of the diamond, not its corner). λ_max = 3.30: it is \|Q\mathbf b\|_\infty = \max(3 + 0.3 \cdot 1,\ 0.3 \cdot 3 + 1), the largest correlation of a column with the data, which depends on ρ.

E3. ℓ1: (1.06, 0); ℓ2: (0.80, 0.51). The ellipses are long along the direction (1, −1): moving weight from one coefficient to the other changes the fit very little, because the two columns are almost the same. ℓ1 uses this freedom to put everything on one coefficient (a corner); ℓ2 prefers to share.

E4. At λ = 1.5: ℓ0 gives (3, 0), ℓ1 (1.5, 0). Both kill the second coordinate (|1| < 1.5), but ℓ0 keeps the first at full size while ℓ1 shrinks it by 1.5. At λ = 0.9 the ℓ0 solution jumps to (3, 1): the hard threshold keeps any coefficient above λ unchanged, so the second coordinate (1 > 0.9) suddenly appears at full size.

E5. ℓ½: (2.75, 0); ℓ1: (1.31, 1.11). Dragging the point across the diagonal makes the ℓ½ solution jump from the x_1 axis to the x_2 axis, (0, 2.75) at (1.8, 2); exactly on the diagonal, at (1.9, 1.9), a third valley between the axes, (1.63, 1.63), wins. The objective has several valleys; a small change of the data can switch the winner, and a local solver can get stuck in the wrong valley. That is why convex ℓ1 is preferred.

E6. The fourth coefficient (true value 1) enters first, just below λ = 4 (at 3.99); then the third (2.75) and the first (2.44). Nonzero: 3 at λ = 2, 4 at λ = 1 (the sixth enters at 1.29), 4 at λ = 0.2. The second coefficient, whose column is strongly correlated with the first, never enters. Ridge has all 6 nonzero at every λ.

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 sparse/src/sparsity/exercise.rs.

C1. shrink_l2(w, λ), soft(w, λ) and hard(w, λ): the answers of the three penalties for one coefficient in an orthonormal basis:

ℓ2:  c = w / (1 + λ)
ℓ1:  if w > λ:  c = w − λ      if w < −λ:  c = w + λ      otherwise c = 0
ℓ0:  if |w| > λ:  c = w        otherwise c = 0

The checks are your answers to A1 (a) to (c) and the energy of A1 (e).

C2. count_for_energy(coeffs, fraction): how many coefficients carry a given share of the energy:

e = the squares of the coefficients, sorted from large to small
total = sum of e
sum = 0
for k = 1, 2, ...:
    sum = sum + e[k]
    if sum ≥ fraction · total: return k

The check is A2 (b): 3 coefficients for 95 %, 4 for 99 %.

C3. lambda_max(a, y): the largest correlation |\mathbf a_j^\top\mathbf y| of a column of A with the data. The checks are A4 (a), λ_max = 3, and demo question E2, λ_max = 3.30 for correlated columns.

C4. project_l1_ball(v, budget): the point of the ℓ1 ball \|\mathbf x\|_1 \le s closest to \mathbf v (the budget picture). Outside the ball it is a soft threshold at the level θ that spends exactly the budget:

if |v_1| + |v_2| + ... ≤ s: return v
m = the magnitudes |v_i|, sorted from large to small
θ = 0
for k = 1, 2, ...:
    t = (m_1 + ... + m_k − s) / k        # if the k largest survive, each loses t
    if m_k > t: θ = t  else: stop
return soft(v_i, θ) for every i

The checks are A3: budget 2 gives the corner (2, 0), budget 3 gives (2.5, 0.5).

Then cargo run -p sparse -- sparsity and open the pictures in out/sparsity/. The program measures how sparse the micrograph is in the 2D Haar basis (4 levels, mean removed), then adds noise of σ = 20 grey levels and shrinks the Haar detail coefficients with each penalty, trying a range of λ and keeping the best one. zoom-*.png show a detail enlarged 3 times.

C5. Read how many of the 65 536 Haar coefficients carry 90 %, 99 % and 99.9 % of the energy. Is the micrograph sparse, compressible, or neither? Compare with the signal of A2.

C6. Compare zoom-l2.png, zoom-l1.png and zoom-l0.png with zoom-noisy.png, and read the best PSNR and the share of exact zeros of each penalty. Which descriptions of A5 do you recognise?

The model solution is sparse/src/sparsity/solution.rs in the download.

C5. 6643 coefficients (10.1 %) for 90 %, 28 466 (43.4 %) for 99 % and 44 334 (67.6 %) for 99.9 %. The micrograph is not sparse: no coefficient is exactly 0. It is compressible only in a weak sense: a tenth of the coefficients carries 90 % of the energy, but the last percent is spread over almost half of them, because the fine texture of the cell is not sparse in Haar. The signal of A2 needed 4 of 8 coefficients for 99 %.

C6. The noisy image has 22.13 dB. ℓ2 (best λ = 0.25): 23.16 dB and not a single zero; the noise is weaker but everywhere (A5 (ii)). ℓ1 (best λ = 14 grey levels): 23.85 dB, the best of the three, with 35.2 % of the coefficients exactly 0; flat regions are clean, the texture is smoothed, and faint square Haar blocks appear (A5 (iii)). ℓ0 (best λ = 31 grey levels): only 22.21 dB, although 66.1 % of the coefficients are 0; the survivors keep their full size, including the noise coefficients just above the threshold, which show up as isolated square blocks (A5 (i)).

Where this leads

The two-unknown picture has an exact answer. With thousands of unknowns, a blur and a mask, we need an iterative solver: the next unit, ISTA (solving with ℓ1) (“a gradient step, then a soft threshold”) and orthogonal matching pursuit. After that, ℓ1 lets us measure less than we want to know: compressed sensing and fast MRI.