# Universal code (data compression)

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Universal_code_(data_compression)
> Markdown URL: https://mediated.wiki/source/Universal_code_(data_compression).md
> Source: https://en.wikipedia.org/wiki/Universal_code_(data_compression)
> Source revision: 1326928418
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Type of prefix code}}
{{refimprove|date=November 2011}}
thumb|Fibonacci, Elias Gamma, and Elias Delta vs binary coding
thumb|Rice with ''k''&nbsp;=&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;8,&nbsp;16 versus binary
In [data compression](/source/data_compression), a '''universal code''' for integers is a [prefix code](/source/prefix_code) that maps the [positive integer](/source/positive_integer)s onto  binary codewords, with the additional property that whatever the true [probability distribution](/source/probability_distribution) on integers, as long as the distribution is monotonic (i.e., ''p''(''i'')&nbsp;≥&nbsp;''p''(''i''&nbsp;+&nbsp;1) for all positive&nbsp;''i''), the [expected](/source/expected_value) lengths of the codewords are within a constant factor of the expected lengths that the [optimal code](/source/optimal_code) for that probability distribution would have assigned.  A universal code is ''asymptotically optimal'' if the ratio between actual and optimal [expected](/source/expected_value) lengths is bounded by a function of the [information entropy](/source/information_entropy) of the code that, in addition to being bounded, approaches 1 as entropy approaches infinity.

In general, most prefix codes for integers assign longer codewords to larger integers.  Such a code can be used to efficiently communicate a message drawn from a set of possible messages, by simply ordering the set of messages by decreasing probability and then sending the index of the intended message.  Universal codes are generally not used for precisely known probability distributions, and no universal code is known to be optimal for any distribution used in practice.

A universal code should not be confused with [universal source coding](/source/universal_source_coding), in which the data compression method need not be a fixed prefix code and the ratio between actual and optimal expected lengths must approach one.  However, note that an asymptotically optimal universal code can be used on [independent identically-distributed sources](/source/Independent_identically-distributed_random_variables), by using increasingly large [blocks](/source/block_code), as a method of universal source coding.

