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.
| symbol | p | −log₂ p | codeword | length |
|---|---|---|---|---|
| OK | 0.550 | 0.86 | 1 | 1 |
| TEMP | 0.150 | 2.74 | 011 | 3 |
| PWR | 0.120 | 3.06 | 010 | 3 |
| SAFE | 0.100 | 3.32 | 001 | 3 |
| FAULT | 0.080 | 3.64 | 000 | 3 |
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.
- 1. A good code gives frequent messages…
- 2. No lossless code can average fewer bits than…
- 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 are achievable by a prefix code iff the Kraft inequality holds:
The source coding theorem
For any prefix (indeed any uniquely decodable) code, . The Shannon code with achieves . So the optimal satisfies
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).
- 1. Lengths (1, 2, 3, 3) satisfy Kraft with sum…
- 2. For p = (½, ¼, ⅛, ⅛), the Huffman average length is…
- 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 is small (e.g. a 0.9/0.1 bit has but ). Coding blocks of symbols gives . Arithmetic coding maps a whole sequence to one interval of width . Its total length is within 2 bits of
Compression = prediction
That formula is the cumulative cross-entropy (log-loss) of the predictive model . 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 . For a Markov chain it’s . Adaptive models (Laplace or KT counts) learn on the fly, so no codebook has to be transmitted.
- 1. Arithmetic coding length ≈
- 2. Block coding with n = 4 limits the overhead to less than…
- 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 with . Then
∎ The code length is the implied probability model . The excess length is exactly the KL between the true and implied distributions.
Shannon code upper bound
satisfies Kraft because . And , so . ∎
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 symbols, with . 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 bits vs .
- 1. L − H equals the KL between p and…
- 2. The Shannon code satisfies Kraft because…
- 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).
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
- C. Shannon (1948). “A mathematical theory of communication.” Bell System Technical Journal, 27.
- D. Huffman (1952). “A method for the construction of minimum-redundancy codes.” Proceedings of the IRE, 40(9).
- T. Cover, J. Thomas (2006). Elements of Information Theory, 2nd ed. Wiley. Ch. 5, 13.
- I. Witten, R. Neal, J. Cleary (1987). “Arithmetic coding for data compression.” Communications of the ACM, 30(6).
- F. Willems, Y. Shtarkov, T. Tjalkens (1995). “The context-tree weighting method: basic properties.” IEEE Transactions on Information Theory, 41(3).
- CCSDS (2020). Lossless Data Compression, CCSDS 121.0-B-3. Blue Book.
- CCSDS (2019). Low-Complexity Lossless and Near-Lossless Multispectral and Hyperspectral Image Compression, CCSDS 123.0-B-2.
- G. Delétang et al. (2024). “Language modeling is compression.” ICLR.
- D. MacKay (2003). Information Theory, Inference, and Learning Algorithms. Cambridge. Ch. 4–6.
- R. Krichevsky, V. Trofimov (1981). “The performance of universal encoding.” IEEE Transactions on Information Theory, 27(2).