Skip to content
Go back

Is Compression Really Prediction?

Over the past few weeks, I have repeatedly encountered the same claim on Hacker News: compression is prediction. The recent discussion has approached it from both directions. Two 3Blue1Brown videos, Reinventing Entropy and But what is cross-entropy?, derive entropy and cross-entropy from the limits of source coding. An ngrok article follows the same mathematics through arithmetic coding and language models. Salvatore Sanfilippo asks how far the resulting identification between prediction and compression should be taken.

These explanations meet at one fact. A probabilistic model assigns a conditional probability to every possible continuation, and an entropy coder converts the probability assigned to the observed continuation into bits. For a sequence x1:nx_{1:n} and a model QQ, the resulting ideal payload length is

log2Q(x1:n)=i=1nlog2Q(xix<i)-\log_2 Q(x_{1:n}) = \sum_{i=1}^n -\log_2 Q(x_i\mid x_{<i})

up to the overhead introduced by the coding procedure. The quantity on the right is also the model’s cumulative logarithmic loss. In this setting, improving prediction under log-loss and reducing the encoded payload are the same optimization problem.

I have spent the last few years working on compression, information theory, and compressed data structures and wanted to give my two cents. I agree with this equivalence, but I do not think it describes the complete compression problem. It applies after several choices have already been made. The encoder and decoder must agree on what kind of object is being represented, which alternatives remain possible, how the probability model is made available, and what the decoder must be able to do with the representation.

Throughout this article, compression means lossless compression unless stated otherwise. Even within that scope, compression can be defined before introducing a sequential model. A finite family of admissible objects gives a counting lower bound without identifying a next symbol. A fixed or data-dependent code can later be interpreted probabilistically, and a distribution over serialized objects can be factored into next-symbol conditionals. That reinterpretation does not choose the family of objects, pay for information unavailable to the decoder, or enforce operations such as random access.

The question is therefore not whether prediction and compression can be made mathematically equivalent. They can. The question is what must be fixed before the equivalence applies, which part of a complete representation its bit count measures, and what remains outside that measurement.

Table of Contents

Open Table of Contents

Compression Before Probability

The ngrok article begins by distinguishing minification from what it calls “true” compression. A minifier removes comments, whitespace, and other parts of a source file that do not affect its execution. The resulting program is shorter, but the original source file cannot be reconstructed from it.

Whether this operation is lossless depends on what the representation is required to preserve. If the object is the original sequence of source bytes, minification is lossy. If the object is the program’s behaviour and the decoder may return any behaviourally equivalent program, a semantics-preserving minifier is lossless relative to that different contract. The transformation has not changed. The object being represented has.

This distinction precedes any probability model. Before asking how likely an object is, the encoder and decoder must agree on what counts as that object and when two decoded outputs count as equivalent. Only then does the length of a description become meaningful.

Once an individual object xx has been fixed, the most permissive effective descriptions are programs that produce it. After choosing a universal machine UU, the Kolmogorov complexity of a binary string xx is

KU(x)=min{p:U(p)=x}K_U(x) = \min \left\{ |p| : U(p)=x \right\}

Thus, KU(x)K_U(x) is the length of the shortest program that outputs xx. Any regularity that can be expressed algorithmically may shorten this description. A string containing a billion zeros has a long literal representation but a short program that prints one billion zeros. The definition does not require the string to have been sampled from a source, and it does not require one symbol to be predicted from the symbols preceding it.

The machine UU is part of the description language. Choosing a different universal machine changes which programs are available and therefore changes the exact value of the complexity. The invariance theorem bounds this dependence. For two fixed universal machines UU and VV, there is a constant cU,Vc_{U,V} such that

KU(x)KV(x)cU,V\left| K_U(x)-K_V(x) \right| \leq c_{U,V}

for every string xx. The constant may depend on the two machines, but not on xx. It accounts for the fixed program needed to simulate one description language in the other.

Kolmogorov complexity gives a limit on the effective description of an individual object, but it does not provide a general compression algorithm. The function KUK_U is not computable. No procedure can determine the length of the shortest program for every string, much less construct that program. A practical compressor must restrict the descriptions it is willing and able to consider.

One such restriction is that the object belongs to a finite family F\mathcal{F}. Once F\mathcal{F} has been fixed, a lossless representation must distinguish every member of that family from every other member. Consider a fixed-length encoding

C:F{0,1}C : \mathcal{F} \longrightarrow \{0,1\}^{\ell}

Lossless decoding requires CC to be injective. Since only 22^\ell binary strings of length \ell exist, injectivity implies

2F2^\ell \geq |\mathcal{F}|

and therefore

log2F\ell \geq \left\lceil \log_2|\mathcal{F}| \right\rceil

An agreed enumeration of F\mathcal{F} attains this bound by assigning each object an index and representing that index in binary. The quantity

log2F\log_2|\mathcal{F}|

is the counting bound of the family. In parts of the succinct data-structure literature, the same quantity is called its worst-case entropy. I will use counting bound because its derivation does not assume that the objects are sampled uniformly, or that they are sampled at all. It only counts the alternatives that the representation must distinguish.

The family F\mathcal{F} is part of the information shared by the encoder and decoder. If the decoder knows only that the object belongs to a larger family G\mathcal{G}, then the representation must distinguish among the members of G\mathcal{G} instead. The lower bound becomes

log2G\log_2|\mathcal{G}|

A restriction from G\mathcal{G} to F\mathcal{F} saves bits only if the decoder already knows that restriction or if the representation communicates it. What counts as redundancy therefore depends on which alternatives have already been excluded.

