TExcelTAD
Home › Information Theory › Rate-Distortion Theory

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 DD is acceptable, how many bits per symbol are needed? The answer is the rate-distortion function R(D)R(D).

Setting

A source emits XX; the compressor produces a reconstruction X^\hat X. A distortion measure d(x,x^)d(x, \hat x) scores each error: Hamming distortion (11 if wrong, 00 if right) for bits, squared error for real values. The rate-distortion function is

R(D)=min⁡P(x^∣x) : E[d(X,X^)]≤DI(X;X^)R(D) = \min_{P(\hat x \mid x) \,:\, E[d(X, \hat X)] \le D} I(X; \hat X)

Shannon's rate-distortion theorem says that R(D)R(D) bits per symbol are enough to reach distortion DD with long blocks, and no scheme can do it with fewer.

Binary source with Hamming distortion

For a fair coin source, accepting a fraction DD of wrong bits:

R(D)=1−H2(D)for 0≤D≤12R(D) = 1 - H_2(D) \quad \text{for } 0 \le D \le \tfrac{1}{2}

For a biased source with P(1)=pP(1) = p: R(D)=H2(p)−H2(D)R(D) = H_2(p) - H_2(D) for D≤min⁡(p,1−p)D \le \min(p, 1 - p), and 0 beyond.

python · try it yourself
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 σ2\sigma^2 and mean squared error DD:

R(D)=12log⁡2σ2Dfor D≤σ2R(D) = \frac{1}{2}\log_2\frac{\sigma^2}{D} \quad \text{for } D \le \sigma^2

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).

python · try it yourself
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, R(D)R(D) is computed by an alternating algorithm like the one for capacity (Computing Capacity: Blahut-Arimoto). A parameter s≥0s \ge 0 trades rate for distortion; each value gives one point of the curve.

python · try it yourself
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:

  1. A transform (DCT in JPEG, MDCT in MP3/AAC, wavelets in JPEG 2000) concentrates energy in few coefficients.
  2. Quantization of each coefficient, coarser for coefficients that matter less to human perception (Quantization).
  3. 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.

python · try it yourself
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 rate+λ⋅distortion\text{rate} + \lambda\cdot\text{distortion}, 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 1−H2(D)=0.51 - H_2(D) = 0.5, that is H2(D)=0.5H_2(D) = 0.5: D≈0.110D \approx 0.110.

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

σ2D=1000\frac{\sigma^2}{D} = 1000, so R=12log⁡21000=4.98R = \frac{1}{2}\log_2 1000 = 4.98.

Exercise 3: Zero rate (code)

For the binary source with P(1)=0.2P(1) = 0.2 and Hamming distortion, what distortion do you get at rate 0 and why?

Answer

D=0.2D = 0.2: always output 0.

Solution
python · try it yourself
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…

  1. the fewest bits per symbol needed to reach average distortion D
  2. the distortion of a given compressor
  3. the capacity of a channel
  4. the entropy of the source

For a Gaussian source, each extra bit per sample reduces the mean squared error by a factor of…

  1. 4
  2. 2
  3. 10
  4. e

Reverse water-filling for independent Gaussian components…

  1. drops components whose variance is below a threshold
  2. gives the most bits to the smallest components
  3. codes all components with the same rate
  4. ignores the largest component