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.
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
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 symbols in some order, . An order-0 model treats each symbol of a message as an independent draw from one probability mass function. My encoder takes it from the message itself: if occurs times,
DESMOND has letters over symbols. In alphabetical order, with the cumulative values of §2 in the last row:
§ 2
slices
The cumulative distribution cuts [0, 1) into slices
Adding the masses in alphabet order gives the cumulative distribution function, with by convention:
Each symbol owns the half-open slice between consecutive values, exactly as wide as its probability:
The slices are half-open, so neighbours share no endpoint: every lies in exactly one of them, and together they tile . For my name, , , and so on up to .
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, . The current interval is a shifted, scaled copy of , and the map that makes the copy is affine:
To encode the next symbol , send its slice through that map. Writing for the mass strictly below , which is when :
The new interval lies inside the old one, so the intervals are nested, , and each step multiplies the width by the probability of the symbol it encodes:
The width left is exactly the probability the model gives the whole message. Read as maps, encoding is composition: with , the map that squeezes onto ,
With probabilities , every bound after symbols is a multiple of . Multiplying (5) through by leaves recurrences in integers, where counts the letters below :
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 disjoint pieces in proportion to . By induction on , the messages of length get pairwise disjoint intervals that tile , so any number points at exactly one of them. Decoding runs (5) forwards again and reads each symbol by inverting the affine map:
Once is known, follows from (5) and the next symbol is read the same way. The decoder also has to know where to stop: the length , or an end-of-message symbol in the alphabet.
§ 5
the codeword
Bits that stay inside the interval
A binary fraction stands for every number that begins with those bits, which is a dyadic interval:
The codeword is the shortest whose whole dyadic interval fits inside the message's interval, so the decoder lands in whatever bits follow:
It is never long. Take , so that . Since , the top of its dyadic interval is , so it fits, and
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
itself can need far more digits than . 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 copies of , which is times the entropy of in bits per symbol:
No dyadic interval inside is wider than , so and . Together with (12):
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 :
So the final interval and its width are
and bits, at bits per letter. Bound (14) allows , and the search (11) stops at :
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.