Kullback-Leibler divergence
The Kullback-Leibler divergence, also called KL divergence or relative entropy, measures how much one probability distribution differs from a second, reference distribution. It is zero when the two distributions are the same and grows as they become more different.[1] Swapping the two distributions usually gives a different number, so the order matters.
Suppose data really follow a distribution but are described with a model . The KL divergence is then the average amount of extra information needed per observation.[2] It is one of the basic quantities of information and is used across information theory, statistics, and machine learning. Solomon Kullback and Richard Leibler introduced it in 1951.[3][4]
The basic idea
Two distributions and give probabilities to the same set of outcomes. plays the role of the truth or reference, and is a model or approximation of it.[2]
For each outcome , the divergence compares the two probabilities using the logarithm of their ratio, . This log ratio is positive where gives the outcome more probability than does, and negative where it gives less. The divergence averages the log ratios, weighting each outcome by its probability under .[1]
The average is never negative, and it is zero only when and are identical.[1] For this reason KL divergence is often treated as a kind of distance between distributions, and it is counted among the statistical distances.
Definition
Discrete distributions
For distributions that give probabilities and to the same set of outcomes, the KL divergence is[1]
- and are the probabilities that and give to outcome .
- The sum runs over all possible outcomes.
- is the expected value when is drawn from .
Outcomes that never produces add nothing, by the convention . If some outcome has but , the convention makes the whole divergence infinite.[1] By this measure, a model that rules out something that can happen is infinitely wrong.
Units
The base of the logarithm only sets the unit of measure.[5]
| Log base | Unit | Typical use |
|---|---|---|
| 2 | Bit | Information theory texts[1] |
| nat | Machine learning and statistics[2][5] |
A value in bits equals the value in nats divided by .
Continuous distributions
When and have probability densities and , the sum becomes an integral:[6]
This integral is a direct generalization of the discrete divergence.[6]
General definition
For general probability measures, the definition uses the Radon-Nikodym derivative . It applies when is absolutely continuous with respect to , meaning that every event with zero probability under also has zero probability under :[7]
If is not absolutely continuous with respect to , the divergence is defined to be .[7] An equivalent definition, from work by Kolmogorov and Pinsker, takes the least upper bound (supremum) of the discrete divergences obtained by sorting the outcomes into finitely many groups.[6]
Worked example: a biased coin
A textbook example compares a fair coin with a biased coin that lands heads with probability .[1] With base-2 logarithms:
The heads term of is negative, because the fair coin gives heads less probability than the model does. The tails term is positive and larger, so the total is positive. The two directions give different values, which shows the asymmetry.[1]
Interpretations
Extra message length
In data compression, a code built for the true distribution needs on average bits per symbol, where is the entropy. A code built for the wrong distribution needs on average bits.[1]
This gives the link to cross-entropy, the average code length when is used for data from :[2]
For the coin example, bit and the cross-entropy is bits. The penalty for using the biased-coin code on a fair coin is bits per toss.
Weight of evidence
Kullback described the log of the likelihood ratio, , as the information that an observation gives for deciding in favor of against . He noted that I. J. Good called this quantity the weight of evidence.[5] The KL divergence is its average when the data really come from .[5]
This average also equals the expected change in the log odds of against from before to after one observation.[5] The log odds here is the logarithm of how much more probable hypothesis is than hypothesis .
Rare samples and large deviations
Draw independent samples from , and let be a distribution whose probabilities are multiples of . The chance that the observed frequencies exactly match lies between and , with the divergence in bits and the number of possible outcomes.[8] Samples that look like become exponentially rare, at a rate set by the divergence.
Sanov's theorem extends this to a set of distributions. For well-behaved sets, the probability that the observed frequencies fall in shrinks at the exponential rate , where is the member of that makes smallest.[8] Results like this make KL divergence a basic tool of large deviations theory.[1]
Telling two distributions apart
In a hypothesis test, independent samples come from either or . A type I error rejects when it is true, and a type II error accepts when is true.
The Chernoff-Stein lemma says that if the type I error is held below a fixed small level, the smallest possible type II error falls like , with the divergence in bits and assumed finite.[1][9] A larger divergence means fewer samples are needed to tell the hypotheses apart. See error exponents in hypothesis testing.
Properties
Nonnegativity
Gibbs' inequality states that , with equality if and only if for every .[1] The proof applies Jensen's inequality to the concave logarithm, summing over the outcomes where :
For continuous distributions, the divergence is zero exactly when the two densities agree almost everywhere, that is, everywhere except on a set that takes up no volume.[2]
Not a metric
KL divergence shares some properties of a metric but not others.[1]
| Property | Metric | KL divergence |
|---|---|---|
| Never negative | Yes | Yes |
| Zero only for identical inputs | Yes | Yes |
| Symmetric | Yes | No |
| Triangle inequality | Yes | No |
| Always finite | Yes | No |
Kullback noted that even the symmetric sum of the two directions fails the triangle inequality, which is why he called it a divergence and not a distance.[5]
Chain rule
For pairs of variables, the divergence splits into a part for the first variable and an averaged part for the second:[1]
The last term is the conditional divergence, which is the divergence between and averaged over drawn from .[1] When both distributions make the two parts independent, this term becomes a plain divergence, so divergences add. For independent samples, the divergence is times that of one sample.
Convexity
KL divergence is convex in the pair . For any between 0 and 1:[1]
Mixing two pairs of distributions in the same proportions never gives a divergence above the matching average of the two. Combined with the identity for the uniform distribution below, this shows that entropy is a concave function.[1]
Data processing inequality
Suppose has distribution or , and is produced from by the same random process in both cases. Then[10]
Processing data cannot make two distributions easier to tell apart. This is the data processing inequality for divergence. Applying it in both directions shows that a one-to-one transformation leaves the divergence unchanged.
Links to entropy and mutual information
- For the uniform distribution on outcomes, . So no distribution on outcomes has entropy above .[1]
- Mutual information is the divergence of a joint distribution from the product of its marginals, . It is zero if and only if and are independent.[1]
- Cross-entropy equals entropy plus divergence, .[2]
Normal distributions
For two normal distributions, the divergence has a closed form. In nats:[6]
- and are the means.
- and are the standard deviations, both positive.
With equal standard deviations , the formula reduces to . For example, and are nats apart in either direction.
When the spreads differ, the two directions disagree. For and , whose standard deviation is 2, nats. In the other direction, nats.
Choosing the direction
When a simple distribution is fitted to a fixed distribution , the two directions give different fits.[2][11]
- Minimizing forces to be nonzero wherever is. If has several peaks, spreads out to cover all of them and blurs them together.
- Minimizing pushes away from regions where is small. If the peaks of are well separated, settles on one of them, and different starting points can find different peaks.
Which direction to use depends on the application.[2] Variational inference, which approximates a hard posterior distribution with a simpler one, uses because computing it needs expectations only under the simple distribution .[12] Expectation propagation works with the other direction.[11]
Uses
Maximum likelihood and cross-entropy loss
The empirical distribution of a data set puts probability on each of its points.[2] Fitting a model by maximum likelihood is the same as minimizing , where is the empirical distribution.[12] Because the data's own entropy is fixed, this is also the same as minimizing the cross-entropy, the loss function commonly used to train neural networks.[2][13]
Variational inference
In Bayesian inference, the posterior distribution of hidden variables given data is often too hard to compute. Variational inference picks a simpler distribution and uses the identity[11]
Because does not depend on , raising the evidence lower bound lowers the divergence. The bound is exact when equals the true posterior.[11]
Variational autoencoders
A variational autoencoder is trained on a lower bound that contains the term . Here is the encoder's distribution over hidden codes and is the prior.[14] This term pulls the encoder toward the prior, and when both are normal it can be computed exactly without sampling.[14]
Fine-tuning language models
In reinforcement learning from human feedback, a language model is tuned to score well with a learned reward model. The InstructGPT work added a per-token KL penalty that keeps the tuned model close to the supervised model it started from, to limit over-optimization of the reward model.[15] A coefficient sets the strength of the penalty.[15]
Model selection
Hirotugu Akaike found a formal link between KL divergence and maximum likelihood in work published in 1973 and 1974.[4] The Akaike information criterion estimates the expected relative KL information lost by a model:
Here is the maximized likelihood and is the number of estimated parameters.[4] The aim is to pick the candidate model that loses the least information.[4]
Related measures
| Measure | Relation to KL divergence | Note |
|---|---|---|
| Jeffreys divergence | Symmetric, with no upper bound[16] | |
| Jensen-Shannon divergence | with | Symmetric and at most [16] |
| f-divergence | KL divergence is the case | Family introduced by Csiszár[17] |
| Rényi divergence | KL divergence is the order | Order 1 is defined as a limit[18] |
Pinsker's inequality
Pinsker's inequality bounds the total variation distance by the KL divergence. With the divergence in nats:[19][17]
A small divergence therefore forces a small total variation distance. The reverse does not hold, since is infinite whenever gives positive probability to an outcome that rules out, however small the total variation distance.
Local behavior and Fisher information
For a smooth family of distributions with parameter , the divergence between nearby members is approximately[20]
Here is a small change in the parameter and is the Fisher information. Swapping the two arguments gives the same approximation, so KL divergence is nearly symmetric for small changes.[20] With several parameters, the Fisher information matrix takes this role and serves as the Fisher information metric of information geometry.[20]
For a coin with heads probability , the approximation is .[20]
History
Kullback and Leibler published On Information and Sufficiency in the Annals of Mathematical Statistics in 1951.[3] The paper examined the meaning of information in relation to Ronald Fisher's idea of a sufficient statistic.[4]
In his 1959 book Information Theory and Statistics, Kullback wrote the quantity as , the mean information per observation for discriminating in favor of one hypothesis against another. He used the word divergence for the symmetric sum and called and directed divergences.[5] He credited the symmetric form to Harold Jeffreys, who had used it in 1946.[5][16]
Modern texts use the names KL divergence and relative entropy for the directed form.[1][2] The symmetric sum is now called the Jeffreys divergence.[16]
References
- Elements of Information Theory, 2nd ed., Chapters 2 and 11, Thomas M. Cover and Joy A. Thomas, Wiley, 2006.
- Deep Learning, Chapter 3: Probability and Information Theory, Ian Goodfellow, Yoshua Bengio and Aaron Courville, MIT Press, 2016.
- On Information and Sufficiency, S. Kullback and R. A. Leibler, Annals of Mathematical Statistics 22(1), 1951.
- Kullback-Leibler information as a basis for strong inference in ecological studies, Kenneth P. Burnham and David R. Anderson, Wildlife Research 28, 2001.
- Information Theory and Statistics, Solomon Kullback, Wiley (Dover reprint 1968), 1959.
- KL-divergence for continuous random variables (Information and Coding Theory lecture notes), Madhur Tulsiani, Toyota Technological Institute at Chicago, 2025.
- KL divergence and lower bounds for deviations (lecture notes), Aurélien Garivier, Université de Toulouse, accessed 2026.
- Lecture 21: Hypothesis Testing, Method of Types and Large Deviation, Carnegie Mellon University, accessed 2026.
- Lecture 24: Large Deviation and Hypothesis Testing, Carnegie Mellon University, 2015.
- Information Theory, Chapter 2: Mutual Information, Yury Polyanskiy and Yihong Wu, MIT OpenCourseWare, 2016.
- Pattern Recognition and Machine Learning, Christopher M. Bishop, Springer, 2006.
- Deep Learning, Chapter 19: Approximate Inference, Ian Goodfellow, Yoshua Bengio and Aaron Courville, MIT Press, 2016.
- Deep Feedforward Networks (lecture slides for Deep Learning, Chapter 6), Ian Goodfellow, 2016.
- Auto-Encoding Variational Bayes, Diederik P. Kingma and Max Welling, arXiv, 2013.
- Training language models to follow instructions with human feedback, Long Ouyang et al., arXiv, 2022.
- Jensen-Shannon divergence and diversity index: Origins and some extensions, Frank Nielsen, accessed 2026.
- f-divergences (lecture notes), Yury Polyanskiy and Yihong Wu, MIT, accessed 2026.
- Rényi Divergence and Kullback-Leibler Divergence, Tim van Erven and Peter Harremoës, arXiv, 2012.
- Information and Coding Theory, Lecture 5: Pinsker's inequality, Madhur Tulsiani, Toyota Technological Institute at Chicago, 2017.
- Local divergence (Statistics 2 lecture notes), Korbinian Strimmer, accessed 2026.
