---
title: The singular value decomposition in computation
heading: Rank, condition, best low-rank approximations and parallel channels
eyebrow: A13 · Lesson 32 · The SVD
xp: 40
---

::: hook
Every matrix, square or not, can be written as $A = U\Sigma V^T$: a rotation, a stretch along perpendicular axes by the **singular values** $\sigma_1 \ge \sigma_2 \ge \cdots \ge 0$, and another rotation. The singular values answer the practical questions numerical work keeps asking. How close is this matrix to singular? What is its rank, really, once noise is counted? What is the best approximation using only k numbers per row and column? In communications they also give the gains of the independent channels hidden inside a multi-antenna link.
:::

::: play
- The dashed circle is the set of unit vectors x; the solid ellipse is where $A = \begin{pmatrix}a & b\\ c & d\end{pmatrix}$ sends them.
- The ellipse's semi-axes are the singular values $\sigma_1$ and $\sigma_2$. Their ratio is the condition number.
- Make the matrix nearly singular, for example a = d = 1 and b = c = 0.9: the ellipse flattens to a needle, $\sigma_2$ approaches 0, and κ grows.
:::

```sim plot
view: complex
range: 2
slider: a = -2..2 step 0.05 = 1.5
slider: b = -2..2 step 0.05 = 0.5
slider: c = -2..2 step 0.05 = 0
slider: d = -2..2 step 0.05 = 1
curve: exp(j*u) | over: u = 0..2*pi | dashed
curve: (a*cos(u) + b*sin(u)) + j*(c*cos(u) + d*sin(u)) | over: u = 0..2*pi | color: ch2
readout: largest singular value = sqrt(((a^2 + b^2 + c^2 + d^2) + sqrt((a^2 + b^2 + c^2 + d^2)^2 - 4*(a*d - b*c)^2))/2) | digits: 4
readout: smallest singular value = sqrt(abs((a^2 + b^2 + c^2 + d^2) - sqrt((a^2 + b^2 + c^2 + d^2)^2 - 4*(a*d - b*c)^2))/2) | digits: 4
```

::: predict
## You compute a matrix's singular values as the square roots of the eigenvalues of $A^TA$, in double precision. The matrix has κ = $10^9$. What happens to its smallest singular value?

- [ ] It comes out accurate to about 7 digits
- [x] It may come out with no correct digits at all
- [ ] It comes out exactly
- [ ] The eigenvalue solver fails

::: reveal
It may be pure noise. Forming $A^TA$ squares the condition number to $10^{18}$, beyond double precision's $10^{16}$, so the smallest eigenvalue of $A^TA$ is lost in rounding. SVD software works on A directly (Golub–Kahan bidiagonalization), which gets small singular values to about $\sigma_1 \times 10^{-16}$ absolute accuracy.
:::
:::

::: nameit
## The decomposition

$A = U\Sigma V^T$ with U and V orthogonal and Σ diagonal. The columns of V are the directions A stretches most and least; $Av_i = \sigma_iu_i$. The $\sigma_i^2$ are the eigenvalues of $A^TA$, but software never computes them that way.

## What the singular values tell you

- **Norm and condition:** $\lVert A\rVert_2 = \sigma_1$, and $\kappa_2(A) = \sigma_1/\sigma_n$.
- **Numerical rank:** the number of singular values above a tolerance, such as $\max(m, n)\,\varepsilon\,\sigma_1$, or the noise level of the data.
- **Distance to singularity:** the nearest singular matrix is $\sigma_n$ away in the 2-norm.

## Best low-rank approximation

Keeping the k largest terms, $A_k = \sum_{i\le k}\sigma_iu_iv_i^T$, gives the best rank-k approximation (Eckart–Young): its error is $\sigma_{k+1}$ in the 2-norm and $\sqrt{\sum_{i>k}\sigma_i^2}$ in the Frobenius norm. Storing $A_k$ takes $k(m + n + 1)$ numbers instead of mn.

## Least squares and regularization

The pseudoinverse $A^+ = V\Sigma^+U^T$ (invert the nonzero $\sigma_i$) gives the minimum-norm least-squares solution. When small singular values are only noise, dividing by them amplifies it; **truncated SVD** drops them, trading a little bias for much less noise.

## Parallel channels

A multi-antenna link $y = Hx + n$ becomes independent scalar channels with gains $\sigma_i$ after rotating by $V$ at the transmitter and $U^T$ at the receiver. With power p per channel and unit noise, the capacity is $\sum_i\log_2(1 + p\sigma_i^2)$ bits per second per hertz.
:::

::: example
## Find the singular values and condition number of $A = \begin{pmatrix}3 & 0\\ 4 & 5\end{pmatrix}$.

::: solution
1. **Form $A^TA$.** For a small exact matrix this route is fine:
   $$A^TA = \begin{pmatrix}25 & 20\\ 20 & 25\end{pmatrix}$$
   Its eigenvalues are 45 and 5.
