◂ the notebook

No. 0922 February 2026.
Probability and statistics, MT2204. Code on GitHub.

A message is an interval: arithmetic coding

Arithmetic coding gives every symbol a slice of [0, 1) as wide as its probability, zooms into the slice, and repeats. The seven letters of my name leave an interval about five millionths wide. A binary codeword that stays inside it, whatever bits follow, takes 19 bits; the entropy of the letters says 17.65 bits is the floor.

DESMONDmessageDEMNOSstart[0.0000000, 1.0000000)DEMNOSD[0.0000000, 0.2857143)DEMNOSE[0.0816327, 0.1224490)DEMNOSS[0.1166181, 0.1224490)DEMNOSM[0.1191170, 0.1199500)DEMNOSO[0.1197120, 0.1198310)DEMNOSN[0.1197800, 0.1197970)final interval, 4.86 × 10⁻⁶ wideD[0.1197800, 0.1197849)0.0001111010101010000₂19 bits · floor 17.65

Any number in the final interval decodes to DESMOND. Write bits until the binary interval they name fits inside it: 19 bits, 0.0001111010101010000. −log₂ of the width, 17.65 bits, is the floor.

[0.1197800, 0.1197849) · width 4.86 × 10⁻⁶ · 17.65 bits so far

DESMONDDEMNOSstartDEMNOSDDEMNOSEDEMNOSSDEMNOSMDEMNOSODEMNOSNfinal interval, 4.86 × 10⁻⁶ wideD0.0001111010101010000₂19 bits · floor 17.65

Any number in the final interval decodes to DESMOND. Write bits until the binary interval they name fits inside it: 19 bits, 0.0001111010101010000. −log₂ of the width, 17.65 bits, is the floor.

[0.1197800, 0.1197849) · width 4.86 × 10⁻⁶ · 17.65 bits so far

Encoding DESMOND with symbol counts taken from the message itself, the fixed model my encoder uses. Each row zooms into the highlighted slice of the row above; pause or step through it with the controls. Computed on this page.

the mathematics

The algorithm, written out

The animation is a picture of a short chain of definitions: a probability mass function, its cumulative distribution, and one affine map per symbol. Here they are in order, with the bounds that make the codeword short, and every number for my name computed from the same model.

§ 1
the model

A probability mass function

Fix an alphabet of KK symbols in some order, A={a1,a2,…,aK}\mathcal{A} = \{a_1, a_2, \dots, a_K\}. An order-0 model treats each symbol of a message x1x2⋯xnx_1 x_2 \cdots x_n as an independent draw from one probability mass function. My encoder takes it from the message itself: if aka_k occurs ckc_k times,

p(ak)=Pr⁡[X=ak]=ckn,p(ak)>0,∑k=1Kp(ak)=1.(1)p(a_k) = \Pr[X = a_k] = \frac{c_k}{n}, \qquad p(a_k) > 0, \qquad \sum_{k=1}^{K} p(a_k) = 1. \tag{1}
p(ak)=Pr⁡[X=ak]=ckn,p(ak)>0,∑k=1Kp(ak)=1.(1)\begin{gathered} p(a_k) = \Pr[X = a_k] = \frac{c_k}{n}, \\ p(a_k) > 0, \qquad \sum_{k=1}^{K} p(a_k) = 1. \end{gathered} \tag{1}

DESMOND has n=7n = 7 letters over K=6K = 6 symbols. In alphabetical order, with the cumulative values of §2 in the last row:

akDEMNOSck211111p(ak)271717171717F(ak)27374757671\def\arraystretch{2.1}\begin{array}{c|cccccc} a_k & \mathrm{D} & \mathrm{E} & \mathrm{M} & \mathrm{N} & \mathrm{O} & \mathrm{S} \\ \hline c_k & 2 & 1 & 1 & 1 & 1 & 1 \\ p(a_k) & \dfrac{2}{7} & \dfrac{1}{7} & \dfrac{1}{7} & \dfrac{1}{7} & \dfrac{1}{7} & \dfrac{1}{7} \\ F(a_k) & \dfrac{2}{7} & \dfrac{3}{7} & \dfrac{4}{7} & \dfrac{5}{7} & \dfrac{6}{7} & 1 \end{array}

§ 2
slices

The cumulative distribution cuts [0, 1) into slices

