Limits of Text Compression
Information Theory, Compression, and Language
Why compression has a fundamental limit
Text can be represented as a stream of bits, but different encodings use different amounts of data. A simple fixed-width encoding such as ASCII uses the same number of bits for every character, regardless of how common that character is. A more efficient encoding assigns shorter representations to common characters and longer representations to rare ones. Methods that exploit patterns across sequences can compress further.
This raises a fundamental question: How small can an encoding become without losing information? Answering it requires more than finding a clever coding scheme. It requires understanding the probabilities of the messages being encoded and how those probabilities relate to the number of bits needed to represent them.
Claude Shannon’s work in the 1940s established the mathematical foundations for studying these limits. One important consequence is that prediction and compression are closely connected:
A good predictor assigns high probability to what is likely to occur next.
A compressor can use those probabilities to assign shorter encodings to likely outcomes and longer encodings to unlikely ones.
Under suitable conditions, the number of bits needed to encode an outcome is related to the negative logarithm of the probability assigned to it.
This connection matters in modern machine learning. Large language models are commonly trained through next-token prediction using cross-entropy loss, a quantity rooted in information theory. From the compression perspective, learning to predict the next token can also be understood as learning a model that enables efficient encoding of text.
The claim that “compression is intelligence” is intriguing but too broad to establish rigorously, since intelligence is difficult to define precisely. A more defensible conclusion is that the mathematics of compression has deep relevance to artificial intelligence.
A simple model of compression
Consider a robot receiving instructions to move up, down, left, or right. The instructions have this probability distribution:
Up: probability 21
Down: probability 41
Left: probability 81
Right: probability 81
Assume that each instruction is selected independently of the previous instructions. The robot receives the instructions as a stream of bits, so the goal is to encode them using as few bits as possible on average.
Fixed-length encoding
A straightforward method assigns two bits to each instruction:
00 for up
01 for down
10 for left
11 for right
This encoding is easy to decode because the robot reads the stream in groups of two bits. It uses exactly two bits per instruction, whether the instruction is common or rare.
Variable-length encoding
A more efficient method uses different-length code words:
0 for up
10 for down
110 for left
111 for right
The average code length is the probability-weighted average of the lengths:
This uses an average of 1.75 bits per instruction, less than the fixed-length encoding’s two bits. The common instruction, up, receives the shortest code word, while the less common instructions receive longer ones.
The key idea is that spending more bits on rare events can still reduce the average number of bits if the common events become sufficiently cheaper to encode.
Prefix-free codes and unambiguous decoding
A code word is the bit string assigned to a symbol or instruction. Variable-length codes can be decoded without ambiguity when no code word is the beginning, or prefix, of another code word.
The robot’s code is prefix-free:
If the first bit is 0, the instruction must be up.
If the first bit is 1, the instruction is not yet known.
If the next bit is 0, the code word is 10, meaning down.
If the next bit is 1, the code word is still incomplete.
A following 0 gives 110, meaning left.
A following 1 gives 111, meaning right.
The receiver reads bits until it has completed one code word, records the instruction, and then begins decoding the next one.
If an additional instruction were assigned the code word 100, decoding would become ambiguous. After receiving 10, the robot would not know whether it had received the complete code word for down or the beginning of the new code word 100.
Prefix-free code: A set of code words in which no code word is a prefix of any other. This property allows a receiver to decode a stream as soon as each complete code word is encountered.
Visualizing code-word space
A binary tree can represent all possible bit strings. At each level, strings become longer; strings beginning with 0 occupy one branch, and strings beginning with 1 occupy the other. Each string corresponds to a region containing all strings that begin with it.
Assigning a code word claims the region beneath it. For example:
Assigning 0 claims half of the binary-string space.
Assigning 10 claims a quarter.
Assigning 110 and 111 claims an eighth each.
The robot’s four code words use the space in proportions that match the instruction probabilities exactly. The most probable instruction receives the largest region but requires the fewest bits.
Why random-looking output indicates efficient compression
Suppose a compressed message has exactly n bits. There are 2n possible bit strings of that length. If an ideal compressor makes its output look like random noise, then every n-bit string is equally likely, with probability
Here, “random noise” means that each bit is independently 0 or 1 with equal probability.
The robot’s variable-length code has this property. At each step in decoding, the next bit acts like an independent fair coin flip:
A first bit of 0 occurs with probability 21 and signals up.
If the first bit is 1, the next bit is 0 for down or 1 to continue toward left or right, each with probability 21 conditional on the first bit.
The same pattern continues through the code tree.
Consequently, the encoded bitstream is indistinguishable from a stream of independent, equally likely bits.
This provides an intuition for why random-looking data is incompressible: if all bit strings of a given length are equally likely, there is no more probable output that a compressor could represent with fewer bits. Reducing the length assigned to one message also affects the available code space for other messages. A shorter code word claims a larger region of the binary tree, so other code words must be moved to longer regions to avoid ambiguity. In this way, a local saving can force costs elsewhere.
For equally likely messages, assigning all messages the same number of bits is the efficient arrangement. Making one message shorter uses more than its share of the available code space and requires other messages to use more.
Information as the number of bits associated with an event
For an ideal compression scheme, a message represented by n bits has probability 2−n. Solving for the number of bits gives:
where p is the probability of the message. Shannon defined this quantity as the information or information content of an event:
The formula can also be written as log2(P(x)1). It measures how many successive halvings of the space of possibilities are needed to reach the probability of the event.
Interpreting information content
Unlikely events carry more information. Learning that a rare event occurred rules out many alternatives.
Likely events carry less information. A highly expected event provides little new knowledge.
An event with probability 1 has information content 0, since it rules out no uncertainty.
An event with probability 21 has information content 1 bit.
An event with probability 81 has information content 3 bits.
Information content is not merely a change of units for probability. In an ideal compression scheme, it corresponds to the number of bits needed to represent a message. When exact, perfect compression is not possible, information content still provides a lower bound on compression when averaged across possible messages.
For probabilities that are not exact powers of 21, the information content can be fractional. For example, an event need not have an individual code word that is exactly a fractional number of bits long. Instead, fractional information is meaningful when considering the average over complete messages or when using coding methods that encode sequences together.
Information in sequences and conditional probabilities
Natural language differs from the robot example in two important ways:
The probability of a character depends strongly on its preceding context.
Character probabilities are generally not exact powers of 21, so their information values are fractional.
For instance, the probability of a letter depends on the letters that came before it. A full sequence’s probability is obtained by multiplying the conditional probabilities of its successive symbols:
This is the chain rule of probability. Taking the negative base-two logarithm turns the product into a sum:
Thus, the information of the entire sequence is the sum of the conditional information contributed by its symbols. A predictable symbol contributes little information; a surprising symbol contributes more.
The actual number of bits used to encode a complete sequence must be an integer, but the information values of its individual symbols need not be. This continuous view is useful because the fractional contributions add neatly across a sequence. Practical coding methods can encode a whole sequence to a length close to the sum of its symbol-level information values.
Estimating probabilities in language
To determine the compressibility of language, a compressor needs probabilities for what may come next. But the “true” probabilities of language are not directly available, and probabilities supplied by a language model are not necessarily identical to the underlying probabilities of actual language.
Character statistics and n-grams
One way to estimate probabilities is to examine short character sequences, often called n-grams, and record what tends to follow them. For example, examining many instances of the pair “th” can provide statistics about which letters follow that pair.
This method has a major limitation: longer contexts frequently have never appeared in the material being analyzed. A statistical table cannot directly estimate the next character after a sequence it has never encountered. Yet longer contexts are often especially useful for prediction because they contain more information about what comes next. That predictability is also a potential source of compression.
Human language understanding as a model
Shannon also explored human prediction as a way to probe language probabilities. In one experiment, he read text aloud or showed it to his wife, Betty, and asked her to guess each next letter. He recorded incorrect guesses explicitly and replaced correct guesses with dashes. The resulting text contained fewer explicit letters but could, in principle, be reconstructed by someone able to make the same predictions.
This demonstrated qualitatively how predictable material can be omitted when the receiver can supply it. However, a person might not make exactly the same guesses on another occasion, so this procedure was not a precise measurement of information.
In a later experiment, Shannon asked more people to guess successive letters and recorded how many guesses each person needed before reaching the correct one. He then related the number of guesses to the implicit probability the guesser assigned to the correct letter.
The important methodological point is that Shannon was not relying only on counts from written text. He was also probing an underlying model of language: the prediction ability of a human mind. The participants functioned as black boxes with sophisticated, difficult-to-describe knowledge of language and context.
Shannon estimated that when a guesser had at least 100 preceding letters of context, English had an entropy of about one bit per character. This suggests that, in principle, sufficiently effective prediction could make English much more compressible than a simple fixed-width encoding would indicate.
Entropy: average information per symbol
For a signal made from symbols with probabilities p1,p2,…,pk, the average information per symbol is the probability-weighted sum of the information values:
This quantity is called entropy. It is the average information carried by a symbol drawn from the distribution.
In the robot example, each code-word length exactly matches the information content of its instruction:
Up has probability 21 and information 1 bit.
Down has probability 41 and information 2 bits.
Left and right each have probability 81 and information 3 bits.
The average information is therefore
The same weighted average gave the robot code’s average length. That is because the code assigns each instruction a number of bits equal to its information content.
Entropy and uncertainty
Entropy is often described as a measure of uncertainty in a distribution:
A highly uneven distribution has low entropy when one outcome dominates. Since that outcome is very predictable, it carries little information on average.
A more even distribution has higher entropy because uncertainty is spread across outcomes.
Distributing probability across more possible symbols can also increase entropy.
The precise compression interpretation is more informative than the loose description “uncertainty”: entropy gives the minimum average number of bits per symbol needed to encode a source with the specified distribution, under the relevant coding assumptions.
Shannon’s noiseless coding theorem
The noiseless coding theorem establishes the fundamental compression limit for a source whose symbols follow a specified distribution:
No encoding can achieve an average number of bits per symbol below the source’s entropy.
It is possible to construct encodings that get arbitrarily close to this limit.
The theorem explains why the robot’s average of 1.75 bits per instruction is optimal for its distribution: that value is the entropy of the instruction source. It also formalizes the broader principle that probability determines the lower limit on lossless compression.
The name “entropy” was reportedly suggested to Shannon by John von Neumann, who noted its resemblance to entropy in statistical mechanics. The story is often repeated, though its exact historical accuracy is uncertain.
Entropy rate for dependent sources
The simple entropy formula applies directly when every symbol follows the same probability distribution, as in the independent robot-instruction example. Language is different: the probability of each symbol depends on context. Its entropy therefore requires a more general quantity, the entropy rate of a stochastic process.
Entropy rate asks essentially the same question as entropy: what is the average information per symbol? The difference is that it accounts for dependencies between successive symbols and averages over possible sequences.
For natural language, there is no simple, exact formula that can be applied to a text corpus to reveal the language’s true entropy rate. Estimating the rate requires some model of how language works. Shannon’s human-guessing experiments were an attempt to probe such a model, rather than merely count patterns in a collection of books.
Connection to cross-entropy and language-model training
A language model assigns probabilities to possible next tokens given the preceding context. If a model assigns probability p to the token that actually occurs, that token contributes −log2p bits of information under the model’s probability assignment. Across a sequence, the corresponding contributions add.
This gives a compression interpretation of prediction:
Better predictions assign higher probabilities to the tokens that actually occur.
Higher assigned probabilities correspond to lower negative-log contributions.
Lower total contributions correspond to a shorter ideal encoding under those probabilities.
Cross-entropy is an information-theoretic quantity used in next-token prediction training. It evaluates probability assignments against the outcomes that occur. In this sense, language-model training can be viewed not only as learning to predict tokens, but also as learning probability assignments that support efficient encoding of text.
The connection does not mean that prediction and compression are identical as practical tasks in every respect. Rather, under probability-based coding, a model’s predictive probabilities determine the lengths of the corresponding ideal code representations. This is why information theory’s mathematics is so relevant to modern machine learning.
Key relationships to remember
Probability and code length: Less probable events require more information and, in an ideal code, more bits.
Information and sequence length: A sequence’s information is the sum of its symbols’ conditional information.
Entropy and compression: Entropy is the average information per symbol and sets a lower bound on average compression length.
Context and language: Language symbols are dependent, so their probabilities—and their compression potential—depend on preceding context.
Prediction and compression: Accurate predictions enable shorter encodings because predictable outcomes can be assigned fewer bits.

Ask anything
We've read this whole study set, so questions about it get the best answers.