AI Glossary / MLOps and deployment / Intermediate

Arithmetic coding

Definition

Arithmetic coding is a lossless entropy-coding method that represents a sequence of symbols by progressively narrowing a numerical interval according to a shared probability model. Likely symbols consume less coding information than unlikely ones, allowing efficient representation without assigning a separate whole-bit codeword to every symbol.

What it means

A probability model estimates how likely the next symbol is. Arithmetic coding turns those estimates into a compact bitstream. The model and the coder have different jobs: the model supplies probabilities, while the coder represents the actual sequence using them.

Good predictions make observed symbols inexpensive to encode. A mismatched model performs poorly; coding cannot create predictability.

Arithmetic coding is lossless for the symbols it receives. It can nevertheless appear inside a lossy image or audio codec, where an earlier stage has already discarded information. Exact recovery of quantized coefficients does not restore detail removed before entropy coding.

How it works

Conceptually, begin with the interval from zero to one. Divide it into adjacent subintervals whose sizes match the symbol probabilities. Select the subinterval corresponding to the first input symbol.

Divide that selected interval again using the next probability distribution, then select the next symbol's portion. Repeating this process leaves a narrow interval identifying the sequence. A suitable number or finite bit representation identifying that interval lets the decoder retrace the choices.

Real implementations do not need to retain an infinitely precise decimal fraction. They use finite integer arithmetic, renormalization and controlled output of bits as coding progresses. Encoder and decoder must agree exactly on their probability counts, rounding conventions and update order.

The stream also needs a stopping rule, such as a known symbol count or end marker. Otherwise, a numerical description can correspond to prefixes of longer sequences. Any model information, termination bits and framing belong in the total compression cost.

Three practical examples

1. Text or event data: A context model predicts the next character or event label, and arithmetic coding represents the observed sequence according to those predictions.

2. Image-compression components: An image codec can entropy-code symbols describing transformed or quantized data. The arithmetic stage's losslessness should be distinguished from the complete codec's reconstruction quality.

3. Model evaluation: Engineers can compare probability models by their coding lengths on held-out sequences. They must include model-transmission costs and avoid confusing excellent training-data predictions with performance on new inputs.

Think of it this way

Think of an address formed by repeatedly choosing smaller regions on a map. The final location identifies the route through those nested choices. Large regions correspond to likely choices and need less additional detail to distinguish them.

A real bitstream follows precise arithmetic and termination rules. Both sides must use identical boundaries; it is not an ordinary decimal address.

Worked example

Worked example

Use a fixed model with A probability 1/2, B probability 1/4 and C probability 1/4. Within any current interval, place A first, then B, then C. Encode the three-symbol message B, A, C, with the length known separately.

Initially the interval is [0, 1). B selects [0.5, 0.75), which has width 0.25.

Within that interval, A takes the first half, giving [0.5, 0.625), width 0.125. C then takes its final quarter, giving [0.59375, 0.625), width 0.03125, or 1/32.

The value 0.609375 lies inside the final interval. With the shared model, a decoder first places it in B's region, then A's subdivision, then C's subdivision. The known length determines when to stop.

The probability of B, A, C under this model is (1/4) × (1/2) × (1/4) = 1/32, corresponding to an ideal information length of five bits. That is an information calculation, not a promise that a complete three-symbol file occupies exactly five bits. Finite precision, termination and the separately supplied length or model can add overhead.

Illustrative case study

Illustrative case study - fictional

A fictional data-platform team compresses device-event sequences. One model uses the same event frequencies everywhere; another conditions predictions on the previous event type.

The second model predicts common transitions better in development data. Before adopting it, the team checks new devices and unusual fault sequences. It also tests decoding with an independently implemented reader, including model resets and end-of-stream handling.

The evaluation includes model-table and block-metadata costs. No measured compression gain is claimed.

Common misconceptions and mistakes

  • Arithmetic coding is not encryption; knowing the coding rules and model permits decoding.
  • Lossless entropy coding can be one stage of an otherwise lossy pipeline.
  • A probability of zero for a possible input needs an escape or other handling rule; the coder cannot allocate an interval of positive width to it automatically.
  • A decimal illustration is not a practical file-format specification.
  • Compression quality depends strongly on the probability model, not just the coder's name.

Business use

Arithmetic coding matters when evaluating compression components, learned probability models and efficient storage formats. Require a clear description of the model, symbol alphabet and compatibility rules.

For an AI-related compressor, distinguish the cost of running a predictive model from the savings it enables. Measure encode and decode throughput, complete output size and exact reconstruction where promised. Include atypical records: a model that assigns them very small probabilities can produce unexpectedly long encodings. Independent block boundaries can improve access and limit the consequences of corrupted state, at some overhead cost.

People also ask

Does arithmetic coding require machine learning?

No. Its probabilities can come from a fixed table, simple adaptive counts or a more complex learned model.

Why can it beat a separate Huffman code for every symbol?

It represents sequences without forcing each symbol's contribution to be a whole number of bits. Actual gains still depend on the model and overhead.

Must the entire message be available before encoding?

Not necessarily. Streaming implementations can emit bits progressively, provided their modeling and framing rules support the workflow.

Check your understanding

An encoder changes its symbol probabilities after each symbol, but the decoder keeps the initial probabilities. Is exact reconstruction assured?

Answer: No. Both sides must use matching distributions at every coding step. Different interval boundaries can produce different decoded symbols.

Related terms

  • Entropy coding
  • Probability model
  • Range coding
  • Adaptive compression
  • Asymmetric numeral systems
  • Lossless compression
  • Huffman coding

Learn next

Repeat the worked example after making C less probable and A more probable. Track how the final interval width changes, then calculate the ideal information length separately from a real encoded-file size.

More in MLOps and deployment

  • Algorithm BSTW - Algorithm BSTW is a lossless data-compression scheme associated with Jon Bentley, Daniel Sleator, Robert Tarjan and Victor Wei.
  • Anamorphic stretch transform - The anamorphic stretch transform (AST) is a signal transformation that uses deliberately nonuniform stretching to reshape a signal for acquisition or...
  • AND-parallelism - AND-parallelism means solving required subproblems concurrently when their results must all contribute to the overall solution.
  • Audio codec - An audio codec is a method, together with an implementation of that method, for encoding and decoding digital audio.
  • Autonomic computing - Autonomic computing is an approach to building computing systems that manage aspects of their own operation according to human-defined goals and policies.
  • Average bitrate - Average bitrate is the total number of bits used or transferred divided by the elapsed duration, usually expressed in bits per second.

Sources

Sources: Witten, Neal and Cleary, Arithmetic Coding for Data Compression: https://www.csd.uoc.gr/~hy474/bibliography/Witten.pdf ; Project Nayuki, reference implementation: https://github.com/nayuki/Reference-arithmetic-coding/blob/master/python/adaptive-arithmetic-compress.py . The paper's model and interval explanations and the reference implementation's adaptive update order were reviewed. The worked interval is an original example, not an implementation test.

Previous: ARIMA model Next: Arity

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer