VIEW THIS AS

Auto mode follows the Route Engine until you choose a viewpoint.

YOU ARE HERE

ROUTE CHECK

CONNECTED TO

WHAT NEXT

Use the canonical route for this room, or HELP if you are unsure.

How Compression Works | Context Tree Weighting — How a Compressor Learns Which Past Still Matters

Three students in school uniforms work through open books at a classroom table, with textbooks and stationery nearby and study notes on the whiteboard behind them.

Context Tree Weighting is a lossless compression and prediction method that asks a deceptively difficult question: how much of the past should a compressor remember? In information theory and universal source coding, longer context can reveal powerful dependencies, but it can also fragment evidence into tiny samples. CTW avoids choosing one fixed context depth by combining many variable-order context models into one probability estimate.

The Context Tree Weighting algorithm sits where data compression, probability modelling, variable-order Markov models, Bayesian-style model averaging, entropy coding and sequential prediction meet. It can learn from a source whose exact parameters are unknown, assign probabilities to the next symbol from the observed history, and turn those probabilities into code lengths through arithmetic or related entropy coding. The central achievement is not merely remembering more history. It is learning which history deserves trust.

That makes CTW a useful answer to the deeper compression problem: information does not become smaller because the machine discovers nonexistent space; it becomes smaller when repeated structure, context and prediction make parts of the message inferable. Context trees provide a disciplined way to represent that inferability. Short contexts supply robust statistics; long contexts capture specific patterns; weighting lets them compete without forcing the encoder to declare one winner for the entire source.

Quick Read

  • CTW is a lossless prediction and compression method for sources with context-dependent structure.
  • It does not commit globally to one Markov order or one context depth.
  • A context tree stores statistics for suffixes of recent history.
  • Short contexts have more evidence but less specificity; long contexts have more specificity but less evidence.
  • CTW recursively mixes model choices so useful context depth can vary with the data.
  • Binary CTW is classically paired with probability estimators such as Krichevsky–Trofimov and an entropy coder.
  • The method has strong theoretical guarantees for appropriate source classes while remaining an implementable compression architecture.
  • Its deeper lesson extends beyond CTW: memory should be paid for only when it reduces future uncertainty.

The One-Sentence Answer

Context Tree Weighting compresses a sequence by predicting each next symbol from a weighted mixture of context models, allowing the useful amount of memory to emerge from the data rather than being fixed in advance.

1. The Problem Is Not Memory. It Is Trust.

Suppose you are predicting the next letter in English. Knowing nothing except global frequencies is useful. Knowing the previous letter is often better. Knowing the previous two or three letters can be better again. After q, u becomes highly likely. After “th”, a different set of continuations becomes plausible. After a whole familiar word stem, prediction can become sharper still.

So why not remember the previous hundred characters?

Because specificity consumes evidence. A context of length two may have occurred thousands of times. A context of length twenty may have occurred once. The longer context feels more informative, but one observation is a fragile basis for probability.

This creates the central context-modelling trade-off:

short context → abundant evidence, weak specificity
long context  → strong specificity, sparse evidence

A fixed-order Markov model chooses one compromise in advance. CTW asks whether the compromise itself can be learned.

2. What a Context Means

For a binary sequence, imagine predicting whether the next symbol is 0 or 1. A depth-zero model ignores history and counts all zeros and ones. A depth-one model keeps separate counts after previous 0 and previous 1. A depth-two model distinguishes 00, 01, 10 and 11. Each extra level doubles the possible binary contexts.

The tree is a map of suffixes of the recent past. The root represents no conditioning context. Descending one level conditions on one previous bit. Descending again conditions on two.

At any node, the compressor can ask: given that this context occurred, what followed?

Those counts become evidence for a conditional probability model.

3. Why Fixed-Order Models Break

A zero-order model misses dependencies. A very high-order model overfits and wastes memory. The best order can also change across a file.

In source code, indentation may be locally predictable from nearby characters while an identifier repeats after a longer gap. In DNA, some motifs may need longer context while other positions are well described by shorter statistics. In an image bitplane, local neighbourhood structure can vary across smooth regions and edges.

One global context depth is therefore an architectural convenience, not necessarily a property of the source.

4. The Context Tree Is a Library of Possible Memories

Take a maximum depth D. For every recent history, CTW can consult suffix contexts from the root down toward depth D. These nodes collectively contain a family of candidate explanations.

One explanation says the root is enough: the next bit depends only on global bias. Another says split at depth one. Another keeps one branch shallow but allows another branch to split deeper. A context tree can therefore represent variable-order structure, not merely one uniform order.

The number of possible pruned subtrees can be enormous. Explicitly testing every candidate tree would be expensive.

CTW’s elegance lies in recursively combining them without enumerating them one by one.

5. Local Evidence: The Krichevsky–Trofimov Estimator

At a binary context node, suppose we have observed a zeros and b ones. We need a probability assignment that remains sensible even before much evidence arrives.

