- Introduced by
- Solomon Kullback and Richard Leibler
- Year
- 1951
- Type
- Asymmetric probability distribution dissimilarity
- Family
- Information-theoretic divergence
What does it do?
Kullback-Leibler divergence, also called relative entropy or information divergence, measures how far one probability distribution differs from a reference distribution.
Solomon Kullback and Richard Leibler introduced it in 1951. It is zero when the two distributions are identical and grows as they diverge, with no upper bound, and it is not symmetric, so the order of the two arguments matters.
The intuition behind the method
Consider two probability distributions over the same outcomes: P, the reference or observed distribution, and Q, an approximation or prediction. KL divergence measures the expected extra cost, in bits or nats, of describing outcomes that occur according to P when the description is built for Q. Where P puts substantial probability on outcomes that Q makes unlikely, the penalty is large. The measure is zero only when the two distributions agree. Because the cost is weighted by P and not by Q, swapping the roles of P and Q gives a different value.
History
Solomon Kullback and Richard Leibler introduced the divergence in 1951 in the paper 'On information and sufficiency', published in the Annals of Mathematical Statistics. Its general treatment as relative entropy appears in the information-theory literature, including Cover and Thomas, who discuss relative entropy alongside entropy and mutual information. The measure's later use in statistics and machine learning follows from this definition.
When to use it
Choose KL divergence when both objects are probability distributions over the same outcomes and one of them is a clear reference, such as observed data compared with a model, or a true distribution compared with an approximation. It suits questions about the information lost when Q is used in place of P, for example in variational inference or when comparing a prior with a posterior. Do not use it when Q assigns zero probability to an outcome that P makes possible, because the value is then infinite. Do not use it when you need a symmetric distance or a true metric; Jensen-Shannon, Hellinger and Wasserstein distances, listed as related methods, are candidates for that. Raw counts must be normalized before use. For continuous data, the densities must first be estimated, usually by binning or by fitting a parametric model, and the divergence then depends on that estimate.
Limitations and considerations
- It is asymmetric: KL(P||Q) and KL(Q||P) generally differ, so the choice of reference distribution changes the result.
- It is infinite whenever Q(x) = 0 for some outcome x with P(x) > 0, so one unsupported outcome can determine the entire value.
- It is not a metric: it fails both symmetry and the triangle inequality.
- Outcomes where Q(x) is very small relative to P(x) produce large log-ratio terms, so a few such outcomes can dominate the sum.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- The value has a direct reading as the expected extra cost, in bits or nats, of using Q in place of P.
- Each outcome is weighted by P(x) and enters through the log ratio P(x)/Q(x), so differences in where P places its mass relative to Q are reflected directly in the sum.
- The identity KL(P||Q) = H(P,Q) - H(P) links the divergence to cross-entropy and entropy, which clarifies what the value measures.
Applications
- Model fitting: measuring how far a model distribution Q departs from a data distribution P, as in variational inference.
- Generative modelling: serving as a term in objectives that compare a model's distribution with a target distribution.
- Anomaly detection: comparing an observed feature distribution with an expected baseline distribution.
- Bayesian updating: quantifying how much a posterior distribution differs from the prior it was derived from.
- Text analysis: comparing word-frequency distributions across corpora, after each has been normalized to probabilities.
How it works
How it works
Check that P and Q are probability distributions over the same set of outcomes, with probabilities summing to 1. If the inputs are raw counts or unnormalized scores, convert them to probabilities first.
Formula
\sum_{x} P(x) = \sum_{x} Q(x) = 1For each outcome x, multiply P(x) by the logarithm of the ratio P(x)/Q(x), and sum these terms over all outcomes: D_KL(P||Q) = sum over x of P(x) log(P(x)/Q(x)). By convention, outcomes with P(x) equal to 0 contribute nothing. An outcome with P(x) greater than 0 and Q(x) equal to 0 makes the sum infinite. For continuous distributions with densities p and q, the sum is replaced by an integral, D_KL(p||q) = integral of p(x) log(p(x)/q(x)) dx. This requires q(x) > 0 wherever p(x) > 0; otherwise the value is infinite.
Formula
D_{\mathrm{KL}}(P\|Q)=\sum_{x} P(x)\,\log\frac{P(x)}{Q(x)}The sum is the divergence in bits if the logarithm is base 2, or in nats if the natural logarithm is used. The choice of base changes the scale of the value but not the direction of any comparison made with the same base.
Formula
D_{\mathrm{KL}}(P\|Q)\,[\text{bits}]=\sum_{x} P(x)\,\log_2\frac{P(x)}{Q(x)}, \qquad D_{\mathrm{KL}}(P\|Q)\,[\text{nats}]=\sum_{x} P(x)\,\ln\frac{P(x)}{Q(x)}Common pitfalls
- Choosing the reference distribution by habit rather than by the question, which reverses the meaning of the result.
- Applying the formula to counts, scores or other unnormalized values, which does not give a divergence between probability distributions.
- Reading the value as a symmetric distance between the two distributions; it measures a directional information loss.
- Computing it on empirical distributions with empty bins and reporting an infinite or smoothed value without stating how the zeros were handled.
- Reading a KL value as large or small without a reference point. The measure has no upper bound and no universal threshold, and its scale depends on the log base used. Interpret a value against a baseline, against the values obtained for competing candidate distributions Q, or against a known scale, and state the base used.
Frequently asked questions
Why is KL divergence asymmetric?
KL divergence measures the expected cost, under P, of using Q in place of P. Reversing the arguments changes both the weights and the log ratios, so KL(P||Q) and KL(Q||P) generally differ. KL(P||Q) is large when P places mass where Q is small; KL(Q||P) is large when Q places mass where P is small.
How do I handle zero probabilities in KL divergence?
If P(x) > 0 and Q(x) = 0 for some outcome x, the divergence is infinite. Smoothing, such as add-one smoothing or pseudocounts, removes exact zeros, but it changes both distributions, so the resulting value describes the smoothed distributions rather than the originals. The smoothing method should be reported. Jensen-Shannon divergence stays finite in this situation and is symmetric, so it is an alternative when these properties are needed.
What is the difference between KL divergence and cross-entropy?
Cross-entropy measures the average bits needed to encode events from P using a code optimized for Q. KL divergence is the difference between cross-entropy and entropy: KL(P||Q) = H(P,Q) - H(P), where H is entropy. KL divergence isolates the extra cost due to Q.
When should I use KL divergence over Jensen-Shannon divergence?
Use KL divergence when one distribution is a clear reference and you want a directional measure of information loss. Use Jensen-Shannon divergence when you want a symmetric measure that treats both distributions equally. Jensen-Shannon divergence is not itself a metric, but its square root is a metric that satisfies the triangle inequality.
Related methods
Sources
Kullback, S., & Leibler, R. A. (1951). On information and sufficiency. Annals of Mathematical Statistics, 22(1), 79-86.
Open DOI referenceCover, T. M., & Thomas, J. A. (1991). Elements of Information Theory. Wiley-Interscience.
Open DOI reference