Search

Search papers and lessons

Entropy

Level 2 · 9 min read

Entropy is the average surprise of a random outcome: a number, in bits, for how unpredictable a distribution is.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Information theory
  • Sign in to see your progress

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 pp, its surprise is log⁡2(1/p)\log_2(1/p) bits. The rarer the outcome, the larger the surprise. A result with probability 1/21/2 carries 1 bit: it answers one yes-or-no question. A result with probability 1/81/8 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 pp. It is zero for a certain coin, peaks at exactly 1 bit for a fair coin, and falls symmetrically on both sides.

0.20.40.60.800.20.40.60.81p, probability of headsentropy (bits)
  • H of a coin with P(heads) = p
The fair coin at p = 0.5 gives the maximum, 1 bit. A coin that is 90% heads gives 0.469 bits: much less uncertain.

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 HH bits per symbol can be encoded in about HH 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

Definition: Entropy of a discrete random variable

Let XX be a random variable taking values xx with probabilities p(x)p(x). Its entropy is

H(X)=−∑xp(x) log⁡2p(x).H(X) = -\sum_{x} p(x)\,\log_2 p(x).

The result is measured in bits. Using the natural logarithm instead gives nats.

Here xx runs over the possible outcomes, p(x)p(x) is the chance of outcome xx, and the sum adds up one term per outcome. The minus sign makes each term positive, because log⁡2p(x)\log_2 p(x) is negative for any probability below 1. Outcomes with p(x)=0p(x) = 0 contribute nothing. We adopt the convention 0log⁡0=00 \log 0 = 0, which is justified because plog⁡pp \log p shrinks to 0 as pp shrinks to 0.

Note that H(X)H(X) 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. H(X)≥0H(X) \ge 0, with equality exactly when one outcome has probability 1. A certain outcome teaches nothing.

Maximum at uniform. If XX has nn outcomes with positive probability, then H(X)≤log⁡2nH(X) \le \log_2 n, with equality when all are equally likely. The argument uses Jensen's inequality (see Jensen's Inequality) on the concave function log⁡\log. Write H(X)=E[log⁡21p(X)]H(X) = \mathbb{E}\left[\log_2 \frac{1}{p(X)}\right]. Jensen gives

E[log⁡21p(X)]≤log⁡2E[1p(X)].\mathbb{E}\left[\log_2 \frac{1}{p(X)}\right] \le \log_2 \mathbb{E}\left[\frac{1}{p(X)}\right].

The inner expectation is ∑p(x)>0p(x)⋅1p(x)=n\sum_{p(x) \gt 0} p(x) \cdot \frac{1}{p(x)} = n, so H(X)≤log⁡2nH(X) \le \log_2 n.

Independence adds. If XX and YY are independent, H(X,Y)=H(X)+H(Y)H(X, Y) = H(X) + H(Y). Two fair coins carry 2 bits.

Cross-entropy and KL divergence

Given a true distribution pp and a model qq, the cross-entropy is H(p,q)=−∑xp(x)log⁡2q(x)H(p, q) = -\sum_x p(x)\log_2 q(x). It equals the entropy of pp plus the KL divergence from pp to qq:

H(p,q)=H(p)+DKL(p ∥ q).H(p, q) = H(p) + D_{\mathrm{KL}}(p \,\|\, q).

Because the KL divergence is never negative, a model qq can never do better than the entropy of the truth. This is the reason classifiers are trained with cross-entropy: minimising it pushes qq toward pp.

The continuous case

For a continuous variable with density p(x)p(x) the analogue is the differential entropy h(X)=−∫p(x)log⁡p(x) dxh(X) = -\int p(x)\log p(x)\,dx. It can be negative, and it changes when you change units. For a Gaussian with variance σ2\sigma^2 and natural logarithm, h=12ln⁡(2πe σ2)h = \frac{1}{2}\ln(2\pi e\,\sigma^2). 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

Example
A fair coin and a biased coin

Question 1: what is the entropy of a fair coin?

  1. Two outcomes, each with p=1/2p = 1/2.
  2. H=−(12log⁡212+12log⁡212)=−(12(−1)+12(−1))=1H = -\left(\tfrac{1}{2}\log_2 \tfrac{1}{2} + \tfrac{1}{2}\log_2 \tfrac{1}{2}\right) = -\left(\tfrac{1}{2}(-1) + \tfrac{1}{2}(-1)\right) = 1.
  3. One bit: one yes-or-no question resolves the coin.

