---
title: Information and entropy
heading: How many bits a random source really produces
eyebrow: A11 · Lesson 49 · Information and entropy
xp: 40
---

::: hook
A fair coin toss carries one bit of information, and a fair die about 2.6 bits. A biased coin that lands heads 99% of the time carries much less: you can nearly predict it, so seeing it tells you little. Shannon made this exact. The **entropy** of a random source, in bits per symbol, is the smallest average number of bits any code can use to describe it, and the same quantity sets how much information a noisy channel can carry. It's where probability becomes the theory of compression and communication.
:::

::: play
- The curve is the binary entropy $H(p) = -p\log_2p - (1 - p)\log_2(1 - p)$: the information in one toss of a coin that lands heads with probability $p$.
- It peaks at 1 bit for a fair coin and falls to 0 for a coin that always lands the same way.
- The readout also gives the capacity of a binary channel that flips each bit with probability $p$: $1 - H(p)$ bits per use.
:::

```sim plot
view: graph
x: p = 0.001..0.999
y: bits = 0..1.05
slider: q = 0.01..0.5 step 0.01 = 0.1
plot: -p*log(p)/log(2) - (1 - p)*log(1 - p)/log(2) | label: H(p) | samples: 600
marker: p = q | label: chosen p
readout: H at the chosen p, bits = -q*log(q)/log(2) - (1 - q)*log(1 - q)/log(2) | digits: 4
readout: capacity of a channel flipping bits with that p = 1 + q*log(q)/log(2) + (1 - q)*log(1 - q)/log(2) | digits: 4
```

::: predict
## A source emits A, B, C and D with probabilities 0.5, 0.25, 0.125 and 0.125. What is the fewest bits per symbol any code can average?

- [ ] 2 bits
- [x] 1.75 bits
- [ ] 1 bit
- [ ] 0.5 bits

::: reveal
1.75 bits. The entropy is $0.5 \times 1 + 0.25 \times 2 + 2 \times 0.125 \times 3 = 1.75$, and the code A = 0, B = 10, C = 110, D = 111 achieves it exactly: common symbols get short codewords. A fixed 2-bit code wastes a quarter of a bit per symbol.
:::
:::

::: nameit
## Entropy

For a source whose symbols have probabilities $p_i$:

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

- $H = 0$ when one symbol is certain; $H = \log_2n$ for $n$ equally likely symbols, the most possible.
- A symbol of probability $p$ carries $-\log_2p$ bits of "surprise"; entropy is the average.

## Source coding

No code can average fewer than $H$ bits per symbol (without losing information), and codes like Huffman's come within one bit of it, or arbitrarily close when they encode blocks of symbols. That's the limit for every lossless compressor.

## Channel capacity

A binary symmetric channel flips each bit with probability $p$. Its **capacity** is

$$C = 1 - H(p) \quad \text{bits per use}$$

Shannon's theorem: below that rate, codes exist that make the error rate as small as you like; above it, they can't. For the Gaussian channel the capacity is $B\log_2(1 + \text{SNR})$ bits per second.
:::

::: example
## How much information does one roll of a fair die carry?

::: solution
1. **Entropy.**
   $$\begin{aligned} H &= \log_2 6 \\ &= 2.585\ \text{bits} \end{aligned}$$
2. **A 3-bit code.** A 3-bit code per roll wastes 0.415 bits.
3. **Five rolls together.** Coding five rolls together into one number from 0 to 7775 needs 13 bits, 2.6 per roll, nearly the limit.

**Answer:** one roll carries 2.585 bits.
:::
:::

::: example
## A source emits A, B, C, D with probabilities 0.5, 0.25, 0.125, 0.125. Find its entropy and a code that achieves it.

::: solution
1. **Entropy.**
   $$\begin{aligned} H &= 0.5(1) + 0.25(2) \\ &\quad + 0.125(3) + 0.125(3) \\ &= 1.75\ \text{bits} \end{aligned}$$
2. **Huffman code.** Huffman's method merges the two least likely symbols repeatedly and gives A = 0, B = 10, C = 110, D = 111.
3. **Average length.**
   $$\begin{aligned} \bar L &= 0.5 + 0.5 + 0.375 + 0.375 \\ &= 1.75\ \text{bits} \end{aligned}$$
4. **Why it's exact.** When every probability is a power of $\frac12$, the code meets the entropy exactly.

**Answer:** the entropy is 1.75 bits, and the code A = 0, B = 10, C = 110, D = 111 achieves it.
:::
:::

::: example
## A channel flips 10% of its bits. What is its capacity?

::: solution
1. **Binary entropy.**
   $$\begin{aligned} H(0.1) &= -0.1\log_2 0.1 \\ &\quad - 0.9\log_2 0.9 \\ &= 0.332 + 0.137 \\ &= 0.469\ \text{bits} \end{aligned}$$
2. **Capacity.**
   $$\begin{aligned} C &= 1 - 0.469 \\ &= 0.531 \end{aligned}$$
   bits per use.
3. **What it means.** With good enough codes, about 53 bits of message can be sent reliably for every 100 sent over this channel, despite one in ten being wrong.

**Answer:** the capacity is 0.531 bits per use.
:::
:::

