Rate-Distortion Theory
Lossless compression cannot go below the entropy. But images, audio and video are compressed far below their entropy, because we accept small changes. Rate-distortion theory answers the lossy question: if an average distortion is acceptable, how many bits per symbol are needed? The answer is the rate-distortion function .
Setting
A source emits ; the compressor produces a reconstruction . A distortion measure scores each error: Hamming distortion ( if wrong, if right) for bits, squared error for real values. The rate-distortion function is
Shannon's rate-distortion theorem says that bits per symbol are enough to reach distortion with long blocks, and no scheme can do it with fewer.
Binary source with Hamming distortion
For a fair coin source, accepting a fraction of wrong bits:
For a biased source with : for , and 0 beyond.
import numpy as np
H2 = lambda q: 0.0 if q <= 0 or q >= 1 else float(-q * np.log2(q) - (1 - q) * np.log2(1 - q))
for D in [0.0, 0.01, 0.05, 0.1, 0.2, 0.3, 0.5]:
print(f"D = {D:4.2f}: fair coin R = {max(0.0, 1 - H2(D)):.4f}, p = 0.2 source R = {max(0.0, H2(0.2) - H2(D)):.4f}")D = 0.00: fair coin R = 1.0000, p = 0.2 source R = 0.7219
D = 0.01: fair coin R = 0.9192, p = 0.2 source R = 0.6411
D = 0.05: fair coin R = 0.7136, p = 0.2 source R = 0.4355
D = 0.10: fair coin R = 0.5310, p = 0.2 source R = 0.2529
D = 0.20: fair coin R = 0.2781, p = 0.2 source R = 0.0000
D = 0.30: fair coin R = 0.1187, p = 0.2 source R = 0.0000
D = 0.50: fair coin R = 0.0000, p = 0.2 source R = 0.0000
Allowing just 1% wrong bits saves 8% of the rate; allowing 11% halves it.
Gaussian source with squared error
For a Gaussian source with variance and mean squared error :
Every extra bit per sample divides the distortion by 4 (about 6 dB of signal-to-distortion ratio per bit, the same "6 dB per bit" rule as for quantizers in Quantization).
import numpy as np
sigma2 = 1.0
for R in [0.5, 1, 2, 3, 4]:
D = sigma2 * 2 ** (-2 * R)
print(f"R = {R} bits/sample: D(R) = {D:.5f}, SNR = {10 * np.log10(sigma2 / D):.2f} dB")R = 0.5 bits/sample: D(R) = 0.50000, SNR = 3.01 dB
R = 1 bits/sample: D(R) = 0.25000, SNR = 6.02 dB
R = 2 bits/sample: D(R) = 0.06250, SNR = 12.04 dB
R = 3 bits/sample: D(R) = 0.01562, SNR = 18.06 dB
R = 4 bits/sample: D(R) = 0.00391, SNR = 24.08 dB
Computing R(D) with Blahut-Arimoto
For a general discrete source and distortion matrix, is computed by an alternating algorithm like the one for capacity (Computing Capacity: Blahut-Arimoto). A parameter trades rate for distortion; each value gives one point of the curve.
import numpy as np
def rd_point(px, d, s, iters=500):
q = np.ones(d.shape[1]) / d.shape[1] # reconstruction distribution
for _ in range(iters):
A = q[None, :] * np.exp(-s * d)
cond = A / A.sum(axis=1, keepdims=True) # P(xhat | x)
q = px @ cond
D = float(np.sum(px[:, None] * cond * d))
R = float(np.sum(px[:, None] * cond * np.log2(np.where(cond > 0, cond / q[None, :], 1))))
return R, D
px = np.array([0.8, 0.2])
d = 1 - np.eye(2) # Hamming distortion
H2 = lambda q: float(-q * np.log2(q) - (1 - q) * np.log2(1 - q))
for s in [1.5, 2.5, 4.0, 6.0]:
R, D = rd_point(px, d, s)
print(f"s = {s}: D = {D:.4f}, R = {R:.4f}, formula H2(0.2) - H2(D) = {H2(0.2) - H2(D):.4f}")s = 1.5: D = 0.1824, R = 0.0366, formula H2(0.2) - H2(D) = 0.0366
s = 2.5: D = 0.0759, R = 0.3345, formula H2(0.2) - H2(D) = 0.3345
s = 4.0: D = 0.0180, R = 0.5919, formula H2(0.2) - H2(D) = 0.5919
s = 6.0: D = 0.0025, R = 0.6970, formula H2(0.2) - H2(D) = 0.6970
Achieving it in practice
The theorem needs long blocks and random codebooks, like the channel coding theorem. Practical lossy coders combine three ideas:
- A transform (DCT in JPEG, MDCT in MP3/AAC, wavelets in JPEG 2000) concentrates energy in few coefficients.
- Quantization of each coefficient, coarser for coefficients that matter less to human perception (Quantization).
- Entropy coding of the quantized values (Huffman or arithmetic coding).
For a vector of independent Gaussian components with different variances, the optimal allocation is reverse water-filling: components with variance below a threshold are dropped entirely, the others are coded to the same distortion. Transform coding approximates this.
import numpy as np
from scipy.optimize import brentq
var = np.array([10.0, 4.0, 1.0, 0.25, 0.05])
D_total = 1.5
theta = brentq(lambda t: np.minimum(t, var).sum() - D_total, 1e-9, var.max())
D = np.minimum(theta, var)
R = 0.5 * np.log2(var / D)
print("threshold:", round(theta, 4))
print("distortion per component:", np.round(D, 4))
print("bits per component: ", np.round(R, 4), " total", round(float(R.sum()), 4))threshold: 0.4
distortion per component: [0.4 0.4 0.4 0.25 0.05]
bits per component: [2.3219 1.661 0.661 0. 0. ] total 4.6439
In software
- JPEG's quality setting picks the quantization step sizes; plotting file size against error for several qualities traces an operational rate-distortion curve.
- Learned image codecs train a neural encoder and decoder with the loss , a Lagrangian form of the same problem; the information bottleneck uses the same trade-off for representations (The Information Bottleneck).
Exercises
Exercise 1: Half the bits
A fair binary source is compressed to 0.5 bits per symbol. What is the smallest possible fraction of wrong bits?
Answer
About 0.11.
Solution
Solve , that is : .
Exercise 2: Gaussian bits
How many bits per sample does a Gaussian source need for an SNR of 30 dB?
Answer
About 4.98 bits.
Solution
, so .
Exercise 3: Zero rate (code)
For the binary source with and Hamming distortion, what distortion do you get at rate 0 and why?
Answer
: always output 0.
Solution
import numpy as np
H2 = lambda q: float(-q * np.log2(q) - (1 - q) * np.log2(1 - q))
for D in [0.15, 0.19, 0.2]:
print(D, round(max(0.0, H2(0.2) - H2(D)), 4))0.15 0.1121
0.19 0.0205
0.2 0.0
With no bits the best guess is always the more likely symbol, which is wrong 20% of the time.
Test yourself
The rate-distortion function R(D) gives…
- the fewest bits per symbol needed to reach average distortion D
- the distortion of a given compressor
- the capacity of a channel
- the entropy of the source
For a Gaussian source, each extra bit per sample reduces the mean squared error by a factor of…
- 4
- 2
- 10
- e
Reverse water-filling for independent Gaussian components…
- drops components whose variance is below a threshold
- gives the most bits to the smallest components
- codes all components with the same rate
- ignores the largest component