TL;DR
Entropy measures how unpredictable a random outcome is, averaged over all the outcomes. Each outcome carries a "surprise" that is large when it was unlikely, and entropy is the average of that surprise. A fair coin has one bit of entropy. A coin that always lands heads has none, because nothing about it is news.
Where you'll see it: in the number of bits needed to compress a message, in the cross-entropy loss that trains classifiers, and in the KL divergence that compares two probability models.
Intuition
Surprise
Start with one outcome. If it has probability , its surprise is bits. The rarer the outcome, the larger the surprise. A result with probability carries 1 bit: it answers one yes-or-no question. A result with probability carries 3 bits, the same as three yes-or-no answers in a row. Surprise adds up over independent events for the same reason the logarithm turns products into sums (see Logarithm).
Surprise belongs to one outcome. A source that emits many outcomes needs one number for the whole distribution, so we average.
Average surprise
Entropy is the expected surprise: weight each outcome's surprise by how often it happens, then add. Outcomes that are common contribute a little surprise but happen often. Rare outcomes contribute a lot of surprise but happen seldom. The average is the typical number of bits you learn per outcome.
flowchart LR A[Outcome with probability p] --> B[Surprise log2 of 1 over p] B --> C[Weight by p] C --> D[Sum over all outcomes] D --> E[Entropy in bits]
The plot shows entropy for a coin that lands heads with probability . It is zero for a certain coin, peaks at exactly 1 bit for a fair coin, and falls symmetrically on both sides.
- H of a coin with P(heads) = p
Why the number matters
Entropy is also a count of bits. Shannon's key result, which we return to in History, is that a source with entropy bits per symbol can be encoded in about bits per symbol on average, and no lossless code can do better. A source that is predictable compresses well, and a source that is unpredictable does not. That is why entropy is the natural measure of information content, and of how much a message can be squeezed.
The math
Definition
Let be a random variable taking values with probabilities . Its entropy is
The result is measured in bits. Using the natural logarithm instead gives nats.
Here runs over the possible outcomes, is the chance of outcome , and the sum adds up one term per outcome. The minus sign makes each term positive, because is negative for any probability below 1. Outcomes with contribute nothing. We adopt the convention , which is justified because shrinks to 0 as shrinks to 0.
Note that depends only on the probabilities, not on the labels. A coin with outcomes "heads" and "tails" has the same entropy as one labelled 0 and 1.
Key properties
Non-negativity. , with equality exactly when one outcome has probability 1. A certain outcome teaches nothing.
Maximum at uniform. If has outcomes with positive probability, then , with equality when all are equally likely. The argument uses Jensen's inequality (see Jensen's Inequality) on the concave function . Write . Jensen gives
The inner expectation is , so .
Independence adds. If and are independent, . Two fair coins carry 2 bits.
Cross-entropy and KL divergence
Given a true distribution and a model , the cross-entropy is . It equals the entropy of plus the KL divergence from to :
Because the KL divergence is never negative, a model can never do better than the entropy of the truth. This is the reason classifiers are trained with cross-entropy: minimising it pushes toward .
The continuous case
For a continuous variable with density the analogue is the differential entropy . It can be negative, and it changes when you change units. For a Gaussian with variance and natural logarithm, . Among all densities with a given variance, the Gaussian has the largest differential entropy, which is the reason the Gaussian is called the least presumptuous model.
Worked examples
Question 1: what is the entropy of a fair coin?
- Two outcomes, each with .
- .
- One bit: one yes-or-no question resolves the coin.
Question 2: what is the entropy of a coin with ?
- and .
- .
- About 0.47 bits. The coin is fairly predictable, so each toss tells you less than half a bit.
A random symbol takes four values with probabilities .
- Surprises in bits: , , , and .
- Weight each by its probability: , , , .
- Add: bits.
- Interpretation: a yes-or-no guessing game can identify the symbol in 1, 2, 3 and 3 questions. Averaged over the probabilities, that is 1.75 questions, which is the best any code can do.
A model outputs probabilities for three classes. How uncertain is it?
- .
- .
- .
- Sum: bits.
- The maximum for three classes is bits, reached by . The model is leaning toward one class but is far from certain.
Tradeoffs and failure modes
Entropy is simple to compute and easy to misuse. The table lists the common traps.
| Situation | Problem | Better choice |
|---|---|---|
| Estimating entropy from a small sample | Plugging in observed frequencies underestimates entropy | More data, or a bias-corrected estimator |
| Continuous variable | Differential entropy can be negative and depends on units | Compare with KL divergence, or fix the units first |
| Scoring a model against the truth | Entropy of the truth alone says nothing about the model | Cross-entropy or KL divergence |
| Measuring the information in a message | The number depends on the assumed source model | Model the source well, or use an adaptive code |
| Judging whether data is useful | High entropy means unpredictable, not meaningful | Look at task-specific measures |
The choice of log base only sets the unit: bits for base 2, nats for base . Compare entropies only in the same unit.
History
The word comes from thermodynamics. In the 1870s Ludwig Boltzmann linked the entropy of a gas to the number of microscopic arrangements that produce the same visible state. Josiah Willard Gibbs generalised the formula, which is the same sum over microstates. Ralph Hartley, in 1928, proposed that the information in a message is the logarithm of the number of messages that could have been sent.
Claude Shannon's 1948 paper "A Mathematical Theory of Communication" brought these threads together. Shannon, working at Bell Labs, wanted to know how much a noisy channel could carry and how far a message could be compressed. He defined entropy for a probability distribution, showed it sets the limit for lossless compression, and used it to define channel capacity. A widely repeated story says John von Neumann suggested the name, since the formula looked like the one from statistical mechanics (the story is told by Shannon himself but is hard to verify). The idea caught on because it gave engineers a precise, unit-carrying number for information and because it applied to every source. Coding theory, statistics and machine learning all use it now.
Check yourself
QuestionWhat does the entropy of a distribution measure, and what unit does it have when the logarithm is base 2?Show answer
Entropy measures the average uncertainty or surprise of an outcome drawn from the distribution. It is the expected value of , so its unit is bits. A higher number means outcomes are harder to predict.
QuestionA variable takes three values with probabilities . Compute its entropy in bits.Show answer
bits. The three outcomes need 1, 2 and 2 yes-or-no questions respectively, and the average is 1.5.
QuestionWhy does a fair six-sided die have more entropy than a die that lands on 6 half the time and on each other face one tenth of the time?Show answer
The fair die has bits, the maximum for six outcomes. The loaded die has bits. The loaded die is more predictable because it favours one outcome, and entropy is largest when no outcome is favoured.
QuestionA classifier gives probability 0.01 to the true class. Why does the cross-entropy loss penalise this so heavily?Show answer
The loss is . With the loss is nats, or 6.64 bits, far larger than the loss of about 0.1 nats when . The logarithm grows without bound as the probability approaches zero, so the model is punished strongly for being confidently wrong.