2. **Singular values.**
   $$\begin{aligned} \sigma_1 &= \sqrt{45} \\ &= 6.708 \\ \sigma_2 &= \sqrt5 \\ &= 2.236 \end{aligned}$$
3. **Condition number.**
   $$\begin{aligned} \kappa_2 &= \sigma_1/\sigma_2 \\ &= 3 \end{aligned}$$
4. **Check.**
   $$\begin{aligned} \sigma_1\sigma_2 &= \sqrt{225} \\ &= 15 \\ &= \lvert\det A\rvert \end{aligned}$$
   as it must for a square matrix.
5. **Eigenvalues understate it.** The eigenvalues of A itself are 3 and 5, whose ratio, $\frac53$, understates how unevenly A stretches; for nonsymmetric matrices only the singular values measure that.

**Answer:** $\sigma_1 = 6.708$, $\sigma_2 = 2.236$ and $\kappa_2 = 3$.
:::
:::

::: example
## A 1000 × 1000 matrix of measurements is approximated by its 50 largest singular terms. How much storage does that take, and what is the error?

::: solution
1. **Storage.** Rank 50 needs
   $$\begin{aligned} &50 \times (1000 + 1000 + 1) \\ &= 100\,050 \end{aligned}$$
   numbers instead of $10^6$: about 10% of the storage.
2. **Error.** The error in the 2-norm is exactly $\sigma_{51}$, so the singular value plot tells you in advance how much each extra term buys.
3. **When the values fall quickly.** With smooth images or correlated sensor arrays, a small rank captures nearly everything.
4. **When they level off.** If they level off at a noise floor, the terms beyond that point are noise, and dropping them cleans the data as well as compressing it.

**Answer:** 100 050 numbers, about 10% of the full matrix, with a 2-norm error of exactly $\sigma_{51}$.
:::
:::

::: example
## A 2 × 2 antenna link has singular values 2 and 0.5. With power 10 (relative to the noise) on each stream, what is its capacity?

::: solution
1. **The strong stream.** Each stream is an independent channel with gain $\sigma_i$:
   $$\log_2(1 + 10 \times 4) = 5.36$$
   bits per second per hertz.
2. **The weak stream.**
   $$\log_2(1 + 10 \times 0.25) = 1.81$$
3. **Total.** The total is 7.16 bits per second per hertz.
4. **Compare with one antenna pair.** A single pair with gain 2 would give 5.36: the second spatial channel adds a third more, even though it is four times weaker.
5. **Water-filling.** Water-filling, which puts more power on the stronger channel, would do slightly better still.

**Answer:** 7.16 bits per second per hertz: 5.36 from the strong stream and 1.81 from the weak one.
:::
:::

::: check
kind: parts
given: p = 3 | 2; q = 4 | 1; r = 5 | 3

## $A = \begin{pmatrix}\given{p} & 0\\ \given{q} & \given{r}\end{pmatrix}$.

::: part
answer: sqrt(((p^2 + q^2 + r^2) + sqrt((p^2 + q^2 + r^2)^2 - 4*(p*r)^2))/2)
tolerance: 0.5%

### What is $\sigma_1$?

::: misses
- [r] The eigenvalues of a triangular matrix are its diagonal, but its singular values aren't.
- [any] $\sigma^2$ are the eigenvalues of $A^TA$: trace $p^2 + q^2 + r^2$, determinant $(pr)^2$.
:::
:::

::: part
answer: ((p^2 + q^2 + r^2) + sqrt((p^2 + q^2 + r^2)^2 - 4*(p*r)^2))/(2*p*r)
tolerance: 0.5%

### What is $\kappa_2 = \sigma_1/\sigma_2$? (Use $\sigma_1\sigma_2 = \lvert\det A\rvert$.)

::: misses
- [any] $\kappa = \frac{\sigma_1^2}{\lvert\det A\rvert}$.
:::
:::
:::

::: check
given: s = 10 | 100; k = 3 | 8
answer: s*2^(-k)
tolerance: 0.5%

## A matrix's singular values halve from each to the next, starting at $\sigma_1$ = {s}. What is the 2-norm error of its best rank-{k} approximation?

::: right
Right. $\sigma_{k+1} = \sigma_12^{-k}$.
:::

::: misses
- [s*2^(-k - 1)] The error is $\sigma_{k+1}$, and $\sigma_{k+1} = \sigma_1 2^{-k}$.
- [any] Eckart–Young: the first singular value left out.
:::
:::

::: check
given: m = 1000 | 4000; n = 500 | 2000; k = 20 | 100
answer: k*(m + n + 1)/(m*n)
tolerance: 0.5%

## What fraction of the storage of an {m} × {n} matrix does its rank-{k} approximation need?

::: right
Right. $k(m + n + 1)$ numbers instead of mn.
:::

