| This article may require copy-editing for grammar, style, cohesion, tone or spelling. You can assist by editing it. (May 2009) |
Arithmetic coding is a method for lossless data compression. Normally, a string of characters such as the words "hello there" is represented using a fixed number of bits per character, as in the ASCII code. Like Huffman coding, arithmetic coding is a form of variable-length entropy encoding that converts a string into another form that represents frequently used characters using fewer bits and infrequently used characters using more bits, with the goal of using fewer bits in total. As opposed to other entropy encoding techniques that separate the input message into its component symbols and replace each symbol with a code word, arithmetic coding encodes the entire message into a single number, a fraction n where (0.0 ≤ n < 1.0).
Both arithmetic and range coding may be examined from the perspective of number theory. It can be interpreted as a generalized change of radix. The best way to introduce the concept is to consider an elementary example. We may look at any sequence of symbols
DABDDB
as a number in a certain base presuming that the involved symbols form an ordered set and each symbol in the ordered set denotes a sequential integer A=0, B=1, C=2, D=3 and so on. If we make a table of frequencies and cumulative frequencies for this message it looks like follows
| Symbol | Cumulative frequency | Frequency of occurrence |
|---|---|---|
| A | 0 | 1 |
| B | 1 | 2 |
| D | 3 | 3 |
If we choose radix=6 equal to the size of the message and convert the expression DABDDB into a decimal number we shall have
65 * 3 + 64 * 0 + 63 * 1 + 62 * 3 + 61 * 3 + 60 * 1 = 23671.
The result 23671 has a length of 15 bits and does not make a message close to the theoretical limit computed via entropy, which must be near 9 bits.
[ − 3 / 6 * log2(3 / 6) − 2 / 6 * log2(2 / 6) − 1 / 6 * log2(1 / 6)] * 6 = 8.75 bits
In order to make it in accordance with information theory we need to slightly generalize the classic formula for changing the radix. We have to compute LOW and HIGH limits and choose a convenient number between them. For the computation of the LOW limit we multiply each next term in the above expression by the product of the frequencies of all previously occurred symbols, so it is turned into the following expression
LOW = 65 * 3 + 3 * [64 * 0 + 1 * [63 * 1 + 2 * [62 * 3 + 3 * [61 * 3 + 3 * [60 * 1]]]]] = 25002.
The HIGH limit must be LOW plus the product of all frequencies. HIGH = LOW + 3 * 1 * 2 * 3 * 3 * 2 = 25002 + 108 = 25110. Now we can choose any number to represent the message from the semi-closed interval [LOW, HIGH), which we can take as a number with the longest possible trail of zeros, for example 25100. In case of a long message this trail of zeros will be much longer and can be either dropped or presented as an exponent. The number 251, received after truncation of zeros, has a length of 8 bits, which is even less than the theoretical limit. In order to represent the computation of the LOW limit in a simple, easy to remember format, we can offer a table. Each row contains the factors for every term in the above formula. We can clearly see the part that distinguishes this computation of the LOW limit from the classical change of the base. It is column 'Part 2', containing the products of frequencies for all previously occurred symbols.
| Symbol | Part 1 | Part 2 | Total |
|---|---|---|---|
| D | 65 * 3 | 23328 | |
| A | 64 * 0 | 3 | 0 |
| B | 63 * 1 | 3 * 1 | 648 |
| D | 62 * 3 | 3 * 1 * 2 | 648 |
| D | 61 * 3 | 3 * 1 * 2 * 3 | 324 |
| B | 60 * 1 | 3 * 1 * 2 * 3 * 3 | 54 |
| 25002 |
In order to complete the topic we have to show how to convert the number 25100 back to the original message. The reverse process can be also shown in a table. It has two logical steps: identification of the symbol and subtraction of the correspondent term from the result.
| Remainder | Identification | Identified symbol | Corrected remainder |
|---|---|---|---|
| 25100 | 25100 / 65 = 3 | D | (25100 - 65 * 3) / 3 = 590 |
| 590 | 590 / 64 = 0 | A | (590 - 64 * 0) / 1 = 590 |
| 590 | 590 / 63 = 2 | B | (590 - 63 * 1) / 2 = 187 |
| 187 | 187 / 62 = 5 | D | (187 - 62 * 3) / 3 = 26 |
| 26 | 26 / 61 = 4 | D | (26 - 61 * 3) / 3 = 2 |
| 2 | 2 / 60 = 2 | B |
In identification we divide the result by the correspondent power of 6. The fractional part of the division is discarded. The result is then matched against the cumulative intervals and the appropriate symbol is selected. When the symbol is identified the result is corrected. The process is continued for the known length of the message or until the remained result is positive. As we can see the only difference compared to the classical formula is the identification of the symbol that is not a sequential integer but the integer associated with the interval. A is always 0 and B is either 1 or 2. D is any of 3,4,5. This is in exact accordance with our intervals that are determined by the frequencies. When all intervals equal to 1 we are having a special case of classic base change, but the computational part is the same.
The generic formula the for computation of the LOW limit for the message of n symbols may be expressed as follows