Kolmogorov complexity and the counting bound answer different versions of the same preliminary question. The first considers the shortest effective description of one object. The second considers the number of bits needed to distinguish every object in a fixed finite family. Neither requires a probability distribution or a next-symbol predictor.

The counting bound treats all members of F\mathcal{F} symmetrically. It determines the optimal worst-case length when every object must fit within the same number of bits. It cannot express that some objects should receive shorter descriptions because they occur more frequently. Assigning unequal lengths requires a rule for deciding which objects receive the shorter ones. A probability distribution supplies that rule.

Possibilities Have Different Probabilities

The counting bound treats every admissible object symmetrically. To assign shorter descriptions to some objects, we need a rule that determines which objects receive them and which objects pay with longer descriptions. A probability distribution supplies that rule.

Let X\mathcal{X} be a finite set of possible objects. For each xXx\in\mathcal{X}, a source specifies a probability

P(x)=Pr(X=x)P(x)=\Pr(X=x)

where P(x)>0P(x)>0 and

xXP(x)=1\sum_{x\in\mathcal{X}}P(x)=1

A probability must be translated into a quantity measured in bits. If two independent outcomes occur with probabilities P(x)P(x) and P(y)P(y), their joint probability is the product P(x)P(y)P(x)P(y), while their bit costs should add. The logarithm performs this conversion. The information content of an outcome xx is

IP(x)=log2P(x)I_P(x) = -\log_2P(x)

An event with probability 2b2^{-b} has information content bb bits. More probable outcomes receive smaller values because fewer bits should be allocated to events that occur more often.

Before the source produces an outcome, its information content is not known. Its expected value is

H(P)=xXP(x)IP(x)=xXP(x)log2P(x)\begin{aligned} H(P) &= \sum_{x\in\mathcal{X}}P(x)I_P(x) \\ &= -\sum_{x\in\mathcal{X}}P(x)\log_2P(x) \end{aligned}

This is the Shannon entropy of the source. When PP is uniform, every object has information content

log2X\log_2|\mathcal{X}|

so Shannon entropy equals the counting bound of the family.

The values IP(x)I_P(x) are ideal bit costs derived from the source distribution. To obtain a representation, we need a binary code

C:X{0,1}C : \mathcal{X} \longrightarrow \{0,1\}^*

with codeword lengths

C(x)=C(x)\ell_C(x) = |C(x)|

The expected number of bits used by the code is

LP(C)=xXP(x)C(x)L_P(C) = \sum_{x\in\mathcal{X}} P(x)\ell_C(x)

The entropy H(P)H(P) averages the real-valued lengths log2P(x)-\log_2P(x) determined by the source. The expected code length LP(C)L_P(C) averages the integer lengths chosen by the encoder. To compare them, we must first determine which collections of integer lengths can belong to a decodable code.

Assigning a different binary string to each object is sufficient when each codeword is presented in isolation. It is not sufficient when codewords are concatenated. A concatenated bitstream could admit two decompositions into codewords and therefore two possible source sequences. A code is uniquely decodable when every concatenation of its codewords has only one decomposition.

A prefix code guarantees this property by requiring that no codeword be a prefix of another. Its codewords can be placed at leaves of a binary tree. Each edge contributes one bit, and the depth of a leaf equals the length of its codeword.

Let mm be at least as large as the longest codeword. A codeword of length C(x)\ell_C(x) has

2mC(x)2^{m-\ell_C(x)}

descendants at depth mm. Since no codeword is a prefix of another, the sets of descendants belonging to distinct codewords are disjoint. The complete binary tree contains only 2m2^m nodes at depth mm, so

xX2mC(x)2m\sum_{x\in\mathcal{X}} 2^{m-\ell_C(x)} \leq 2^m

Dividing by 2m2^m gives

xX2C(x)1\sum_{x\in\mathcal{X}} 2^{-\ell_C(x)} \leq 1

This is the Kraft–McMillan inequality. The tree argument proves it for prefix codes. The same inequality is necessary for every uniquely decodable code, even when its codewords do not form leaves of a prefix tree. Conversely, any collection of non-negative integer lengths satisfying the inequality can be realized by a prefix code.

The source probabilities satisfy a related identity. From the definition of information content,

2IP(x)=P(x)2^{-I_P(x)} = P(x)

and therefore

xX2IP(x)=xXP(x)=1\sum_{x\in\mathcal{X}} 2^{-I_P(x)} = \sum_{x\in\mathcal{X}} P(x) = 1

The ideal lengths IP(x)I_P(x) satisfy the same constraint as codeword lengths, with equality. They may nevertheless be fractional, so they do not necessarily specify a binary code.

The connection also runs in the opposite direction. Given a uniquely decodable code CC, define its Kraft sum

SC=xX2C(x)S_C = \sum_{x\in\mathcal{X}} 2^{-\ell_C(x)}

The Kraft–McMillan inequality gives SC1S_C\leq1. The quantities 2C(x)2^{-\ell_C(x)} may therefore sum to less than one, but normalization turns them into a probability distribution:

QC(x)=2C(x)SCQ_C(x) = \frac{2^{-\ell_C(x)}}{S_C}

Solving for the codeword length gives

C(x)=log2QC(x)log2SC\ell_C(x) = -\log_2Q_C(x)-\log_2S_C

The code lengths are therefore information contents under the induced distribution QCQ_C, shifted by the same non-negative amount log2SC-\log_2S_C. When the Kraft inequality is tight, so that SC=1S_C=1, the correspondence is exact:

C(x)=log2QC(x)\ell_C(x) = -\log_2Q_C(x)