Adding the masses in alphabet order gives the cumulative distribution function, with F(a0)=0F(a_0) = 0 by convention:

F(ak)=∑j=1kp(aj),0=F(a0)<F(a1)<⋯<F(aK)=1.(2)F(a_k) = \sum_{j=1}^{k} p(a_j), \qquad 0 = F(a_0) < F(a_1) < \cdots < F(a_K) = 1. \tag{2}
F(ak)=∑j=1kp(aj),0=F(a0)<F(a1)<⋯<F(aK)=1.(2)\begin{gathered} F(a_k) = \sum_{j=1}^{k} p(a_j), \\ 0 = F(a_0) < F(a_1) < \cdots < F(a_K) = 1. \end{gathered} \tag{2}

Each symbol owns the half-open slice between consecutive values, exactly as wide as its probability:

I(ak)=[ F(ak−1), F(ak) ),∣I(ak)∣=F(ak)−F(ak−1)=p(ak).(3)I(a_k) = \big[\, F(a_{k-1}),\ F(a_k) \,\big), \qquad \lvert I(a_k) \rvert = F(a_k) - F(a_{k-1}) = p(a_k). \tag{3}
I(ak)=[ F(ak−1), F(ak) ),∣I(ak)∣=F(ak)−F(ak−1)=p(ak).(3)\begin{aligned} I(a_k) &= \big[\, F(a_{k-1}),\ F(a_k) \,\big), \\ \lvert I(a_k) \rvert &= F(a_k) - F(a_{k-1}) \\ &= p(a_k). \end{aligned} \tag{3}

The slices are half-open, so neighbours share no endpoint: every u∈[0,1)u \in [0, 1) lies in exactly one of them, and together they tile [0,1)[0, 1). For my name, I(D)=[0,27)I(\mathrm{D}) = [0, \tfrac{2}{7}), I(E)=[27,37)I(\mathrm{E}) = [\tfrac{2}{7}, \tfrac{3}{7}), and so on up to I(S)=[67,1)I(\mathrm{S}) = [\tfrac{6}{7}, 1).

a choice

Any fixed order of the alphabet works, as long as the encoder and decoder agree. Mine sorts the symbols, so D comes first.

§ 3
encoding

Each symbol is an affine map

Encoding starts from the whole unit interval, [L0,H0)=[0,1)[L_0, H_0) = [0, 1). The current interval [L,H)[L, H) is a shifted, scaled copy of [0,1)[0, 1), and the map that makes the copy is affine:

ϕ[L,H)(u)=L+(H−L) u,ϕ[L,H)([0,1))=[L,H).(4)\phi_{[L,H)}(u) = L + (H - L)\, u, \qquad \phi_{[L,H)}\big([0, 1)\big) = [L, H). \tag{4}
ϕ[L,H)(u)=L+(H−L) u,ϕ[L,H)([0,1))=[L,H).(4)\begin{aligned} \phi_{[L,H)}(u) &= L + (H - L)\, u, \\ \phi_{[L,H)}\big([0, 1)\big) &= [L, H). \end{aligned} \tag{4}

To encode the next symbol xix_i, send its slice through that map. Writing F(x−)F(x^-) for the mass strictly below xx, which is F(ak−1)F(a_{k-1}) when x=akx = a_k:

Li=Li−1+(Hi−1−Li−1) F(xi−),Hi=Li−1+(Hi−1−Li−1) F(xi).(5)\begin{aligned} L_i &= L_{i-1} + (H_{i-1} - L_{i-1})\, F(x_i^-), \\ H_i &= L_{i-1} + (H_{i-1} - L_{i-1})\, F(x_i). \end{aligned} \tag{5}

The new interval lies inside the old one, so the intervals are nested, [0,1)⊃[L1,H1)⊃⋯⊃[Ln,Hn)[0,1) \supset [L_1, H_1) \supset \cdots \supset [L_n, H_n), and each step multiplies the width by the probability of the symbol it encodes:

Wi=Hi−Li=Wi−1 p(xi)⟹Wn=∏i=1np(xi)=Pr⁡[x1x2⋯xn].(6)W_i = H_i - L_i = W_{i-1}\, p(x_i) \quad\Longrightarrow\quad W_n = \prod_{i=1}^{n} p(x_i) = \Pr[x_1 x_2 \cdots x_n]. \tag{6}
Wi=Hi−Li=Wi−1 p(xi),Wn=∏i=1np(xi)=Pr⁡[x1x2⋯xn].(6)\begin{aligned} W_i &= H_i - L_i = W_{i-1}\, p(x_i), \\ W_n &= \prod_{i=1}^{n} p(x_i) = \Pr[x_1 x_2 \cdots x_n]. \end{aligned} \tag{6}

