ArticleslgStudy

science

Arithmetic coding

Arithmetic coding is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Arithmetic coding rather than just read about it. In short: Arithmetic coding (AC) is a form of entropy coding used in lossless data compression. Normally, a string of characters is represented using a fixed number of bits per character, as in the ASCII code.

Arithmetic coding — main illustration
Arithmetic coding — illustration

Key takeaways

  • Arithmetic coding belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Arithmetic coding to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Arithmetic coding from memory before moving on to harder problems.

Reference excerpt

Arithmetic coding (AC) is a form of entropy coding used in lossless data compression. Normally, a string of characters is represented using a fixed number of bits per character, as in the ASCII code. When a string is converted to arithmetic encoding, frequently used characters will be stored with fewer bits and less-frequently occurring characters will be stored with more bits, resulting in fewer bits used in total. Arithmetic coding differs from other forms of entropy encoding, such as Huffman coding, in that rather than separating the input into component symbols and replacing each with a code, arithmetic coding encodes the entire message into a single number, an arbitrary-precision fraction q, where 0.0 ≤ q < 1.0.

Relationship to entropy Arithmetic coding achieves compression by subdividing the interval [0, 1) into sub-intervals proportional to symbol probabilities. When symbol probabilities are unequal, more probable symbols receive larger sub-intervals, which require fewer bits to specify a point within. The theoretical limit on this compression is given by the entropy of the source, which Shannon's source coding theorem establishes as the minimum average number of bits per symbol that any lossless method can achieve. Arithmetic coding approaches this limit closely, especially for long messages. When all symbols are equally likely, each sub-interval has the same size, and no symbol can be represented with fewer bits than any other. In this case the entropy reaches its maximum of log2n bits per symbol (where n is the alphabet size), and no compression is possible. For example, a stream of independent fair coin flips has entropy of exactly 1 bit per symbol – the full cost of storage – so arithmetic coding provides no benefit. Similarly, independent ternary symbols with equal probabilities have entropy of about 1.585 bits per symbol, the maximum for a three-symbol alphabet, and are likewise incompressible. Compression becomes possible when probabilities are unequal, because the sub-intervals become unequal in size. For instance, a binary source with probabilities 0.9 and 0.1 has entropy of approximately 0.469 bits per symbol. The symbol with probability 0.9 receives 90% of each interval, so most encoding steps barely shrink the interval, and fewer bits are needed to identify the final value. This allows arithmetic coding to achieve a compression ratio of roughly 2.1:1.

Implementation details and examples

Equal probabilities In the simplest case, the probability of each symbol occurring is equal. For example, consider a set of three symbols, A, B, and C, each equally likely to occur. Encoding the symbols one by one would require 2 bits per symbol, which is wasteful: one of the bit variations is never used. That is to say, symbols A, B and C might be encoded respectively as 00, 01 and 10, with 11 unused. A more efficient solution is to represent a sequence of these three symbols as a rational number in base 3 where each digit represents a symbol. For example, the sequence "ABBCAB" could become 0.0112013, in arithmetic coding as a value in the interval [0, 1). The next step is to encode this ternary number using a fixed-point binary number of sufficient precision to recover it, such as 0.00101100012 – this is only 10 bits; 2 bits are saved in comparison with naïve block encoding. This is feasible for long sequences because there are efficient, in-place algorithms for converting the base of arbitrarily precise numbers. To decode the value, knowing the original string had length 6, one can simply convert back to base 3, round to 6 digits, and recover the string.

Defining a model In general, arithmetic coders can produce near-optimal output for any 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 optimal the output will be. Example: a simple, static model for describing the output of a particular monitoring instrument over time might be:

60% chance of symbol NEUTRAL 20% chance of symbol POSITIVE 10% chance of symbol NEGATIVE 10% chance of symbol END-OF-DATA. (The presence of this symbol means that the stream will be 'internally terminated', as is fairly common in data compression; when this symbol appears in the data stream, the decoder will know that the entire stream has been decoded.) 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 continually change their prediction of the data based on what the stream actually contains. The decoder must have the same model as the encoder.