A fixed-length code is the simplest instance. If every object receives the same length, then every value 2C(x)2^{-\ell_C(x)} is equal, and normalization produces the uniform distribution over the objects. A non-uniform code induces a non-uniform distribution in which shorter codewords correspond to more probable objects.

This does not mean that a probability distribution was required to define the code. The counting argument in the previous section produced a fixed-length code without assuming a source. It means that once a uniquely decodable code has been chosen, its lengths can always be given a probabilistic interpretation. Codes constrain probability assignments, and probability assignments suggest code lengths.

The induced distribution also proves that entropy lower-bounds expected code length. Averaging

C(x)=log2QC(x)log2SC\ell_C(x) = -\log_2Q_C(x)-\log_2S_C

under the source distribution gives

LP(C)=xXP(x)log2QC(x)log2SCL_P(C) = -\sum_{x\in\mathcal{X}} P(x)\log_2Q_C(x) -\log_2S_C

Subtracting the source entropy yields

LP(C)H(P)=xXP(x)log2P(x)QC(x)log2SC=DKL(PQC)log2SC\begin{aligned} L_P(C)-H(P) &= \sum_{x\in\mathcal{X}} P(x) \log_2 \frac{P(x)}{Q_C(x)} -\log_2S_C \\ &= D_{\mathrm{KL}}\left(P\mathbin\Vert Q_C\right) -\log_2S_C \end{aligned}

Both terms are non-negative. The first is the Kullback–Leibler divergence from the source distribution to the distribution induced by the code. The second is non-negative because SC1S_C\leq1. Therefore every uniquely decodable code satisfies

LP(C)H(P)L_P(C) \geq H(P)

The excess length has two sources. The divergence measures how poorly the code lengths match the source probabilities. The term log2SC-\log_2S_C measures unused coding capacity when the Kraft inequality is not tight.

It remains to determine whether the entropy bound can be approached. Round each ideal length upward:

(x)=log2P(x)\ell(x) = \left\lceil -\log_2P(x) \right\rceil

Since

(x)log2P(x)\ell(x) \geq -\log_2P(x)

we have

2(x)P(x)2^{-\ell(x)} \leq P(x)

and therefore

xX2(x)xXP(x)=1\sum_{x\in\mathcal{X}} 2^{-\ell(x)} \leq \sum_{x\in\mathcal{X}} P(x) = 1

The rounded lengths satisfy the Kraft–McMillan inequality, so a prefix code with those lengths exists. They also satisfy

log2P(x)(x)<log2P(x)+1-\log_2P(x) \leq \ell(x) < -\log_2P(x)+1

Averaging gives

H(P)LP(C)<H(P)+1H(P) \leq L_P(C) < H(P)+1

Together, the lower bound and this construction give the one-symbol form of Shannon’s Source Coding Theorem. No uniquely decodable binary code has expected length below the entropy of a known source, while a prefix code can always remain within one bit of it.

Encoding blocks of outcomes amortizes this rounding cost over several source symbols. As the block length increases, the additional cost per symbol can approach zero. Entropy is therefore the limiting expected number of bits required per outcome when the source distribution is known.

Up to this point, each value xXx\in\mathcal{X} has been treated as a complete object. The equivalence between code lengths and probability assignments is already present, but no next symbol has appeared. Prediction enters only after an object is represented as an ordered sequence and its probability is factored into conditional probabilities for the successive symbols.

When Compression Becomes Prediction

Now suppose that the object is an ordered sequence

x1:n=x1,,xnΣnx_{1:n}=x_1,\ldots,x_n\in\Sigma^n

over an alphabet Σ\Sigma. Write

x<i=x1,,xi1x_{<i}=x_1,\ldots,x_{i-1}

for the prefix before position ii. The probability of the complete sequence satisfies the chain rule

P(x1:n)=i=1nP(xix<i)P(x_{1:n}) = \prod_{i=1}^n P(x_i\mid x_{<i})

No independence assumption is involved. Each conditional distribution may depend on the complete prefix.

Applying log2-\log_2 turns the product into a sum:

log2P(x1:n)=i=1nlog2P(xix<i)-\log_2P(x_{1:n}) = \sum_{i=1}^n -\log_2P(x_i\mid x_{<i})

The information content of the complete sequence is therefore the sum of the conditional information contents of its symbols. Averaging over all possible sequences gives

H(X1:n)=i=1nH(XiX<i)H(X_{1:n}) = \sum_{i=1}^n H(X_i\mid X_{<i})

where

H(XiX<i)=E[log2P(XiX<i)]H(X_i\mid X_{<i}) = \mathbb{E}\left[-\log_2P(X_i\mid X_{<i})\right]

measures the information that remains at position ii after the prefix is known.

A compressor rarely knows the true conditional distributions. It instead uses a model QQ that returns, for every prefix, a distribution

Q(x<i)Q(\cdot\mid x_{<i})

over Σ\Sigma, with

aΣQ(ax<i)=1\sum_{a\in\Sigma}Q(a\mid x_{<i})=1

Returning only the most likely symbol is not sufficient. The encoder already knows which symbol occurs and needs a code length for that symbol, whether or not it was the model’s first choice. Two models may select the same most likely continuation while assigning different probabilities to the observed symbol.

The model defines a probability for the complete sequence:

Q(x1:n)=i=1nQ(xix<i)Q(x_{1:n}) = \prod_{i=1}^n Q(x_i\mid x_{<i})

Its cumulative logarithmic loss is

LQ(x1:n)=i=1nlog2Q(xix<i)=log2Q(x1:n)\begin{aligned} \mathcal{L}_Q(x_{1:n}) &= \sum_{i=1}^n -\log_2Q(x_i\mid x_{<i}) \\ &= -\log_2Q(x_{1:n}) \end{aligned}

A high probability assigned to the observed symbol produces a small loss. A low probability produces a large loss. A model used for lossless coding must assign positive probability to every symbol that may occur, since probability zero would give an infinite code length.

During generation, a model chooses or samples a symbol from this distribution. During compression, the actual symbol is already known. The distribution is used to determine how much of the code space that symbol receives. Prediction in this equivalence means assigning probabilities, not guessing one continuation and replacing the data with that guess.

The model still does not produce a bitstream. An entropy coder must convert its probability assignments into a decodable representation. Arithmetic coding begins with the interval [0,1)[0,1). At position ii, it partitions the current interval into adjacent subintervals whose widths are proportional to

Q(x<i)Q(\cdot\mid x_{<i})

and retains the subinterval assigned to the observed symbol xix_i.

If the current interval has width wi1w_{i-1}, the selected interval has width

wi=wi1Q(xix<i)w_i = w_{i-1}Q(x_i\mid x_{<i})

Starting from w0=1w_0=1, the final width is

wn=i=1nQ(xix<i)=Q(x1:n)w_n = \prod_{i=1}^n Q(x_i\mid x_{<i}) = Q(x_{1:n})

The emitted binary prefix identifies a dyadic interval. To decode the sequence unambiguously, that dyadic interval must be contained in the final arithmetic-coding interval, together with an agreed termination convention. The number of required bits is therefore

log2Q(x1:n)-\log_2Q(x_{1:n})

plus a bounded coding overhead in the ideal arithmetic-coding model.

Decoding repeats the same subdivisions. After recovering x<ix_{<i}, the decoder evaluates the same distribution Q(x<i)Q(\cdot\mid x_{<i}), partitions its interval in the same order, and determines which subinterval contains the encoded value. Encoder and decoder must begin from the same state, perform the same updates, use the same symbol ordering, and agree on where the sequence ends.

This proves one direction of the equivalence. A sequential probabilistic model can be converted into a lossless compressor whose payload length follows the model’s log-loss. The ngrok article and the two 3Blue1Brown videos (Reinventing Entropy and But what is cross-entropy?) explain this direction through entropy, cross-entropy, and conditional prediction.

The converse also holds. A uniquely decodable compressor assigns lengths to complete strings, and those lengths induce probability weights. After normalization, the resulting distribution can be factored into next-symbol conditionals. Language Modeling Is Compression also constructs predictors directly from changes in compressed length when candidate symbols are appended to a prefix.

The equivalence is therefore not limited to compressors explicitly implemented as a probability model followed by arithmetic coding. At a mathematical level, codes and probability assignments can be translated into one another. This generality also limits what the equivalence tells us. It applies after the possible objects, their serialization, and the information available to the decoder have been fixed. It does not determine any of them.

The log-loss measures the data encoded under QQ. It does not yet account for how QQ was chosen or how the decoder obtains it.

The Source Is Unknown

The log-loss identity derived above measures the data under an already available model. In practice, that model must be estimated from the observed sequence, transmitted, or learned through a procedure the decoder can reproduce.

This section reaches the resulting description length in two ways. Maximum likelihood gives the best in-sample log-loss within a fixed model family. Counting type classes gives nearly the same length without assuming that the sequence was sampled from that model. The lower-order gap is bounded by the information needed to identify the fitted type.

Consider first the family of zero-order models, which use the same distribution at every position and ignore the preceding symbols. Let q(a)q(a) be the probability assigned to a symbol aΣa\in\Sigma. The model assigns the complete sequence the probability

q(S)=i=1nq(si)q(S) = \prod_{i=1}^n q(s_i)

If nan_a denotes the number of occurrences of aa in SS, equal factors can be collected:

q(S)=aΣq(a)naq(S) = \prod_{a\in\Sigma} q(a)^{n_a}

The corresponding logarithmic loss is

log2q(S)=aΣnalog21q(a)-\log_2q(S) = \sum_{a\in\Sigma} n_a\log_2\frac{1}{q(a)}

Once the sequence has been observed, the counts determine which distribution in this model family assigns it the smallest loss. Define the empirical distribution

P^S(a)=nan\widehat{P}_S(a) = \frac{n_a}{n}

For any distribution qq that assigns positive probability to every symbol occurring in SS,

log2q(S)=aΣnalog21q(a)=naΣP^S(a)log21q(a)\begin{aligned} -\log_2q(S) &= \sum_{a\in\Sigma} n_a\log_2\frac{1}{q(a)} \\ &= n\sum_{a\in\Sigma} \widehat{P}_S(a) \log_2\frac{1}{q(a)} \end{aligned}

where

P^S(S):=i=1nP^S(si)\widehat{P}_S(S) := \prod_{i=1}^n \widehat{P}_S(s_i)

The loss obtained from the empirical distribution is

log2P^S(S)=naΣP^S(a)log21P^S(a)-\log_2\widehat{P}_S(S) = n\sum_{a\in\Sigma} \widehat{P}_S(a) \log_2 \frac{1}{\widehat{P}_S(a)}

Subtracting the two quantities gives

log2q(S)+log2P^S(S)=naΣP^S(a)log2P^S(a)q(a)=nDKL(P^Sq)0\begin{aligned} -\log_2q(S) + \log_2\widehat{P}_S(S) &= n\sum_{a\in\Sigma} \widehat{P}_S(a) \log_2 \frac{\widehat{P}_S(a)}{q(a)} \\ &= nD_{\mathrm{KL}}\left(\widehat{P}_S \mathbin\Vert q\right) \\ &\geq 0 \end{aligned}