The width left is exactly the probability the model gives the whole message. Read as maps, encoding is composition: with ψx(u)=F(x−)+p(x) u\psi_x(u) = F(x^-) + p(x)\, u, the map that squeezes [0,1)[0, 1) onto I(x)I(x),

[Ln,Hn)=ψx1∘ψx2∘⋯∘ψxn([0,1)).(7)[L_n, H_n) = \psi_{x_1} \circ \psi_{x_2} \circ \cdots \circ \psi_{x_n}\big([0, 1)\big). \tag{7}

With probabilities ck/nc_k / n, every bound after ii symbols is a multiple of n−in^{-i}. Multiplying (5) through by nin^i leaves recurrences in integers, where C(x−)=nF(x−)C(x^-) = n F(x^-) counts the letters below xx:

niLi=n⋅ni−1Li−1+ni−1Wi−1 C(xi−),niWi=ni−1Wi−1 c(xi).(8)n^{i} L_i = n \cdot n^{i-1} L_{i-1} + n^{i-1} W_{i-1}\, C(x_i^-), \qquad n^{i} W_i = n^{i-1} W_{i-1}\, c(x_i). \tag{8}
niLi=n⋅ni−1Li−1+ni−1Wi−1 C(xi−),niWi=ni−1Wi−1 c(xi).(8)\begin{aligned} n^{i} L_i &= n \cdot n^{i-1} L_{i-1} \\ &\quad + n^{i-1} W_{i-1}\, C(x_i^-), \\ n^{i} W_i &= n^{i-1} W_{i-1}\, c(x_i). \end{aligned} \tag{8}

in the figure

Each row of the animation is one application of (5): the highlighted slice of a row becomes the next row, stretched to the full width.

§ 4
decoding

Why the interval names the message

Each step splits the current interval into KK disjoint pieces in proportion to pp. By induction on nn, the KnK^n messages of length nn get pairwise disjoint intervals that tile [0,1)[0, 1), so any number v∈[Ln,Hn)v \in [L_n, H_n) points at exactly one of them. Decoding runs (5) forwards again and reads each symbol by inverting the affine map:

ui=v−Li−1Hi−1−Li−1∈[0,1),xi=ak  for the k with F(ak−1)≤ui<F(ak).(9)u_i = \frac{v - L_{i-1}}{H_{i-1} - L_{i-1}} \in [0, 1), \qquad x_i = a_k \ \text{ for the } k \text{ with } F(a_{k-1}) \le u_i < F(a_k). \tag{9}
ui=v−Li−1Hi−1−Li−1∈[0,1),xi=ak  for the k withF(ak−1)≤ui<F(ak).(9)\begin{gathered} u_i = \frac{v - L_{i-1}}{H_{i-1} - L_{i-1}} \in [0, 1), \\ x_i = a_k \ \text{ for the } k \text{ with} \\ F(a_{k-1}) \le u_i < F(a_k). \end{gathered} \tag{9}

Once xix_i is known, [Li,Hi)[L_i, H_i) follows from (5) and the next symbol is read the same way. The decoder also has to know where to stop: the length nn, or an end-of-message symbol in the alphabet.

§ 5
the codeword

Bits that stay inside the interval

A binary fraction 0.b1b2…bℓ0.b_1 b_2 \ldots b_\ell stands for every number that begins with those bits, which is a dyadic interval:

D(m,ℓ)=[ m 2−ℓ, (m+1) 2−ℓ ),m=∑j=1ℓbj 2ℓ−j.(10)D(m, \ell) = \big[\, m\, 2^{-\ell},\ (m + 1)\, 2^{-\ell} \,\big), \qquad m = \sum_{j=1}^{\ell} b_j\, 2^{\ell - j}. \tag{10}
D(m,ℓ)=[ m 2−ℓ, (m+1) 2−ℓ ),m=∑j=1ℓbj 2ℓ−j.(10)\begin{gathered} D(m, \ell) = \big[\, m\, 2^{-\ell},\ (m + 1)\, 2^{-\ell} \,\big), \\ m = \sum_{j=1}^{\ell} b_j\, 2^{\ell - j}. \end{gathered} \tag{10}