== Universal and non-universal codes ==
These are some universal codes for integers; an asterisk ([*](/source/Asterisk)) indicates a code that can be trivially restated in [lexicographical order](/source/lexicographical_order), while a double dagger ([���](/source/%EF%BF%BD%EF%BF%BD%EF%BF%BD)) indicates a code that is asymptotically optimal:
* [Elias gamma coding](/source/Elias_gamma_coding) *
* [Elias delta coding](/source/Elias_delta_coding) * ‡
* [Elias omega coding](/source/Elias_omega_coding) *{{explain|reason=How is this trivially restated in lexicographical order?|date=August 2019}} ‡
* [Exp-Golomb coding](/source/Exponential-Golomb_coding) *, which has Elias gamma coding as a special case. (Used in [H.264/MPEG-4 AVC](/source/H.264%2FMPEG-4_AVC))
* [Fibonacci coding](/source/Fibonacci_coding)
* [Levenshtein coding](/source/Levenshtein_coding) * ‡, the original universal coding technique [http://www.compression.ru/download/articles/int/levenstein_1968_on_the_redundancy_and_delay.pdf]
* [Byte coding](/source/Byte_coding) where a special bit pattern (with at least two bits) is used to mark the end of the code — for example, if an integer is encoded as a sequence of [nibble](/source/nibble)s representing digits in [base 15](/source/base_15) instead of the more natural [base 16](/source/base_16), then the highest nibble value (i.e., a sequence of four ones in binary) can be used to indicate the end of the integer.
* [Variable-length quantity](/source/Variable-length_quantity)

These are non-universal ones:

* [Unary coding](/source/Unary_coding), which is used in Elias codes
* [Rice coding](/source/Golomb_coding), which is used in the [FLAC](/source/FLAC) [audio codec](/source/audio_codec) and which has unary coding as a special case
* [Golomb coding](/source/Golomb_coding), which has Rice coding and unary coding as special cases.

Their nonuniversality can be observed by noticing that, if any of these are used to code the [Gauss–Kuzmin distribution](/source/Gauss%E2%80%93Kuzmin_distribution) or the [Zeta distribution](/source/Zeta_distribution) with parameter s=2, expected codeword length is infinite.  For example, using unary coding on the Zeta distribution yields an expected length of

: <math>E(l) = \frac{6}{\pi^2} \sum_{l=1}^\infty \frac{1}{l} = \infty . \,</math>

On the other hand, using the universal Elias gamma coding for the Gauss–Kuzmin distribution results in an expected codeword length (about 3.51 bits) near entropy (about 3.43 bits).

==Relationship to practical compression==
[Huffman coding](/source/Huffman_coding) and [arithmetic coding](/source/arithmetic_coding) (when they can be used) give at least as good, and often better compression than any universal code.

However, universal codes are useful when Huffman coding cannot be used — for example, when one does not know the exact probability of each message, but only knows the rankings of their probabilities.

Universal codes are also useful when Huffman codes are inconvenient. For example, when the transmitter but not the receiver knows the probabilities of the messages, Huffman coding requires an overhead of transmitting those probabilities to the receiver. Using a universal code does not have that overhead.

Each universal code, like each other self-delimiting (prefix) binary code, has its own "implied probability distribution" given by {{math|1=''P''(''i'')=2<sup>−''l''(''i'')</sup>}} where {{math|''l''(''i'')}} is the length of the ''i''th codeword and ''P''(''i'') is the corresponding symbol's probability.  If the actual message probabilities are ''Q''(''i'') and [Kullback–Leibler divergence](/source/Kullback%E2%80%93Leibler_divergence) <math>D_\text{KL}(Q \| P)</math> is minimized by the code with {{math|''l''(''i'')}}, then the optimal Huffman code for that set of messages will be equivalent to that code.  Likewise, how close a code is to optimal can be measured by this divergence.  Since universal codes are simpler and faster to encode and decode than Huffman codes (which is, in turn, simpler and faster than [arithmetic encoding](/source/arithmetic_encoding)), the universal code would be preferable in cases where <math>D_\text{KL}(Q \| P)</math> is sufficiently small.
[https://web.archive.org/web/20080807041150/http://www.cs.tut.fi/~albert/Dev/pucrunch/ Lossless Data Compression Program: Hybrid LZ77 RLE]

For any [geometric distribution](/source/geometric_distribution) (an exponential distribution on integers), a Golomb code is optimal.  With universal codes, the implicit distribution is approximately a [power law](/source/power_law) such as <math>1/n^2</math> (more precisely, a [Zipf distribution](/source/Zipf_distribution)).
For the [Fibonacci code](/source/Fibonacci_code), the implicit distribution is approximately <math>1/n^q</math>, with

:<math>q = 1/\log_2(\varphi) \simeq 1.44,</math>

where <math>\varphi</math> is the [golden ratio](/source/golden_ratio). For the ternary [comma code](/source/comma_code) (i.e., encoding in base 3, represented with 2 bits per symbol), the implicit distribution is a power law with <math>q=1+\log_3(4/3) \simeq 1.26</math>.  These distributions thus have near-optimal codes with their respective power laws.

==External links==
* [http://www.ics.uci.edu/~dan/pubs/DC-Sec3.html Data Compression], by Debra A. Lelewer and Daniel S. Hirschberg ([University of California, Irvine](/source/University_of_California%2C_Irvine))
* ''[http://www.inference.phy.cam.ac.uk/mackay/itila/ Information Theory, Inference, and Learning Algorithms]'', by [David MacKay](/source/David_MacKay_(scientist)), has a chapter on codes for integers, including an introduction to Elias codes.
* [https://web.archive.org/web/20070214150309/http://www-lat.compression.ru/download/integers.html Кодирование целых чисел] has mostly English-language papers on universal and other integer codes.

 <!-- do I really need both categories? -->

{{Compression Methods}}

Category:Data compression
Category:Entropy coding

---
Adapted from the Wikipedia article [Universal code (data compression)](https://en.wikipedia.org/wiki/Universal_code_(data_compression)) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Universal_code_(data_compression)?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