::: check
given: n = 4 | 10 | 26
answer: log(n)/log(2)
unit: bits
tolerance: 0.5%

## A source picks one of {n} symbols, all equally likely. What is its entropy?

::: right
Right. $\log_2n$ = {answer}.
:::

::: misses
- [log(n)] Use base 2 for bits.
- [n] Entropy is a logarithm of the number of choices.
- [any] Not yet. $H = \log_2n$ for equally likely symbols.
:::
:::

::: check
given: p = 0.01 | 0.1 | 0.3
answer: -p*log(p)/log(2) - (1 - p)*log(1 - p)/log(2)
unit: bits
tolerance: 0.5%

## A coin lands heads with probability {p}. How much information does one toss carry?

::: right
Right. $H(p)$ = {answer}.
:::

::: misses
- [-log(p)/log(2)] That's the surprise of heads alone; average over both outcomes.
- [any] Not yet. $-p\log_2p - (1 - p)\log_2(1 - p)$.
:::
:::

::: check
given: p = 0.01 | 0.05 | 0.2
answer: 1 + p*log(p)/log(2) + (1 - p)*log(1 - p)/log(2)
unit: bits
tolerance: 0.5%

## A binary channel flips each bit with probability {p}. What is its capacity per use?

::: right
Right. $1 - H(p)$ = {answer}.
:::

::: misses
- [1 - p] Errors cost more than their own fraction: you don't know which bits are wrong.
- [any] Not yet. $C = 1 - H(p)$.
:::
:::

::: check
given: B = 3 | 20; s = 20 | 30
answer: B*log(1 + 10^(s/10))/log(2)
unit: kb/s
tolerance: 0.5%

## A channel has bandwidth {B} kHz and an SNR of {s} dB. What is its Shannon capacity?

::: right
Right. $B\log_2(1 + \text{SNR})$ = {answer}.
:::

::: misses
- [B*log(1 + s)/log(2)] Convert the SNR from dB first.
- [any] Not yet. $C = B\log_2(1 + \text{SNR})$ with SNR as a power ratio.
:::
:::

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

## A source emits three symbols with probabilities {a}, {b} and the rest.

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

### What is its entropy?

::: misses
- [log(3)/log(2)] That's for equally likely symbols, the maximum.
- [any] $-\sum p_i\log_2p_i$.
:::
:::

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

### What average length does the code 0, 10, 11 give?

::: misses
- [any] Weight each codeword's length by its probability.
:::
:::
:::

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

## Find the binary entropy and where it peaks.

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

### What is $H(p)$?
:::

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

### Its derivative is $\log_2\frac{1 - p}{p}$. Evaluate it (it's zero at $p = \frac12$).
:::

::: right
Right. The derivative vanishes at $p = \frac12$, where $H = 1$ bit: a fair coin is the least predictable.
:::
:::

::: check
kind: python

## Write `entropy(ps)` returning $-\sum p\log_2p$ (skipping zeros). Set `h1` for a fair die and `h2` for the probabilities 0.5, 0.25, 0.125, 0.125.

```python starter
import math

def entropy(ps):
    return 0.0

h1 = entropy([1 / 6] * 6)
h2 = entropy([0.5, 0.25, 0.125, 0.125])
print(h1, h2)
```

```python solution
import math

def entropy(ps):
    return -sum(p * math.log2(p) for p in ps if p > 0)

h1 = entropy([1 / 6] * 6)
h2 = entropy([0.5, 0.25, 0.125, 0.125])
print(h1, h2)
```

```python tests
import math
assert abs(h1 - math.log2(6)) < 1e-12 and abs(h2 - 1.75) < 1e-12, f"A die carries 2.585 bits and the four-symbol source 1.75; got {h1}, {h2}."
assert entropy([1, 0]) == 0, "A certain outcome carries no information."
```

::: right
Right. 2.585 and 1.75 bits; the second matches the Huffman code's average length exactly.
:::
:::

::: explain
## Why does a channel that flips 10% of bits carry far less than 90% of a bit per use?

::: model
If you knew which bits were flipped you could fix them, and you'd keep 90%. But the receiver doesn't know: every received bit is uncertain, and resolving that uncertainty costs information. The uncertainty per bit is the binary entropy $H(0.1) = 0.47$ bits, so only $1 - 0.47 = 0.53$ bits of each use can carry the message. Error-correcting codes spend the rest on redundancy that lets the receiver locate and undo the flips; Shannon showed that at any rate below 0.53, the error rate can be driven as low as you like, and above it no code can do that.
:::
:::

::: practice

::: check
given: p = 0.001..0.5
answer: -p*log(p)/log(2) - (1 - p)*log(1 - p)/log(2)
tolerance: 0.5%

## Binary entropy at $p$ = {p}, in bits?

::: misses
- [any] $H(p)$.
:::
:::

::: check
given: B = 1..1000; s = 0..40
answer: B*log(1 + 10^(s/10))/log(2)
tolerance: 0.5%

## Shannon capacity in kb/s for {B} kHz at {s} dB SNR?

::: misses
- [any] $B\log_2(1 + 10^{s/10})$.
:::
:::
:::
