Foundations to Frontier
Tests
A. Mathematical Foundations › A7 Information Theory

Source Coding, Huffman Coding, Compression and Prediction

Entropy is the limit of lossless compression: no code can average fewer than H bits per symbol, and good codes come within one bit. You will meet prefix codes and the Kraft inequality, prove the source coding theorem, build optimal Huffman codes, see how block and arithmetic coding close the gap, and learn the deep equivalence between compression and prediction: a model that predicts better compresses better, which is why language models are compressors and why spacecraft telemetry is compressed with predictive coders.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Short words for common things

In Morse code, the most common English letter E is a single dot, while a rare letter like Q is long. That idea, short codes for frequent messages and long codes for rare ones, is the heart of compression. The best possible average length is the entropy. And to choose good codes you need to know, or predict, how likely each message is. So compressing well and predicting well are the same skill.

Huffman coding of rover status messages
FAULT000SAFE00101PWR010TEMP0110101OK101
symbolp−log₂ pcodewordlength
OK0.5500.8611
TEMP0.1502.740113
PWR0.1203.060103
SAFE0.1003.320013
FAULT0.0803.640003
code blocks of symbols
entropy H = 1.876 bits · Huffman L = 1.900 bits/symbol · block-1 Huffman = 1.900 bits/symbol (fixed-length: 3 bits)
Huffman repeatedly merges the two least likely nodes, so rare messages get long codewords and common ones short codewords. Its average length always satisfies H ≤ L < H + 1, and it equals H exactly when every probability is a power of ½. Coding blocks of n symbols shrinks the overhead to under 1/n bit per symbol. Arithmetic coding removes it almost entirely.
Why engineers care

Deep-space downlink is slow and precious; Mars orbiters relay at a few megabits per second at best. Every probe compresses its telemetry and images (CCSDS lossless standards are built on predictive coding). Modern LLMs, evaluated in bits per token, are among the best text compressors ever built.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A good code gives frequent messages…
  2. 2. No lossless code can average fewer bits than…
  3. 3. A better predictor gives…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Prefix codes and Kraft

A prefix code has no codeword that is the start of another, so it decodes instantly with no separators. Codeword lengths ℓi\ell_i are achievable by a prefix code iff the Kraft inequality holds:

∑i2−ℓi≤1\sum_i2^{-\ell_i}\le1

The source coding theorem

For any prefix (indeed any uniquely decodable) code, L=∑piℓi≥H(X)L = \sum p_i\ell_i\ge H(X). The Shannon code with ℓi=⌈−log⁡2pi⌉\ell_i = \lceil-\log_2p_i\rceil achieves L<H+1L < H + 1. So the optimal L∗L^* satisfies

H(X)≤L∗<H(X)+1H(X)\le L^* < H(X) + 1

Huffman coding

Repeatedly merge the two least probable symbols into one node. Reading the 0/1 branch labels from the root gives each codeword. Huffman’s code is optimal among all prefix codes for a given distribution (Huffman, 1952).

📝 Quick check (Level 1)
0/3 answered
  1. 1. Lengths (1, 2, 3, 3) satisfy Kraft with sum…
  2. 2. For p = (½, ¼, ⅛, ⅛), the Huffman average length is…
  3. 3. The Shannon code length for p = 0.3 is…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

Closing the gap: blocks and arithmetic coding

Huffman wastes up to a bit per symbol, which hurts when HH is small (e.g. a 0.9/0.1 bit has H=0.469H = 0.469 but L=1L = 1). Coding blocks of nn symbols gives H≤Ln/n<H+1/nH\le L_n/n < H + 1/n. Arithmetic coding maps a whole sequence to one interval of width ∏q(xt∣x<t)\prod q(x_t\mid x_{<t}). Its total length is within 2 bits of

ℓ(x1:n)=∑t−log⁡2q(xt∣x<t)\ell(x_{1:n}) = \sum_t-\log_2q(x_t\mid x_{<t})

Compression = prediction

That formula is the cumulative cross-entropy (log-loss) of the predictive model qq. Any probabilistic predictor plus arithmetic coding gives a compressor, and any compressor implies a predictor. For a stationary source the best achievable rate is the entropy rate H(X)=lim⁡H(Xn∣X<n)H(\mathcal X) = \lim H(X_n\mid X_{<n}). For a Markov chain it’s ∑iπiH(Ti⋅)\sum_i\pi_iH(T_{i\cdot}). Adaptive models (Laplace or KT counts) learn qq on the fly, so no codebook has to be transmitted.

🐍 Exercise 1: Huffman coding
Build Huffman codes with a heap. For a spacecraft status alphabet, check that the code is prefix-free, that its Kraft sum is 1, that H ≤ L < H + 1, and that it is at least as good as Shannon lengths. Show that L = H for dyadic probabilities and that block coding cuts the overhead.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Arithmetic coding length ≈
  2. 2. Block coding with n = 4 limits the overhead to less than…
  3. 3. For a source with memory, the best rate is…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Proof of the lower bound

