S8 · Lesson 30 · Entropy and source coding
Measuring information in bits, entropy and the binary entropy function, the source coding theorem, Huffman codes and their efficiency
Shannon's 1948 paper began by asking how much information a message really contains, and answered with a formula: a symbol that occurs with probability p carries −log₂ p bits, and a source's average, its entropy, is the least number of bits per symbol any lossless code can use. That one number explains why text compresses, why random data doesn't, and how close a practical code can get. Huffman's 1952 algorithm builds the best possible prefix code for any set of probabilities, often within a few percent of the entropy, and it still sits inside JPEG, MP3 and ZIP.
- The binary entropy function : the information per symbol of a source emitting 1 with probability p.
- A fair coin (p = 0.5) carries a full bit per toss. A biased one carries less: at p = 0.1, only 0.469 bits, so long runs of its output can be compressed to under half their length.
- At p = 0 or 1 the outcome is certain and carries nothing.
Predict
A source emits A, B, C and D with probabilities 0.5, 0.25, 0.125 and 0.125. How many bits per symbol does the best lossless code need, on average?
The entropy is = 1.75 bits, and because every probability is a power of 1/2, a code with lengths 1, 2, 3, 3 (A = 0, B = 10, C = 110, D = 111) hits it exactly. Fixed 2-bit codes waste 12.5%.
Name it
Information and entropy
A symbol of probability p carries self-information bits: rare events carry more. A source's entropy is the average:
with for M symbols, the maximum when all are equally likely. For two symbols it's the binary entropy function H(p), peaking at 1 bit for p = ½.
The source coding theorem
No uniquely decodable code can average fewer than H bits per symbol, and codes exist that come within 1 bit of it, or arbitrarily close by coding blocks of symbols together. A code's efficiency is H/L for average length L.
Huffman coding
Repeatedly merge the two least probable symbols into one node whose probability is their sum, until one node remains; reading the branches from the root gives an optimal prefix code. For probabilities 0.4, 0.3, 0.2 and 0.1: merge 0.1 and 0.2 → 0.3, then 0.3 and 0.3 → 0.6, then 0.4 and 0.6, giving lengths 1, 2, 3, 3 and L = 1.9 bits, against H = 1.846: 97.2% efficient.
Prefix codes: no codeword is the start of another, so a stream decodes without separators.
Worked example
Find the entropy of a source with probabilities 0.4, 0.3, 0.2 and 0.1, and the efficiency of its Huffman code.
Show the solution
- Entropy. The four terms are
- Huffman code. The lengths are 1, 2, 3 and 3, so the average is
- Efficiency.
- Doing better. Coding pairs of symbols would approach 1.846 more closely.
Answer the entropy is 1.846 bits, and the Huffman code, at 1.9 bits, is 97.2% efficient.
Worked example
A binary source emits 1s with probability 0.1. How much could a million of its bits be compressed?
Show the solution
- Entropy.
- A million bits. On average they need only less than half.
- In practice. Run-length or arithmetic coding gets close to that bound for long sequences; a fair coin's output (H = 1) can't be compressed at all.
Answer to about 469,000 bits on average, less than half.
Worked example
A binary source emits 1s with probability 0.1. How efficient is Huffman coding of single symbols, and of pairs?
Show the solution
- Entropy.
- Single symbols. They can't be coded in less than 1 bit each, so the efficiency is 47%.
- Pairs. The pairs have probabilities 0.81, 0.09, 0.09 and 0.01, and Huffman gives them lengths 1, 2, 3 and 3: which is 0.645 per symbol.
- Efficiency of pairs. Longer blocks approach the entropy.
Answer 47% efficient for single symbols and 73% for pairs.
Check
An event has probability 0.1. How much information does its occurrence carry?
Check
A four-symbol source has probabilities 0.5, 0.2, 0.15 and the remainder. What's its entropy?
Check
What's the binary entropy H(0.11)?
Check
A source has entropy 2.2 bits, and a code for it averages 2.25 bits per symbol. How efficient is the code?
Check
A source has 26 equally likely symbols. What's its entropy?
Check
How many bits, at least, does a lossless code need on average for 1000 outputs of a binary source with P(1) = 0.02?
Check
A three-symbol source has probabilities 0.6, 0.2 and the remainder (with 0.6 the largest). Huffman gives the largest a 1-bit code and the others 2 bits each.
(a)
What is its entropy?
(b)
How efficient is the Huffman code?
Check
A binary source emits 1s with probability p.
Step 1
What is its entropy?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the entropy of a pair of its symbols?
Step 3
What is a 1-bit-per-symbol code's efficiency, in percent?
Explain why
Why is entropy the limit of lossless compression?
A long sequence of n symbols from a source of entropy H almost always falls among about "typical" sequences, each roughly equally likely, while all other sequences together have vanishing probability. A code needs only to give each typical sequence a distinct label, which takes about nH bits, so H bits per symbol suffice for long blocks. Fewer bits per symbol can't work: with fewer than labels, some typical sequences, which together carry nearly all the probability, would have to share labels and couldn't be decoded. Entropy is thus both achievable and unbeatable, and practical codes like Huffman's approach it, exactly so when the probabilities are powers of 1/2.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.