Arithmetic coding
Arithmetic coding is a lossless entropy coding method that represents a whole message as a sub-interval of [0,1), giving near-optimal compression when combined with an accurate probability model.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Arithmetic coding is a core lossless compression technique used by engineers when compressing symbols that follow known or learned probabilities. It is used inside image and video codecs and other multimedia systems (for example as the entropy coder in standards and codecs that follow transform/quantisation stages) because it approaches the theoretical entropy limit and handles skewed probability distributions efficiently.
Key ideas
Start with definitions (simple language):
- Alphabet: the finite set of distinct symbols that can appear in a message, e.g., {A, B, C}.
- Symbol: one element from the alphabet.
- Probability model: an assignment of probabilities p(s) to each symbol s in the alphabet. The model may be static (fixed before coding) or adaptive (updated as symbols are processed).
- Cumulative distribution function (CDF): for discrete alphabet, CDF(s) = Σ p(t) for t < s (symbols ordered). It defines intervals for each symbol.
- Interval and range: arithmetic coding represents the entire message as a sub-interval [low, high) inside [0,1). The range is high − low.
- Encoder: algorithm that maps a message to a number (tag) inside the final interval, emitting bits progressively.
- Decoder: algorithm that recovers the message from the tag bits using the same probability model.
- Renormalization: practical step to keep numbers in finite precision by emitting output bits whenever the current interval allows a fixed leading bit; it avoids needing infinite precision.
- Tag: any number in [low, high) representing the message; sent as bits.
Principle and derivation (step-by-step):
- Assume a known probability model p(s) over the alphabet, with Σ p(s) = 1 and p(s) > 0 for all used symbols.
- Order symbols in some fixed order. Compute CDF(s) = Σ_{t < s} p(t). For symbol s its sub-interval (relative to [0,1)) is [CDF(s), CDF(s) + p(s)).
- To encode a sequence of symbols s1, s2, ..., sn, start with [low_0, high_0) = [0,1). For each symbol si do:
- Compute range_i = high_{i−1} − low_{i−1}.
- Set low_i = low_{i−1} + range_i · CDF(si).
- Set high_i = low_{i−1} + range_i · (CDF(si) + p(si)). This maps the symbol's own sub-interval into the current interval, shrinking the interval.
- After processing all symbols you get interval [low_n, high_n). Any number t in this interval encodes the entire message. In practice, encoders choose a binary fraction inside the interval and output its bits. To avoid infinite-precision arithmetic, renormalization is used to emit definite leading bits when possible and rescale the interval.
Why the algorithm is optimal (intuitively): the length of the final interval is Π p(si) for a static model, and for an adaptive model it equals the product of conditional probabilities Π p(si | s1..si−1); taking −log2 of that product gives the ideal bit count predicted by information theory. Formally, for a stationary memoryless source the expected code length per symbol approaches the source entropy H = −Σ p(s) log2 p(s) when the message is long and the probability model matches the source.
What to draw (diagrams exam answers expect):
- Draw the interval [0,1) on a horizontal line. Partition it into segments for each symbol using the CDF boundaries; label each segment with symbol and its probability p(s) and CDF values.
- Show how after encoding the first symbol you pick its segment and then partition that segment by the same relative proportions for the second symbol. Label low and high for each step and arrow to the next zoomed segment.
- For renormalization, draw [0,1) with the three regions used in the E1/E2/E3 method: [0,0.5), [0.5,1), and the middle [0.25,0.75) to illustrate MSB emission and underflow handling.
Assumptions clearly stated:
- Encoder and decoder share exactly the same probability model and symbol ordering at encode and decode time. The decoder must also know when to stop: either the message length (number of symbols) is known in advance or a special end-of-message (EOF) symbol with nonzero probability is encoded.
- Arithmetic coding works with finite-precision arithmetic in real implementations; renormalization and integer arithmetic approximations are necessary. Exact infinite-precision arithmetic is the mathematical model.
Limitations and practical notes:
- Finite precision leads to implementation details: renormalization, carry propagation, and underflow handling (often labelled E3 in texts). A finalization (flush) step is required at the end to emit a terminating bit and any deferred underflow bits so that the final tag lies within [low, high).
- Adaptive arithmetic coding updates counts/probabilities as symbols are processed; this avoids a separate pass to compute symbol frequencies but requires careful decoder synchronization.
Formulas
range = high − low — range is the current interval width (dimensionless; applies to real interval inside [0,1)).
low' = low + range · CDF(symbol) — new low; CDF(symbol) is cumulative probability of symbols before the chosen one (dimensionless; applies at each symbol step).
high' = low + range · (CDF(symbol) + p(symbol)) — new high; p(symbol) is the symbol probability (dimensionless).
tag ∈ [low_n, high_n) — any real number in final interval encodes the whole message (dimensionless; tag represented as binary fraction).
H(X) = − Σ p(x) · log2 p(x) — entropy of source X in bits per symbol; p(x) dimensionless.
E[L_per_symbol] ≥ H(X) — expected encoded length per symbol (in bits/symbol) is at least the source entropy (bits/symbol). For a block of n symbols, the expected total length satisfies E[L_total] ≥ n · H(X). Equality is approached with optimal coding and long messages.
bits_needed = ceil(−log2(range_final)) — minimal number of bits required to select a binary tag inside the final interval; because a dyadic interval of width 2^{−k} fits inside when k ≥ −log2(range_final).
Notes on symbols: CDF(s) is the cumulative probability up to but not including s. All logs are base 2 for bits.
Worked examples
Example 1 — encoding a small sequence with a static model (numerical, step-by-step):
Alphabet = {A, B, C} with probabilities p(A)=0.5, p(B)=0.3, p(C)=0.2. Order A, B, C.
CDF(A)=0.0, CDF(B)=0.5, CDF(C)=0.8.
Encode message = "BA".
Step 0: start [low_0, high_0) = [0.0, 1.0).
Step 1: symbol B: range_1 = 1.0 − 0.0 = 1.0.
low_1 = 0.0 + 1.0 · 0.5 = 0.5.
high_1 = 0.0 + 1.0 · (0.5 + 0.3) = 0.8.
So interval after B: [0.5, 0.8).
Step 2: symbol A inside [0.5, 0.8): range_2 = 0.8 − 0.5 = 0.3.
low_2 = 0.5 + 0.3 · 0.0 = 0.5.
high_2 = 0.5 + 0.3 · (0.0 + 0.5) = 0.5 + 0.15 = 0.65.
Final interval: [0.5, 0.65). Its length = 0.15. Bits required ≈ −log2(0.15) ≈ 2.737 bits, so you need 3 bits to choose a tag inside.
Choose tag = 0.625 (binary 0.101) because 0.5 ≤ 0.625 < 0.65.
Final encoded bitstream (minimum fixed-length tag) = 101. Bold final numeric result: 101 (binary), tag = 0.625.
Example 2 — decoding the tag 0.625 with same model to recover "BA": Start [low_0, high_0) = [0,1), tag = 0.625. Step 1: range = 1.0. Locate which symbol's interval contains (tag−low)/range = 0.625.
- A interval [0.0,0.5) -> 0.625 not in.
- B interval [0.5,0.8) -> 0.625 in -> symbol = B. Update low = 0.5, high = 0.8. Step 2: range = 0.3. Normalized position (tag − low)/range = (0.625 − 0.5)/0.3 = 0.125/0.3 ≈ 0.4167. Check symbol intervals relative to this range:
- A corresponds to [0.0,0.5) -> 0.4167 in -> symbol = A. Update to low = 0.5, high = 0.65. Message recovered: "BA". Final answer confirmed.
Example 3 — show renormalization idea (short sketch of steps needed in integer implementation):
- On each symbol update, multiply interval endpoints by 2 (shift left) and output a bit while the MSB of low and high are equal; if MSB differ but both are in middle region handle underflow counter (E3) then continue. This is the practical algorithm used in textbooks and implementations.
Common mistakes
- Miscomputing CDF boundaries by including the current symbol's probability in CDF(s) instead of summing symbols before s.
- Choosing a tag outside [low, high) (e.g., using rounded binary with poor rounding logic) — must ensure tag lies in interval.
- Thinking arithmetic coding outputs one codeword per symbol. It outputs a single tag for an entire message (bits are produced progressively via renormalization).
- Confusing entropy inequality: E[L_per_symbol] ≥ H(X), but practical coder approaches H(X) for long messages and good model (not necessarily equal for short messages). Forgetting to signal termination (known message length or an explicit EOF symbol) so the decoder knows when to stop.
For your exam
Types of questions to expect:
- Definitions and short notes: define arithmetic coding, CDF, renormalization, static vs adaptive model.
- Derivations: show how the interval update formulas are derived; show relationship between final interval length and Σ log p(s).
- Numericals: encode or decode a short message given probabilities; trace renormalization steps.
- Programs/algorithms: write or explain pseudocode for encoder and decoder including renormalization and E3 underflow handling.
- Diagrams: draw interval partitioning and show successive refinement for first two symbols.
What full-mark answers include:
- Clear definitions of terms used and assumptions (shared model, ordering).
- Correct interval update formulas with derivation or explanation.
- For numerical problems: show step-by-step interval values, ranges, and final tag or bits, with units (dimensionless).
- For algorithm questions: include renormalization, underflow handling, and how encoder/decoder stay synchronized.
Quick check
- What is the CDF value used for when encoding a symbol?
- If p(A)=0.75 and p(B)=0.25, which symbol will produce the smaller sub-interval on first encoding step?
- True or False: arithmetic coding always outputs exactly one codeword per symbol. Answers: 1. To find the lower bound of the symbol's sub-interval (it is the sum of probabilities of symbols before it). 2. B (p(B)=0.25 gives smaller interval). 3. False (it encodes whole message as one tag, emitting bits progressively via renormalization).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?