Terms with P^S(a)=0\widehat{P}_S(a)=0 contribute zero. Since the divergence is non-negative, no zero-order distribution assigns the observed sequence a smaller log-loss than P^S\widehat{P}_S. Equality holds exactly when

q=P^Sq=\widehat{P}_S

The empirical frequencies are therefore the maximum-likelihood estimate within the family of zero-order models. Equivalently, they minimize the in-sample logarithmic loss over that family.

The resulting cost per symbol is the zero-order empirical entropy:

H0(S)=aΣnanlog2nna\mathcal{H}_0(S) = \sum_{a\in\Sigma} \frac{n_a}{n} \log_2\frac{n}{n_a}

with the convention that terms for which na=0n_a=0 contribute zero. Multiplying by nn gives

nH0(S)=aΣnalog2nna=log2P^S(S)n\mathcal{H}_0(S) = \sum_{a\in\Sigma} n_a\log_2\frac{n}{n_a} = -\log_2\widehat{P}_S(S)

Unlike Shannon entropy, H0(S)\mathcal{H}_0(S) is not defined from a source distribution that exists independently of the data. It is a property of the individual sequence SS, obtained by fitting a zero-order model to its observed symbol frequencies. It does not assert that the sequence was generated by independent draws from that distribution.

The same quantity can be reached without beginning from a probabilistic model. Consider a bitvector BB of length nn containing exactly mm ones. If the decoder knows nn and mm, then BB belongs to the family

Bn,m={B{0,1}n:B contains exactly m ones}\mathcal{B}_{n,m} = \left\{ B\in\{0,1\}^n : B\text{ contains exactly }m\text{ ones} \right\}

A member of this family is determined by choosing which mm positions contain a one, so

Bn,m=(nm)|\mathcal{B}_{n,m}| = \binom{n}{m}

The counting bound from the first section says that identifying an arbitrary member of this family requires

log2(nm)\log_2\binom{n}{m}

bits, up to integer rounding.

For 0<m<n0<m<n, set

p=mnp=\frac{m}{n}

The endpoint cases m=0m=0 and m=nm=n contain only one bitvector and have both counting bound and empirical entropy equal to zero. Under the zero-order model that assigns probability pp to a one, every member of $\mathcal{B}_{n,m} receives the same probability:

pm(1p)nm=(mn)m(nmn)nm=2nH0(B)\begin{aligned} p^m(1-p)^{n-m} &= \left(\frac{m}{n}\right)^m \left(\frac{n-m}{n}\right)^{n-m} \\ &= 2^{-n\mathcal{H}_0(B)} \end{aligned}

The total probability assigned to the family is therefore

(nm)2nH0(B)\binom{n}{m} 2^{-n\mathcal{H}_0(B)}

Since this probability cannot exceed one,

(nm)2nH0(B)1\binom{n}{m} 2^{-n\mathcal{H}_0(B)} \leq 1

and hence

log2(nm)nH0(B)\log_2\binom{n}{m} \leq n\mathcal{H}_0(B)

For the reverse bound, consider the number of ones produced by the fitted Bernoulli model. This count can take only the n+1n+1 values from 00 to nn. When p=m/np=m/n, the count mm is a mode of the resulting binomial distribution. Its probability is therefore at least the average probability of the possible counts:

(nm)pm(1p)nm1n+1\binom{n}{m} p^m(1-p)^{n-m} \geq \frac{1}{n+1}

Substituting the expression in terms of empirical entropy gives

(nm)2nH0(B)1n+1\binom{n}{m} 2^{-n\mathcal{H}_0(B)} \geq \frac{1}{n+1}

Taking logarithms yields

nH0(B)log2(n+1)log2(nm)n\mathcal{H}_0(B) - \log_2(n+1) \leq \log_2\binom{n}{m}

Together,

nH0(B)log2(n+1)log2(nm)nH0(B)n\mathcal{H}_0(B) - \log_2(n+1) \leq \log_2\binom{n}{m} \leq n\mathcal{H}_0(B)

The counting bound and the best zero-order log-loss differ by at most log2(n+1)\log_2(n+1) bits. The counting argument conditions on the value of mm and distinguishes the bitvectors that remain possible. The probabilistic argument distributes probability across bitvectors with every possible number of ones, then evaluates BB under the model fitted from its own count.

If nn is known but mm is not, the value of mm must also be represented. There are n+1n+1 possible values, so a fixed-width representation uses

log2(n+1)\left\lceil \log_2(n+1) \right\rceil

bits. Once this cost is included, the counting and probabilistic descriptions agree within the same lower-order term.

The relation extends to a general alphabet. Let the composition of SS be the vector

(na)aΣ(n_a)_{a\in\Sigma}

and consider its type class

T(na)={TΣn:T contains exactly na occurrences of each aΣ}\mathcal{T}(n_a) = \left\{ T\in\Sigma^n : T\text{ contains exactly }n_a\text{ occurrences of each }a\in\Sigma \right\}

where aΣna=n\sum_{a\in\Sigma}n_a=n. A sequence in this class is obtained by choosing which positions contain each symbol, so

T(na)=n!aΣna!|\mathcal{T}(n_a)| = \frac{n!}{\prod_{a\in\Sigma}n_a!}

Every sequence in this type class receives the same probability under the empirical distribution:

