Back to the on-screen lesson ·
Coding a whole message as a subinterval of [0, 1); reaching the entropy without blocking.
Paper packet. Every task here also exists on screen, where it is checked automatically; answers written on paper are not assessed by Nydus. When you are back at a device, enter your answers there.
By the end of this lesson you will be able to define the entropy rate of a stationary source, prove that its two natural definitions agree, explain why memory can only lower it, and compute it for i.i.d. sources and for stationary Markov chains as the stationary average of the row entropies. You will run arithmetic coding by hand on a short message, find the interval it produces and the number of bits it needs, and explain why its overhead is two bits per message rather than up to one bit per symbol, which is how practical compressors reach the entropy rate without grouping symbols into blocks.
Arithmetic coding represents a whole message as a subinterval of $[0, 1)$. Split $[0, 1)$ into pieces proportional to the symbol probabilities, in a fixed order; the first symbol selects its piece; the second symbol splits that piece in the same proportions and selects again; and so on. After $n$ symbols the interval has width $P(x_1) P(x_2) \cdots P(x_n) = P(x^n)$, and any number inside it identifies the message, given $n$. A binary fraction with $\lceil -\log_2 P(x^n) \rceil + 1$ bits is guaranteed to lie inside, so the code length is at most $-\log_2 P(x^n) + 2$ bits: the message's own information plus two bits, however long the message is. Per symbol this tends to the entropy, or the entropy rate when the probabilities are conditional on the past, with no blocking and no waste per symbol, unlike Huffman codes. The probabilities may change as the message proceeds, which is what adaptive compressors and modern context-mixing methods exploit; the decoder reproduces the same splits and reads the symbols back.
Another way: picture
The unit interval with $a$, $b$, $c$ marked as pieces of widths $0.5$, $0.25$, $0.25$. Zoom into $b = [0.5, 0.75)$: it is split again in the same proportions, and the message $ba$ is the left half, $[0.5, 0.625)$. Every extra symbol zooms in once more.
Another way: steps
Fix the alphabet order and the cumulative probabilities; here $a = [0, 0.5)$, $b = [0.5, 0.75)$, $c = [0.75, 1)$. Start with $[0, 1)$ and, for each symbol, replace the current interval by the sub-piece the symbol names, in the same proportions. Coding $bac$: $b$ selects $[0.5, 0.75)$ of width $0.25$; inside it, $a$ takes the first half, $[0.5, 0.625)$; inside that, $c$ takes the last quarter, $[0.59375, 0.625)$, of width $2^{-5}$.
The width after $n$ symbols is exactly $P(x_1) P(x_2) \cdots P(x_n) = P(x^n)$: the interval is the probability. Every two-symbol message and its interval:
| message | interval $[\text{lo}, \text{hi})$ | width | $-\log_2$ width |
|---|---|---|---|
| $aa$ | $[0, 0.25)$ | $0.25$ | $2$ |
| $ab$ | $[0.25, 0.375)$ | $0.125$ | $3$ |
| $ac$ | $[0.375, 0.5)$ | $0.125$ | $3$ |
| $ba$ | $[0.5, 0.625)$ | $0.125$ | $3$ |
| $bb$ | $[0.625, 0.6875)$ | $0.0625$ | $4$ |
| $ca$ | $[0.75, 0.875)$ | $0.125$ | $3$ |
| $cc$ | $[0.9375, 1)$ | $0.0625$ | $4$ |
Any number inside the final interval identifies the message. An interval of width $w$ always contains a binary fraction with $\lceil -\log_2 w \rceil + 1$ bits, so the code length is at most $$-\log_2 P(x^n) + 2$$ bits — the message's own information plus two bits, no matter how long the message is. Divide by $n$: the overhead per symbol is $2/n$, which vanishes. A Huffman symbol code, by contrast, pays its rounding on every symbol: a coin with $P(\text{heads}) = 0.9$ costs $1$ bit per flip against an entropy of $0.469$, while arithmetic coding of $1000$ flips costs about $469 + 2$ bits, or $0.471$ per flip.
Two more advantages follow from the same design. The probabilities may change at every step — a model that adapts to the text so far costs nothing extra, since only the current proportions matter. And the probabilities need not be dyadic, or even rational: the interval simply narrows by whatever factor the model gives.
Common mistakes
$a = [0, 0.5)$, $b = [0.5, 0.75)$, $c = [0.75, 1)$. After $b$: $[0.5, 0.75)$. After $a$: left half, $[0.5, 0.625)$.
Zoom twice.
After $c$: last quarter of that, $[0.59375, 0.625)$, width $2^{-5} = P(b) P(a) P(c)$. The binary fraction $0.10011 = 0.59375$ lies inside: five bits, plus one for safety, for a message of $5$ bits of information.
Width is the product of the probabilities.
A coin with $P(\text{heads}) = 0.9$: $H = 0.469$, but any symbol code spends $1$ bit per flip.
Arithmetic coding of $1000$ flips spends about $-\log_2 P(x^{1000}) + 2 \approx 469 + 2$ bits: $0.471$ per flip, essentially the entropy.
Two bits of overhead for the whole message.
First $c$: the last quarter of $[0, 1)$, that is $[0.75, 1)$, of width $0.25$.
Second $c$: the last quarter of that, $0.75 + 0.75 \cdot 0.25 = 0.9375$ to $1$.
The proportions repeat inside every interval.
$[0.9375, 1)$, width $0.0625 = P(c)^2$.
$8$ copies of $x$ ($p = \tfrac{1}{2}$) and $4$ of $y$ ($p = \tfrac{1}{4}$).
Width $= 2^{-8} \cdot 4^{-4} = 2^{-16}$, so $-\log_2 w = 16$.
$8 \cdot 1 + 4 \cdot 2 = 16$ bits of information in the message.
$\lceil 16 \rceil + 1 = 17$ bits: one bit of overhead spread over twelve symbols.
After $c$: $[0.75, 1)$; then the last quarter of that.
$[0.9375, 1)$, width $\tfrac{1}{16} = P(c)^2$.
An arithmetic coder uses $a = [0, 0.5)$, $b = [0.5, 0.75)$, $c = [0.75, 1)$. Which interval $[\text{lo}, \text{hi})$ represents the message $cc$?
lo = lo, hi = hi
Symbols $x$ and $y$ have probabilities $\tfrac{1}{2}$ and $\tfrac{1}{4}$. After arithmetic-coding a message with $1$ copies of $x$ and $2$ copies of $y$, what is $-\log_2$ of the interval width?
Computed value: answer
The same message, $3$ copies of $x$ (probability $\tfrac{1}{2}$) and $1$ copies of $y$ (probability $\tfrac{1}{4}$), is arithmetic-coded with the rule 'use $\lceil -\log_2 \text{width} \rceil + 1$ bits'. How many bits does it take?
Computed value: answer
An arithmetic coder uses $a = [0, 0.5)$, $b = [0.5, 0.75)$, $c = [0.75, 1)$. Which interval $[\text{lo}, \text{hi})$ represents the message $ba$?
lo = lo, hi = hi
Why does arithmetic coding approach the entropy where a Huffman symbol code may not?
Lesson test: one question per skill, one attempt each, no hints. Your answers are checked when you submit.
Symbols $x$ and $y$ have probabilities $\tfrac{1}{2}$ and $\tfrac{1}{4}$. After arithmetic-coding a message with $2$ copies of $x$ and $5$ copies of $y$, what is $-\log_2$ of the interval width?
Computed value: answer
You can find the entropy rate of a source with memory and code a message to within two bits of its information. This closes source coding; the next unit turns to noisy channels.
10. Your turn: the interval for $cc$, step 2