and the HIGH limit is computed as
,
where Ci are the cumulative frequencies and fk are the frequencies of occurrences. Indexes denote the position of the symbol in a message. In case all frequencies fk are equal to 1 the formulas turn into the special case used for expressing the number in a different base.
It is easy to show that the computed LOW limit never exceeds nn independently of the order of symbols, where n is the size of the message. That means that the binary length of the LOW limit can be estimated as log2(nn). After the computation of the HIGH limit and the reduction of the message by selecting a number from the interval [LOW, HIGH) with the longest trail of zeros we can presume that this length can be reduced on
number of bits. Since each frequency in a product occurs exactly same number of times as the value of this frequency, we can use the size of the alphabet A for the computation of the product

We can see it clearly in the above example. The product of all frequencies in the message is 3 * 1 * 2 * 3 * 3 * 2, where 3 occurs exactly 3 times, 2 occurs exactly 2 times and so on. Applying log2 for the estimated number of bits in the message we have the result
.
Using the numbers from above example we can see the exact match with the Shannon entropy limit calculated before
6 * log2(6) − 3 * log2(3) − 2 * log2(2) − 1 * log2(1) = 8.75.
It is not a coincidence. The formulas

and
![-[\sum_{i=1}^A p_i * log_2(p_i)] * n = E * n](http://upload.wikimedia.org/math/8/a/6/8a69d4f2a484acb0434660f5585bea58.png)
can be algebraically converted into each other. The latter represents the entropy E multiplied by the length of the message. It uses pi probabilities of the occurrences of the involved symbols. The entropy E is introduced by Claude Shannon in his fundamental work Mathematical Theory of Communications as a statistical characteristic used in estimation of the quantity of information.
Another fundamental property that should be mentioned is the relation between entropy and the number of possible permutations. It is already shown that compression is achieved as a result of uneven distribution of symbols. It depends on the product of all frequencies in a message and not on the order of symbols. If we consider the distribution as a fixed parameter we can simply enumerate all possible messages and pass the index of the message instead of the message itself. The maximum possible index will be equal to the number of permutations in a message. The bit length of this number is estimated by the formula
.
The two previous expressions estimate the number of bits in a compressed message as
.
These two estimates may have a noticeable difference for short messages but starting from 1000 symbols and longer they are a fraction of percent off each other. That means that entropy is close to the bit length of the number of possible permutations divided by the size of the message. The explanation of why these formulas provide a close result is known as the Sterling approximation for the logarithm of factorial
n * log2(n) − n ≈ log2(n!).
Both sides of the expression tend to each other with the increasing of n. Taking all these relationship into consideration we may define arithmetic coding as converting a message to a whole number, whose length is close to the length of the number of possible permutations for the provided statistical distribution of symbols and does not depend on the particular order of symbols.
Traditionally arithmetic coding was explained by using probabilities and not frequencies. From a theoretical point of view it does not make any difference because they can be converted into each other by multiplying both parts of equations by a constant. However, some interesting properties were overlooked by researchers only because the use of probabilities. Such as the idea that arithmetic coding is only a generalized form of changing the base and a relationship between entropy and the number of permutations. Here we can show the traditional approach to the explanation by simply dividing the expression for the LOW limit by the constant nn, or, for our particular case, by the constant 66. In this case the left-hand side will contain the cumulative probabilities and probabilities and the right-hand side will be a fraction. The table illustrating the computation of the LOW limit will also be changed accordingly
| Symbol | Part 1 | Part 2 | Total |
|---|---|---|---|
| D | 3/6 | 3/6 | |
| A | 0/6 | 3/6 | 0/62 |
| B | 1/6 | 3/6 * 1/6 | 3/63 |
| D | 3/6 | 3/6 * 1/6 * 2/6 | 18/64 |
| D | 3/6 | 3/6 * 1/6 * 2/6 * 3/6 | 54/65 |
| B | 1/6 | 3/6 * 1/6 * 2/6 * 3/6 * 3/6 | 54/66 |
| 0.53587962962962962962962962962963 |
In the same way the HIGH limit should be computed as LOW plus the product of all probabilities and the reduction of the message can be achieved by the selection of the shortest fraction between two intervals [LOW, HIGH). It is proven that the result always belongs to the interval [0,1). Each step in the encoding adds a smaller and smaller number, which contributes to the growth of the message and needs the special treatment of the numerical processing that is known as renormalization. When the concept was derived and explained by mathematicians and passed to programmers for implementation they appeared to be bounded by the concept and started implementation close to the probability concept, which resulted in a tremendous inconveniences in programming and caused a delay in delivering a reliable implementation of years or, possibly, of decades. The domination of the probability concept can be even seen in every patent on arithmetic coding filed before the year 2000. The explanation of arithmetic coding as mapping a message onto the [0,1) interval was included into claims risking the patents to be easily circumvented by programs that do not compute probabilities and not dealing with renormalization. In the frequency concept many computational challenges are avoided. In the same way as probabilities frequencies are slightly adjusted to convenient numbers. They are scaled to make the base presented as a power of 2 and one multiplication turns into a binary shift. The long products of frequencies are computed as the mantissa and exponent, where the mantissa is maintained over the compression and the exponent resulted also as an additional binary shift. The final computational part is adding numbers that overlap and manage the propagation of carry
47568690
34598908
996245
and so on. The frequency concept is reduced to the computation of integers shifted relatively to each other and adding them in a stair looking structure. This also perfectly explains the fractional bit lengths in an optimal encoding mentioned by Shannon, which states the possibility of encoding a particular symbol in − log2(p) bits although the number is fractional. This fractional bit length is achieved by a variable shift computed on every step. When the implementation details for every arithmetic coder varies they all have one common thing: the limits LOW and HIGH are computed on every step. The frequency type approach does not need the computation of the HIGH limit at all, it is not part of the numerical implementation and the computational burden is twice lower.
The advantage of arithmetic coding over other similar methods of data compression is in the convenience of adaptation. The matter of adaptation is the changing of the frequency tables while processing the data. The decoded data matches the original data in case the frequency table in decoding is replaced in the same way and in the same step as in encoding. The synchronization is, usually, based on a combination of symbols occurring during the encoding and decoding process. Adaptive arithmetic coding significantly improves the compression ratio compared to static methods, it may be as effective as 2 to 3 times better in the result.
Arithmetic coders produce near-optimal output for a given set of symbols and probabilities (the optimal value is −log2P bits for each symbol of probability P, see source coding theorem). Compression algorithms that use arithmetic coding start by determining a model of the data – basically a prediction of what patterns will be found in the symbols of the message. The more accurate this prediction is, the closer to optimality the output will be.
Example: a simple, static model for describing the output of a particular monitoring instrument over time might be:
Models can also handle alphabets other than the simple four-symbol set chosen for this example. More sophisticated models are also possible: higher-order modelling changes its estimation of the current probability of a symbol based on the symbols that precede it (the context), so that in a model for English text, for example, the percentage chance of "u" would be much higher when it follows a "Q" or a "q". Models can even be adaptive, so that they continuously change their prediction of the data based on what the stream actually contains. The decoder must have the same model as the encoder.
As an example of how a sequence of symbols is encoded, consider a sequence taken from a set of three symbols, A, B, and C, each equally likely to occur. Simple block encoding would use 2 bits per symbol, which is wasteful: one of the bit variations is never used.
A more efficient solution is to represent the sequence as a rational number between 0 and 1 in base 3, where each digit represents a symbol. For example, the sequence "ABBCAB" could become 0.0112013. The next step is to encode this ternary number using a fixed-point binary number of sufficient precision to recover it, such as 0.0010110012 — this is only 9 bits, 25% smaller than the naive block encoding. This is feasible for long sequences because there are efficient, in-place algorithms for converting the base of arbitrarily precise numbers.
Finally, knowing the original string had length 6, one can simply convert back to base 3, round to 6 digits, and recover the string.
In general, each step of the encoding process, except for the very last, is the same; the encoder has basically just three pieces of data to consider:
The encoder divides the current interval into sub-intervals, each representing a fraction of the current interval proportional to the probability of that symbol in the current context. Whichever interval corresponds to the actual symbol that is next to be encoded becomes the interval used in the next step.
Example: for the four-symbol model above:
When all symbols have been encoded, the resulting interval unambiguously identifies the sequence of symbols that produced it. Anyone who has the same final interval and model that is being used can reconstruct the symbol sequence that must have entered the encoder to result in that final interval.
It is not necessary to transmit the final interval, however; it is only necessary to transmit one fraction that lies within that interval. In particular, it is only necessary to transmit enough digits (in whatever base) of the fraction so that all fractions that begin with those digits fall into the final interval.
Consider the process for decoding a message encoded with the given four-symbol model. The message is encoded in the fraction 0.538 (using decimal for clarity, instead of binary; also assuming that there are only as many digits as needed to decode the message.)
The process starts with the same interval used by the encoder: [0,1), and using the same model, dividing it into the same four sub-intervals that the encoder must have. The fraction 0.538 falls into the sub-interval for NEUTRAL, [0, 0.6); this indicates that the first symbol the encoder read must have been NEUTRAL, so this is the first symbol of the message.
Next divide the interval [0, 0.6) into sub-intervals:
Since .538 is within the interval [0.48, 0.54), the second symbol of the message must have been NEGATIVE.
Again divide our current interval into sub-intervals:
Now .538 falls within the interval of the END-OF-DATA symbol; therefore, this must be the next symbol. Since it is also the internal termination symbol, it means the decoding is complete. If the stream was not internally terminated, there needs to be some other way to indicate where the stream stops. Otherwise, the decoding process could continue forever, mistakenly reading more symbols from the fraction than were in fact encoded into it.
The same message could have been encoded by the equally short fractions .534, .535, .536, .537 or .539. This suggests that the use of decimal instead of binary introduced some inefficiency. This is correct; the information content of a three-digit decimal is approximately 9.966 bits; the same message could have been encoded in the binary fraction .10001010 (equivalent to .5390625 decimal) at a cost of only 8 bits. (The final zero must be specified in the binary fraction, or else the message would be ambiguous without external information such as compressed stream size.)
This 8 bit output is larger than the information content, or entropy of the message, which is 1.57 * 3 or 4.71 bits. The large difference between the example's 8 (or 7 with external compressed data size information) bits of output and the entropy of 4.71 bits is caused by the short example message not being able to exercise the coder effectively. The claimed symbol probabilities were [.6, .2, .1, .1], but the actual frequencies in this example are [.33, 0, .33 .33]. If the intervals are readjusted for these frequencies, the entropy of the message would be 1.58 bits and the same NEUTRAL NEGATIVE ENDOFDATA message could be encoded as intervals [0, 1/3); [1/9, 2/9); [5/27, 6/27); and a binary interval of [1011110, 1110001). This could yield an output message of 111, or just 3 bits. This is also an example of how statistical coding methods like arithmetic encoding can produce an output message that is larger than the input message, especially if the probability model is off.
The above explanations of arithmetic coding contain some simplification. In particular, they are written as if the encoder first calculated the fractions representing the endpoints of the interval in full, using infinite precision, and only converted the fraction to its final form at the end of encoding. Rather than try to simulate infinite precision, most arithmetic coders instead operate at a fixed limit of precision which they know the decoder will be able to match, and round the calculated fractions to their nearest equivalents at that precision. An example shows how this would work if the model called for the interval [0,1) to be divided into thirds, and this was approximated with 8 bit precision. Note that as now the precision is known, so are the binary ranges we'll be able to use.
| Symbol | Probability (expressed as fraction) | Interval reduced to eight-bit precision (as fractions) | Interval reduced to eight-bit precision (in binary) | Range in binary |
|---|---|---|---|---|
| A | 1/3 | [0, 85/256) | [0.00000000, 0.01010101) | 00000000 - 01010100 |
| B | 1/3 | [85/256, 171/256) | [0.01010101, 0.10101011) | 01010101 - 10101010 |
| C | 1/3 | [171/256, 1) | [0.10101011, 1.00000000) | 10101011 - 11111111 |
A process called renormalization keeps the finite precision from becoming a limit on the total number of symbols that can be encoded. Whenever the range is reduced to the point where all values in the range share certain beginning digits, those digits are sent to the output. For however many digits of precision the computer can handle, it is now handling fewer than that, so the existing digits are shifted left, and at the right, new digits are added to expand the range as widely as possible. Note that this result occurs in two of the three cases from our previous example.
| Symbol | Probability | Range | Digits that can be sent to output | Range after renormalization |
|---|---|---|---|---|
| A | 1/3 | 00000000 - 01010100 | 0 | 00000000 - 10101001 |
| B | 1/3 | 01010101 - 10101010 | None | 01010101 - 10101010 |
| C | 1/3 | 10101011 - 11111111 | 1 | 01010110 - 11111111 |
An interactive visualization tool for teaching arithmetic coding, dasher.tcl, was also the first prototype of the assistive communication system, Dasher.
There is great similarity between arithmetic coding and Huffman coding – in fact, it has been shown that Huffman is just a specialized case of arithmetic coding – but because arithmetic coding translates the entire message into one number represented in base b, rather than translating each symbol of the message into a series of digits in base b, it will sometimes approach optimal entropy encoding much more closely than Huffman can.
In fact, a Huffman code corresponds closely to an arithmetic code where each of the frequencies is rounded to a nearby power of ½ — for this reason Huffman deals relatively poorly with distributions where symbols have frequencies far from a power of ½, such as 0.75 or 0.375. This includes most distributions where there are either a small numbers of symbols (such as just the bits 0 and 1) or where one or two symbols dominate the rest.
For an alphabet {a, b, c} with equal probabilities of 1/3, Huffman coding may produce the following code:
This code has an expected (2 + 2 + 1)/3 ≈ 1.667 bits per symbol for Huffman coding, an inefficiency of 5 percent compared to log23 ≈ 1.585 bits per symbol for arithmetic coding.
For an alphabet {0, 1} with probabilities 0.625 and 0.375, Huffman encoding treats them as though they had 0.5 probability each, assigning 1 bit to each value, which does not achieve any compression over naive block encoding. Arithmetic coding approaches the optimal compression ratio of:
.When the symbol 0 has a high probability of 0.95, the difference is much greater:
.One simple way to address this weakness is to concatenate symbols to form a new alphabet in which each symbol represents a sequence of symbols in the original alphabet. In the above example, grouping sequences of three symbols before encoding would produce new "super-symbols" with the following frequencies:
With this grouping, Huffman coding averages 1.3 bits for every three symbols, or 0.433 bits per symbol, compared with one bit per symbol in the original encoding.
Range encoding is regarded by one group of engineers as a different technique and by another group only as a different name for arithmetic coding. There is no unique opinion but some people believe that, when processing is applied as one step per symbol, it is range coding, and when one step is required per every bit it is arithmetic coding. On another opinion arithmetic coding is the computing of two boundaries on interval [0,1) and choosing the shortest fraction from it, and range encoding is computing boundaries on the interval [0,nn) and choosing the number with the longest trail of zeros from within. Many researchers believe that slight difference in the approach makes range encoding patent free. To back up this idea they provide reference to the article of G. Nigel N. Martin, which is not reader friendly and is subject to interpretation. It is cited in the Glen Langdon article An Introduction to Arithmetic Coding, IBM J. RES. DEVELOP. VOL. 28, No 2, March 1984, which makes the method suggested by Martin as prior art recognized by an industry expert. It is close to the first topic of the current article with the difference that both the LOW and HIGH limits are computed on every step and that probabilities are still used for narrowing down the interval and not the frequencies. The article of G. N. N. Martin was amazingly dropped out of attention of many researchers who were filing patents on arithmetic coding explaining the matter of their algorithms as building a long proper fraction, which put all their patents at risk to be circumvented by those who do it differently because a patent is a very formal document and language definitions should be very precise. It is not necessary that all patents on arithmetic coding are now void in the light of Matin's article but it opens the ground for debates, which could have been avoided if authors at least mentioned the approach.
A variety of specific techniques for arithmetic coding are covered by US patents. Some of these patents may be essential for implementing the algorithms for arithmetic coding that are specified in some formal international standards. When this is the case, such patents are generally available for licensing under what is called "reasonable and non-discriminatory" (RAND) licensing terms (at least as a matter of standards-committee policy). In some well-known instances (including some involving IBM patents) such licenses are available free, and in other instances, licensing fees are required. The availability of licenses under RAND terms does not necessarily satisfy everyone who might want to use the technology, as what may be "reasonable" fees for a company preparing a proprietary software product may seem much less reasonable for a free software or open source project.
At least one significant compression software program, bzip2, deliberately discontinued the use of arithmetic coding in favor of Huffman coding due to the patent situation. Also, encoders and decoders of the JPEG file format, which has options for both Huffman encoding and arithmetic coding, typically only support the Huffman encoding option, due to patent concerns; the result is that nearly all JPEGs in use today use Huffman encoding.[1]
Some US patents relating to arithmetic coding are listed below.
Note: This list is not exhaustive. See the following link for a list of more patents.[2] The Dirac codec uses arithmetic coding and is not patent pending.[3]
Patents on arithmetic coding may exist in other jurisdictions, see software patents for a discussion of the patentability of software around the world.
Every programmatic implementation of arithmetic encoding has a different compression ratio and performance. While compression ratios vary only a little (usually under 1%) the code execution time can vary by a factor of 10. Choosing the right encoder from a list of publicly available encoders is not a simple task because performance and compression ratio depend also on the type of data, particularly on the size of the alphabet (number of different symbols). One of two particular encoders may have better performance for small alphabets while the other may show better performance for large alphabets. Most encoders have limitations on the size of the alphabet and many of them are designed for a dual alphabet only (zero and one).
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
stock | retire | vm
Why are we here?
All text is available under the terms of the GNU Free Documentation License
This page is cache of Wikipedia. History