Entropy Coding from Scratch — From Huffman to Arithmetic Coding
"Information content becomes code length" — that one line is all of compression. From Shannon's bound to building a Huffman code by hand, the integer-bit ceiling, the arithmetic coders and ANS that break it, and what is actually running inside JPEG, PNG, H.264 and zstd. Ending with how to diagnose a compression ratio that has stopped improving.
Why Morse code was already right
In Morse, the letter E — the most common in English — is a single dot, while the rare Q is dash-dash-dot-dash. The inventors of 1837 knew no information theory, but they had the principle by instinct: frequent things get short codes.
In 1948 Shannon gave that instinct an exact form, and went further than "should be shorter": he pinned down how short it can possibly get. Entropy coding is the family of techniques for approaching that bound. It is the last stage inside JPEG, inside PNG, inside gzip, inside H.264 — always there, always last.
The central idea: information content is code length
In the information theory article we defined the information content of an event as — the "surprise", larger for rarer events. Coding theory gives that quantity a physical meaning.
What this says, plainly, is that a symbol occurring with probability should be given bits. A symbol at gets one bit, at two bits, at eight bits. An abstract quantity called surprise turns directly into a concrete count of bits. That is the most beautiful thing in this field.
Under the ideal assignment, the average code length per symbol is the expectation of the information content — the entropy itself.
Put in words: walk over every symbol the source can emit, multiply how often it turns up () by how many bits it deserves (), and add all of those up. is that weighted average — the bill you pay per symbol once every symbol has been priced correctly.
Shannon's source coding theorem then guarantees that this is an unbreakable floor: no uniquely decodable code has an average length below , and simultaneously, anything below bits is always achievable. Both the ceiling and the floor of compression come from this single quantity.
Prefix codes and the Kraft inequality
Code lengths cannot be chosen freely. To decode without separators, no codeword may be a prefix of another — a prefix code. Use both 0 and 01 and you cannot commit after reading 0.
That constraint collapses into a single inequality on the lengths :
This is the Kraft inequality. Read it as: short codewords are expensive. A one-bit codeword consumes of the budget, a two-bit one , and the total may not exceed 1. "Frequent symbols get short codes" becomes an optimisation problem precisely because they are competing for a finite budget.
Building a Huffman code by hand
The procedure for the optimal prefix code is startlingly short: take the two least probable items, merge them, and put the sum back. Repeat until one item remains.
Try it on five symbols: , , , , .
- The two smallest, and (0.10 each), merge into
- Now . Take and as the two smallest, giving
- Now . The two smallest, and , merge into
- Merge and at the root
Label the branches 0 and 1 and you get , , , , — none a prefix of another. The average code length is
Read in words, each term is "how often that symbol appears × how long its codeword is": turns up 40% of the time and costs 2 bits, turns up 10% of the time and costs 3. The total is what one average symbol costs you.
That is measured against an entropy of bits. Fixed-length coding of five symbols needs 3 bits, so we have gone 3.00 → 2.20 bits, sitting 0.08 above the theoretical floor. Huffman coding is provably optimal among codes that assign an integer number of bits per symbol, which means that 0.08 is not sloppiness — it is structural leakage.
Comments
Sign in to comment