P^S(T)=aΣna>0(nan)na=2nH0(S)\begin{aligned} \widehat{P}_S(T) &= \prod_{\substack{a\in\Sigma \\ n_a>0}} \left(\frac{n_a}{n}\right)^{n_a} \\ &= 2^{-n\mathcal{H}_0(S)} \end{aligned}

The total probability assigned to the type class is

T(na)2nH0(S)|\mathcal{T}(n_a)| 2^{-n\mathcal{H}_0(S)}

Since this probability cannot exceed one,

T(na)2nH0(S)|\mathcal{T}(n_a)| \leq 2^{n\mathcal{H}_0(S)}

and therefore

log2n!aΣna!nH0(S)\log_2 \frac{n!}{\prod_{a\in\Sigma}n_a!} \leq n\mathcal{H}_0(S)

For the reverse direction, there are at most

(n+1)Σ(n+1)^{|\Sigma|}

possible type vectors. Under the multinomial distribution P^S\widehat{P}_S, the observed count vector (na)aΣ(n_a)_{a\in\Sigma} is a mode. Its probability must therefore be at least the reciprocal of the number of possible types:

T(na)2nH0(S)1(n+1)Σ|\mathcal{T}(n_a)|2^{-n\mathcal{H}_0(S)} \geq \frac{1}{(n+1)^{|\Sigma|}}

Rearranging gives

T(na)2nH0(S)(n+1)Σ|\mathcal{T}(n_a)| \geq \frac{2^{n\mathcal{H}_0(S)}}{(n+1)^{|\Sigma|}}

Taking logarithms produces

nH0(S)Σlog2(n+1)log2n!aΣna!nH0(S)n\mathcal{H}_0(S) - |\Sigma|\log_2(n+1) \leq \log_2 \frac{n!}{\prod_{a\in\Sigma}n_a!} \leq n\mathcal{H}_0(S)

This is the general form of the method of types. Equivalently, there exists a quantity Δ(S)\Delta(S) such that

log2n!aΣna!=nH0(S)Δ(S)\log_2 \frac{n!}{\prod_{a\in\Sigma}n_a!} = n\mathcal{H}_0(S) - \Delta(S)

where

0Δ(S)Σlog2(n+1)0 \leq \Delta(S) \leq |\Sigma|\log_2(n+1)

For a fixed alphabet, this gives

log2n!aΣna!=nH0(S)O(logn)\log_2 \frac{n!}{\prod_{a\in\Sigma}n_a!} = n\mathcal{H}_0(S) - O(\log n)

The two constructions condition on the same empirical information in different ways. The counting argument first fixes the composition and then counts the sequences that remain possible. The probabilistic argument fits the maximum-likelihood zero-order distribution and evaluates the observed sequence under it. Their difference is bounded by the information needed to identify the type.

Both quantities depend only on the composition of SS. Reordering the symbols leaves every nan_a unchanged and therefore leaves both the type class and H0(S)\mathcal{H}_0(S) unchanged. A zero-order model assigns the same probability to a symbol wherever it occurs, even when the preceding symbols make some continuations more likely than others.

Assume n1n\geq 1 and 0k<n0\leq k<n. To introduce context without assuming a known source, group positions according to the symbols that precede them. Fix a context length kk. For aΣa\in\Sigma and ωΣk\omega\in\Sigma^k, define

nωa={i{k+1,,n}:sik:i1=ω, si=a}n_{\omega a} = \left| \left\{ i\in\{k+1,\ldots,n\} : s_{i-k:i-1}=\omega,\ s_i=a \right\} \right|

and let

nω=aΣnωan_\omega = \sum_{a\in\Sigma} n_{\omega a}

be the number of symbols observed after ω\omega.

Within this group, the empirical conditional distribution is

P^S(aω)=nωanω\widehat{P}_S(a\mid\omega) = \frac{n_{\omega a}}{n_\omega}

for every observed context with nω>0n_\omega>0. The same maximum-likelihood argument used in the zero-order case applies independently to each context. The smallest log-loss obtained by assigning one distribution to the symbols following ω\omega is

aΣnωa>0nωalog2nωnωa\sum_{\substack{a\in\Sigma \\ n_{\omega a}>0}} n_{\omega a} \log_2 \frac{n_\omega}{n_{\omega a}}

Summing over the observed contexts gives

nHk(S)=ωΣknω>0aΣnωa>0nωalog2nωnωan\mathcal{H}_k(S) = \sum_{\substack{\omega\in\Sigma^k \\ n_\omega>0}} \sum_{\substack{a\in\Sigma \\ n_{\omega a}>0}} n_{\omega a} \log_2 \frac{n_\omega}{n_{\omega a}}

This defines the kk-th order empirical entropy under the boundary convention that only positions with a complete length-kk context contribute to the sum.

Equivalently, let SωS_\omega be the sequence formed by collecting, in their original order, all symbols that follow occurrences of ω\omega. Then

Hk(S)=1nωΣknω>0SωH0(Sω)\mathcal{H}_k(S) = \frac{1}{n} \sum_{\substack{\omega\in\Sigma^k \\ n_\omega>0}} |S_\omega| \mathcal{H}_0(S_\omega)

Each SωS_\omega is itself a sequence over Σ\Sigma. The zero-order analysis applies separately to it. Its empirical distribution is the maximum-likelihood model for the symbols observed after ω\omega, while its multinomial type class counts the alternative sequences with the same conditional composition.

The first kk symbols have no complete length-kk context. They may be encoded separately as one block in

log2Σk=klog2Σ\left\lceil \log_2|\Sigma|^k \right\rceil = \left\lceil k\log_2|\Sigma| \right\rceil