The codeword is the shortest whose whole dyadic interval fits inside the message's interval, so the decoder lands in [Ln,Hn)[L_n, H_n) whatever bits follow:

ℓ∗=min⁡{ ℓ:D(m,ℓ)⊆[Ln,Hn) },m=⌈Ln 2ℓ⌉.(11)\ell^{*} = \min \big\{\, \ell : D(m, \ell) \subseteq [L_n, H_n) \,\big\}, \qquad m = \big\lceil L_n\, 2^{\ell} \big\rceil. \tag{11}
ℓ∗=min⁡{ℓ:D(m,ℓ)⊆[Ln,Hn)},m=⌈Ln 2ℓ⌉.(11)\begin{gathered} \ell^{*} = \min \{ \ell : D(m, \ell) \subseteq [L_n, H_n) \}, \\ m = \big\lceil L_n\, 2^{\ell} \big\rceil. \end{gathered} \tag{11}

It is never long. Take ℓ=⌈log⁡2(1/Wn)⌉+1\ell = \lceil \log_2 (1/W_n) \rceil + 1, so that 2−ℓ≤Wn/22^{-\ell} \le W_n / 2. Since m<Ln2ℓ+1m < L_n 2^{\ell} + 1, the top of its dyadic interval is (m+1) 2−ℓ<Ln+2⋅2−ℓ≤Ln+Wn=Hn(m + 1)\, 2^{-\ell} < L_n + 2 \cdot 2^{-\ell} \le L_n + W_n = H_n, so it fits, and

ℓ∗≤⌈log⁡21Wn⌉+1=⌈∑i=1nlog⁡21p(xi)⌉+1.(12)\ell^{*} \le \left\lceil \log_2 \frac{1}{W_n} \right\rceil + 1 = \left\lceil \sum_{i=1}^{n} \log_2 \frac{1}{p(x_i)} \right\rceil + 1. \tag{12}
ℓ∗≤⌈log⁡21Wn⌉+1=⌈∑i=1nlog⁡21p(xi)⌉+1.(12)\begin{aligned} \ell^{*} &\le \left\lceil \log_2 \frac{1}{W_n} \right\rceil + 1 \\ &= \left\lceil \sum_{i=1}^{n} \log_2 \frac{1}{p(x_i)} \right\rceil + 1. \end{aligned} \tag{12}

Codewords of different messages of the same length sit inside disjoint intervals, so none is a prefix of another: a prefix's dyadic interval would contain the longer codeword's, and with it a point of both messages' intervals.

why not send L

LnL_n itself can need far more digits than ℓ∗\ell^{*}. The codeword only has to land inside the interval, and stay inside whatever comes after it.

§ 6
entropy

Within two bits of the entropy

With the model taken from the message, the sum in (12) collects ckc_k copies of log⁡2(n/ck)\log_2 (n / c_k), which is nn times the entropy of pp in bits per symbol:

∑i=1nlog⁡21p(xi)=∑k=1Kcklog⁡2nck=n H(p),H(p)=−∑k=1Kp(ak)log⁡2p(ak).(13)\sum_{i=1}^{n} \log_2 \frac{1}{p(x_i)} = \sum_{k=1}^{K} c_k \log_2 \frac{n}{c_k} = n\, H(p), \qquad H(p) = -\sum_{k=1}^{K} p(a_k) \log_2 p(a_k). \tag{13}
∑i=1nlog⁡21p(xi)=∑k=1Kcklog⁡2nck=n H(p),H(p)=−∑k=1Kp(ak)log⁡2p(ak).(13)\begin{aligned} &\sum_{i=1}^{n} \log_2 \frac{1}{p(x_i)} \\ &\quad = \sum_{k=1}^{K} c_k \log_2 \frac{n}{c_k} = n\, H(p), \\ &H(p) = -\sum_{k=1}^{K} p(a_k) \log_2 p(a_k). \end{aligned} \tag{13}

No dyadic interval inside [Ln,Hn)[L_n, H_n) is wider than WnW_n, so 2−ℓ∗≤Wn2^{-\ell^{*}} \le W_n and ℓ∗≥log⁡2(1/Wn)\ell^{*} \ge \log_2 (1/W_n). Together with (12):

