---
title: Entropy and source coding
heading: Measuring information in bits, entropy and the binary entropy function, the source coding theorem, Huffman codes and their efficiency
eyebrow: S8 · Lesson 30 · Entropy and source coding
xp: 50
---

::: hook
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.
:::

::: play
- The binary entropy function $H(p) = -p\log_2p - (1 - p)\log_2(1 - p)$: 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.
:::
:::

```sim plot
view: graph
x: p = 0.001..0.999
y: H = 0..1.05
slider: q = 0.01..0.99 = 0.1
plot: -p*log(p)/log(2) - (1 - p)*log(1 - p)/log(2) | samples: 600 | label: H(p), bits
point: q, -q*log(q)/log(2) - (1 - q)*log(1 - q)/log(2) | label: H(q)
readout: H(q), bits = -q*log(q)/log(2) - (1 - q)*log(1 - q)/log(2)
```

::: 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?

- [ ] 2, one of four symbols
- [x] 1.75
- [ ] 1
- [ ] 4

::: reveal
The entropy is $0.5(1) + 0.25(2) + 2 \times 0.125(3)$ = 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%.
:::
:::

::: nameit
## Information and entropy

A symbol of probability p carries **self-information** $I = -\log_2p$ bits: rare events carry more. A source's **entropy** is the average:

$$H = -\sum_ip_i\log_2p_i\text{ bits/symbol}$$

with $0 \le H \le \log_2M$ 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.
:::

::: 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.

::: solution
1. **Entropy.** The four terms $-p\log_2 p$ are
   $$\begin{aligned} H &= 0.529 + 0.521 \\ &\quad + 0.464 + 0.332 \\ &= 1.846\ \text{bits} \end{aligned}$$
2. **Huffman code.** The lengths are 1, 2, 3 and 3, so the average is
   $$\begin{aligned} \bar{L} &= 0.4 + 0.6 + 0.6 + 0.3 \\ &= 1.9\ \text{bits} \end{aligned}$$
3. **Efficiency.**
   $$\frac{1.846}{1.9} = 97.2\%$$
4. **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.
:::
:::

::: example
## A binary source emits 1s with probability 0.1. How much could a million of its bits be compressed?

::: solution
1. **Entropy.**
   $$H(0.1) = 0.469\ \text{bits per symbol}$$
2. **A million bits.** On average they need only
   $$10^6 \times 0.469 = 469\,000\ \text{bits}$$
   less than half.
3. **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.
:::
:::

::: example
## A binary source emits 1s with probability 0.1. How efficient is Huffman coding of single symbols, and of pairs?

::: solution
1. **Entropy.**
   $$H(0.1) = 0.469\ \text{bits per symbol}$$
2. **Single symbols.** They can't be coded in less than 1 bit each, so the efficiency is 47%.
3. **Pairs.** The pairs have probabilities 0.81, 0.09, 0.09 and 0.01, and Huffman gives them lengths 1, 2, 3 and 3:
   $$\begin{aligned} \bar{L} &= 0.81 + 0.18 + 0.27 + 0.03 \\ &= 1.29\ \text{bits per pair} \end{aligned}$$
   which is 0.645 per symbol.
4. **Efficiency of pairs.**
   $$\frac{0.469}{0.645} = 73\%$$
   Longer blocks approach the entropy.

**Answer:** 47% efficient for single symbols and 73% for pairs.
:::
:::

::: check
given: p = 0.5 | 0.1 | 0.01 | 0.0001
answer: -log(p)/log(2)
tolerance: 0.5%

## An event has probability {p}. How much information does its occurrence carry?

::: right
Right. $-\log_2p$ = {answer} bits.
:::

::: misses
- [-log(p)] Use log base 2 for bits.
- [-ln(p)] That's in nats; divide by ln 2 for bits.
- [any] Not yet. $I = -\log_2p$.
:::
:::

::: check
given: a = 0.4 | 0.5 | 0.6; b = 0.2 | 0.25; c = 0.1 | 0.15
answer: -(a*log(a) + b*log(b) + c*log(c) + (1 - a - b - c)*log(1 - a - b - c))/log(2)
tolerance: 0.5%

## A four-symbol source has probabilities {a}, {b}, {c} and the remainder. What's its entropy?

::: right
Right. $-\sum p_i\log_2p_i$ = {answer} bits.
:::

::: misses
- [2] That's the maximum, for equally likely symbols.
- [any] Not yet. Include the fourth probability, 1 − (the others).
:::
:::