A simple maximum-likelihood estimate would use a/(a+b) and b/(a+b). But if a symbol has not appeared yet, this can assign probability zero. In sequential lossless coding, zero probability is dangerous: an unseen event becomes impossible to encode under that model.

The Krichevsky–Trofimov estimator uses a half-count prior intuition. Predictive probabilities take the form:

P(next=0) = (a + 1/2) / (a + b + 1)
P(next=1) = (b + 1/2) / (a + b + 1)

No ordinary unseen binary outcome receives zero probability. Early uncertainty is acknowledged rather than hidden.

6. Why Half a Count Matters

Imagine a context observed once, followed by 0. Maximum likelihood says the next symbol is certainly 0. That confidence is absurdly strong for one observation.

KT smoothing keeps both possibilities alive. As evidence accumulates, the half-count influence becomes negligible. The estimate moves toward empirical frequency.

This is an important compression principle: early humility can save enormous later cost. A model that becomes absolutely certain too quickly may be cheap until reality disagrees, then catastrophically expensive.

7. The Weighting Move

At a node, CTW has two broad stories available.

  1. Treat this node as a leaf and use its local probability estimate.
  2. Trust the more specific child contexts beneath it.

Instead of selecting one story permanently, the weighted probability combines them. In the classic binary formulation with equal prior weighting, the node’s weighted probability is conceptually a mixture of its own KT probability and the product of weighted probabilities from its children.

That recursion is the heart of CTW.

If deeper contexts predict well, their probability contribution earns influence. If they fragment evidence and perform poorly, the shallower estimate remains competitive.

8. One Tree, Many Models

This is where CTW becomes conceptually beautiful. The physical context tree is not merely one model. Through weighting, it represents a mixture over a large family of pruned context trees.

Each pruning corresponds to a different decision about where history matters and where it does not. Some branches can be deep. Others can stop early.

Rather than ask the encoder to choose the correct model before seeing enough data, CTW carries competing explanations forward and lets cumulative predictive evidence determine their effective influence.

Compression becomes model selection without a brittle winner-takes-all moment.

9. Probability Becomes Code Length Again

CTW is fundamentally a probability assignment and prediction mechanism. To make a compressed file, those probabilities must become bits.

Arithmetic coding is a natural partner. If CTW assigns probability p to the symbol that actually occurs, the ideal incremental coding cost is approximately -log₂p bits. Good predictions are cheap. Surprises are expensive.

The sequence of CTW probabilities therefore defines an ideal cumulative code length:

L ≈ - Σ log₂ P(x_t | history)

This reconnects CTW directly to the entropy-coding article before it. The entropy coder is the cashier. CTW is one of the machines deciding the prices.

10. Universal Coding: What If We Do Not Know the Source?

Many textbook coding examples assume the source distribution is known. Real compressors usually do not receive a perfect probability manual with the input.

A universal coding method learns from the observed sequence and seeks performance close to an informed benchmark over a defined source class. The extra code length paid for not knowing the source in advance is redundancy or regret, depending on the formal setup.

CTW is important because it combines practical sequential prediction with strong theoretical results for context-tree source classes. It does not need to know the true tree structure or parameters beforehand.

11. Unknown Parameters Are Not the Same as Unknown Structure

There are at least two kinds of ignorance.

First, we may know the correct context structure but not the transition probabilities. KT-style estimation addresses parameter uncertainty.

Second, we may not know which contexts should exist as meaningful leaves. Weighting across tree structures addresses model-structure uncertainty.

CTW handles both in one recursive architecture. That is much more interesting than simply keeping a large table of counts.

12. Why Maximum Depth Still Matters

CTW avoids choosing one exact order, but it still needs an outer memory horizon in practical finite-depth implementations. Maximum depth D says how far into the past the context tree is allowed to look.

Set D too small and long dependencies are invisible. Set it too large and memory, update cost and sparse branches grow. Weighting protects against some overfitting, but computation is still real.

The design question becomes more subtle: not “which exact context length is correct?” but “what is the longest context worth making available to the mixture?”

13. Sparse Contexts and the Cost of Specificity

A depth-20 binary context space contains more than a million possible histories. Most short files will visit only a tiny fraction.

Implementations therefore create nodes as needed rather than allocating a complete tree. Even then, every stored node costs memory and update work.

This turns context modelling into a resource-allocation problem. Which histories deserve persistent state? Which can be pruned? Which statistics can be forgotten without hurting prediction?

The theoretical tree meets the cache hierarchy.

14. Pruning: Memory Must Earn Its Rent

Practical CTW variants may prune or limit context-tree growth. A branch that receives little evidence may not justify its memory footprint. A stale branch may become irrelevant after source drift.

Pruning is not merely an implementation nuisance. It expresses the same principle as compression itself: keep distinctions only when they improve future reconstruction enough to justify their cost.

A compressor that remembers every possible pattern forever can spend more memory maintaining its model than the model saves in output bits.

15. CTW and the Bias–Variance Trade-Off

Short contexts are biased: they ignore real dependencies. Long contexts have high variance: their