46.04.01 · information-theory / information-statistics

Hypothesis Testing and Stein's Lemma: The KL Divergence as the Error Exponent

shipped3 tiersLean: none

Anchor (Master): Cover & Thomas 2006 Elements of Information Theory 2e (Wiley) §11.1-11.8; Csiszár & Körner 2011 Information Theory: Coding Theorems for Discrete Memoryless Systems 2e (Cambridge) §I.4; Lehmann & Romano 2005 Testing Statistical Hypotheses 3e (Springer) §3.2-3.3

Intuition Beginner

You have a coin. You suspect it is biased, but you are not sure. You flip it many times and count the heads. If it is fair, you expect about half heads. If it is biased toward heads, you expect more. After enough flips, you can tell which coin you have with high confidence.

This is the hypothesis testing problem in its simplest form. You observe data and must decide between two explanations — two hypotheses about the distribution that generated the data. The fundamental question: how many samples do you need before you can decide reliably?

The answer depends on how different the two distributions are. If the coin is fair (50/50) versus a coin that lands heads 99 percent of the time, a single flip is often enough. But if the coin is fair versus a coin that is 51/49, you need many flips to tell them apart. The KL divergence — introduced in 46.01.02 — measures exactly this distinguishability.

Stein's lemma gives the precise answer. If you draw n independent samples and require that the probability of a false alarm stays below some small threshold, then the best you can do is make the probability of a miss decay like , where is the KL divergence between the two distributions. The divergence is the error exponent: it tells you the rate at which your mistakes vanish. Double the divergence, and you need half as many samples to achieve the same reliability.

The trade-off between the two kinds of errors is the heart of the subject. You cannot make both errors small simultaneously — there is always a cost. Stein's lemma pins down one side of this trade-off: fix the false-alarm rate, and the miss rate has a definite, optimal exponential rate given by the KL divergence.

Visual Beginner

Figure: two bar charts side by side. The left shows a Bernoulli(0.5) distribution — two equal bars at 0.5. The right shows a Bernoulli(0.7) distribution — bars at 0.3 and 0.7. Below each pair, the log-likelihood ratio for each outcome is shown. The two distributions overlap, but the likelihood ratio tells you how to weight the evidence from each observation.

Quantity Value
(null) Bernoulli(0.5)
(alternative) Bernoulli(0.7)
in bits 0.188
in bits 0.210
Samples for type II error (at type I ) about 36

The asymmetry is striking: in general. Stein's lemma uses when is the null, not the other direction. Swapping the hypotheses changes the exponent.

Worked example Beginner

A factory produces light bulbs. Under normal operation, each bulb is good with probability 0.8 (distribution ). A faulty machine produces bulbs that are good with probability only 0.5 (distribution ). You test bulbs. You want to detect the faulty machine without raising too many false alarms.

The KL divergence between and is:

Compute each piece. The first term: bits. The second term: bits. Adding them: bits.

By Stein's lemma, if you hold the false-alarm probability below 5 percent, the miss probability decays as roughly . For bulbs, the miss probability is about . For bulbs, it drops to about . For bulbs, about .

With just 30 bulbs, you can distinguish the good machine from the faulty one with miss probability below half a percent while keeping false alarms below 5 percent. The exponent 0.278 bits per sample governs the entire scaling.

Notice the asymmetry. If you swap roles and treat the faulty machine as the null hypothesis, the exponent becomes bits. The error decays faster when the alternative is more concentrated (the faulty machine is easier to reject when it is the null).

Check your understanding Beginner

Formal definition Intermediate+

Let be a finite alphabet. Let and be two probability distributions on with (i.e., ). Consider independent and identically distributed draws from either (the null hypothesis ) or (the alternative hypothesis ).

Definition (Binary hypothesis test). A (possibly randomised) test is a function , where is the probability of rejecting (accepting ) after observing the sequence .

Definition (Type I and Type II errors). The type I error (false alarm, size) and type II error (miss probability) are:

The type I error is the probability of wrongly declaring when is true. The type II error is the probability of wrongly declaring when is true.

Definition (Likelihood ratio test). For a threshold , the likelihood ratio test rejects when the ratio of densities exceeds the threshold:

where is a randomisation parameter chosen to achieve exact size .

Definition (Error exponent). For a sequence of tests with type I errors for all , the type II error exponent is:

when the limit exists. This is the exponential rate at which the type II error vanishes.