Let ri=2−ℓi/cr_i = 2^{-\ell_i}/c with c=∑2−ℓj≤1c = \sum2^{-\ell_j}\le1. Then

L−H=∑pilog⁡2pi2−ℓi=KL⁡(p ∥ r)−log⁡2c≥0L - H = \sum p_i\log_2\frac{p_i}{2^{-\ell_i}} = \operatorname{KL}(p\,\|\,r) - \log_2c\ge0

∎ The code length ℓ\ell is the implied probability model 2−ℓ2^{-\ell}. The excess length is exactly the KL between the true and implied distributions.

Shannon code upper bound

ℓi=⌈−log⁡2pi⌉\ell_i = \lceil-\log_2p_i\rceil satisfies Kraft because 2−ℓi≤pi2^{-\ell_i}\le p_i. And ℓi<−log⁡2pi+1\ell_i < -\log_2p_i + 1, so L<H+1L < H + 1. ∎

Huffman optimality (sketch)

In some optimal code, the two least likely symbols are siblings at the deepest level (swapping arguments). Merging them reduces the problem to k−1k - 1 symbols, with Lk=Lk−1+pk−1+pkL_k = L_{k-1} + p_{k-1} + p_k. Induction completes the proof. ∎

Exercise 3.1: Huffman code for (0.4, 0.3, 0.2, 0.1)

Merge 0.1 and 0.2 into 0.3. Merge 0.3 and 0.3 into 0.6. Merge 0.4 and 0.6. The lengths are (1, 2, 3, 3), so L=0.4+0.6+0.6+0.3=1.9L = 0.4 + 0.6 + 0.6 + 0.3 = 1.9 bits vs H≈1.846H\approx1.846.

📝 Quick check (Level 3)
0/3 answered
  1. 1. L − H equals the KL between p and…
  2. 2. The Shannon code satisfies Kraft because…
  3. 3. Huffman for (0.4, 0.3, 0.2, 0.1) gives L =

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Predictive telemetry compression

Spacecraft housekeeping telemetry is strongly correlated in time: a nominal state tends to stay nominal. A static code ignores this and pays the marginal entropy. A predictor conditioned on the previous symbol pays only the entropy rate. The exercise compresses a 4-state Markov status stream with adaptive order-0 and order-1 models (ideal arithmetic-coding length). The order-1 model reaches the entropy rate and beats general-purpose zlib. The same principle underlies the CCSDS 121/123 lossless standards (Rice coding of prediction residuals) and the observation that large language models are strong general compressors (Delétang et al., 2024).

🐍 Exercise 2 (research): compression is prediction
Simulate a Markov telemetry stream. Compute its entropy rate, and compute the ideal code length under adaptive order-0 and order-1 predictors. Compare with the raw encoding and with zlib.

Source coding in AI, robotics and aerospace

  • Space data systems: CCSDS 121.0 (adaptive Rice coding, used on many NASA and ESA missions) and CCSDS 123.0 (predictive multispectral and hyperspectral image compression).
  • Language models as compressors: log-loss in bits per token is a compression rate. Arithmetic coding with an LLM beats gzip and PNG on many data types (Delétang et al., 2024).
  • Context-tree weighting and PPM: optimal universal predictors and compressors for Markov sources (Willems, Shtarkov and Tjalkens, 1995).
  • Learned image codecs: an entropy model plus arithmetic coding of latents (Ballé et al., 2018; see A7-10).

References

  1. C. Shannon (1948). “A mathematical theory of communication.” Bell System Technical Journal, 27.
  2. D. Huffman (1952). “A method for the construction of minimum-redundancy codes.” Proceedings of the IRE, 40(9).
  3. T. Cover, J. Thomas (2006). Elements of Information Theory, 2nd ed. Wiley. Ch. 5, 13.
  4. I. Witten, R. Neal, J. Cleary (1987). “Arithmetic coding for data compression.” Communications of the ACM, 30(6).
  5. F. Willems, Y. Shtarkov, T. Tjalkens (1995). “The context-tree weighting method: basic properties.” IEEE Transactions on Information Theory, 41(3).
  6. CCSDS (2020). Lossless Data Compression, CCSDS 121.0-B-3. Blue Book.
  7. CCSDS (2019). Low-Complexity Lossless and Near-Lossless Multispectral and Hyperspectral Image Compression, CCSDS 123.0-B-2.
  8. G. Delétang et al. (2024). “Language modeling is compression.” ICLR.
  9. D. MacKay (2003). Information Theory, Inference, and Learning Algorithms. Cambridge. Ch. 4–6.
  10. R. Krichevsky, V. Trofimov (1981). “The performance of universal encoding.” IEEE Transactions on Information Theory, 27(2).