Exercise: regularisation
Before this unit: Linear deconvolution: inverse, Wiener, Tikhonov (previous unit); Day 4: Image formation; Day 3: Bit depth and noise.
Goal
By the end of this exercise you should be able to:
- write a restoration as data term + λ · penalty, and say what each penalty keeps and destroys,
- compute filter factors for Tikhonov and for stopping an iteration early,
- choose λ with the discrepancy principle and the L-curve, and say when they disagree,
- derive the filter factors from the SVD and λ from the MAP reading (math).
Do the pen-and-paper part first, then the demo. Questions marked (math) are for those who want the derivations; the concept questions do not depend on them.
Background
| Idea | What it means |
|---|---|
| Restoration | minimise \lVert A x - y\rVert^2 + \lambda\cdot\text{penalty}(x) |
| Penalties | energy \lVert x\rVert^2; roughness \lVert D^2 x\rVert^2; total variation \sum\lvert x_{i+1} - x_i\rvert; constraint x \ge 0 |
| Filter factor | fraction φ kept of a frequency (singular direction) with blur gain s; Tikhonov s^2/(s^2 + \lambda) |
| Landweber | x \leftarrow x + A^\top(y - Ax) from x = 0; after k steps \varphi = 1 - (1 - s^2)^k |
| Discrepancy principle | choose λ with RMS residual = σn |
| L-curve | log penalty against log residual norm for many λ; choose the corner |
| MAP | λ = σn² / τ² (noise variance / prior variance) |
By hand
B1. Tikhonov filter factors \varphi = s^2/(s^2 + \lambda). Fill in the table for s = 1, 0.5, 0.1 and λ = 0.01, 0.1. Which frequencies are restored, which suppressed? What happens to the noise in the s = 0.1 component, which the inverse multiplies by 1/s = 10?
B2. Landweber filter factors 1 - (1 - s^2)^k. (a) Compute them for s = 0.5 and k = 1, 4, 16, and for s = 0.1 and k = 16, 100. (b) How many iterations does the s = 0.1 component need to be half restored? (c) Explain with these numbers why stopping early acts like a larger λ.
B3. The demo’s step-and-ramp profile has noise σn = 0.02. Restoring with the roughness penalty gives these RMS residuals:
| λ | 10⁻⁶ | 10⁻⁴ | 10⁻³ | 0.01 | 0.1 | 1 | 10 |
|---|---|---|---|---|---|---|---|
| RMS residual | 0.0151 | 0.0163 | 0.0168 | 0.0176 | 0.0180 | 0.0189 | 0.0231 |
- Which λ does the discrepancy principle choose (interpolate linearly in \log\lambda)?
- At λ = 10⁻⁶ the residual is smaller than σn. What does that tell you about the estimate?
B4. The same restoration gives residual norm ρ and penalty η:
| λ | 10⁻⁶ | 10⁻⁴ | 0.01 | 1 | 100 |
|---|---|---|---|---|---|
| ρ | 0.241 | 0.260 | 0.281 | 0.303 | 0.561 |
| η | 47.7 | 4.53 | 0.480 | 0.144 | 0.048 |
Sketch the L-curve (log η against log ρ). Where are the vertical and the horizontal branches, and roughly which λ is the corner?
B5. Match the four restored step-and-ramp profiles to the penalties energy, roughness, total variation, roughness + non-negativity: (A) the step is sharp, the ramp is a staircase; (B) smooth and right in shape, but the whole profile is lower than the data; (C) smooth, with dips below zero next to the steps; (D) smooth, nothing below zero.
B6. The noise in your images doubles. (a) Does the discrepancy λ grow or shrink? (b) For an iterative method, should you stop earlier or later? (c) The noise is now correlated between neighbouring pixels (the camera smooths it). Why may the discrepancy principle choose a much too large λ?
B1.
| s | λ = 0.01 | λ = 0.1 |
|---|---|---|
| 1 | 0.990 | 0.909 |
| 0.5 | 0.962 | 0.714 |
| 0.1 | 0.500 | 0.091 |
Strong frequencies (s near 1) are kept almost entirely; the weak s = 0.1 is kept half with λ = 0.01 and almost removed with λ = 0.1. Its noise, amplified 10 times by the inverse, is amplified only 0.5 \times 10 = 5 or 0.091 \times 10 = 0.91 times.
B2. (a) s = 0.5: 1 - 0.75^k = 0.25, 0.684, 0.990. s = 0.1: 1 - 0.99^{16} = 0.149, 1 - 0.99^{100} = 0.634. (b) 0.99^k = 0.5: k = \ln 0.5/\ln 0.99 = 69 iterations. (c) After 16 iterations the strong component is restored (0.99) but the weak one only to 0.15, as if a large λ suppressed it. As k grows the weak components (and their noise) are filled in, as if λ were lowered: the roll-off moves to smaller s, around s^2 \approx 1/k.
B3. (a) σn = 0.02 lies between 0.0189 (λ = 1) and 0.0231 (λ = 10): \log_{10}\lambda = (0.02 - 0.0189)/(0.0231 - 0.0189) = 0.26, λ ≈ 1.8. (The demo, with a finer grid, gives 2.57.) (b) The estimate explains part of the noise: it is over-fitted, with wild oscillations (its error against the truth is 2.29, far worse than the data).
B4. From λ = 10⁻⁶ to 10⁻⁴ to 0.01, η falls by a factor 10 each time while ρ grows only from 0.241 to 0.281: a steep, nearly vertical branch. From λ = 1 to 100, ρ almost doubles (0.303 to 0.561) while η falls only by 3: a flat, horizontal branch. The corner lies around λ = 0.01 to 1. (The demo’s corner is λ = 0.18.)
B5. (A) total variation, (B) energy (it shrinks everything towards 0, including the mean), (C) roughness (smooth, but nothing stops the undershoots), (D) roughness + non-negativity.
B6. (a) It grows: the target residual σn is larger, and the residual grows with λ. (b) Earlier: the noise appears in the iterates sooner. (c) Smoothed noise has little power at high frequencies, so the estimate can only explain it at low frequencies: the residual cannot reach σn until the estimate is very smooth. The discrepancy principle assumes white noise. (In the demo: λ = 11.7 instead of the best 0.32.)
Math questions
B7. (math) With the SVD A = U\Sigma V^\top, show that the Tikhonov solution of \min\|A\mathbf x - \mathbf y\|^2 + \lambda\|\mathbf x\|^2 is \hat{\mathbf x} = \sum_i \frac{s_i^2}{s_i^2 + \lambda}\frac{\mathbf u_i^\top\mathbf y}{s_i}\mathbf v_i. With \mathbf y = A\mathbf x + \mathbf n, what noise does term i carry?
B8. (math) Show that the squared residual \|A\hat{\mathbf x}_\lambda - \mathbf y\|^2 of B7 increases with λ (so the discrepancy equation has one solution).
B9. (math) Gaussian noise of variance σn² and a Gaussian prior D\mathbf x \sim \mathcal N(0, \tau^2 I): show that the most probable \mathbf x given \mathbf y is Tikhonov’s with λ = σn²/τ².
B10. (math) Show that for small s^2, Landweber after k steps keeps about the same fraction as Tikhonov with λ = 1/k: compare 1 - e^{-ks^2} with s^2/(s^2 + 1/k) at s^2 = 1/k and for s^2 \ll 1/k and s^2 \gg 1/k.
B7. The normal equations (A^\top A + \lambda I)\mathbf x = A^\top\mathbf y become, with A^\top A = V\Sigma^2V^\top and A^\top\mathbf y = V\Sigma U^\top\mathbf y, and \mathbf x = V\mathbf z: (s_i^2 + \lambda)z_i = s_i\,\mathbf u_i^\top\mathbf y. So z_i = \frac{s_i}{s_i^2 + \lambda}\mathbf u_i^\top\mathbf y = \frac{s_i^2}{s_i^2 + \lambda}\frac{\mathbf u_i^\top\mathbf y}{s_i}. With \mathbf u_i^\top\mathbf y = s_i x_i + n_i, term i is \varphi_i x_i + \varphi_i n_i/s_i: the noise n_i is multiplied by \varphi_i/s_i = s_i/(s_i^2 + \lambda) \le 1/(2\sqrt\lambda), bounded however small s_i is.
B8. A\hat{\mathbf x} - \mathbf y = \sum_i (\varphi_i - 1)(\mathbf u_i^\top\mathbf y)\mathbf u_i plus the part of \mathbf y outside the range of A (independent of λ). So \|A\hat{\mathbf x} - \mathbf y\|^2 = \sum_i\big(\frac{\lambda}{s_i^2 + \lambda}\big)^2(\mathbf u_i^\top\mathbf y)^2 + \text{const}, and each \frac{\lambda}{s_i^2 + \lambda} increases with λ.
B9. p(\mathbf x\mid\mathbf y) \propto p(\mathbf y\mid\mathbf x)\,p(\mathbf x) \propto \exp\!\big(-\|A\mathbf x - \mathbf y\|^2/2\sigma_n^2\big)\exp\!\big(-\|D\mathbf x\|^2/2\tau^2\big). Maximising it = minimising \frac{1}{2\sigma_n^2}\|A\mathbf x - \mathbf y\|^2 + \frac{1}{2\tau^2}\|D\mathbf x\|^2; multiply by 2\sigma_n^2: \|A\mathbf x - \mathbf y\|^2 + \frac{\sigma_n^2}{\tau^2}\|D\mathbf x\|^2.
B10. (1 - s^2)^k = e^{k\ln(1 - s^2)} \approx e^{-ks^2} for small s^2. At s^2 = 1/k: Landweber 1 - e^{-1} = 0.63, Tikhonov 0.5. For s^2 \ll 1/k both are \approx ks^2; for s^2 \gg 1/k both are \approx 1. The two curves have the same position and similar shape (Landweber’s step is a little sharper).
Explore
A 1D profile (256 samples) is blurred with a Gaussian (σ = 3 samples) and noise σn = 0.02 is added (fixed seed). Top left: truth (dashed), data (thin blue), estimate (red). Top right: the residual (data − blurred estimate) with the ±σn band, or (choose show) the filter factors with the blur’s |H|. Bottom left: the L-curve (penalty against residual norm, log–log) with the current λ (red), the corner (orange) and the discrepancy λ (green). Bottom right: error against the truth (orange) and RMS residual (blue) for 37 values of λ, with σn dashed; vertical lines: current, discrepancy and corner. Restore by switches between λ and stopping Landweber after k iterations. Total variation and non-negativity are computed iteratively; their curves fill in after a moment.
E1. Presets λ too small and λ too large. For each, give the error and the residual from the readout, and compare the residual with σn. Look at the residual panel: where does the residual of the large λ show structure?
E2. Presets discrepancy principle and L-curve corner. Which λ does each choose, with which error? Which λ is best on the path, and how much better is it?
E3. Preset the two rules disagree (correlated noise). Read the discrepancy λ, the corner λ and the best λ with their errors. Which rule is closer to the best, and why does the other fail?
E4. Presets roughness keeps the mean, energy shrinks it, TV keeps the step, staircases the ramp, TV shrinks the narrow peak and non-negativity removes the dips. For each, write one sentence on what the penalty kept and what it destroyed, with a number from the readout or the plot.
E5. Preset iterations as λ: semi-convergence. At which k is the error smallest? Give the error at k = 100, 1000 and 10 000. Which k does the discrepancy rule choose? Watch the filter factors while you move k.
E6. Set the penalty to energy (profile step and ramp, no correlated noise). Compare the discrepancy λ, the corner λ and the best λ, with their errors. Which rule does well here?
E1. λ = 10⁻⁶: error 2.29, residual 0.0151, below σn = 0.02: the estimate fits the noise and oscillates wildly. λ = 100: error 0.0767, residual 0.0351, above σn; the residual shows bumps at the step edges and at the top and foot of the ramp: image structure the over-smooth estimate failed to explain.
E2. Discrepancy: λ = 2.57, error 0.0588. Corner: λ = 0.178, error 0.0539. Best on the path: λ = 0.1, error 0.0537. Both rules land near the best error; the corner is almost optimal, the discrepancy principle slightly over-smooths (9 % more error).
E3. Discrepancy λ = 11.7 (error 0.0651), corner λ = 0.056 (error 0.0611), best λ = 0.316 (error 0.0586). The corner is closer here. The discrepancy principle assumes white noise; correlated noise has little high-frequency power, so the residual stays below σn until the estimate is very smooth.
E4. Energy (λ = 0.3) shrinks the whole profile: mean 0.256 against 0.331 in the truth (factor 1/1.3); roughness keeps the mean (0.333). TV (λ = 0.01) keeps the step sharp and flat at 0.80 but turns the ramp into small flat steps; its error 0.0219 is far below the best roughness error (0.0537), because this profile is mostly flat pieces. TV on the narrow peak (λ = 0.02): the peak of height 1.0 comes out at 0.31; TV pays for every jump, and a narrow spot has two jumps for little area. Non-negativity: roughness with λ = 10⁻⁴ dips to −1.16 (error 0.323); adding x ≥ 0 removes the dips (minimum 0) and the error falls to 0.109.
E5. Smallest error at k = 32 (0.0542). At k = 100: 0.0586, at 1000: 0.143, at 10 000: 0.375 (semi-convergence). The residual keeps falling all the time. The discrepancy rule stops at k = 6: early, a little blurred, but safe. The filter-factor step moves from high |H| (low frequencies) towards low |H| as k grows.
E6. Discrepancy λ = 0.0189 (error 0.0552), corner λ = 0.0056 (error 0.0635), best λ = 0.0178 (error 0.0553). Here the discrepancy principle is almost exactly right and the corner too far to the left: the rules have no guarantee, and checking with a known-truth test is the honest way.
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 deconvolution/src/regularise/exercise.rs. Everything is 1D, as in the demo: a profile of n samples, the blur an n \times n matrix A. The penalty matrix D (the identity, or one row (1, -2, 1) per second difference) is given.
C1. blur_matrix(n, sigma): row i holds the Gaussian weights e^{-(i - j)^2/2\sigma^2} for |i - j| \le \lceil 3\sigma\rceil, divided by their sum.
C2. tikhonov(a, y, lambda, penalty): the minimiser of \|A\mathbf x - \mathbf y\|^2 + \lambda\|D\mathbf x\|^2, from the normal equations (the slide “The math: filter factors”):
M = AᵀA + λ DᵀD
b = Aᵀy
solve M x = b
The checks: the two-pixel blur of the deconvolution unit (B3 there: (1.04, 0.33) for \lambda = 0.1), and the roughness penalty returns a ramp unchanged for every \lambda.
C3. tikhonov_factor(s, lambda) and landweber_factor(s, k): the filter factors s^2/(s^2 + \lambda) and 1 - (1 - s^2)^k. The checks are your tables of B1 and B2.
C4. landweber(a, y, k, omega): k steps from zero.
x = 0
repeat k times:
r = y − A x # what the estimate does not explain yet
x = x + ω · Aᵀ r # blur the difference back, add it
The check: on a diagonal blur with gains 1, 0.5 and 0.1, each component after k steps is its filter factor.
C5. rms_residual(a, x, y) = \sqrt{\|A\mathbf x - \mathbf y\|^2/m}, and discrepancy_lambda(a, y, sigma, penalty): the \lambda whose RMS residual equals \sigma_n, found by halving an interval of \log_{10}\lambda:
lo = −8, hi = 4 # log10 λ
repeat 40 times:
mid = (lo + hi) / 2
if rms residual of tikhonov(10^mid) < σn: lo = mid # fits the noise: more λ
else: hi = mid
λ = 10^((lo + hi) / 2)
Then cargo run -p deconvolution -- regularise and open the pictures in out/regularise/. The program makes a profile of 128 samples (a step and a ramp), blurs it (\sigma = 2), adds noise (\sigma_n = 0.02), and restores it with both penalties for \lambda from 10^{-7} to 100, and with Landweber for k = 1 to 3162. It prints the error against the truth, the RMS residual, the L-curve’s two norms \|A\mathbf x - \mathbf y\| and \|D\mathbf x\|, and the mean. The pictures show the truth (black), the data (grey) and an estimate.
C6. Compare the column “mean” of the two penalties as \lambda grows. Then open roughness-small-lambda.png, roughness-best-lambda.png and roughness-large-lambda.png: describe each.
C7. For the roughness penalty, which \lambda gives the smallest error, which does the discrepancy principle choose, and how much worse is it? Use the columns \|A\mathbf x - \mathbf y\| and \|D\mathbf x\| to find the corner of the L-curve, as in B4.
C8. How does the error of Landweber change with k, and how does the residual? Where would the discrepancy principle stop? Compare landweber-32.png with landweber-3162.png.
The model solution is deconvolution/src/regularise/solution.rs in the download.
C6. With the energy penalty the mean falls from 0.434 (small \lambda) to 0.395 at \lambda = 0.1, 0.217 at 1 and 0.004 at 100: everything is pulled towards 0. With the roughness penalty it stays at 0.434 for every \lambda, because a constant has no roughness. Small \lambda (10^{-6}): the estimate swings wildly far beyond the plot, error 1.34, while its RMS residual (0.0123) is below \sigma_n: it explains the noise. Best \lambda (0.1): close to the truth with small wiggles, the steps a little rounded, error 0.0561 (the data alone: 0.0647). Large \lambda (100): smooth and rounded, the step a hill and the end of the ramp cut off, error 0.0893.
C7. The smallest error is 0.0561 at \lambda = 0.1. The discrepancy principle picks \lambda = 1.11 (RMS residual 0.02), error 0.0616, 10 % worse and on the smooth side, as in the demo. The L-curve: from \lambda = 10^{-3} to 10^{-2}, \|D\mathbf x\| halves (1.08 to 0.52) while \|A\mathbf x - \mathbf y\| grows by 5 % (0.168 to 0.175): the vertical branch. From \lambda = 1 to 10, \|A\mathbf x - \mathbf y\| grows by half (0.224 to 0.334): the horizontal branch. The corner lies in between, around \lambda = 0.03 to 0.3, close to the best \lambda.
C8. The error falls from 0.0739 (k = 1) to 0.0566 at k = 10, then rises: 0.0694 at 100, 0.1254 at 1000, 0.2154 at 3162. The residual falls all the time, from 0.0362 to 0.0140: semi-convergence. The residual first drops below \sigma_n = 0.02 at k = 10 of the values tried, right at the smallest error. At k = 32 the estimate is close to the truth with some ripple; at k = 3162 it oscillates from sample to sample with a large amplitude: the weak, noisy components have been filled in.
Where this leads
The next unit takes the probabilistic reading seriously for photon counts: Richardson–Lucy maximises the Poisson likelihood with an iteration that keeps every pixel non-negative, and its number of iterations is the regularisation parameter, exactly like Landweber’s here. The same “data + λ · penalty” pattern returns on the graph-cut day (with labels instead of intensities; segmentation as energy), in tomography (iterative reconstruction), in the days on Markov random fields (total variation; quadratic smoothing and total variation), wavelets (shrinkage) and sparse representations (sparsity), and with a learned prior in diffusion models for inverse problems.