Counterexamples to common slips

  • The exponent is , not . Stein's lemma is asymmetric. The exponent uses the divergence from the null to the alternative. Swapping hypotheses changes the exponent.

  • Stein's lemma does not say both errors vanish simultaneously. It fixes the type I error below and then gives the type II exponent. If you need both errors to vanish at the same rate, the exponent is the Chernoff information, which is smaller than either directional divergence.

  • The alphabet must be finite (or the distributions must satisfy regularity conditions). For continuous distributions, Stein's lemma holds under appropriate conditions on the densities, but the proof requires more care with measure-theoretic details.

Key theorem with proof Intermediate+

Theorem (Stein's lemma). Let and be two probability distributions on a finite alphabet with . For any :

where is the minimum achievable type II error among all tests of size at most .

Equivalently, for any and all sufficiently large , the best type II error satisfies:

Proof sketch (direct part — achievability). We construct a test that achieves the exponent. The idea is to accept when the empirical distribution (type) of the observed sequence is "close" to and reject otherwise.

Define the acceptance region:

for a small . The log-likelihood ratio per sample is:

Under , each term has expected value and finite variance. By the law of large numbers, the average converges to in probability. Therefore , which means the type I error for all sufficiently large .

The type II error under is:

We bound this using the large-deviations upper bound. For any :

Hence , achieving the exponent.

Proof sketch (converse — optimality). For the converse, we show that no test can do better. Suppose a test has . Then:

For any with :

The type II error is . Using the method of types or large deviations, one can show that the dominant contribution to comes from sequences whose log-likelihood ratio is near , and no test can make vanish faster than . The formal argument uses the fact that for any test with bounded type I error:

for any and all large enough , because the set must carry probability at least under , and the cheapest way (in -probability) to cover such a set is to use the -probability mass in regions where is largest, which corresponds to the region near the exponent .

Bridge. Stein's lemma builds toward the large-deviations framework of Sanov's theorem 37.07.05, where the KL divergence appears as the rate function governing the exponential rarity of atypical empirical distributions; it appears again in channel coding 46.03.01 as the reliability function of the channel, where the error exponent for decoding is governed by a divergence between the output distributions induced by different codewords. The foundational reason Stein's lemma works is that the log-likelihood ratio is a sum of i.i.d. random variables under both hypotheses, so the law of large numbers drives it to under and to under , creating an exponentially growing gap between the two distributions on their typical sets. This is exactly the mechanism behind the Neyman-Pearson optimality of the likelihood-ratio test 45.02.01 — the NP lemma tells you the test structure is optimal, and Stein's lemma tells you the asymptotic rate at which that optimal test drives the error to zero. The central insight generalises to multiple hypotheses, composite hypotheses, and the Chernoff information for symmetric errors.

Exercises Intermediate+

Lean formalization Intermediate+

Mathlib does not define hypothesis tests on discrete distributions, nor does it provide the error-probability functionals that Stein's lemma requires. The PMF type for probability mass functions exists, and basic properties of the KL divergence on PMF could in principle be developed, but no such development is present. Specifically, Mathlib lacks: a TestFunction type mapping sequences to , type I and type II error definitions as sums over acceptance/rejection regions, the likelihood-ratio decision rule with threshold and randomisation, and any asymptotic result about error exponents. The Chernoff information — the tilted-sum minimisation over — and its relation to the symmetric error exponent are unformalised. A Codex.InformationTheory.SteinLemma module would need to build the test framework on PMF, prove the LLN for the per-sample log-likelihood ratio, and then establish the achievability and converse bounds that constitute Stein's lemma. This unit ships without formalization.

Advanced results Master

The Neyman-Pearson lemma as the finite-sample foundation

Stein's lemma is the asymptotic shadow of the Neyman-Pearson lemma 45.02.01. The NP lemma states that for testing against based on observations, the likelihood-ratio test that rejects when (with randomisation on the boundary) is the unique most powerful test of its size. This is a finite-sample result: it holds for every , not just asymptotically.

The NP lemma gives Stein's lemma because the LR test is optimal at every , so the best achievable type II error is exactly the type II error of the LR test. The asymptotic behaviour of is then determined by the large-deviations properties of the log-likelihood ratio under . Specifically, the acceptance region satisfies because the log-likelihood ratio concentrates around its -mean , and the -probability of the region where is large is exponentially small with rate .

Chernoff information and the symmetric error exponent

Stein's lemma fixes the type I error and gives the type II exponent. A different question asks for the best symmetric error exponent: if both errors must vanish at the same rate, what is the optimal exponent?

Definition (Chernoff information). The Chernoff information between and is:

The function is the moment-generating function of the log-likelihood ratio evaluated at . It is convex in , with , so for all and the minimum of is achieved at a unique interior point (unless , in which case ).

Theorem (Chernoff, 1952). For testing vs with equal prior weights , the minimum Bayes error satisfies:

where denotes equality to first order in the exponent.

The Chernoff information satisfies because the symmetric constraint is stronger than fixing one error. It equals when the tilted distribution is equidistant from and in divergence, which occurs when the log-likelihood ratio has a symmetric distribution under the tilted measure.

Worked example: for and , the Chernoff information is approximately nats bits. Compare with nats and nats. The Chernoff information is smaller than both, reflecting the cost of the symmetric constraint.

The Bayesian formulation

Consider a Bayesian setting with prior probabilities and . The Bayes error is:

The optimal test is the Bayes rule: reject when , i.e., when the likelihood ratio exceeds the prior odds . The error exponent is:

regardless of the prior weights (as long as ). The prior affects the pre-factor but not the exponent. This is because the dominant contribution to the error comes from the "confused" region where and look similar, and the cost of this confusion is governed by the Chernoff information.

When the prior weights are unequal (, say), the Bayes test thresholds at a very high likelihood ratio, which reduces type I error at the cost of type II error. The exponent remains , but the pre-factor shifts: the type II error dominates, and the overall error is approximately .

Relation to large deviations and Sanov's theorem

Stein's lemma is a special case of Sanov's theorem 37.07.05. Sanov's theorem states that the probability of seeing an empirical distribution (type) in a set under i.i.d. sampling from is:

where is the distribution in the closure of closest to in KL divergence.

For hypothesis testing, the acceptance region consists of sequences whose types lie in a set of "P-like" types. The type II error is the -probability of seeing a type in , which by Sanov is approximately where is the closest type in to . As grows and the type class refinement increases, converges to , and the exponent converges to .

The method of types provides an alternative proof of Stein's lemma that avoids the LLN-based argument. The type class of a distribution has -probability approximately by the method of types. The acceptance region is a union of type classes close to , and the total -probability is the sum over these type classes, each contributing approximately . The dominant type class is the one minimising subject to , which gives and the exponent .

Synthesis. Stein's lemma is the foundational result that gives the KL divergence its operational meaning as an error exponent: the best achievable type II error rate under a fixed type I constraint is . The bridge is that the Neyman-Pearson lemma determines the test structure (likelihood ratio threshold) and Stein's lemma determines the asymptotic rate (exponential decay with exponent ). The Chernoff information generalises this to the symmetric setting, where both errors must vanish simultaneously, and satisfies because the symmetric constraint is stronger. The central insight builds toward large deviations 37.07.05, where Sanov's theorem shows that the KL divergence is the universal rate function for the rarity of atypical events. This generalises to composite hypotheses, sequential testing, and channel coding, where the error exponent is the divergence between output distributions induced by different messages.

Full proof set Master

Proposition (Data-processing inequality for hypothesis testing). Let and be distributions on , and let be a deterministic function. Then the optimal type II error based on observations of is at least the optimal type II error based on direct observations of , for any type I constraint.

Proof. Let and be the pushforward distributions on . Any test on the processed data induces a test on the raw data with the same type I and type II errors:

Therefore the set of achievable pairs based on is contained in the set of achievable pairs based on : every test on processed data is also a test on raw data, but not conversely. The optimal type II error based on is at least as large as the optimal type II error based on :

By the data-processing inequality for KL divergence, , so processing reduces both the divergence and the achievable error exponent.

Proposition (Stein's lemma via the method of types). Let and be distributions on a finite alphabet with . For any :

Proof (achievability). The set of types (empirical distributions) achievable with samples from is . For each type , the type class (the set of sequences with empirical distribution ) has:

by the method of types 46.02.01.

Define the acceptance region as where:

for small . Then . Since grows polynomially in and is small, , so for large .

The type II error is:

Since is continuous and consists of types close to , the minimum of over is approximately . The sum is dominated by the smallest exponent:

Since is arbitrary, the achievability direction gives for any and large .

Proof (converse). For any test with , the acceptance region satisfies . Write for some set of types . Then:

For this to hold for large , we need for any , meaning contains types arbitrarily close to .

The type II error is:

where . Since must contain types close to (from the type I constraint), is close to , and . Therefore for any and large .

Proposition (Chernoff information as the symmetric exponent). For testing vs with equal priors, the minimum Bayes error satisfies:

where .

Proof sketch. The Bayes error for prior is:

By the inequality for any :

Taking logs and minimising over :

For the converse, the optimal test is the Bayes rule with threshold at the prior odds. The error is bounded below by the contribution from the "ambiguous" region where , and this region has -probability approximately by the large-deviations analysis of the tilted distribution.

Connections Master

  • 46.01.02 — The KL divergence defined there is the error exponent in Stein's lemma; this unit gives it its first operational interpretation as the exponential rate of type II error decay in hypothesis testing.
  • 45.02.01 — The Neyman-Pearson lemma establishes that the likelihood-ratio test is the unique most powerful test at every sample size ; Stein's lemma is the asymptotic () consequence of NP optimality.
  • 37.07.05 — Sanov's theorem in large deviations shows that the KL divergence is the universal rate function for the rarity of atypical empirical distributions; Stein's lemma is the specialisation to the hypothesis-testing setting.
  • 46.02.01 — The AEP and typical sets provide the structural foundation: the -typical set and -typical set diverge exponentially, and the divergence rate is .
  • 46.03.01 — Channel coding reliability: the error exponent for decoding over a noisy channel is governed by divergences between output distributions induced by different codewords, generalising the hypothesis-testing framework.
  • 46.04.02 — The method of types and large deviations for information theory builds directly on the type-class analysis used in the proof of Stein's lemma.

Historical & philosophical context Master

Herman Chernoff introduced the Chernoff information in his 1952 paper "A Measure of Asymptotic Efficiency for Tests of a Hypothesis Based on the Sum of Observations" (Annals of Mathematical Statistics 23, 493-507). Chernoff was working on the problem of sequential analysis, where the sample size is not fixed in advance but depends on the observations. He identified the best achievable symmetric error exponent as the minimum of the moment-generating function of the log-likelihood ratio, a quantity now known as the Chernoff information. The result unified the Bayesian and frequentist approaches to hypothesis testing by showing that the exponent is the same regardless of the prior weights.

Herman Chernoff and Louis LeCam independently developed the large-deviations approach to hypothesis testing in the 1950s and 1960s. The connection to information theory was made explicit by Thomas Cover in his 1973 paper "Enumerative Source Encoding" (IEEE Transactions on Information Theory 19, 73-77), which used the method of types to give information-theoretic proofs of large-deviations results. Cover and Thomas's 1991 textbook "Elements of Information Theory" (Wiley) synthesised these connections in Chapter 12 (Chapter 11 in the 2006 second edition), where hypothesis testing appears as a direct application of the AEP and typical sets.

The Stein lemma itself is attributed to Herman Chernoff's 1952 paper, though the specific name "Stein's lemma" appears to have entered the information-theory literature through the lecture notes of Edward Thomas (a student of Cover). The result is a special case of the more general large-deviations principle: the probability of a large deviation under one measure, as measured by another, decays at a rate given by the KL divergence. This principle underpins Sanov's theorem, the Gärtner-Ellis theorem, and the contraction principle in large deviations, all of which appear in 37.07.05.

The philosophical significance is that the KL divergence — a quantity that appears unnatural at first glance (it is not symmetric, not a metric, and does not satisfy the triangle inequality) — has a natural operational meaning as the rate at which one distribution becomes distinguishable from another. This operational interpretation justifies the divergence's central role in information theory, statistics, and machine learning.

Bibliography Master

@article{chernoff1952,
  author  = {Chernoff, H.},
  title   = {A Measure of Asymptotic Efficiency for Tests of a Hypothesis Based on the Sum of Observations},
  journal = {Annals of Mathematical Statistics},
  volume  = {23},
  pages   = {493--507},
  year    = {1952},
}
@book{cover-thomas2006,
  author    = {Cover, T. M. and Thomas, J. A.},
  title     = {Elements of Information Theory},
  edition   = {2nd},
  publisher = {Wiley},
  year      = {2006},
}
@book{csiszar-korner2011,
  author    = {Csisz{\'a}r, I. and K{\"o}rner, J.},
  title     = {Information Theory: Coding Theorems for Discrete Memoryless Systems},
  edition   = {2nd},
  publisher = {Cambridge University Press},
  year      = {2011},
}
@book{lehmann-romano2005,
  author    = {Lehmann, E. L. and Romano, J. P.},
  title     = {Testing Statistical Hypotheses},
  edition   = {3rd},
  publisher = {Springer},
  year      = {2005},
}
@article{cover1973,
  author  = {Cover, T. M.},
  title   = {Enumerative Source Encoding},
  journal = {IEEE Transactions on Information Theory},
  volume  = {19},
  pages   = {73--77},
  year    = {1973},
}
@book{dembo-zeitouni1998,
  author    = {Dembo, A. and Zeitouni, O.},
  title     = {Large Deviations Techniques and Applications},
  edition   = {2nd},
  publisher = {Springer},
  year      = {1998},
}