9. Information Theory & Entropy
Measuring the true information content of data
In 1948, Claude Shannon published "A Mathematical Theory of Communication" — a paper that single-handedly created the field of information theory. Before Shannon, engineers had no rigorous way to measure information. They knew intuitively that some messages carried more content than others, but there was no math behind it. Shannon provided the math, and in doing so, established the fundamental limits of both data compression and reliable communication.
This page explores the core ideas of information theory and how they connect directly to everything in compression.
What Is Information?
Shannon's key insight was counterintuitive: information is surprise. The more surprising an event is, the more information it carries. Common, expected events carry very little information because they tell you almost nothing you didn't already know.
The Weather Analogy
Imagine you live in the Sahara Desert. Someone tells you: "It's sunny today." That's completely unsurprising — it's almost always sunny. This message carries very little information.
Now imagine someone says: "It's snowing in the Sahara." That's extraordinary — it almost never happens. This message carries a huge amount of information, precisely because it's so unexpected.
The sunny-day message is highly probable, so it's low-information. The snow message is extremely improbable, so it's high-information. Information and probability are inversely related.
This connects directly to compression. If a symbol appears very frequently, we don't need many bits to represent it — it's expected, so it's "cheap." Rare symbols need more bits because they carry more surprise. Huffman coding is built on exactly this principle.
Self-Information
Shannon formalized "surprise" with a precise formula. The self-information (or information content) of an event with probability p(x) is:
I(x) = -log₂(p(x))
Why logarithm base 2? Because it gives us the answer in bits — the natural unit for binary-encoded data. Why the negative sign? Because probabilities are between 0 and 1, so log₂ of a probability is always negative — the minus sign makes the result positive.
Key properties:
- A certain event (p = 1) has 0 bits of information — no surprise at all.
- An impossible event (p = 0) would have infinite information — maximum surprise.
- Information is additive: two independent events together carry the sum of their individual information.
Worked Examples
| Event | Probability | Self-Information |
|---|---|---|
| Fair coin lands heads | 1/2 = 0.5 | -log₂(0.5) = 1 bit |
| Rolling a 6 on a fair die | 1/6 ≈ 0.167 | -log₂(1/6) = log₂(6) ≈ 2.585 bits |
| A 1-in-a-million event | 1/1,000,000 | -log₂(1/1,000,000) = log₂(1,000,000) ≈ 19.93 bits |
The coin flip result is worth 1 bit — makes sense, since one bit is exactly what you need to distinguish two equally likely outcomes. The die roll is worth about 2.6 bits. A one-in-a-million event carries about 20 bits of information.
Shannon Entropy
Self-information tells us about a single event. But if we have a source that emits symbols according to some probability distribution, we want to know the average information per symbol. That's Shannon entropy:
H(X) = -Σ p(x) log₂(p(x))
It's the weighted average of self-information across all symbols, where each symbol's self-information is weighted by how often it occurs. The result is measured in bits per symbol.
Complete Worked Example
A source emits four symbols with these probabilities:
| Symbol | Probability | Self-Information | Weighted Contribution |
|---|---|---|---|
| A | 0.5 | -log₂(0.5) = 1.0 bits | 0.5 × 1.0 = 0.5 |
| B | 0.25 | -log₂(0.25) = 2.0 bits | 0.25 × 2.0 = 0.5 |
| C | 0.125 | -log₂(0.125) = 3.0 bits | 0.125 × 3.0 = 0.375 |
| D | 0.125 | -log₂(0.125) = 3.0 bits | 0.125 × 3.0 = 0.375 |
H = 0.5 + 0.5 + 0.375 + 0.375 = 1.75 bits/symbol
This means that on average, each symbol from this source carries 1.75 bits of information. No lossless compression scheme can represent these symbols using fewer than 1.75 bits per symbol on average.
Notice that this particular distribution has a nice property: each probability is
a power of 1/2, so Huffman coding can match entropy exactly with codes
A=0, B=10, C=110, D=111
— averaging exactly 1.75 bits/symbol.
Entropy as a Compression Limit
The Fundamental Theorem
Shannon's source coding theorem proves that entropy defines the theoretical minimum average bits per symbol for any lossless compression algorithm. No algorithm — no matter how clever — can compress data below its entropy and still allow perfect reconstruction.
Huffman coding gets close to this limit but is constrained to integer bit lengths per symbol. When a symbol's ideal cost is 0.15 bits, Huffman must round up to 1 bit — a 6.7× penalty.
Arithmetic coding and ANS (Asymmetric Numeral Systems) break through this barrier by encoding entire messages as single numbers, achieving fractional bits per symbol and approaching entropy arbitrarily closely.
Entropy is both a floor and a benchmark. When you evaluate a compression algorithm, you compare its output size to the entropy of the source. The gap between them is wasted space — bits that a better algorithm could eliminate.
Maximum and Minimum Entropy
The entropy of a source depends entirely on its probability distribution. Two extreme cases define the boundaries.
Maximum Entropy
All symbols equally likely (uniform distribution).
H = log₂(n)
Example: 256 equally likely byte values → H = log₂(256) = 8 bits/symbol. Every byte carries maximum surprise. The data is incompressible — it looks like random noise.
Minimum Entropy
One symbol has probability 1, all others have probability 0.
H = 0 bits/symbol
Example: a file containing only the letter 'A' repeated a million times. Every symbol is perfectly predictable — zero surprise. The entire file can be described in just a few bytes ("one million A's").
Real data falls between these extremes. The more skewed the distribution (some symbols much more common than others), the lower the entropy and the more compressible the data.
Entropy of English
Shannon's Experiment
Shannon himself estimated the entropy of English text through a clever experiment. He gave human subjects passages of text and asked them to guess the next character. By measuring how often they guessed correctly, he could estimate how predictable (and therefore how low-entropy) English really is.
If English used 27 characters (26 letters + space) with equal probability, the entropy would be log₂(27) ≈ 4.75 bits per character.
Shannon's experiments showed the actual entropy of English is roughly 1.0 to 1.5 bits per character.
That's a massive gap. English text is about 70-80% redundant. The patterns, rules, and statistical regularities of the language make most characters partially predictable from context. This is exactly why text compresses so well — there's an enormous amount of redundancy to exploit.
This also explains why different file types compress differently. English text (low entropy) compresses dramatically. Encrypted data or compressed data (near-maximum entropy) barely compresses at all — the redundancy has already been removed.
Cross-Entropy and Redundancy
In practice, a compressor doesn't know the true probability distribution P of the data. It uses a model — an estimate Q. When the model is imperfect, you pay a price. That price is measured by cross-entropy:
H(P, Q) = -Σ p(x) log₂(q(x))
Cross-entropy measures the average bits per symbol when you design your codes based on distribution Q, but the data actually follows distribution P. A fundamental result:
H(P, Q) ≥ H(P)
Cross-entropy is always at least as large as the true entropy. The difference, H(P, Q) - H(P), is called the Kullback-Leibler divergence (or redundancy). It represents the wasted bits due to using the wrong model.
Practical Implication
Better models lead to better compression. This is why modern compressors invest heavily in modeling: adaptive frequency tables, context mixing, neural networks. The closer Q is to P, the smaller the redundancy, and the closer the output size gets to the entropy limit.
Conditional Entropy and Context
So far we've treated each symbol independently. But in real data, symbols are correlated — knowing previous symbols helps predict the next one. Conditional entropy captures this:
H(X | Y) = entropy of X given that we know Y
Conditional entropy is always less than or equal to unconditional entropy: H(X | Y) ≤ H(X). Knowing context can never increase uncertainty.
English Letter Prediction
Consider the letter 'q' in English. If the previous letter was 'q', the next letter is almost certainly 'u'. That means:
H(next letter | previous = 'q') ≈ 0 bits
Compare this to the unconditional entropy of a random English letter (about 4.1 bits without context). Context slashes the uncertainty dramatically.
Similarly, after "th", the next letter is very likely 'e', 'a', 'i', or 'o'. After "thi", it's almost certainly 's' or 'n'. Each additional context character further reduces the entropy of the next character.
This is why modern compression algorithms use context modeling. PPM (Prediction by Partial Matching) considers the previous N characters to predict the next one. LZMA uses context-dependent probability models for its arithmetic coder. The more context you use, the lower the conditional entropy, and the better your compression — up to the point where you run out of data to estimate the probabilities reliably.
Information Theory Beyond Compression
Shannon's framework extends far beyond compression. Entropy and related concepts appear throughout computer science and engineering.
| Application | Information Theory Concept | What It Means |
|---|---|---|
| Channel capacity | Shannon limit | The maximum rate at which data can be sent reliably over a noisy channel |
| Error correction | Redundancy & Hamming distance | Adding controlled redundancy so errors can be detected and corrected |
| Machine learning | Cross-entropy loss | The standard loss function for classification — measures how well a model's predicted distribution matches reality |
| Cryptography | Key entropy | A 128-bit key has 128 bits of entropy if all keys are equally likely, meaning 2128 possibilities to search |
| Decision trees | Information gain | Choosing the split that maximally reduces entropy of the target variable |
Key Takeaways
- Information equals surprise — rare events carry more information than common ones
- Shannon entropy H(X) = -Σ p(x) log₂(p(x)) measures the average information per symbol in bits
- Entropy is the theoretical floor for lossless compression — no algorithm can beat it
- Maximum entropy (uniform distribution) means incompressible data; minimum entropy (single certain outcome) means maximally compressible
- English text has about 1.0-1.5 bits/character of entropy, far below the 4.75 bits a uniform distribution would require — text is highly compressible
- Better probability models reduce cross-entropy, which translates directly to better compression ratios
Try It Yourself — Entropy Explorer
Type any string below to see its character frequencies, a visual histogram, Shannon entropy, and detailed compression statistics.
Frequency Table
Frequency Histogram
Shannon Entropy
Compression Statistics
Every compression algorithm in this tutorial is, at its core, trying to approach Shannon's entropy limit. RLE exploits runs of identical symbols. Huffman assigns shorter codes to probable symbols. LZ77 and LZW exploit repeated sequences. Arithmetic coding and ANS achieve fractional bits per symbol. The Burrows-Wheeler Transform restructures data to make patterns more exploitable. All of them are strategies for squeezing out the redundancy that entropy theory tells us is there.