---
title: Rate–distortion theory and quantization
heading: The fewest bits that describe a source to within a given distortion, the Gaussian source's 6 dB per bit, a binary source with Hamming distortion, Lloyd–Max quantizers and how close they come, entropy-coded quantization's 1.53 dB gap, and source–channel separation
eyebrow: S8 · Lesson 32 · Rate–distortion theory and quantization
xp: 50
---

::: hook
Lesson 30 compressed sources without losing anything. Audio, images and sensor readings are continuous, so a finite number of bits can never describe them exactly: something must be lost. Cover and Thomas ask the right question for lossy coding: for a given amount of distortion, what's the least number of bits per sample any scheme could use? For a Gaussian source the answer is the 6 dB per bit that lesson 8 found for PCM, and it shows how far practical quantizers fall short.
:::

::: play
- A Gaussian source of variance σ² described with R bits per sample. The solid line is the rate–distortion bound: the least mean-squared distortion any coder can reach, $D = \sigma^2 2^{-2R}$, falling 6.02 dB per bit.
- The dots are the best fixed-rate scalar quantizers (Lloyd–Max) at 1 to 4 bits. The dashed line is a uniform quantizer followed by ideal entropy coding at high rate, 1.53 dB above the bound.
- No scheme can go below the line. Getting closer than the dashed line takes coding many samples together: vector quantization.
:::

```sim plot
view: graph
x: R = 0..5
y: D = -32..2 dB
plot: -6.0206*R | samples: 100 | label: rate–distortion bound
plot: -6.0206*R + 1.5329 | samples: 100 | dashed | color: ch3 | label: uniform quantizer + entropy coding
point: 1, 10*log(0.3634) | label: Lloyd–Max
point: 2, 10*log(0.1175)
point: 3, 10*log(0.03454)
point: 4, 10*log(0.009497)
```

::: predict
## A Gaussian source must be described with mean-squared error a hundredth of its variance (20 dB SNR). What's the least number of bits per sample any coder can use?

- [ ] 1.66 bits
- [x] 3.32 bits
- [ ] 6.64 bits
- [ ] 20 bits

::: reveal
$R(D) = \tfrac12\log_2(\sigma^2/D) = \tfrac12\log_2 100$ = 3.32 bits. Each bit buys 6.02 dB, so 20 dB takes 20/6.02 = 3.32 bits. A practical quantizer needs more: the best fixed-rate scalar quantizer at 4 bits reaches only 20.2 dB.
:::
:::

::: nameit
## The rate–distortion function

For a source X, a reconstruction $\hat X$ and a distortion measure $d(x, \hat x)$, the **rate–distortion function** is the least mutual information between them consistent with an average distortion D:

$$R(D) = \min_{p(\hat x|x):\,E[d(X, \hat X)] \le D} I(X; \hat X)$$

Cover and Thomas prove it's achievable, by coding long blocks of samples together, and that nothing does better. It decreases as D grows and reaches zero when describing nothing (always guessing the mean) is good enough.

## Two sources worth knowing

- **Gaussian, squared error:** $R(D) = \tfrac12\log_2(\sigma^2/D)$ for D ≤ σ², or $D(R) = \sigma^2 2^{-2R}$. Each bit cuts the distortion by a factor of four: 6.02 dB. The Gaussian is the hardest source of a given variance to compress.
- **Binary (Bernoulli 1/2), Hamming distortion:** $R(D) = 1 - H(D)$ for D ≤ 1/2, where H is the binary entropy. Allowing 10% of the bits to be wrong saves almost half the rate: R(0.1) = 0.531 bit.

## Scalar quantizers

A **Lloyd–Max** quantizer is the best fixed-rate scalar quantizer: its reconstruction levels sit at the centroids (conditional means) of their cells, and its cell boundaries halfway between levels. For a Gaussian source with one bit, the levels are ±σ√(2/π) = ±0.798σ and $D = (1 - 2/\pi)\sigma^2$ = 0.363σ², 1.6 dB worse than R(D) at one bit. At high rate, a uniform quantizer followed by entropy coding of its outputs comes within $10\log(\pi e/6)$ = 1.53 dB of the bound, a quarter of a bit: the price of quantizing one sample at a time.

## Separation

Shannon's **source–channel separation theorem**: a source can be sent over a channel with distortion D if and only if $R(D) \le C$ (per channel use, adjusted for how many source samples each use carries). So the best system can be built in two independent parts, a compressor down to R(D) and a channel code up to C, without loss, at least for long blocks. Modern systems are built this way: codecs designed knowing nothing of the radio, and radios knowing nothing of the content.
:::

::: example
## A sensor's Gaussian noise-like output must be stored with 30 dB SNR. How many bits per sample does the bound require, and how many would a uniform quantizer with ideal entropy coding need?

::: solution
1. **The bound.**
   $$\begin{aligned} R &= \frac{30}{6.02} \\ &= 4.98\ \text{bits per sample} \end{aligned}$$
2. **Entropy-coded uniform quantizer.** It's 1.53 dB worse, which costs 1.53/6.02 = 0.25 bit:
   $$\begin{aligned} R &= \frac{30 + 1.53}{6.02} \\ &= 5.24\ \text{bits per sample} \end{aligned}$$
3. **Fixed-rate Lloyd–Max quantizer.** It would need a whole number of bits, and even 5 bits falls short of 30 dB, so it would need 6.

**Answer:** 4.98 bits per sample at the bound, 5.24 with an entropy-coded uniform quantizer and 6 with a fixed-rate Lloyd–Max quantizer.
:::
:::

::: example
## A Gaussian source producing 1000 samples per second is sent over a channel with capacity 3000 bits per second. What's the smallest mean-squared distortion any system can achieve?

::: solution
1. **Bits per sample.** Each sample gets at most 3 bits on average.
2. **The distortion bound.** By separation,
   $$\begin{aligned} D &= \sigma^2 2^{-2 \times 3} \\ &= \sigma^2/64 \end{aligned}$$
   an SNR of 18.1 dB.
3. **What reaches it.** No combination of compression and channel coding can beat it, and long-block separated designs approach it.
4. **A simple scheme.** A one-sample-at-a-time 3-bit Lloyd–Max quantizer with ideal channel coding gets 0.0345σ², 14.6 dB.

**Answer:** $D = \sigma^2/64$, an SNR of 18.1 dB.
:::
:::

::: example
## Fair random bits may be reproduced with 5% of them wrong; and a Gaussian signal must be kept at 40 dB SNR. What rates do the bounds require?

::: solution
1. **Random bits at 5% error.**
   $$\begin{aligned} R(0.05) &= 1 - H(0.05) \\ &= 1 - 0.286 \\ &= 0.714 \end{aligned}$$
   bit per bit, a saving of 29%.
2. **Gaussian at 40 dB.**
   $$\begin{aligned} R &= \frac{1}{2}\log_2(10^4) \\ &= 6.64\ \text{bits per sample} \end{aligned}$$
3. **Practical quantizers.** A uniform quantizer with ideal entropy coding needs about a quarter bit more (1.53 dB), and a fixed-rate Lloyd–Max quantizer about 0.72 bit more at high rates (4.35 dB, the Panter–Dite constant $\pi\sqrt3/2$).

**Answer:** 0.714 bit per bit for the random bits and 6.64 bits per sample for the Gaussian signal.
:::
:::

::: check
given: v = 1 | 4 | 10; D = 0.01 | 0.05 | 0.2
answer: 0.5*log(v/D)/log(2)
tolerance: 0.5%

## A Gaussian source of variance {v} must be described with mean-squared error {D}. What's the least rate, in bits per sample?

::: right
Right. $\tfrac12\log_2(\sigma^2/D)$ = {answer} bits.
:::

::: misses
- [log(v/D)/log(2)] Include the ½: each bit gains a factor of four in distortion.
- [any] Not yet. $R(D) = \tfrac12\log_2(\sigma^2/D)$.
:::
:::

::: check
given: R = 1 | 2.5 | 4 | 6
answer: 10*log(2^(2*R))
unit: dB
tolerance: 0.5%

## A Gaussian source is coded at the rate–distortion bound with {R} bits per sample. What signal-to-distortion ratio results?

::: right
Right. $10\log_{10}2^{2R}$ = 6.02R = {answer} dB.
:::

::: misses
- [10*log(2^R)] Distortion falls by a factor of four per bit, not two.
- [any] Not yet. $D = \sigma^22^{-2R}$.
:::
:::

::: check
given: D = 0.02 | 0.05 | 0.1 | 0.2
answer: 1 + D*log(D)/log(2) + (1 - D)*log(1 - D)/log(2)
tolerance: 0.5%

## A stream of fair random bits may be reproduced with a fraction {D} of them wrong. What's the least rate, in bits per source bit?

::: right
Right. $1 - H(D)$ = {answer}.
:::

::: misses
- [1 - D] Errors don't save rate one-for-one; it's the entropy that counts.
- [any] Not yet. $R(D) = 1 - H(D)$ with H the binary entropy.
:::
:::