bits, or handled through an agreed boundary convention. When k=0k=0, there is only the empty context and the definition reduces to H0(S)\mathcal{H}_0(S).

Longer contexts divide the observed positions into smaller groups. Refining a group cannot increase the minimum fitted log-loss when both models are evaluated on the same set of positions, since the refined model can always reuse the distribution of the original group. More context can therefore reduce the empirical data term.

This reduction eventually exposes a limitation of the measure. If every observed context is followed by only one distinct symbol, then every SωS_\omega is constant and

H0(Sω)=0\mathcal{H}_0(S_\omega)=0

for all observed contexts. Consequently,

Hk(S)=0\mathcal{H}_k(S)=0

This always occurs at k=n1k=n-1, where only one position has a complete context, and it may occur much earlier when sufficiently long contexts determine their observed continuations.

The value Hk(S)=0\mathcal{H}_k(S)=0 does not give a zero-length lossless representation of SS. It gives a zero data term after the fitted context distributions are available. A decoder can assign probability one to an observed continuation only if it already knows which continuation followed that context. As the empirical loss decreases, information may have moved from the encoded sequence into the fitted model. The complete description must account for how the decoder obtains that model.

The Model Is Part of the Message

That missing information is the model itself.

A naïve dense order-kk table contains one row for each of the σk\sigma^k contexts and one count for each possible continuation. Using one log2(n+1)\lceil\log_2(n+1)\rceil-bit field per count requires

σk+1log2(n+1)=O(σk+1log(n+1))\sigma^{k+1}\left\lceil\log_2(n+1)\right\rceil = O\left(\sigma^{k+1}\log(n+1)\right)

bits. A sparse representation removes entries for contexts that never occur, but it must still identify the observed contexts and their continuations. Increasing kk can reduce the empirical data term by moving more information into this structure.

Let MM contain everything the decoder needs to reproduce the probabilities used by the encoder. A two-part description has length

L(M,S)=L(M)+L(SM)L(M,S) = L(M)+L(S\mid M)

The term L(M)L(M) describes the model. The term L(SM)L(S\mid M) encodes the sequence using that model. In a sufficiently expressive model family, minimizing only the second term rewards memorization. A more complex model improves the complete description only when the reduction in L(SM)L(S\mid M) exceeds the additional cost of describing MM. This is the basic two-part form of the minimum description length principle.

The data term also separates uncertainty in the source from mismatch in the model. Suppose objects are generated according to a distribution PP, while the compressor assigns probabilities according to QQ. If Q(x)>0Q(x)>0 whenever P(x)>0P(x)>0, the expected ideal data length is the cross-entropy

H(P,Q)=xP(x)log2Q(x)H(P,Q) = -\sum_x P(x)\log_2 Q(x)

Subtracting the source entropy gives

H(P,Q)H(P)=xP(x)log2P(x)Q(x)=DKL(PQ)\begin{aligned} H(P,Q)-H(P) &= \sum_x P(x)\log_2\frac{P(x)}{Q(x)} \\ &= D_{\mathrm{KL}}(P\mathbin\Vert Q) \end{aligned}

and therefore

H(P,Q)=H(P)+DKL(PQ)H(P,Q) = H(P)+D_{\mathrm{KL}}(P\mathbin\Vert Q)

For sequential distributions, relative entropy decomposes across positions:

DKL(P1:nQ1:n)=i=1nEX<iP[DKL(P(X<i)Q(X<i))]D_{\mathrm{KL}}\left(P_{1:n}\mathbin\Vert Q_{1:n}\right) = \sum_{i=1}^n \mathbb{E}_{X_{<i}\sim P} \left[ D_{\mathrm{KL}} \left( P(\cdot\mid X_{<i}) \mathbin\Vert Q(\cdot\mid X_{<i}) \right) \right]

Each term is the expected number of additional bits paid at one position because the model’s conditional distribution differs from the source distribution.

This distinction matters when discussing whether a better predictor “reduces entropy.” For a fixed source PP, improving QQ under expected logarithmic loss means reducing the cross-entropy H(P,Q)H(P,Q), equivalently reducing the mismatch term DKL(PQ)D_{\mathrm{KL}}(P\mathbin\Vert Q). It does not change H(P)H(P). The ngrok article correctly associates better probability estimates with shorter encodings, but its final use of entropy merges these two quantities.

Whether L(M)L(M) belongs to each transmitted file depends on the accounting boundary. If the model is fixed by a file format, built into the decoder, or otherwise shared in advance, it does not belong to the conditional description length of an individual message. It remains part of the system that makes that description meaningful, but charging its complete size to every message would also be misleading.

In an offline two-part code, if the fitted model is not already available to the decoder, a description of its tables, parameters, or weights must accompany the encoded data. Language Modeling Is Compression calls the ratio obtained without parameter size the raw compression rate. Its adjusted compression rate adds the parameter size to the compressed output. A larger model may obtain a lower log-loss while producing a worse adjusted rate when it is amortized over too little data.

A third option is prequential or online coding. Encoder and decoder begin from the same initial state. Let Qi1Q_{i-1} be the model available after the prefix s<is_{<i} has been processed. The ideal prequential length is

Lpreq(S)=i=1nlog2Qi1(sis<i)L_{\mathrm{preq}}(S) = \sum_{i=1}^n -\log_2 Q_{i-1}(s_i\mid s_{<i})

After decoding sis_i, the decoder performs the same update as the encoder and reconstructs QiQ_i. The final parameters therefore need not be transmitted.

