Objective
Be able to compute the entropy of a discrete distribution, explain what it measures, build a Huffman code and use cross-entropy and KL divergence to measure the cost of a wrong model. These are the tools the rest of the module is built on.
Intuition: surprise, averaged
An outcome with probability carries bits of surprise. A fair coin landing heads: 1 bit. A specific card from a shuffled deck: bits. Something certain: 0 bits. Rare events are surprising, and independent surprises add because their probabilities multiply.
Entropy is the expected surprise:
with . Read it as "the average number of well-chosen yes/no questions needed to learn ". With natural logs the unit is the nat; bits.
| Distribution | Entropy (bits) |
|---|---|
| Fair coin | 1 |
| Coin with | |
| Fair die | |
| Sum of two dice | |
| Uniform on outcomes |
The binary entropy function is symmetric about , peaks at 1 bit there and falls to 0 at . It appears constantly, so it is worth remembering and .
Maximum entropy
Entropy is a measure of spread, so the natural question is which distribution is most spread out under a given constraint.
- Finite support of size : the uniform distribution, with . Every other distribution has strictly less (proof below via KL divergence).
- Support with a fixed mean: the geometric distribution.
- Support with a fixed mean: the exponential distribution (for continuous variables, using differential entropy ).
- The real line with a fixed variance : the normal distribution, with differential entropy bits.
In general, maximising entropy subject to gives , an exponential family. This is why "assume the normal distribution" is the least-committal choice when you only know a mean and a variance.
Joint and conditional entropy
For a pair , the joint entropy is . The conditional entropy averages the entropy left in after seeing :
The chain rule says learning the pair is learning and then given :
Two consequences: (conditioning cannot increase entropy on average), and with equality exactly when and are independent.
Example: roll a fair die, the face and its parity. is a function of , so . The other order gives , as it must: once you know the parity, three faces remain.
Source coding: why entropy counts bits
A prefix code assigns each symbol a binary codeword so that no codeword is a prefix of another; a stream of codewords can then be decoded without separators. Codeword lengths are achievable by a prefix code if and only if they satisfy the Kraft inequality .
Source coding theorem. Every prefix code has expected length , and the best one has . Coding blocks of symbols at once brings the per-symbol cost within of .
The lower bound is a one-line consequence of the next section: with , . The ideal length of symbol is , exactly its surprise.
A prefix code is also a questioning strategy: the first bit answers "is the symbol in the left half of the tree?", and so on. So is a lower bound on the expected number of yes/no questions needed to identify , and the optimal code is the optimal strategy.
Huffman's algorithm
Huffman coding builds an optimal prefix code greedily: repeatedly merge the two least likely symbols (or merged groups) into one node whose probability is their sum, until one node remains. Each merge adds one bit to every symbol inside the two merged nodes.
Worked example
a Huffman code
A source emits A, B, C, D, E with probabilities . Build a Huffman code, find its expected length and compare it with the entropy and with a fixed-length code.
Show solutionHide solution
Merges (always the two smallest):
- D (0.10) + E (0.05) → DE (0.15).
- C (0.15) + DE (0.15) → CDE (0.30).
- B (0.30) + CDE (0.30) → BCDE (0.60).
- A (0.40) + BCDE (0.60) → root.
Codewords (left = 0): A = 0, B = 10, C = 110, D = 1110, E = 1111. Lengths ; Kraft sum .
Expected length:
Entropy:
A fixed-length code needs bits. Huffman saves 32% and sits within 0.05 bits of the entropy bound; the gap exists because ideal lengths like are not integers. When every probability is a power of , Huffman hits exactly.
Cross-entropy and KL divergence
Suppose the true distribution is but you design your code (or your bets, or your model) for , using lengths . The expected length is the cross-entropy
where the Kullback–Leibler divergence
is the extra bits per symbol you pay for using the wrong model.
Properties worth knowing cold:
- Gibbs' inequality: , with equality only if . Proof: by Jensen.
- Not symmetric: in general, so it is not a distance. For coins, bits but bits.
- Infinite when rules out what allows: if the divergence is infinite. Overconfident models are punished without limit.
- Maximum entropy, proved: with uniform on points, .
Machine-learning link. Minimising the average log loss is minimising the cross-entropy between the empirical distribution and the model. Since does not depend on , this is the same as minimising , which is the same as maximum likelihood.
Worked example
the cost of the wrong code
Four symbols occur with probabilities . An engineer assumes they are equally likely and uses a 2-bit code. How many bits per symbol does this cost, and how much is wasted?
Show solutionHide solution
Optimal cost. All probabilities are powers of , so the ideal lengths are integers (codewords 0, 10, 110, 111) and
Actual cost. With uniform, every symbol costs bits, so .
Waste. bits per symbol, a 14% overhead. Directly: .
The same distribution reappears in the betting lesson, where these 0.25 bits become a growth rate.
Knowledge check
A coin lands heads with probability 0.9. What is the entropy of one flip, to three decimals?
Knowledge check
You must identify the face of a fair six-sided die using an optimal prefix code (equivalently, an optimal yes/no questioning strategy) for one roll at a time. What is the expected number of bits?
Knowledge check
A fair coin is encoded with the ideal code for a coin with , i.e. lengths for heads and for tails. What is the expected number of bits per flip?
Key takeaways
- : expected surprise, and the minimum average number of yes/no questions to identify .
- The uniform distribution maximises entropy on a finite set (); the normal maximises it for a given variance.
- Chain rule: ; conditioning never increases entropy on average.
- Prefix codes satisfy Kraft, have , and Huffman achieves .
- Cross-entropy ; KL divergence is the non-negative, asymmetric cost of using the wrong model, and minimising log loss minimises it.
Finished reading?
Answer the 3 knowledge checks to complete this lesson (0 done).