::: misses
- [k/n] Count the columns of U and V and the singular values.
- [any] $\frac{k(m + n + 1)}{mn}$.
:::
:::

::: check
given: s1 = 2 | 3; s2 = 0.5 | 1; p = 10 | 100
answer: log(1 + p*s1^2)/log(2) + log(1 + p*s2^2)/log(2)
unit: bits
tolerance: 0.5%

## A 2 × 2 antenna link has singular values {s1} and {s2}, with power {p} per stream relative to the noise. What is its capacity per hertz?

::: right
Right. Two independent channels, each with its own $\log_2(1 + p\sigma^2)$.
:::

::: misses
- [log(1 + p*s1^2)/log(2)] Add the second channel.
- [any] $\sum\log_2(1 + p\sigma_i^2)$.
:::
:::

::: check
given: k = 3 | 6 | 8
answer: 2*k
tolerance: 0.5%

## A matrix has $\kappa = 10^{\given{k}}$. Solving least squares through $A^TA$, about how many digits can be lost?

::: right
Right. $\kappa(A^TA) = \kappa(A)^2$, so the loss doubles: use QR or the SVD instead.
:::

::: misses
- [k] Forming $A^TA$ squares the condition number.
- [any] $\log_{10}\kappa^2$.
:::
:::

::: check
kind: choice

## A 100 × 100 matrix from measurements has singular values 50, 20, 8, 3, then 96 values all near $10^{-6}$, the noise level. What is its useful rank?

- [ ] 100, since none is exactly zero
- [x] 4
- [ ] 1
- [ ] 96

::: right
Right. The numerical rank counts singular values above the noise level; the rest describe noise.
:::

::: misses
- [1] Exact zeros never appear in floating point or in measured data.
- [3] The four large values all stand well clear of the noise.
- [4] Those 96 are the noise.
:::
:::

::: check
kind: derivation
vars: a = -3..3, b = -3..3

## $A = \begin{pmatrix}a & -b\\ b & a\end{pmatrix}$, a rotation times a scaling.

::: step
answer: a^2 + b^2

### What is the (1, 1) entry of $A^TA$?
:::

::: step
answer: sqrt(a^2 + b^2)

### What are its singular values?
:::

::: right
Right. $A^TA = (a^2 + b^2)I$, so both singular values equal $\sqrt{a^2 + b^2}$ and κ = 1: multiplying by a complex number is perfectly conditioned.
:::
:::

::: check
kind: python

## A 2 × 2 antenna link has channel matrix $H = \begin{pmatrix}1 & 0.5\\ 0.3 & 0.8\end{pmatrix}$. Set `s` to its singular values (`np.linalg.svd(H, compute_uv=False)`) and `C` to the capacity $\sum\log_2(1 + 10\sigma_i^2)$, with power 10 per stream.

```python starter
import numpy as np

s = np.array([1.0, 1.0])
C = 0.0
print(s, C)
```

```python solution
import numpy as np

H = np.array([[1.0, 0.5], [0.3, 0.8]])
s = np.linalg.svd(H, compute_uv=False)
C = np.sum(np.log2(1 + 10 * s**2))
print(s, C)
```

```python tests
import numpy as np
assert abs(s[0] - 1.317849) < 1e-5 and abs(s[1] - 0.493228) < 1e-5, f"Expect singular values 1.3178 and 0.4932; got {s}."
assert abs(s[0] * s[1] - abs(np.linalg.det(np.array([[1.0, 0.5], [0.3, 0.8]])))) < 1e-12, "Their product is |det H| = 0.65."
assert abs(C - 5.9784) < 1e-3, f"The capacity is 5.98 bits/s/Hz; got {C}."
```

::: right
Right. 1.318 and 0.493, giving 5.98 bits per second per hertz from two parallel channels.
:::
:::

::: explain
## Why is the truncated SVD the best low-rank approximation?

::: model
In the bases U and V, the matrix is just the diagonal of singular values, and rotations don't change 2-norm or Frobenius-norm distances. A rank-k matrix can keep at most k independent directions, so it must leave out at least one direction of a (k + 1)-dimensional space. Within the span of the first k + 1 right singular vectors, A stretches every vector by at least $\sigma_{k+1}$, while a rank-k matrix must send some unit vector there to zero, so the difference has norm at least $\sigma_{k+1}$. Keeping the k largest terms achieves exactly that error, so no rank-k matrix does better.
:::
:::

::: practice

::: check
given: s = 5 | 40; k = 4 | 10
answer: s*2^(-k)
tolerance: 0.5%

## Rank-{k} error, singular values halving from {s}?

::: misses
- [any] $\sigma_12^{-k}$.
:::
:::

::: check
given: k = 1..8 int
answer: 2*k
tolerance: 0.5%

## Digits at risk through $A^TA$ with κ = $10^{\given{k}}$?

::: misses
- [any] Twice as many.
:::
:::
:::
