ArticleslgStudy

science

Context mixing

Context mixing 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 Context mixing rather than just read about it. In short: Context mixing is a type of data compression algorithm in which the next-symbol predictions of two or more statistical models are combined to yield a prediction that is often more accurate than any of the individual predictions. For example, one simple method (not necessarily the best) is to average the probabilities assigned by each model.

Key takeaways

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

Reference excerpt

Context mixing is a type of data compression algorithm in which the next-symbol predictions of two or more statistical models are combined to yield a prediction that is often more accurate than any of the individual predictions. For example, one simple method (not necessarily the best) is to average the probabilities assigned by each model. The random forest is another method: it outputs the prediction that is the mode of the predictions output by individual models. Combining models is an active area of research in machine learning. The PAQ series of data compression programs use context mixing to assign probabilities to individual bits of the input.

Application to Data Compression Suppose that we are given two conditional probabilities, P(X|A) and P(X|B), and we wish to estimate P(X|A,B), the probability of event X given both conditions A and B. There is insufficient information for probability theory to give a result. In fact, it is possible to construct scenarios in which the result could be anything at all. But intuitively, we would expect the result to be some kind of average of the two. The problem is important for data compression. In this application, A and B are contexts, X is the event that the next bit or symbol of the data to be compressed has a particular value, and P(X|A) and P(X|B) are the probability estimates by two independent models. The compression ratio depends on how closely the estimated probability approaches the true but unknown probability of event X. It is often the case that contexts A and B have occurred often enough to accurately estimate P(X|A) and P(X|B) by counting occurrences of X in each context, but the two contexts either have not occurred together frequently, or there are insufficient computing resources (time and memory) to collect statistics for the combined case. For example, suppose that we are compressing a text file. We wish to predict whether the next character will be a linefeed, given that the previous character was a period (context A) and that the last linefeed occurred 72 characters ago (context B). Suppose that a linefeed previously occurred after 1 of the last 5 periods (P(X|A=0.2) and in 5 out of the last 10 lines at column 72 (P(X|B)=0.5). How should these predictions be combined? Two general approaches have been used, linear and logistic mixing. Linear mixing uses a weighted average of the predictions weighted by evidence. In this example, P(X|B) gets more weight than P(X|A) because P(X|B) is based on a greater number of tests. Older versions of PAQ uses this approach. Newer versions use logistic (or neural network) mixing by first transforming the predictions into the logistic domain, log(p/(1-p)) before averaging. This effectively gives greater weight to predictions near 0 or 1, in this case P(X|A). In both cases, additional weights may be given to each of the input models and adapted to favor the models that have given the most accurate predictions in the past. All but the oldest versions of PAQ use adaptive weighting. Most context mixing compressors predict one bit of input at a time. The output probability is simply the probability that the next bit will be a 1.

Linear Mixing We are given a set of predictions Pi(1)=n1i/ni, where ni=n0i+n1i, and n0i and n1i are the counts of 0 and 1 bits respectively for the i'th model. The probabilities are computed by weighted addition of the 0 and 1 counts:

S0=∑iwin0i S1=∑iwin1i S=S0+S1 P(0)=S0S P(1)=S1S The weights wi are initially equal and always sum to 1. Under the initial conditions, each model is weighted in proportion to evidence. The weights are then adjusted to favor the more accurate models. Suppose we are given that the actual bit being predicted is y (0 or 1). Then the weight adjustment is:wi←max⁡[0,wi+(y−P(1))Sn1i−S1niS0S1]Compression can be improved by bounding ni so that the model weighting is better balanced. In PAQ6, whenever one of the bit counts is incremented, the part of the other count that exceeds 2 is halved. For example, after the sequence 000000001, the counts would go from (n0,n1)=(8,0) to (5,1).

Logistic Mixing Let Pi(1) be the prediction by the i'th model that the next bit will be a 1. Then the final prediction P(1) is calculated:

xi=stretch(Pi(1)) P(1)=squash(∑iwixi) where P(1) is the probability that the next bit will be a 1, Pi(1) is the probability estimated by the i'th model, and

stretch(x)=ln⁡(x/(1−x)) squash(x)=stretch−1(x)=1/(1+e−x) After each prediction, the model is updated by adjusting the weights to minimize coding cost.

wi←wi+ηxi(y−P(1)) where η is the learning rate (typically 0.002 to 0.01), y is the predicted bit, and (y−P(1)) is the prediction error.

List of Context Mixing Compressors All versions below use logistic mixing unless otherwise indicated.

All PAQ versions (Matt Mahoney, Serge Osnach, Alexander Ratushnyak, Przemysław Skibiński, Jan Ondrus, and others) [1]. PAQAR and versions prior to PAQ7 used linear mixing. Later versions used logistic mixing. All LPAQ versions (Matt Mahoney, Alexander Ratushnyak) [2]. ZPAQ (Matt Mahoney) [3]. WinRK 3.0.3 (Malcolm Taylor) in maximum compression PWCM mode [4]. Version 3.0.2 was based on linear mixing. NanoZip (Sami Runsas) in maximum compression mode (option -cc) [5]. xwrt 3.2 (Przemysław Skibiński) in maximum compression mode (options -i10 through -i14) [6] as a back end to a dictionary encoder. cmm1 through cmm4, M1, and M1X2 (Christopher Mattern) use a small number of contexts for high speed. M1 and M1X2 use a genetic algorithm to select two bit masked contexts in a separate optimization pass. ccm (Christian Martelock). bit (Osman Turan) [7]. pimple, pimple2, tc, and px (Ilia Muraviev) [8]. enc (Serge Osnach) tries several methods based on PPM and (linear) context mixing and chooses the best one. [9] fpaq2 (Nania Francesco Antonio) using fixed weight averaging for high speed. cmix (Byron Knoll) mixes many models, and is currently ranked first in the Large Text Compression benchmark, as well as the Silesia corpus and has surpassed the winning entry of the Hutter Prize although it is not eligible due to using too much memory.

References

Worked examples

Example 1 — a first encounter with Context mixing

Start with the simplest possible case. Write down what Context mixing 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 Context mixing 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 Context mixing 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 Context mixing

In research
Context mixing 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 Context mixing 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
Context mixing is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data compression, so understanding it makes those chapters shorter.
In everyday life
Look for Context mixing 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 “Context mixing” →

How to study Context mixing in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Context mixing 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 Context mixing out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Context mixing in simple terms?

Context mixing is a type of data compression algorithm in which the next-symbol predictions of two or more statistical models are combined to yield a prediction that is often more accurate than any of the individual predictions. For example, one simple method (not necessarily the best) is to averag…

Why does Context mixing 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 Context mixing?

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 Context mixing.

Tags

  • Data compression

Keep exploring