::: check
given: s = 0.5 | 1 | 2
answer: s*sqrt(2/pi)
tolerance: 0.5%

## A 1-bit Lloyd–Max quantizer codes a zero-mean Gaussian of standard deviation {s}. Where are its two reconstruction levels (give the positive one)?

::: right
Right. The centroid of the positive half, $\sigma\sqrt{2/\pi}$ = {answer}.
:::

::: misses
- [s] The best level is the mean of the positive half, which is less than σ.
- [s/2] The centroid is $\sigma\sqrt{2/\pi}$ = 0.798σ.
- [any] Not yet. Each level is the conditional mean of its cell.
:::
:::

::: check
given: S = 20 | 30 | 40 | 50
answer: (S + 1.5329)/6.0206
tolerance: 0.5%

## How many bits per sample does a uniform quantizer with ideal entropy coding need for {S} dB SNR on a Gaussian source, at high rate?

::: right
Right. The bound's S/6.02 plus a quarter bit for the 1.53 dB gap: {answer} bits.
:::

::: misses
- [S/6.0206] That's the rate–distortion bound; scalar quantization is 1.53 dB worse.
- [any] Not yet. Add 1.53 dB to the target, then divide by 6.02.
:::
:::

::: check
given: C = 1000 | 4000 | 8000; f = 500 | 1000
answer: 10*log(2^(2*C/f))
unit: dB
tolerance: 0.5%

## A Gaussian source of {f} samples per second is sent over a channel of capacity {C} bit/s. What's the best possible signal-to-distortion ratio?

::: right
Right. C/f bits per sample, 6.02 dB each: {answer} dB.
:::

::: misses
- [10*log(2^(C/f))] Each bit is worth 6.02 dB, a factor of four in distortion.
- [any] Not yet. By separation, R(D) can be at most the capacity per sample.
:::
:::

::: check
kind: parts
given: S = 20 | 35 | 50; D = 0.02 | 0.1 | 0.25

## A Gaussian source must be kept at {S} dB SNR; separately, fair bits may be reproduced with a fraction {D} wrong.

::: part
answer: S/(20*log(2))
unit: bits
tolerance: 0.5%

### What rate per sample does the Gaussian bound require?

::: misses
- [any] $\frac12\log_2(\sigma^2/D)$.
:::
:::

::: part
answer: 1 + D*log(D)/log(2) + (1 - D)*log(1 - D)/log(2)
unit: bits
tolerance: 0.5%

### What rate per bit does the binary bound require?

::: misses
- [any] $1 - H(D)$.
:::
:::
:::

::: check
kind: derivation
vars: v = 0.01..100, D = 0.001..0.99, R = 0..10

## A Gaussian source of variance v is coded with mean-squared error D, or at rate R.

::: step
answer: 0.5*log(v/D)/log(2)

### What is the least rate for distortion D (D < v)?
:::

::: step
answer: v*2^(-2*R)

### What is the least distortion at rate R?
:::

::: step
answer: 10*log(2^(2*R))

### What SNR is that, in dB?
:::

::: right
Right. 6.02 dB per bit, the same slope as an ideal quantizer, but without the 1.53 dB a scalar quantizer gives up.
:::
:::

::: explain
## Why does coding many samples together beat quantizing each sample separately, even for a source with independent samples?

::: model
A scalar quantizer must tile the line of possible values with cells, and a vector quantizer tiles a space of many dimensions. In many dimensions, cells can be shaped more like spheres, which cover space with less mean-squared error per cell than cubes do; the gain approaches the 1.53 dB that separates the uniform quantizer from the bound. A second gain comes from shaping where the cells go: in many dimensions, independent Gaussian samples almost always land near the surface of a sphere of a predictable radius, so a block code need only cover that thin shell, while a scalar quantizer wastes codes on combinations that essentially never occur. Rate–distortion theory captures both gains in the limit of long blocks, which is why its bound is achievable only by coding samples jointly.
:::
:::

::: practice

::: check
given: v = 0.1..100; D = 0.001..0.9
answer: 0.5*log(v/(D*v))/log(2)
tolerance: 0.5%

## Gaussian R(D): variance {v}, D = {D} × variance?

::: misses
- [any] $\tfrac12\log_2(\sigma^2/D)$.
:::
:::

::: check
given: D = 0.01..0.45
answer: 1 + D*log(D)/log(2) + (1 - D)*log(1 - D)/log(2)
tolerance: 0.5%

## Binary R(D) at D = {D}?

::: misses
- [any] $1 - H(D)$.
:::
:::
:::