Question 2: what is the entropy of a coin with P(heads)=0.9P(\text{heads}) = 0.9?

  1. log⁡20.9≈−0.1520\log_2 0.9 \approx -0.1520 and log⁡20.1≈−3.3219\log_2 0.1 \approx -3.3219.
  2. H=−(0.9×(−0.1520)+0.1×(−3.3219))=0.1368+0.3322=0.469H = -(0.9 \times (-0.1520) + 0.1 \times (-3.3219)) = 0.1368 + 0.3322 = 0.469.
  3. About 0.47 bits. The coin is fairly predictable, so each toss tells you less than half a bit.
Example
Four outcomes with unequal chances

A random symbol takes four values with probabilities 12,14,18,18\tfrac{1}{2}, \tfrac{1}{4}, \tfrac{1}{8}, \tfrac{1}{8}.

  1. Surprises in bits: log⁡22=1\log_2 2 = 1, log⁡24=2\log_2 4 = 2, log⁡28=3\log_2 8 = 3, and 33.
  2. Weight each by its probability: 12(1)=0.5\tfrac{1}{2}(1) = 0.5, 14(2)=0.5\tfrac{1}{4}(2) = 0.5, 18(3)=0.375\tfrac{1}{8}(3) = 0.375, 18(3)=0.375\tfrac{1}{8}(3) = 0.375.
  3. Add: H=0.5+0.5+0.375+0.375=1.75H = 0.5 + 0.5 + 0.375 + 0.375 = 1.75 bits.
  4. 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.
Example
A three-class classifier

A model outputs probabilities (0.7,0.2,0.1)(0.7, 0.2, 0.1) for three classes. How uncertain is it?

  1. 0.7log⁡210.7=0.7×0.5146=0.36020.7 \log_2 \tfrac{1}{0.7} = 0.7 \times 0.5146 = 0.3602.
  2. 0.2log⁡210.2=0.2×2.3219=0.46440.2 \log_2 \tfrac{1}{0.2} = 0.2 \times 2.3219 = 0.4644.
  3. 0.1log⁡210.1=0.1×3.3219=0.33220.1 \log_2 \tfrac{1}{0.1} = 0.1 \times 3.3219 = 0.3322.
  4. Sum: H=1.1568H = 1.1568 bits.
  5. The maximum for three classes is log⁡23=1.585\log_2 3 = 1.585 bits, reached by (1/3,1/3,1/3)(1/3, 1/3, 1/3). 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.

SituationProblemBetter choice
Estimating entropy from a small samplePlugging in observed frequencies underestimates entropyMore data, or a bias-corrected estimator
Continuous variableDifferential entropy can be negative and depends on unitsCompare with KL divergence, or fix the units first
Scoring a model against the truthEntropy of the truth alone says nothing about the modelCross-entropy or KL divergence
Measuring the information in a messageThe number depends on the assumed source modelModel the source well, or use an adaptive code
Judging whether data is usefulHigh entropy means unpredictable, not meaningfulLook at task-specific measures

The choice of log base only sets the unit: bits for base 2, nats for base ee. 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 −∑pln⁡p-\sum p \ln p 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 log⁡2(1/p(X))\log_2(1/p(X)), so its unit is bits. A higher number means outcomes are harder to predict.

QuestionA variable takes three values with probabilities (0.5,0.25,0.25)(0.5, 0.25, 0.25). Compute its entropy in bits.Show answer

H=0.5×1+0.25×2+0.25×2=0.5+0.5+0.5=1.5H = 0.5 \times 1 + 0.25 \times 2 + 0.25 \times 2 = 0.5 + 0.5 + 0.5 = 1.5 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 H=log⁡26=2.585H = \log_2 6 = 2.585 bits, the maximum for six outcomes. The loaded die has H=0.5×1+5×0.1×log⁡210=0.5+1.661=2.161H = 0.5 \times 1 + 5 \times 0.1 \times \log_2 10 = 0.5 + 1.661 = 2.161 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 −log⁡q(true class)-\log q(\text{true class}). With q=0.01q = 0.01 the loss is −log⁡0.01=4.61-\log 0.01 = 4.61 nats, or 6.64 bits, far larger than the loss of about 0.1 nats when q=0.9q = 0.9. The logarithm grows without bound as the probability approaches zero, so the model is punished strongly for being confidently wrong.

Sources