n H(p)  ≤  ℓ∗  ≤  ⌈n H(p)⌉+1  <  n H(p)+2.(14)n\, H(p) \;\le\; \ell^{*} \;\le\; \lceil n\, H(p) \rceil + 1 \;<\; n\, H(p) + 2. \tag{14}
n H(p)≤ℓ∗≤⌈n H(p)⌉+1<n H(p)+2.(14)\begin{aligned} n\, H(p) \le \ell^{*} &\le \lceil n\, H(p) \rceil + 1 \\ &< n\, H(p) + 2. \end{aligned} \tag{14}

The overhead is at most two bits for the whole message, not per symbol. A Huffman code over the same model can lose almost a bit on every symbol.

§ 7
my name

DESMOND, worked through

The integer recurrences (8), run on my name; each bound is the integer divided by 7i7^{i}:

ixi7iLi7iHi7iWi00111D0222E4623S404224M28628825O2012201426N14 09214 09427D98 64498 6484\def\arraystretch{1.3}\begin{array}{rcrrr} i & x_i & 7^{i} L_i & 7^{i} H_i & 7^{i} W_i \\ \hline 0 & & 0 & 1 & 1 \\ 1 & \mathrm{D} & 0 & 2 & 2 \\ 2 & \mathrm{E} & 4 & 6 & 2 \\ 3 & \mathrm{S} & 40 & 42 & 2 \\ 4 & \mathrm{M} & 286 & 288 & 2 \\ 5 & \mathrm{O} & 2012 & 2014 & 2 \\ 6 & \mathrm{N} & 14\,092 & 14\,094 & 2 \\ 7 & \mathrm{D} & 98\,644 & 98\,648 & 4 \end{array}

So the final interval and its width are

[L7,H7)=[ 98 64477, 98 64877)≈[ 0.1197800, 0.1197849 ),W7=(27)2 (17)5=4823 543≈4.857×10−6,\begin{aligned} [L_{7}, H_{7}) &= \Big[\, \frac{98\,644}{7^{7}},\ \frac{98\,648}{7^{7}} \Big) \approx [\,0.1197800,\ 0.1197849\,), \\ W_{7} &= \left(\frac{2}{7}\right)^{2}\,\left(\frac{1}{7}\right)^{5} = \frac{4}{823\,543} \approx 4.857 \times 10^{-6}, \end{aligned}
[L7,H7)=[ 98 64477, 98 64877)≈[ 0.1197800, 0.1197849 ),W7=(27)2 (17)5=4823 543≈4.857×10−6,\begin{aligned} [L_{7}, H_{7}) &= \Big[\, \frac{98\,644}{7^{7}},\ \frac{98\,648}{7^{7}} \Big) \\ &\approx [\,0.1197800,\ 0.1197849\,), \\ W_{7} &= \left(\frac{2}{7}\right)^{2}\,\left(\frac{1}{7}\right)^{5} \\ &= \frac{4}{823\,543} \approx 4.857 \times 10^{-6}, \end{aligned}

and log⁡2(1/W7)=7 H(p)≈17.65\log_2 (1/W_{7}) = 7\, H(p) \approx 17.65 bits, at H(p)≈2.522H(p) \approx 2.522 bits per letter. Bound (14) allows 18≤ℓ∗≤1918 \le \ell^{*} \le 19, and the search (11) stops at ℓ∗=19\ell^{*} = 19:

0.00011110101010100002=62 800219,D(62 800,19)≈[ 0.1197815, 0.1197834 )⊆[L7,H7).0.0001111010101010000_2 = \frac{62\,800}{2^{19}}, \qquad D(62\,800, 19) \approx [\,0.1197815,\ 0.1197834\,) \subseteq [L_{7}, H_{7}).
0.00011110101010100002=62 800219,D(62 800,19)≈[ 0.1197815, 0.1197834 )⊆[L7,H7).\begin{gathered} 0.0001111010101010000_2 = \frac{62\,800}{2^{19}}, \\ D(62\,800, 19) \approx [\,0.1197815,\ 0.1197834\,) \\ \subseteq [L_{7}, H_{7}). \end{gathered}

what I don’t understand yet

My encoder holds the interval as a 28-digit decimal, and it collapses after a few dozen symbols. Real coders use integers and renormalise as they go. I want to derive why that loses nothing.