::: check
given: p = 0.05 | 0.11 | 0.25 | 0.4
answer: -(p*log(p) + (1 - p)*log(1 - p))/log(2)
tolerance: 0.5%

## What's the binary entropy H({p})?

::: right
Right. {answer} bits.
:::

::: misses
- [-p*log(p)/log(2)] Include the other outcome's term.
- [any] Not yet. $H(p) = -p\log_2p - (1 - p)\log_2(1 - p)$.
:::
:::

::: check
given: H = 1.5 | 2.2 | 3.1; d = 0.05 | 0.15 | 0.4; let L = H + d
answer: 100*H/L
unit: %
tolerance: 0.5%

## A source has entropy {H} bits, and a code for it averages {L} bits per symbol. How efficient is the code?

::: right
Right. H/L = {answer}%.
:::

::: misses
- [100*L/H] Efficiency is entropy over average length, at most 100%.
- [any] Not yet. Efficiency = H/L.
:::
:::

::: check
given: M = 4 | 26 | 256 | 1000
answer: log(M)/log(2)
tolerance: 0.5%

## A source has {M} equally likely symbols. What's its entropy?

::: right
Right. $\log_2M$ = {answer} bits, the maximum for {M} symbols.
:::

::: misses
- [M] Entropy counts bits, which grow as the logarithm of the number of symbols.
- [any] Not yet. Each symbol has probability 1/M.
:::
:::

::: check
given: N = 1000 | 1000000; p = 0.02 | 0.1 | 0.3
answer: N*(-(p*log(p) + (1 - p)*log(1 - p))/log(2))
tolerance: 0.5%

## How many bits, at least, does a lossless code need on average for {N} outputs of a binary source with P(1) = {p}?

::: right
Right. N × H(p) = {answer} bits.
:::

::: misses
- [N] That's the raw length; a biased source carries less than a bit per symbol.
- [any] Not yet. Multiply the entropy by the number of symbols.
:::
:::

::: check
kind: parts
given: a = 0.5 | 0.6 | 0.7; b = 0.2 | 0.25

## A three-symbol source has probabilities {a}, {b} and the remainder (with {a} the largest). Huffman gives the largest a 1-bit code and the others 2 bits each.

::: part
answer: -a*log(a)/log(2) - b*log(b)/log(2) - (1 - a - b)*log(1 - a - b)/log(2)
unit: bits
tolerance: 0.5%

### What is its entropy?

::: misses
- [any] $-\sum p_i\log_2p_i$.
:::
:::

::: part
answer: (-a*log(a)/log(2) - b*log(b)/log(2) - (1 - a - b)*log(1 - a - b)/log(2))/(2 - a)*100
unit: %
tolerance: 0.5%

### How efficient is the Huffman code?

::: misses
- [any] H over the average length 2 − a.
:::
:::
:::

::: check
kind: derivation
vars: p = 0.01..0.5

## A binary source emits 1s with probability p.

::: step
answer: -p*log(p)/log(2) - (1 - p)*log(1 - p)/log(2)

### What is its entropy?
:::

::: step
answer: 2*(-p*log(p)/log(2) - (1 - p)*log(1 - p)/log(2))

### What is the entropy of a pair of its symbols?
:::

::: step
answer: (-p*log(p)/log(2) - (1 - p)*log(1 - p)/log(2))*100

### What is a 1-bit-per-symbol code's efficiency, in percent?
:::

::: right
Right. Block coding is what lets a code beat one bit per symbol for skewed sources.
:::
:::

::: explain
## Why is entropy the limit of lossless compression?

::: model
A long sequence of n symbols from a source of entropy H almost always falls among about $2^{nH}$ "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 $2^{nH}$ 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

::: check
given: p = 0.5 | 0.25 | 0.125 | 0.1 | 0.01 | 0.001
answer: -log(p)/log(2)
tolerance: 0.5%

## Information in an event of probability {p}?

::: misses
- [any] $-\log_2p$.
:::
:::

::: check
given: p = 0.02 | 0.05 | 0.08 | 0.1 | 0.2 | 0.3 | 0.4
answer: -(p*log(p) + (1 - p)*log(1 - p))/log(2)
tolerance: 0.5%

## H({p})?

::: misses
- [any] Binary entropy.
:::
:::

::: check
given: M = 2 | 4 | 8 | 10 | 26 | 64 | 256
answer: log(M)/log(2)
tolerance: 0.5%

## Entropy of {M} equally likely symbols?

::: misses
- [any] $\log_2M$.
:::
:::
:::