Encoding and decoding: overview In general, each step of the encoding process, except for the last, is the same; the encoder has basically just three pieces of data to consider:

The next symbol that needs to be encoded The current interval (at the very start of the encoding process, the interval is set to [0,1], but that will change) The probabilities the model assigns to each of the various symbols that are possible at this stage (as mentioned earlier, higher-order or adaptive models mean that these probabilities are not necessarily the same in each step.) 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:

… excerpt ends here. Continue reading the full article.

Illustrations

Arithmetic coding: An arithmetic coding example assuming a fixed probability distribution of three symbols "A", "B", and "C". Probability of "A" is 50%, probability of "B" is 33% and probability of "C" is 17%. Furthermore, we assume that the recursion depth is known in each step. In step one we code "B" which is inside the interval [0.5, 0.83): The binary number "0.10x" is the shortest code that represents an interval that is entirely inside [0.5, 0.83). "x" means an arbitrary bit sequence. There are two extreme cases: the smallest x stands for zero which represents the left side of the represented interval. Then the left side of the interval is dec(0.10) = 0.5. At the other extreme, x stands for a finite sequence of ones which has the upper limit dec(0.11) = 0.75. Therefore, "0.10x" represents the interval [0.5, 0.75) which is inside [0.5, 0.83). Now we can leave out the "0." part since all intervals begin with "0." and we can ignore the "x" part because no matter what bit-sequence it represents, we will stay inside [0.5, 0.75).
An arithmetic coding example assuming a fixed probability distribution of three symbols "A", "B", and "C". Probability of "A" is 50%, probability of "B" is 33% and probability of "C" is 17%. Furthermore, we assume that the recursion depth is known in each step. In step one we code "B" which is inside the interval [0.5, 0.83): The binary number "0.10x" is the shortest code that represents an interval that is entirely inside [0.5, 0.83). "x" means an arbitrary bit sequence. There are two extreme cases: the smallest x stands for zero which represents the left side of the represented interval. Then the left side of the interval is dec(0.10) = 0.5. At the other extreme, x stands for a finite sequence of ones which has the upper limit dec(0.11) = 0.75. Therefore, "0.10x" represents the interval [0.5, 0.75) which is inside [0.5, 0.83). Now we can leave out the "0." part since all intervals begin with "0." and we can ignore the "x" part because no matter what bit-sequence it represents, we will stay inside [0.5, 0.75).
Arithmetic coding: Encoding the message "WIKI" with arithmetic coding
Encoding the message "WIKI" with arithmetic coding
Arithmetic coding: The above example visualised as a circle, the values in red encoding "WIKI" and "KIWI" – in the SVG image, hover over an interval to highlight it and show its statistics
The above example visualised as a circle, the values in red encoding "WIKI" and "KIWI" – in the SVG image, hover over an interval to highlight it and show its statistics
Arithmetic coding: A diagram showing decoding of 0.538 (the round dot) in the example model. The region is divided into subregions proportional to symbol frequencies, then the subregion containing the point is successively subdivided in the same way.
A diagram showing decoding of 0.538 (the round dot) in the example model. The region is divided into subregions proportional to symbol frequencies, then the subregion containing the point is successively subdivided in the same way.

Worked examples

Example 1 — a first encounter with Arithmetic coding

Start with the simplest possible case. Write down what Arithmetic coding claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Arithmetic coding before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Arithmetic coding ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Arithmetic coding

In research
Arithmetic coding appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Arithmetic coding in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Arithmetic coding is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data compression, Entropy coding, so understanding it makes those chapters shorter.
In everyday life
Look for Arithmetic coding outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Arithmetic coding” →

How to study Arithmetic coding in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Arithmetic coding means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Arithmetic coding out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Arithmetic coding in simple terms?

Arithmetic coding (AC) is a form of entropy coding used in lossless data compression. Normally, a string of characters is represented using a fixed number of bits per character, as in the ASCII code.

Why does Arithmetic coding matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Arithmetic coding?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Arithmetic coding.

Tags

  • Data compression
  • Entropy coding

Keep exploring