1. Home
  2. Explore
  3. Computer Science
  4. Limits of Text Compression

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Limits of Text Compression

Speaker 1Speaker 2

Limits of Text Compression

13:27
0:00-13:27
  1. Corey

    Let’s begin with the basic question: how efficiently can we encode text—or any stream of symbols—as bits? ASCII uses eight bits for each character, but cleverer codes can give common characters shorter representations. And if we take patterns in longer sequences into account, we can often compress further. So what sets the ultimate limit?

  2. Maria

    That question goes back to Claude Shannon’s work in the 1940s, which helped launch information theory. It also connects to modern machine learning: large language models are commonly trained to predict the next token using an objective called cross-entropy loss. Cross-entropy has its roots in information theory, and prediction and compression turn out to be mathematically connected.

  3. Corey

    So we can reframe next-token prediction as trying to build an efficient text compressor. That connection makes compression a useful way to understand what the training objective is doing, and why information theory appears in machine learning. People sometimes sum this up by saying, “compression is intelligence,” although intelligence is too loosely defined for that to be a precise conclusion. What we can say is that the mathematics of compression is surprisingly relevant to artificial intelligence.

  4. Maria

    To get at the limits of compression, let’s start with a simpler case than language. Imagine a robot on a distant moon receiving movement instructions from Earth: up, down, left, or right. The instructions are independent, and their probabilities are uneven: up occurs half the time, down a quarter of the time, and left and right an eighth each.

  5. Corey

    A straightforward encoding assigns two bits to every instruction: perhaps 00 for up, 01 for down, 10 for left, and 11 for right. It’s easy to decode, but it uses two bits per instruction whether the instruction is common or rare. That means it ignores the fact that up is much more likely than left or right.

  6. Maria

    A more efficient proposal assigns different-length code words: 0 for up, 10 for down, 110 for left, and 111 for right. The average length is a weighted sum: half the instructions use one bit, a quarter use two, and the remaining quarter use three. That comes to 1.75 bits per instruction, better than the flat two-bit code.

  7. Corey

    The short code for up saves enough bits to outweigh the longer codes for the rare instructions. But variable-length codes raise an important question: how can the robot tell where one instruction ends and the next begins? It can’t simply divide the incoming stream into equal-sized chunks anymore.

  8. Maria

    The answer is that none of these code words is the beginning—or prefix—of another code word. If the robot reads a 0, it knows it has up. If it reads 10, it knows it has down. A 1 by itself is ambiguous, but the robot keeps reading until the bits form a complete code word: 110 means left, and 111 means right.

  9. Corey

    That rule is called being prefix-free, or a prefix code. It’s what makes the variable-length encoding unambiguous. For instance, if we added a new instruction encoded as 100, the robot would see 10 and not know whether to stop and decode down or continue toward the new instruction.

  10. Maria

    There’s a helpful way to picture the constraint: imagine a branching diagram of every binary string. The strings beginning with 0 occupy one half of the space, and those beginning with 1 occupy the other half; the same split happens recursively at each branch. Choosing 0 as a code word for up claims all the space beneath it, so no other code word can begin with 0.

  11. Corey

    In this robot code, the four code words claim fractions of the total space that match the instructions’ probabilities: one half for up, one quarter for down, and one eighth each for left and right. Nothing is left over. That striking match between probability and code length hints at a deeper connection between how likely something is and how many bits it takes to describe it.

  12. Maria

    But how can we tell whether 1.75 bits per instruction is truly the best possible? Perhaps some clever method could encode long sequences together and do even better. To approach that question, imagine what an ideal compressed stream might look like: random noise, with each bit independently equally likely to be 0 or 1.

  13. Corey

    The robot’s prefix code actually produces that kind of bitstream. The first bit is 0 half the time, because up occurs half the time; otherwise it is 1. Once the first bit is 1, the next bit is equally likely to be 0 or 1, distinguishing down from the two remaining possibilities. The same balance continues as the robot decodes the code tree, so each bit behaves like an independent fair coin flip.

  14. Maria

    Why would random-looking output be a sign of perfect compression? From the receiver’s point of view, there are 2 to the n possible bit strings of length n. If they all occur equally often, then no one of those strings is more predictable than another. In a perfectly compressed representation, there’s no predictable structure left to exploit.

  15. Corey

    The underlying messages represented by those equally likely strings must also be equally likely in this idealized picture. If one message were more likely, we might hope to give it a shorter code. But shortening one message uses up space in the binary code tree, forcing other messages to take longer codes to remain distinguishable.

  16. Maria

    It’s like pressing down a bump in a rug: the space you save for one message has to go somewhere else. At a minimum, other messages get pushed to longer code words. For equally likely messages, giving them equal-length codes avoids that unfavorable trade-off, which is why a random-looking, equally distributed set of outputs is the natural ideal.

  17. Corey

    Now suppose a particular message has probability p, and in a perfect code it uses n bits. There are 2 to the n equally likely bit strings of that length, so its probability is 1 over 2 to the n. Solving for the number of bits gives n equals negative log base 2 of p.

  18. Maria

    That expression, negative log base 2 of p, is the information content of an event. You can read it as asking how many times you would have to halve the space of possibilities to reach a fraction p. An unlikely event has high information content; a highly expected event has low information content.

  19. Corey

    This is more than a change of units for probability. Under perfect compression, the information content of a message is precisely its number of bits. When exact perfect compression isn’t possible—for example, when probabilities don’t line up with whole numbers of bits—the information content still gives a lower bound on the average number of bits needed.

  20. Maria

    That’s why fractional information values matter. A single letter might have an information value of, say, 4.19 bits, even though we can’t assign it a code word that is literally 4.19 bits long. The interpretation comes from whole messages and averages, rather than requiring every symbol to have its own exact fractional-length code word.

  21. Corey

    Language makes this much more interesting than the robot example. The probability of the next letter depends heavily on the context that came before it, and those probabilities usually aren’t neat powers of one half. A language model can estimate a probability for each next letter, but its predictions aren’t automatically the same thing as the true underlying probabilities of language.

  22. Maria

    For a phrase, the probability of the whole sequence is the product of the conditional probabilities of each successive letter, given what came before. That’s the chain rule of probability. And because logarithms turn multiplication into addition, the information of the full phrase is the sum of the information values of its individual letters.

  23. Corey

    That gives us a useful way to think about the length of a compressed message: add up the fractional information values as you go. A practical encoding eventually has to use whole bits, but working with continuous information values makes the mathematics much cleaner. A compression algorithm using a good language model can get close to that summed value, though the specific algorithm is a topic for the next part of the series.

  24. Maria

    There’s still a big question: where do the probabilities for language come from? Early on, Shannon studied short sequences of characters, often called n-grams, by looking through books and recording what tended to follow a sequence such as “th.” That works for sequences you’ve observed, but it can’t directly tell you what follows a longer string that never appeared in your sample.

  25. Corey

    And longer contexts matter because they can make the next character more predictable, which is exactly where compression can benefit. Shannon therefore explored a different kind of model: a person’s ability to predict language. In a well-known experiment, he asked his wife Betty to guess the next letter of a text, then recorded the correct letter only when she guessed wrong.

  26. Maria

    The reduced text acted as a prompt: if Shannon had an exact replica of Betty who guessed the same way, she could use the recorded letters to reproduce the original. That illustrated how predictability could allow compression, but it wasn’t yet a precise measurement. People might not make the same guesses on a second attempt.

  27. Corey

    In a later study, Shannon interviewed more people and recorded how many guesses it took them to identify the next letter. He used those guessing counts to infer probabilities. The important point is that he wasn’t only calculating statistics from a pile of text; he was probing an underlying model of language—human brains that could use context to make predictions.

  28. Maria

    Now we can ask the central averaging question: for a signal made of symbols, what is the average information per symbol? If each symbol is drawn from the same distribution, we multiply each symbol’s information, negative log base 2 of its probability, by that probability, and add the results. The formula is the sum over symbols of p times negative log base 2 of p.

  29. Corey

    That average is called entropy, usually written as H. It’s the average information per symbol for that distribution, and it describes the lower limit on the average bits needed to encode symbols following it. In the robot example, the code matched each symbol’s information exactly, so the average length of 1.75 bits was also the entropy.

  30. Maria

    The formula also gives a useful visual intuition. Represent each probability as the width of a bar, and its information value as the height of a rectangle above it. The total area of the rectangles is the weighted average information: the entropy. The story that von Neumann suggested the name because nobody really knows what entropy is may be apocryphal, but Shannon did adopt the term.

  31. Corey

    Entropy also reflects how spread out a distribution is. If one outcome dominates, it carries little information because it is so predictable, and the entropy is low. If probability is spread more evenly across possible outcomes, uncertainty is greater and entropy is higher; spreading it over more possible symbols can raise it further.

  32. Maria

    People often describe entropy as uncertainty, which is a helpful intuition. The more precise connection to compression is that entropy gives the minimum average number of bits per symbol for a source following that distribution. Shannon’s noiseless coding theorem says no encoding can beat that limit, and that it’s possible to get arbitrarily close.

  33. Corey

    For natural language, the distribution of the next symbol changes with context, so the simple entropy formula for one fixed distribution isn’t enough. The more general idea is the entropy rate of a stochastic process: the average information per symbol, accounting for the probabilities of possible sequences and the context that precedes each symbol.

  34. Maria

    There isn’t a neat, exact formula we can simply apply to all of English, because its underlying probability distribution isn’t known in that way. That’s why Shannon estimated language entropy using observation and human prediction. With at least 100 preceding letters of context, he estimated English at about one bit per character—a surprisingly low figure compared with the eight bits used by ASCII.

  35. Corey

    So the path from compression to information theory is: ask how many bits messages need, notice that ideal code lengths depend on probability, express that relationship as negative log base 2 of probability, and average it to get entropy. For language, context makes those probabilities far more complicated, but the same basic ideas still apply.

  36. Maria

    And that brings us back to modern machine learning. Prediction and compression share the same mathematical foundation, which is why information theory’s ideas show up in how language models are trained. The next step is cross-entropy—how it differs from entropy and why it’s used in training large language models—along with related topics such as model distillation and compression across languages.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent