Arithmetic Coding — Definition, Formula & Example
Arithmetic coding is a lossless data compression method that represents an entire message as a single number between 0 and 1. It does this by shrinking an interval step by step, with each symbol's share of the interval set by its probability.
Let a source alphabet have probabilities and cumulative probabilities under a fixed ordering. Arithmetic coding maps a sequence to the half-open interval defined by the recursion below, so that . The codeword is a binary fraction whose dyadic interval (all binary expansions beginning with those bits) lies inside . Such a codeword always exists with at most bits, which is within 2 bits of the message's information content.
Key Formula
Where:
- = Lower endpoint of the interval after encoding k symbols
- = Width of the interval after encoding k symbols
- = Total probability of all symbols ordered before the symbol
- = Probability of the k-th symbol of the message
How It Works
The decoder mirrors the encoder. It reads the codeword as a number and checks which symbol's subinterval contains . It outputs that symbol and rescales the subinterval back to . Then it repeats until it has decoded the known message length or reaches an end-of-message symbol.
Worked Example
Problem: A source emits A, B, and C with probabilities 0.5, 0.25, and 0.25, in that order. The symbols therefore own the subintervals [0, 0.5), [0.5, 0.75), and [0.75, 1). Encode the message BAC with arithmetic coding.
Step 1: Encode B. It takes the slice of [0, 1) that starts at F(B) = 0.5 and has width 0.25.
Step 2: Encode A. It takes the first half of the current interval, because F(A) = 0 and p(A) = 0.5.
Step 3: Encode C. It takes the last quarter of the current interval, because F(C) = 0.75. The final interval is [0.59375, 0.625).
Step 4: Pick bits. Every binary number that begins 0.10011 lies in , which is exactly the final interval. The length matches the information content, since .
Answer: BAC encodes to the 5-bit codeword 10011, which stands for any number in [0.59375, 0.625).
Why It Matters
Arithmetic coding can spend a fractional number of bits per symbol. Huffman coding cannot, because it must give every symbol a whole number of bits. This makes arithmetic coding far better for skewed sources, such as a symbol with probability 0.99. It powers CABAC in H.264 and HEVC video and the range coder in LZMA (7-Zip), and it is a standard topic in college data compression and information theory courses.
Common Mistakes
Mistake: Assuming the codeword alone tells the decoder when to stop.
Correction: The number 0.59375 also lies inside the intervals for BACA, BACAA, and so on. The decoder must know the message length in advance, or the alphabet must include a reserved end-of-message symbol.
