Part Of: Information Theory sequence
Followup To: An Introduction to Bayesian Inference
Content Summary: 4600 words, 23 min read.
Introduction
Imagine moving to a new town, and trying to learn its climate from the weather record. Consider two hypotheses:
- Hypothesis 1 gives rain a 70% chance
- Hypothesis 2 gives rain a 50% chance.
A rainy day increases our belief in hypothesis 1. A dry day increases our belief in hypothesis 2.
Suppose the actual climate produces rain on 70% of days. Hypothesis 1 was correct! But it takes time for the evidence to demonstrate this. Evidence will slowly increase our belief in hypothesis 1, but with a setback on every dry day.
At what rates do our beliefs update? How many observations before our belief in hypothesis 1 is 100 times stronger than hypothesis 2?
Today, we will introduce concepts from information theory to answer these questions. The evidence for hypothesis 1 grows by 0.082 nats per day, on average. Starting from even odds, we reach 100:1 confidence in hypothesis 1 after about 56 days ().
That 0.082 is the relative entropy of hypothesis 2. This is one of the most widely used quantities in statistics and machine learning. We will build it in three steps: surprise, cross-entropy and entropy.
Relative entropy also governs hypothesis tests: it sets how fast a test’s errors shrink as the record grows. And it is the rate of learning: our belief in hypothesis 2 typically falls exponentially, at a rate of 0.082 per day.
Let’s begin.
Surprise as Information Content
To quantify how much we can learn from the weather, we first need a way to quantify information. Our intuitions are naturally evoked in the context of books:
- Additivity. Two similar-length books can hold roughly as much information as a book twice their length.
- Monotonicity. A book written in English can hold more information than a book of equal length written in a smaller alphabet, e.g., binary (0s and 1s).
The number of possible messages W provides a natural way to operationalize these intuitions. Consider a book of n characters written in an alphabet of size a. The choice of a new character is not constrained by the extant content. With independent choice of letters, the rule of product applies:
W satisfies monotonicity, but not additivity. Doubling the book length squares W. Taking the natural log repairs additivity:
Hartley (1928) defined ln W as information, since it satisfies both intuitions.
With base-2 logarithms, the unit of information is the bit. Thus, a binary book of n characters holds n bits of information. This post uses natural logarithms, whose unit is the nat (about 1.44 bits), because the math is cleaner.
Another perspective examines the probability a hypothesis Q gives each possible message y. A hypothesis treating all W messages as equally likely gives:
This allows us to formulate a probabilistic version of Hartley’s measure:
Monotonicity carries over directly. The other property requires an assumption. Recall that in probability theory, the chain rule lets us compose events:
If the hypothesis treats outcomes as independent, , then the combinatorial rule of product, for two books with and possible messages, matches the probabilistic product rule:
Taking the log turns the product into a sum. For independent outcomes, the measure is additive:
Sometimes events are not independent. Weather can arrive in spells: rain makes rain tomorrow more likely.
Without independence, surprise still adds, but each outcome’s term is measured given the ones before it. This post’s hypotheses treat days as independent.
Unlike Hartley’s measure, the probability version does not require equally likely outcomes. To handle unequal chances, we assume locality. Two hypotheses Q and with must produce the same measure, however they differ elsewhere. Appendix A shows that the logarithm is the only function that is local, additive and monotone, up to the choice of unit.
We have arrived at our new definition. Let the information content of outcome y under hypothesis Q be:
Outcomes that our hypothesis deems improbable provide more information than outcomes that are expected. So we call hypothesis Q’s surprise at outcome y. Consider an outcome that a hypothesis predicts is impossible, Q(y) = 0. The surprise approaches infinity. When an outcome obtains that a hypothesis predicts as certain, its surprise is zero.
Hypothesis 1 (70% rain) is more surprised on a dry day () than on a rainy one (). Hypothesis 2 (50% rain) is equally surprised by either outcome ( nats, or 1 bit).
Cross-Entropy as Long-Run Surprise
Let the truth P denote the probability distribution of the data generating process. Because the weather is stochastic, a wet climate will sometimes generate a dry first few days. Any particular surprise might be misleading. We would like to know the average surprise of a hypothesis over a long record of T days.
Let’s write for a day’s weather. Assume the days are independent and identically distributed (i.i.d.), and drawn from P. Group the days by their weather, with as the number of days with weather y:
By the law of large numbers, each frequency settles on its probability, . So the average surprise converges to:
An expectation is a weighted average over some distribution. Let denote the expected surprise over the truth. We define cross-entropy H(P,Q) as this long-run surprise:
From here on, y plays three roles. Inside an expectation, a probability or a long-run limit, it is a day drawn at random from the truth or from a hypothesis. Elsewhere, is a day of the record we saw, and a bare y is either outcome, rain or dry.
Let’s calculate the cross-entropy of our hypotheses. Hypothesis 2 (50% rain) has:
Consider a new hypothesis which predicts a climate of 90% rain. Call this hypothesis 3. Then,
Recall that hypothesis 1 is correct, and . Even correct hypotheses are surprised:
Entropy as Unavoidable Surprise
Why do even true hypotheses pay 0.61 nats per day? Perfect knowledge of the data generating process informs long-run trends. It is not necessarily an oracle of outcomes.
Let entropy H(P) denote the cross-entropy of a true hypothesis.
Our town’s climate is 70% rain. What about other truths? If the town’s climate were 100% rain, then a true hypothesis would incur zero surprise. The same result obtains for a 0% rain climate. The further the climate departs from a deterministic outcome, the larger its entropy. For a binary variable like our climate, maximum entropy obtains with the uniform distribution:
At the town’s 70%, the entropy is the 0.61 nats that hypothesis 1 paid.
The entropy of a truth measures how unpredictable it is, even to the true hypothesis. Claude Shannon had a subject guess passages of English one letter at a time, given the text before it. From the guesses he estimated that English carries between 0.6 and 1.3 bits per letter (Shannon 1951).
Entropy behaves the way a measure of uncertainty should (Shannon 1948). It keeps the additivity of surprise. It is largest when every outcome is equally likely. And it is concave: its graph lies below each of its tangent lines (Appendix B), which is why the entropy dome above has its shape.
In our town, every false hypothesis had a cross-entropy larger than the entropy of the truth (0.61 nats). This discovery that entropy is a floor generalizes to any hypothesis Q. This is Gibbs’ inequality, derived in Appendix B:
Gibbs’ inequality becomes an equality for any Q that matches P. All other hypotheses have a gap. Entropy is a property of the truth, and is a floor shared across all hypotheses.
Gibbs’ inequality has a nice geometric interpretation. We’ve already illustrated the entropy dome across all possible truths. Now fix a hypothesis Q and let the truth vary. Its cross-entropy is:
At the left edge, and the cross-entropy is . At the right edge, and it is . In between it is a weighted average of the two, so for a fixed Q it is a straight line.
At , the cross-entropy line touches the entropy dome and is tangent to it there. At the tangent point, Gibbs’ inequality becomes an equality. For all other truths, the cross-entropy is above the entropy dome, and the gap grows with the distance.
Take hypothesis 2. If the truth were 50% rain, its gap would be zero. At our truth, 70% rain, the gap is 0.082, and it grows the further the truth sits from 50%.
Relative Entropy as Extra Surprise
Gibbs’ inequality suggests a gap between cross-entropy and entropy. Let relative entropy D denote the gap. This quantity is also known as the KL divergence, after Kullback & Leibler (1951). The relationship between cross-entropy, entropy, and relative entropy becomes:
As the difference of expectations, each drawn from P, the relative entropy can be written as a log ratio:
From this equation, we can directly calculate and compare it with our derivation of and :
This matches our earlier calculation:
A similar derivation shows that hypothesis 3 has relative entropy of 0.154. For hypothesis 1, where , the numerator and denominator cancel. Relative entropy here is zero because that is the value of . By Gibbs’ inequality, relative entropy is never negative, and it is zero only when . So relative entropy is the extra surprise a hypothesis pays for not knowing the truth. This is why the cross-entropy bowl bottoms out at the truth.
The town’s average surprise under a hypothesis that gives each day a chance of rain q. Every height is the gray floor, the climate’s entropy, plus the teal gap, the relative entropy. The gap closes only at the truth, 70%.
Since cross-entropy is the expected surprise over the truth, cross-entropy bowls are generated from a weighted sum of mirror-image surprise curves. Only a truth of 50% rain produces a symmetric bowl. For every truth, each bowl bottoms out at Q = P, at height H(P), so the bottoms trace the entropy dome.
Gibbs’ inequality also explains why the uniform truth has the most entropy, for any number of outcomes. Let u represent a counterfactual truth uniformly distributed across N outcomes (e.g., 50% climate) and P represent the current truth (70% climate). Consider the cross-entropy , treating the counterfactual climate u as a hypothesis about the actual climate P:
Since u is uniform its value doesn’t depend on y. Factor it out of the sum.
Set the hypothesis to u in the relationship between the three quantities, and replace its cross-entropy with :
Relative entropy is never negative, so no truth has more entropy than \ln N, and only the uniform truth reaches it.
In our town, hypothesis 2 is the uniform one, so the truth’s entropy falls short of the maximum by the 0.082 nats we found above.
Because relative entropy is never negative and vanishes only at the truth, it is often read as a distance between distributions. But it is not symmetric. Take hypotheses 1 and 2, first with 70% rain as the truth and 50% as the hypothesis, then the other way around:
So relative entropy is a divergence, not a distance.
The Weight of Evidence
Relative entropy can adjudicate our beliefs in hypotheses. Let the odds Λ₁₂ represent the comparative strength of our beliefs in a pair of hypotheses:
Bayes’ theorem updates a prior p into a posterior p′ through the likelihood , the probability hypothesis i gave the outcome:
Here is the probability the learner gave y, the same for every hypothesis. We can use this to relate the odds of the posterior to the odds of the priors:
A Bayesian update thus involves multiplying the odds by the likelihood ratio . By taking the log of both sides, we can reinterpret this update as an addition of the log-odds .
Let’s define weight of evidence for this change (Good 1950). Importantly, this quantity can be expressed as a difference in surprise!
Each outcome in our town weighs in differently:
A rainy day adds 0.34 nats to the log-odds no matter what our learner believes. But the effect of the weight of evidence on probability is not fixed: the same nats shift a 50% prior to 58%, and a 90% prior to 93%.
Given that hypothesis 1 is correct, we would expect to increase on average. But the weight of evidence is strongly negative on dry days. This might seem surprising. The confusion fades when we calculate the expected weight of evidence (Kullback & Leibler’s mean information for discrimination per observation):
On average, we expect the log-odds to increase by 0.082 nats per day.
Since the weight of evidence is a difference in surprise, the expected weight of evidence is a difference in expected surprise. But this is a difference in cross-entropy!
Expanding each term:
The entropies cancel, and the expected weight of evidence between two hypotheses is the difference between their relative entropies:
In our town, hypothesis 1 is correct: , so . Its expected weight of evidence simplifies:
We have weighed the correct hypothesis 1 (70% rain) against hypothesis 2 (50% rain). But what if no hypothesis is correct? Recall hypothesis 3, which gives rain a 90% chance. For hypothesis 3 over hypothesis 2:
The new expected weight of evidence is:
Even though both hypotheses miss the climate by 20 points, the evidence favors hypothesis 2 because its relative entropy is smaller (0.082 vs 0.154 nats).
When Evidence Misleads
In our town, the climate produces rain on 70% of days. What if the weather happens to produce rain on only 27 of our first 58 days (~47%)? In that case, hypothesis 2 (a 50% rain climate) better explains the data, despite it being false. We might call this misleading evidence.
In the long run, the frequency of rain will approach the truth. But sometimes we must act on our hypotheses in the short run. To make an informed decision, we must quantify the risk of misleading evidence. But the risk of misleading evidence is a counterfactual question, which compares the observed record with a broader space of possible records. With only one record, we must rely on our hypotheses to explore this broader space.
A hypothesis test is a rule that looks at a record and decides which hypothesis made it. It does this by defining a decision threshold, compared with each record’s total weight of evidence. Records whose total is at or above the threshold are attributed to hypothesis 1; those below to hypothesis 2. We treat hypothesis 1 as the null hypothesis. A positive result rejects it in favor of hypothesis 2. False positives (type I errors) are when a hypothesis test attributes a record made by hypothesis 1 to hypothesis 2. False negatives (type II errors) are the converse. Choosing the threshold trades off between these errors. Higher thresholds lower false negatives and raise false positives.
The threshold k is measured in nats. Every rainy day contributes the same weight of evidence. So with a binary outcome, the total weight of evidence can be determined by the number of rainy days. Recall that counts the days with weather y, so is the number of rainy days. A record with totals about −0.82 nats, and one with totals about 0.028 nats. So every threshold k between −0.82 and 0.028 nats makes the same split. With such a threshold, any hypothesis-generated record whose total falls below k resides in the critical region:
Each walk is one imagined town’s running weight of evidence, ending at its 58-day total. Each bar gives the chance of a total, which is the fraction of all towns run by that hypothesis whose records end there. Adding the bars on the wrong side of the threshold gives a 4.3% false positive rate and a 7.4% false negative rate.
A Bayesian will select a threshold that minimizes her expected cost. Let be the cost of a false positive and be the cost of a false negative. The expected cost of accepting hypothesis 1 is . The expected cost of rejecting it in favor of hypothesis 2 is . She should accept hypothesis 1 when the expected cost of rejecting exceeds accepting:
Dividing by and taking the log allows us to interpret the log-odds of this inequality:
Recall that the log-odds add the weight of evidence of each observation to the prior log-odds:
Combining the two expressions:
Thus, a Bayesian should accept hypothesis 1 when the total weight of evidence for it exceeds a particular threshold k:
A frequentist does not use beliefs over her hypotheses. She arrives at a threshold from the hypotheses, by setting a ceiling on the false positive rate. This ceiling is the significance level α. By convention, many fields set , but this is not universal: particle physics uses . She then strives to minimize the false negative rate \varepsilon such that the false positive rate stays at or below this ceiling.
The Neyman–Pearson lemma proves that setting the threshold k against the total weight of evidence is the way to minimize the false negative rate while respecting the ceiling (Neyman & Pearson 1933). This test is the likelihood ratio test.
Let’s define and as the distributions of records made by hypotheses 1 and 2, respectively. The two error rates are:
Then, a frequentist will choose the best threshold that minimizes the rate of false negatives (i.e., maximizes the power ), while also meeting the ceiling \alpha constraint on false positives. Raising k lowers and raises . So the best k is the highest one the ceiling allows.
In the town, , associated with . Any of these thresholds produces the following error rates.
Rejecting at instead is forbidden, because the false positive rate becomes 0.0744, which exceeds the ceiling.
An ROC curve (receiver operating characteristic) can visualize the tradeoff made between the two error rates. Several thresholds are shown as discrete points. The threshold at 35 rainy days is the most powerful threshold possible that is still under the ceiling.
Overall, a Bayesian sets her decision rule by comparing the record’s total weight of evidence with a threshold set by her beliefs and costs. The error rates stem from that. A frequentist minimizes her decision rule’s false negative rate under the constraint that the false positive rate is at most \alpha.
Both views put a threshold k on the same total weight of evidence. The Bayesian derives it from beliefs and costs, the frequentist from a ceiling on one hypothesis’s error rate. For this record, the two routes reach the same rule.
The Speed of Learning
Recall the expected weight of evidence for one day:
Our town has , so the second term drops out. Over T days, the expected total weight of evidence becomes:
Over 100 days, we expect to accrue 8.2 nats in favor of hypothesis 1. Log-odds that grow linearly mean odds that grow exponentially:
Over those 100 days, the odds for hypothesis 1 typically grow 3,600-fold, and our belief in hypothesis 2 tends toward zero. Any one town’s record is noisy, but simulated towns track the expected slope of 0.082 nats per day:
The same convergence phenomenon appears in hypothesis testing. Recall that and are the distributions of records made by hypotheses 1 and 2. The former assumes ; the latter assumes . Over T days, the expected total weight of evidence becomes:
The days are independent. The standard deviation (SD), the square root of the average squared distance from the mean, is:
These are the center and spread of the total under and under . As T grows, the centers grow apart in proportion to T, while the widths grow only in proportion to . Together, the overlap where errors live shrinks:
For a Bayesian setting a threshold based on equal priors and equal costs, false positive and false negative rates shrink at the same pace, the Chernoff information rate of 0.021 nats per day (Cover & Thomas 2006). But frequentists minimize false negatives with a ceiling on false positives. The frequentists’ threshold moves with the record to stay at the 5% ceiling (the dashed gray line below), so false positives stay near 5%. But the false negative rate approaches zero faster, at 0.082 nats per day.
Error rates for records of 1 to 200 days. Faint lines are the true rates, which zigzag because the number of rainy days is a whole number. Bold curves are the normal approximation from each total’s mean and standard deviation, with the 5% test’s cutoff lowered by half a rainy day, its average shortfall from keeping false positives at or under 5%. At 58 days, our town’s record, both rules reject hypothesis 1 when fewer than 35 days are rainy.
How fast does the false negative rate decline with increasing T? Keep fixed, and the smallest achievable false negative rate falls exponentially with the number of observations. The Chernoff–Stein lemma (Cover & Thomas 2006) identifies its rate of decline as the relative entropy .
Why this relative entropy, and not another? Split the records in two: let A be the records that the hypothesis test attributes to hypothesis 1, and R be the rest. Consider a particular record made by either hypothesis. Then the rates of false positives (FP), true negatives (TN), true positives (TP) and false negatives (FN) are:
We need a way to constrain . Each day’s observation adds a weight of evidence:
Across all observations in a record, the total weight of evidence becomes:
Exponentiate both sides and solve for :
Recall that the average total under is , and that it grows faster than the spread. So a typical hypothesis-1 record has a total near its average:
Substituting:
So a typical hypothesis-1 record is exponentially less likely under hypothesis 2.
Write the false negative rate as a sum over A, and substitute:
The frequentist’s constraint, , forces A to hold nearly all of hypothesis 1’s probability, so most records in A are typical. As an approximation to the rate of decline, not to the probability at any given T, treat every record in A as typical (Appendix C). Each then carries the same exponential factor, which comes out of the sum:
The remaining sum is the true negative rate, the probability that hypothesis 1 produces a record in A. It lies between and 1. Since this factor doesn’t shrink exponentially with T, it does not change the rate of decline:
The false negative rate drops exponentially with the number of days T, with the relative entropy of hypothesis 1 over hypothesis 2 as its rate.
The full derivation of the Chernoff–Stein lemma doesn’t require these approximations. Let be the smallest false negative rate on T-day records whose false positives are capped at . Then:
So the same relative entropy, nats per day, sets two speeds: how fast the Bayesian’s log-odds for hypothesis 1 grow, and how fast the frequentist’s false negatives shrink. The speeds differ in kind. The Bayesian’s is an expected gain that holds from the first day. The frequentist’s is a long-run rate: at 58 days it predicts , against an actual 7.4%.
Wrapping Up
We began with two hypotheses about a town’s climate: 70% rain vs 50% rain. How many observations does it take to tell them apart? Real weather is noisy. But over time, evidence accumulates at a predictable rate.
We then undertook to measure that accumulation.
- Surprise measures the information in an outcome.
- Cross-entropy is the surprise we expect in the long run.
- Entropy is the surprise even a correct hypothesis cannot avoid.
Across possible truths, entropy traces a dome whose peak is at the uniform distribution. Fix the truth and vary the hypothesis, and cross-entropy traces a bowl. The bowl meets the dome at the truth. The gap everywhere else is relative entropy.
What happens when we compare hypotheses to one another, rather than to the truth? We defined the weight of evidence as the difference between their surprises. On average, this quantity becomes the difference between their relative entropies.
A Bayesian’s log-odds for hypothesis 1 grow at that rate, on average. And a frequentist hypothesis test, holding false positives at a fixed ceiling, can drive false negatives down at that same rate in the long run.
The two results are different, but they share a source. Relative entropy is both the extra surprise of a false hypothesis and the rate at which evidence separates it from the truth.
Glossary
The table below gathers the post’s symbols, for a hypothesis Q scored against the truth P and for two hypotheses Q_1 and Q_2. Each row is built from the rows above it. Values are in nats, except the error rates, which are probabilities for 58-day records.
References
- Cover & Thomas (2006). Elements of information theory.
- Good (1950). Probability and the weighing of evidence.
- Hartley (1928). Transmission of information.
- Kullback & Leibler (1951). On information and sufficiency.
- Neyman & Pearson (1933). On the problem of the most efficient tests of statistical hypotheses.
- Shannon (1948). A mathematical theory of communication.
- Shannon (1951). Prediction and entropy of printed English.
Appendix A: Why Logarithms
Surprise rests on one fact about a function g of the whole numbers (Shannon 1948). Suppose g grows with n and turns products into sums:
Then g is a logarithm. Apply the rule to m repeats of a whole number r:
Now take whole numbers r and t, both greater than 1, and any whole number n. Some whole number m puts between neighboring powers of r:
Apply g, which grows, and turn each power into a multiple as above. The logarithm also grows and turns products into sums, so the same holds for \ln:
The rule forces g(1) to be zero, and g grows, so g(r) is positive for every r above 1:
Divide the first chain by and the second by . Both ratios land in the same interval, of width 1/n:
We can take n as large as we like, so the two ratios are equal. Rearrange, and name the common value K, which is positive because g grows:
For surprise, Locality lets us write s(p) for the surprise of an outcome with probability p. Let g(n) be the surprise of one of n equally likely outcomes. Additivity makes g turn products into sums, and Monotonicity makes g grow with n:
So g is a logarithm. To reach any fraction a/b, apply Additivity to a/b and 1/a:
Solve for , and substitute the logarithm:
Every other probability lies between fractions as close to it as we like, and Monotonicity squeezes s between their values:
Appendix B: Gibbs’ Inequality
The proof needs one fact about the logarithm: it is concave, so its graph lies below each of its tangent lines. Its tangent line at 1 touches it only there:
lies below its tangent line x-1 and touches it only at x = 1. Applied to , this gives , with equality only when Q is P.
Apply it to the ratio inside the gap between the two averages:
The average of the ratio is the total probability Q gives to the outcomes P allows, which cannot exceed one:
So the right side is at most zero, which gives Gibbs’ inequality, with equality only when Q is P:
Gibbs’ inequality also makes entropy concave. Mix distributions with weights that sum to one:
Score the mixture against each distribution, as if that distribution were the truth. Write out the cross-entropies and gather the weights inside the sum over outcomes:
The sum in parentheses is the mixture, so the weighted cross-entropies add up to the mixture’s own entropy:
Gibbs’ inequality puts each cross-entropy at or above that distribution’s own entropy:
Average these with the weights:
The two sides are equal only when the distributions agree.
Appendix C: The Chernoff–Stein Rate
Recall that we treated every record in A as typical to pull the exponential factor out of the sum. That hides where false negatives come from. At 58 days, 83% of them come from records with 35 to 37 rainy days, just past the threshold. On those records, hypothesis 2’s probability is a sixth or more of hypothesis 1’s, far above the 0.85% of a typical record. A bound avoids the approximation. Every record in A has a log likelihood ratio of at least k, so undoing the log:
Sum over A:
To hold false positives at 5%, the threshold sits about 1.645 standard deviations below the mean total, the cutoff that leaves 5% of a bell-shaped spread below it:
At 58 days the two terms nearly cancel, so the bound says nothing, and the false negative rate is 7.4%. But grows more slowly than T, so in the long run the exponent per day approaches . The lemma’s other half shows that the false negative rate can fall no faster (Cover & Thomas 2006).












