Mathwords logoMathwords

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 p(s)p(s) and cumulative probabilities F(s)=∑t<sp(t)F(s) = \sum_{t < s} p(t) under a fixed ordering. Arithmetic coding maps a sequence s1s2…sns_1 s_2 \dots s_n to the half-open interval [Ln, Ln+Wn)[L_n,\, L_n + W_n) defined by the recursion below, so that Wn=∏k=1np(sk)W_n = \prod_{k=1}^{n} p(s_k). The codeword is a binary fraction whose dyadic interval (all binary expansions beginning with those bits) lies inside [Ln, Ln+Wn)[L_n,\, L_n + W_n). Such a codeword always exists with at most ⌈−log⁡2Wn⌉+1\lceil -\log_2 W_n \rceil + 1 bits, which is within 2 bits of the message's information content.

Key Formula

Lk=Lk−1+Wk−1 F(sk),Wk=Wk−1 p(sk),L0=0,  W0=1L_k = L_{k-1} + W_{k-1}\,F(s_k), \qquad W_k = W_{k-1}\,p(s_k), \qquad L_0 = 0,\; W_0 = 1
Where:
  • LkL_k = Lower endpoint of the interval after encoding k symbols
  • WkW_k = Width of the interval after encoding k symbols
  • F(sk)F(s_k) = Total probability of all symbols ordered before the symbol sks_k
  • p(sk)p(s_k) = Probability of the k-th symbol of the message

How It Works

The decoder mirrors the encoder. It reads the codeword as a number xx and checks which symbol's subinterval contains xx. It outputs that symbol and rescales the subinterval back to [0,1)[0, 1). 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.
L1=0+1(0.5)=0.5,W1=1(0.25)=0.25L_1 = 0 + 1(0.5) = 0.5, \qquad W_1 = 1(0.25) = 0.25
Step 2: Encode A. It takes the first half of the current interval, because F(A) = 0 and p(A) = 0.5.
L2=0.5+0.25(0)=0.5,W2=0.25(0.5)=0.125L_2 = 0.5 + 0.25(0) = 0.5, \qquad W_2 = 0.25(0.5) = 0.125
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).
L3=0.5+0.125(0.75)=0.59375,W3=0.125(0.25)=0.03125L_3 = 0.5 + 0.125(0.75) = 0.59375, \qquad W_3 = 0.125(0.25) = 0.03125
Step 4: Pick bits. Every binary number that begins 0.10011 lies in [19/32, 20/32)[19/32,\, 20/32), which is exactly the final interval. The length matches the information content, since W3=1/32W_3 = 1/32.
0.59375=1932=0.100112,−log⁡2 ⁣(132)=5 bits0.59375 = \tfrac{19}{32} = 0.10011_2, \qquad -\log_2\!\left(\tfrac{1}{32}\right) = 5 \text{ bits}
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.

Related Terms

  • Coding Theory — Studies codes for compression and error correction, including this one
  • Interval — Message is encoded by repeatedly narrowing an interval
  • Probability — Symbol probabilities set each subinterval's width
  • Compression — Data compression is the goal this method serves