Arithmetic Coding and Lempel-Ziv Universal Source Coding
Anchor (Master): Cover & Thomas 2006 Elements of Information Theory 2e (Wiley) §13.1-13.5; Wyner & Ziv 1989 Some Asymptotic Properties of the Lempel-Ziv Algorithm (IEEE Trans. IT); Lempel & Ziv 1976 On the Complexity of Finite Sequences (IEEE Trans. IT)
Intuition Beginner
Huffman coding is optimal for lossless compression, but it has a weakness: each symbol gets its own codeword. If your source has probabilities like 0.4, 0.3, 0.2, 0.1, Huffman assigns codewords of length 1, 2, 3, 3. The average length is 2.0 bits, while the entropy is 1.85 bits. You waste 0.15 bits per symbol because you cannot assign fractional-length codewords to individual symbols.
Arithmetic coding fixes this. Instead of assigning a codeword to each symbol separately, it encodes the entire message as a single number between 0 and 1. As each symbol arrives, the current interval shrinks by a factor equal to that symbol's probability. After processing the whole message, you pick any number inside the final interval and transmit its binary expansion. Because the interval width equals the product of all the symbol probabilities, the number of bits needed is approximately the negative log of that product, which is the total information content. You can get within two bits of the entropy for the entire message, regardless of alphabet size.
Now consider a different problem. What if you do not know the source statistics? You cannot assign probabilities because you do not have them. Lempel-Ziv coding solves this by building a dictionary as it reads the input. It works by breaking the message into phrases: the first time you see a pattern, you add it to the dictionary. The next time you see the same pattern, you refer back to it. The longer the input, the better the dictionary becomes. No probability model is needed.
The remarkable fact is that Lempel-Ziv is universal. For any stationary ergodic source (a source whose statistics are time-invariant in the long run), the compression ratio of LZ converges to the entropy rate as the message length grows. You do not need to know anything about the source beforehand. The algorithm learns the statistics from the data itself.
Visual Beginner
| Coding method | Knows source stats? | Uses codewords? | Bits per symbol |
|---|---|---|---|
| Huffman | Yes | Yes, one per symbol | |
| Arithmetic | Yes | No, encodes whole message | |
| Lempel-Ziv (LZ78) | No | Yes, dictionary phrases | as |
Figure: interval subdivision in arithmetic coding for the message "ABA" over alphabet {A, B} with p(A) = 0.6, p(B) = 0.4. Start with [0, 1). After A: [0, 0.6). After B: [0.36, 0.6). After A: [0.36, 0.504). The width of the final interval is 0.144 = 0.6 * 0.4 * 0.6. The number of bits needed is bits.
Worked example Beginner
Encode the message "BAAB" using arithmetic coding over the alphabet {A, B} with p(A) = 0.6, p(B) = 0.4.
Step 1. Set up the initial interval [0, 1). Partition it: A gets [0, 0.6), B gets [0.6, 1.0).
Step 2. Process B. The current interval becomes [0.6, 1.0). Subdivide: A gets [0.6, 0.84), B gets [0.84, 1.0).
Step 3. Process A. The current interval becomes [0.6, 0.84). Subdivide: A gets [0.6, 0.744), B gets [0.744, 0.84).
Step 4. Process A. The current interval becomes [0.6, 0.744). Subdivide: A gets [0.6, 0.6864), B gets [0.6864, 0.744).
Step 5. Process B. The final interval is [0.6864, 0.744). Width = 0.0576 = 0.4 * 0.6 * 0.6 * 0.4. The number of bits needed is bits. The midpoint 0.7152 in binary is 0.10110111..., so transmitting the first 5 bits suffices.
The entropy of this four-symbol message is bits. We used 5 bits, which is within the guaranteed 2-bit overhead.
Check your understanding Beginner
Formal definition Intermediate+
Let be a finite source alphabet with known conditional probabilities (reducing to for a memoryless source). The source emits a sequence .
Definition (Arithmetic coding). Arithmetic coding maps to a sub-interval of as follows. Let be the cumulative distribution function (for memoryless sources; the conditional version uses ). Initialize . For each symbol , update:
where is the cumulative probability up to but not including , and includes . The final interval has width:
Encode by transmitting any number in using bits.
Definition (LZ78 parsing). Given an infinite source sequence , the LZ78 parsing produces a sequence of distinct phrases where is the longest prefix of the remaining input that already appears in the dictionary , concatenated with the next symbol. Formally, if where is a previous phrase (or empty) and , then the encoder transmits the pair (index of , ). The new phrase is added to .
The number of bits for phrase is (pointer plus new symbol), and the total encoded length for source symbols is:
Counterexamples to common slips
Arithmetic coding is not Huffman coding. Huffman assigns a fixed codeword to each symbol. Arithmetic coding encodes the entire message jointly. The gap between Huffman's bound and arithmetic coding's bound grows when symbol probabilities are very different from inverse powers of two.
LZ78 is not LZ77. LZ77 uses a sliding window and back-references; LZ78 builds an explicit dictionary that grows monotonically. Their universality properties differ in convergence rate.
Universality does not mean optimal for finite lengths. LZ achieves the entropy rate asymptotically. For finite block lengths, a code designed with knowledge of the source distribution will outperform LZ. The universality advantage is that LZ works without such knowledge.
Key theorem with proof Intermediate+
Theorem (Arithmetic coding near-optimality). Let be drawn from a source with known probabilities. The arithmetic code for has length satisfying:
Therefore the expected length per symbol satisfies .
Proof. The final interval has width . The number of bits needed to specify a point in an interval of width is . By choosing the midpoint and transmitting its binary expansion:
Accounting for the one-bit ambiguity in terminating the binary expansion adds at most one more bit, giving the bound . Taking expectations and dividing by yields the per-symbol bound.
Theorem (LZ78 universality). For any stationary ergodic source with entropy rate , the compression rate of LZ78 satisfies:
Proof sketch. The proof proceeds in three stages. (1) The number of phrases satisfies and for stationary sources (each phrase is at least one symbol long, and new phrases are always added). (2) The entropy of the first symbols decomposes as where is the -th phrase. Since LZ78 phrases are distinct, the set of phrases grows, and the conditional entropy of each new phrase is bounded. (3) The encoding cost for phrase is approximately , and the combinatorial bound (from the entropy of the phrase process) combined with the upper bound on gives:
Bridge. The universality of LZ78 builds toward the rate-distortion framework in 46.02.05 by establishing that lossless compression at the entropy rate is achievable even without source knowledge, and appears again in 46.04.04 where the Blahut-Arimoto algorithm provides a computational method for finding the rate-distortion function; the foundational reason arithmetic coding achieves the entropy bound is the interval-subdivision mechanism whose width equals the source probability product, which generalises to the joint typicality encoding used in channel coding 46.03.02; the central insight is that universality emerges from the growth rate of the dictionary matching the entropy rate of the source, putting these together gives a landscape where compression, estimation, and communication share the same entropy-theoretic backbone.
Exercises Intermediate+
Lean formalization Intermediate+
Mathlib has no formalisation of arithmetic coding, the Lempel-Ziv algorithm, or the concept of universal source coding. The interval arithmetic underlying arithmetic coding requires precise reasoning about floating-point-like representations on , which is absent. The LZ78 parsing algorithm requires inductive definitions on streams and dictionary growth, neither of which exists in Mathlib. The universality theorem relies on the Shannon-McMillan-Breiman theorem for stationary ergodic sources, which is not formalised in Mathlib's probability theory library. The gap between what Mathlib has (basic measure theory, PMF types) and what is needed (streaming algorithms, ergodic theorems, phrase-count asymptotics) is substantial. This unit ships without a lean_module.
Advanced results Master
Precision and implementation of arithmetic coding
The theoretical description of arithmetic coding uses infinite-precision real arithmetic on . Practical implementations use finite-precision integer arithmetic with a technique called bit-stuffing or E3 scaling. Whenever the interval lies entirely within or , the encoder doubles the interval (shifts one bit out) and continues. When , a more subtle rescaling (the E3 mapping) prevents the interval from collapsing around 0.5.
The key implementation result is that arithmetic coding can be performed using fixed-precision integer registers of width bits, where is typically 16 or 32. The encoder outputs bits incrementally as the interval narrows, without waiting for the entire message. The decoder reads bits incrementally as well, maintaining its own interval. The throughput is linear in the message length, making arithmetic coding practical for real-time applications.
LZ78 and the entropy rate: sharp convergence
The Wyner-Ziv 1989 paper established the sharp convergence rate of LZ78 compression. For a memoryless source with entropy , the compression rate satisfies:
This is slower than the convergence of arithmetic coding with known statistics, but the advantage is universality. The redundancy (excess bits over entropy) vanishes logarithmically.
The proof technique connects LZ78 to the Ornstein isomorphism theorem in ergodic theory. The phrase parsing creates a tree whose growth rate is governed by the entropy rate of the source. The key identity is:
which holds almost surely for any stationary ergodic source. This result also connects to Kolmogorov complexity 42.04.08: the LZ78 compression length is an upper bound on the Kolmogorov complexity , since any universal Turing machine can decompress the LZ78 encoding. Therefore , and the universality of LZ78 provides an algorithmic proof that for computable stationary ergodic sources.
LZW and practical variants
The LZW variant (Welch 1984) modifies LZ78 by removing the explicit transmission of the new symbol. Instead, the encoder always transmits a dictionary index, and the decoder infers the new symbol from the next phrase. This saves bits per phrase and is the basis of the Unix compress utility, GIF image encoding, and the PKZIP/ZIP format (as one of the available algorithms).
The LZ77 variant (Ziv & Lempel 1977) uses a sliding window of fixed size instead of a growing dictionary. Each phrase is encoded as a triple (offset, length, next symbol), where offset and length refer back into the window. LZ77 achieves universality with a different convergence rate and is the algorithm behind gzip and DEFLATE.
Relationship to the entropy power inequality
The entropy power inequality (EPI), studied in 46.01.04, states that for independent continuous random variables. The EPI bounds the performance of universal coding for mixed sources: if a source is a mixture of two component sources, the entropy power of the mixture controls the compression rate. Arithmetic coding on the mixture distribution achieves this rate, while LZ78 achieves it adaptively.
Synthesis. Arithmetic coding achieves the entropy bound to within two bits for the entire message by mapping it to a sub-interval of whose width equals the probability product; this builds toward the joint typicality encoding of channel coding where similar interval constructions appear; the central insight is that Lempel-Ziv universality emerges from the interplay between dictionary growth and source entropy rate, putting these together we see that lossless compression, universal estimation, and Kolmogorov complexity share the same entropy-theoretic foundation; the foundational reason arithmetic coding outperforms Huffman is that joint encoding of the entire message avoids the integer-length penalty per symbol, and this generalises to any block coding scheme where the block length determines the resolution of the probability partition.
Full proof set Master
Proposition (Arithmetic coding interval width). After encoding symbols from a source with conditional probabilities , the interval produced by arithmetic coding has width .
Proof. By induction on . Base case: , interval has width 1 = . Inductive step: assume has width . After processing , the new interval is:
The new width is:
By the inductive hypothesis, .
Proposition (LZ78 phrase-count lower bound). For a source with entropy rate , the number of LZ78 phrases satisfies for all sufficiently large .
Proof. Each phrase can be specified by a pointer (at most bits) and a new symbol ( bits), so the description length of is at most . Since the phrases partition the input, the total description length .
But by the source coding theorem, . Therefore:
For the crude bound, note that , so .
Connections Master
46.02.02— Shannon's source coding theorem establishes as the fundamental compression limit; arithmetic coding achieves it and LZ78 achieves it universally.42.04.08— Kolmogorov complexity is lower-bounded by the LZ78 compression length minus a constant; the universality of LZ78 provides an algorithmic approximation to incompressibility.46.02.05— Rate-distortion theory generalises the lossless compression framework to lossy coding, where the entropy is replaced by the rate-distortion function .46.01.04— Differential entropy and the entropy power inequality provide the continuous-source analogues of the discrete-source results here.46.08.01— LDPC codes use graphical structures for iterative decoding; the phrase-tree of LZ78 is a simpler graph-based data structure that also achieves information-theoretic limits.
Historical & philosophical context Master
Arithmetic coding was introduced by Jorma Rissanen in 1976 at IBM Research ("Generalized Kraft Inequality and Arithmetic Coding," IBM Journal of Research and Development 20(3):198-203). Rissanen recognised that the Kraft inequality could be satisfied not only by assigning integer-length codewords (Huffman) but by mapping messages to sub-intervals of the unit interval. The idea had been foreshadowed by Shannon's 1948 paper, which described encoding by cumulative probability evaluation, but Rissanen made it practical by developing the incremental encoding and decoding algorithms.
The Lempel-Ziv algorithm was introduced by Abraham Lempel and Jacob Ziv in their 1976 and 1977 papers ("On the Complexity of Finite Sequences," IEEE Trans. IT 22(1):75-81, 1976; "A Universal Algorithm for Sequential Data Compression," IEEE Trans. IT 23(3):337-343, 1977). Their insight was that compression could be achieved by exploiting the repeat structure of the input without any probabilistic model. The LZ78 variant (the 1978 paper "Compression of Individual Sequences via Variable-Rate Coding," IEEE Trans. IT 24(5):530-536) built an explicit dictionary, as described in this unit.
The universality proof was completed by Ziv in 1978 and sharpened by Wyner and Ziv in 1989 ("Some Asymptotic Properties of the Lempel-Ziv Algorithm," IEEE Trans. IT 35(6):1252-1259). The connection to Kolmogorov complexity was recognised early: the LZ78 compression length provides a computable upper bound on the Kolmogorov complexity of any string, making it an approximation to the uncomputable .
Terry Welch's 1984 variant (LZW), published as "A Technique for High-Performance Data Compression" (IEEE Computer 17(6):8-19), became the most widely deployed compression algorithm of the 1990s, appearing in GIF, Unix compress, and early versions of ZIP. The patent encumbrance of LZW (Unisys, 1985-2003) motivated the adoption of DEFLATE (a combination of LZ77 and Huffman coding) in later formats.
Bibliography Master
@article{rissanen1976,
author = {Rissanen, J.},
title = {Generalized {Kraft} Inequality and Arithmetic Coding},
journal = {IBM Journal of Research and Development},
volume = {20},
number = {3},
pages = {198--203},
year = {1976},
}
@article{lempel-ziv1976,
author = {Lempel, A. and Ziv, J.},
title = {On the Complexity of Finite Sequences},
journal = {IEEE Transactions on Information Theory},
volume = {22},
number = {1},
pages = {75--81},
year = {1976},
}
@article{ziv-lempel1977,
author = {Ziv, J. and Lempel, A.},
title = {A Universal Algorithm for Sequential Data Compression},
journal = {IEEE Transactions on Information Theory},
volume = {23},
number = {3},
pages = {337--343},
year = {1977},
}
@article{ziv-lempel1978,
author = {Ziv, J. and Lempel, A.},
title = {Compression of Individual Sequences via Variable-Rate Coding},
journal = {IEEE Transactions on Information Theory},
volume = {24},
number = {5},
pages = {530--536},
year = {1978},
}
@article{wyner-ziv1989,
author = {Wyner, A. D. and Ziv, J.},
title = {Some Asymptotic Properties of the {Lempel-Ziv} Algorithm},
journal = {IEEE Transactions on Information Theory},
volume = {35},
number = {6},
pages = {1252--1259},
year = {1989},
}
@article{welch1984,
author = {Welch, T. A.},
title = {A Technique for High-Performance Data Compression},
journal = {IEEE Computer},
volume = {17},
number = {6},
pages = {8--19},
year = {1984},
}
@book{cover-thomas2006,
author = {Cover, T. M. and Thomas, J. A.},
title = {Elements of Information Theory},
edition = {2nd},
publisher = {Wiley},
year = {2006},
}