The model cost is paid through the online log-loss rather than through a separate description of the final parameters. Before sufficient data have been observed, the current model will typically predict less well than a model fitted to the complete sequence. More generally, the difference appears as prequential regret relative to that offline fit. The initialization, update rule, training procedure, numerical conventions, and any randomness affecting them must be shared or described. Any unshared information needed to reproduce the learning procedure must be added to the prequential length.

The Shortest Bitstream May Be the Wrong Representation

Every code considered so far has been judged by one operation: reconstructing the complete object. The two-part description length says nothing about what can be done with the encoded data before that reconstruction is complete.

Consider a vector

A=(a0,,an1)A=(a_0,\ldots,a_{n-1})

whose values belong to {0,,u1}\{0,\ldots,u-1\}. Let

b=log2ub=\lceil\log_2 u\rceil

A bit-packed representation assigns exactly bb consecutive bits to each value, using nbnb bits for the payload apart from alignment and metadata. The representation of aia_i begins at bit position ibib. If the storage-word width is wbw\geq b, recovering aia_i requires reading at most two adjacent words, shifting their contents, and applying a mask. The addresses and shifts are computed directly from ii, so access takes O(1)O(1) time.

This representation does not exploit differences in frequency. Every value receives the same number of bits. If the values follow a non-uniform distribution, or if their probabilities depend on earlier values, an entropy coder may produce a shorter stream:

L(AM)i=0n1log2Q(aia<i)L(A\mid M) \approx \sum_{i=0}^{n-1} -\log_2 Q(a_i\mid a_{<i})

The shorter stream provides a different access contract. In an ordinary arithmetic-coded stream, the decoding state at position ii depends on the symbols that precede it. If the model also uses their context, its next distribution depends on the same prefix. Recovering aia_i requires decoding from the beginning of the stream or from an earlier checkpoint whose coding and model states have been stored.

The bit-packed vector may occupy more space while answering vector[i] directly. Its compression comes from restricting the possible value at each position to an alphabet of size uu, rather than from predicting which value will occur. When all values remain equally plausible, fixed-width packing uses the information supplied by that restriction without requiring a non-uniform model.

A compressed archive needs an encoding EE and a decoder DD satisfying

D(E(A))=AD(E(A))=A

Once the whole vector can be reconstructed, the encoding has fulfilled its contract. A compressed representation may be required to satisfy a stronger condition. It must support an access algorithm such that

Access(R(A),i)=ai\operatorname{Access}(R(A),i)=a_i

without first reconstructing all of AA.

Checkpoints can give an entropy-coded stream faster access, but each checkpoint occupies space. Smaller blocks reduce the amount of decoding required for an access and increase the number of stored states. Larger blocks save metadata and increase access time. The objective is no longer to minimize the bitstream without qualification. It becomes

minRR(A)subject toTAccess(R)t\min_R |R(A)| \qquad \text{subject to} \qquad T_{\operatorname{Access}}(R)\leq t

for a chosen access-time bound tt.

The operation need not be random access, and the object need not be a vector. The same issue arises whenever compressed data must be searched, traversed, compared, or partially decoded. The required operations constrain which short descriptions are useful and how much auxiliary information they need.

Bitstream length alone answers only the bare reconstruction problem. A compressed representation must also encode enough structure for its required operations. The decoder contract now has three explicit parts: the objects it must distinguish, the information it already shares with the encoder, and the operations it must perform without full reconstruction. Prediction determines conditional code lengths inside this contract. It does not determine the contract itself.

So, Is Compression Prediction?

The two 3Blue1Brown videos (Reinventing Entropy and But what is cross-entropy?) derive the connection between coding, entropy, and cross-entropy. The ngrok article shows how a model supplies conditional probabilities and arithmetic coding turns them into a bitstream. Salvatore Sanfilippo asks whether this makes prediction and compression the same concept. Language Modeling Is Compression studies both directions of the equivalence and explicitly accounts for model parameters through raw, adjusted, and prequential compression rates.

Kolmogorov complexity makes this visible for an individual string, while the counting bound makes it visible for a finite family: a description-length problem can be posed before a next-symbol predictor exists.

What I felt these explanations left implicit was the compression problem that must be fixed before the equivalence becomes meaningful. The encoder and decoder need an agreed family of objects, a serialization, a boundary between transmitted and shared information, and a decoding contract. None of these choices is determined by next-symbol prediction.

Once those choices have been made, the equivalence is broad. A sequential probability model assigns ideal payload lengths through logarithmic loss. In the other direction, a uniquely decodable code induces probability weights, and a distribution over serialized objects can be factored into next-symbol conditionals. More precisely, every uniquely decodable code over a fixed object family induces a distribution QCQ_C. Its codeword lengths equal log2QC-\log_2 Q_C plus the constant contribution of its Kraft slack. Once the objects have been serialized, QCQ_C can be factored into next-symbol conditionals.

That generality is also the limit of the slogan Compression is Prediction. Recasting a representation probabilistically does not explain why its objects were chosen, whether its model must be transmitted, or which operations it supports. A model with lower log-loss can produce a larger complete file after its parameters are included. An entropy-coded stream can use fewer bits than a bit-packed vector while failing to provide constant-time access. An empirical entropy of zero can still leave the decoder without the model needed to reconstruct the sequence.

Compression is therefore prediction after the coding problem has been fixed, and only at the level measured by the induced code lengths. For a shared sequential model under logarithmic loss, cumulative prediction error gives the ideal payload length up to coding overhead. It does not define what must be represented, what the decoder already knows, or what the representation must allow the decoder to do.


Share this post on:

Next Post
Who Owns the Memory? Part 3: How Big Is your Type?