KL Divergence
KL Divergence measures how much one probability distribution differs from a reference distribution . Key result: it is always non-negative, equals zero only when , but is not symmetric and is not a true distance. Applies in: information theory, statistics, machine learning, thermodynamics. Origin: Solomon Kullback and Richard Leibler, 1951.The Kullback–Leibler divergence (also called relative entropy) quantifies the "information lost" when distribution is used to approximate the true distribution . Concretely, it is the expected number of extra bits (or nats) you pay for encoding samples from using a code optimized for instead of the optimal code for . It matters because it is the natural measure of distributional discrepancy that arises from coding theory, maximum-likelihood estimation, and Bayesian inference all at once. The surprising fact: although it behaves like a "squared distance" for nearby distributions, in general, so swapping the two arguments can change the answer dramatically — even from finite to infinite.
The Core Insight
KL divergence is the expectation, under , of the log-ratio of the two densities. It answers: "On average over the true world , how surprised would I be if I believed ?" Because is positive where over-predicts and negative where it under-predicts, and because weights the average, the convexity of forces the total to be non-negative. It is an asymmetric gap, not a metric.
Formal Statement
KL Divergence — Discrete Case
Let and be probability distributions on a discrete set , with mass functions and . The Kullback–Leibler divergence of from is
Here is the true probability, the model probability, and the logarithm base sets the units: base 2 gives bits, base gives nats. Conventions: (justified by ), and if any where .
KL Divergence — Continuous Case
For distributions with densities and on (with respect to Lebesgue measure),
The general measure-theoretic form uses the Radon–Nikodym derivative :
Gibbs' Inequality / Non-negativity
For any two distributions on the same space,
Why It Holds / Derivation
Non-negativity via Jensen's Inequality
We prove [4].
Step 1. Write the divergence as a negative expectation:Solomon Kullback & Richard Leibler | 1907–1994, 1914–2003
Kullback and Leibler, both cryptanalysts and statisticians, introduced the quantity in their 1951 paper "On Information and Sufficiency" while working on statistical decision theory and the discrimination between hypotheses. Kullback was a chief scientist at the U.S. National Security Agency and authored the influential 1959 monograph Information Theory and Statistics. Their original motivation was the discrimination information between two statistical hypotheses — how decisively data favors over — which links directly to the Neyman–Pearson Lemma and the theory of Sufficient Statistics.
Building Intuition
Imagine you must design a code to compress messages whose letters truly follow distribution . The optimal code (Shannon's source coding) assigns roughly bits to letter if you believe the distribution is . The true expected code length is then , the cross-entropy . The unavoidable minimum is the entropy . KL divergence is exactly the excess:
So KL divergence = wasted bits from using the wrong codebook. It cannot be negative (you can never beat the optimal code) and is zero only when your codebook matches reality. The asymmetry now makes sense: paying with the wrong codebook in one direction is a different penalty than the other.
KL divergence as the gap between cross-entropy and entropy, and a mismatched density pair
Worked Examples
Two Coins — Discrete KL in Bits
Given: True coin . Model coin . Compute in bits (base-2 log). Step 1. Apply [1]:Two Gaussians — Continuous KL in Nats
Given: and . The closed-form result is(Multiply by to convert to bits: bits.)
Common Misconceptions
KL divergence is NOT a distance metric. It fails two of the three metric axioms:- Asymmetry: in general (see Example 1).
- Triangle inequality fails: there is no general .
Infinite Divergence from Support Mismatch
Let be uniform on and uniform on . Then but , so the term . Hence
Connections
KL divergence is the hub of a web of information-theoretic quantities. Mutual Information is the KL divergence between a joint distribution and the product of its marginals: . Cross-Entropy equals , which is why minimizing cross-entropy loss is exactly minimizing KL to the data distribution. Minimizing over a parametric family is equivalent to Maximum Likelihood Estimation. The local quadratic approximation of KL recovers the Fisher Information Matrix as its Hessian — the foundation of information geometry. Finally, Shannon Entropy is the self-reference baseline that KL measures excess against.
KL and Fisher Information — Local Structure
For a smooth parametric family , the KL divergence between nearby parameters is, to second order,
This shows KL is locally a (Riemannian) squared distance, even though it is globally asymmetric.
Real-World Applications
- Machine learning loss functions — Cross-entropy/KL loss trains nearly all classification networks; minimizing it is minimizing .
- Variational inference & VAEs — The Evidence Lower Bound (ELBO) contains a KL term regularizing the approximate posterior toward the prior; this is the core of variational autoencoders.
- Reinforcement learning — Trust Region Policy Optimization (TRPO) and PPO constrain policy updates by bounding between old and new policies to ensure stable improvement.
- Model selection — The Akaike Information Criterion (AIC) is derived as an estimator of expected KL divergence between the true and fitted models.
- Hypothesis testing & cryptanalysis — The original Kullback–Leibler use: the divergence equals the expected log-likelihood-ratio, governing the error exponents of Neyman–Pearson tests (Stein's lemma).
- Statistical physics — Relative entropy bounds free-energy differences; the non-negativity underpins the second law and the Jarzynski/Crooks fluctuation theorems.
- Anomaly & drift detection — Monitoring between a reference data distribution and live streaming data flags distribution shift in deployed ML systems.
Historical Development
- 1948 — Claude Shannon introduces entropy and source coding in A Mathematical Theory of Communication, providing the bit-counting framework KL later exploits.
- 1951 — Solomon Kullback and Richard Leibler publish "On Information and Sufficiency" (Annals of Mathematical Statistics), defining the divergence as discrimination information between hypotheses.
- 1959 — Kullback's monograph Information Theory and Statistics systematizes relative entropy in statistical inference.
- 1957 — E. T. Jaynes formulates the Maximum Entropy Principle, in which minimizing KL to a prior generalizes entropy maximization.
- 1960s — Stein's lemma connects KL to the optimal error exponent of hypothesis tests, formalizing the cryptanalytic origin.
- 1973 — Hirotugu Akaike derives AIC, showing model selection approximates minimizing expected KL divergence.
- 1985–1990s — Shun'ichi Amari develops information geometry, treating KL as a dual-flat divergence with the Fisher metric, unifying it with differential geometry.
- 2013–2014 — Kingma & Welling (VAEs) and Schulman et al. (TRPO) embed KL divergence at the heart of modern deep learning objectives.