KL Divergence

⚡ Key Takeaways

KL Divergence DKL(P∥Q)D_{\mathrm{KL}}(P\|Q) measures how much one probability distribution PP differs from a reference distribution QQ. Key result: it is always non-negative, equals zero only when P=QP=Q, 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 Core Insight

KL divergence is the expectation, under PP, of the log-ratio of the two densities. It answers: "On average over the true world PP, how surprised would I be if I believed QQ?" Because log⁡pq\log\frac{p}{q} is positive where PP over-predicts and negative where it under-predicts, and because PP weights the average, the convexity of −log⁡-\log forces the total to be non-negative. It is an asymmetric gap, not a metric.

Formal Statement

DefinitionKL Divergence — Discrete Case

Let PP and QQ be probability distributions on a discrete set X\mathcal{X}, with mass functions p(x)p(x) and q(x)q(x). The Kullback–Leibler divergence of QQ from PP is

1
DKL(P∥Q)=∑x∈Xp(x) log⁡p(x)q(x)\quad D_{\mathrm{KL}}(P\|Q)=\sum_{x\in\mathcal{X}} p(x)\,\log\frac{p(x)}{q(x)}

Derivation hint: Take the expectation under PP of the pointwise log-ratio log⁡(p/q)\log(p/q); this is the definition, not a theorem. Here p(x)p(x) is the true probability, q(x)q(x) the model probability, and the logarithm base sets the units: base 2 gives bits, base ee gives nats. Conventions: 0log⁡0q=00\log\frac{0}{q}=0 (justified by lim⁡t→0+tlog⁡t=0\lim_{t\to0^+}t\log t=0), and plog⁡p0=+∞p\log\frac{p}{0}=+\infty if any p(x)>0p(x)>0 where q(x)=0q(x)=0.

DefinitionKL Divergence — Continuous Case

For distributions with densities p(x)p(x) and q(x)q(x) on Rn\mathbb{R}^n (with respect to Lebesgue measure),

2
DKL(P∥Q)=∫Rnp(x) log⁡p(x)q(x) dx\quad D_{\mathrm{KL}}(P\|Q)=\int_{\mathbb{R}^n} p(x)\,\log\frac{p(x)}{q(x)}\,dx

Derivation hint: Replace the sum in [1] by an integral; this requires PP to be absolutely continuous with respect to QQ for finiteness. The general measure-theoretic form uses the Radon–Nikodym derivative dPdQ\frac{dP}{dQ}:

3
DKL(P∥Q)=∫log⁡ ⁣(dPdQ)dP\quad D_{\mathrm{KL}}(P\|Q)=\int \log\!\left(\frac{dP}{dQ}\right) dP

Derivation hint: When P≪QP\ll Q, the density ratio p/qp/q is exactly dP/dQdP/dQ; integrating against dPdP unifies the discrete and continuous cases.

TheoremGibbs' Inequality / Non-negativity

For any two distributions P,QP,Q on the same space,

4
DKL(P∥Q)≥0,with equality iff P=Q (a.e.)\quad D_{\mathrm{KL}}(P\|Q)\ge 0,\qquad\text{with equality iff } P=Q \text{ (a.e.)}

Derivation hint: Apply Jensen's inequality to the convex function −log⁡-\log with the expectation taken under PP; strict convexity gives the equality condition.

Why It Holds / Derivation

ProofNon-negativity via Jensen's Inequality

We prove [4].

Step 1. Write the divergence as a negative expectation:

5
−DKL(P∥Q)=∑xp(x)log⁡q(x)p(x)=EP ⁣[log⁡q(X)p(X)]\quad -D_{\mathrm{KL}}(P\|Q)=\sum_x p(x)\log\frac{q(x)}{p(x)}=\mathbb{E}_{P}\!\left[\log\frac{q(X)}{p(X)}\right]

Derivation hint: Flip the ratio inside the log, which negates the whole expression by log⁡(a/b)=−log⁡(b/a)\log(a/b)=-\log(b/a).

Step 2. The function log⁡\log is concave, so Jensen's inequality E[log⁡Z]≤log⁡E[Z]\mathbb{E}[\log Z]\le \log \mathbb{E}[Z] applies with Z=q(X)/p(X)Z=q(X)/p(X):

6
EP ⁣[log⁡q(X)p(X)]≤log⁡EP ⁣[q(X)p(X)]\quad \mathbb{E}_{P}\!\left[\log\frac{q(X)}{p(X)}\right]\le \log \mathbb{E}_{P}\!\left[\frac{q(X)}{p(X)}\right]

Derivation hint: Jensen for a concave φ\varphi states E[φ(Z)]≤φ(E[Z])\mathbb{E}[\varphi(Z)]\le \varphi(\mathbb{E}[Z]).

Step 3. Evaluate the inner expectation (the pp's cancel):

7
EP ⁣[q(X)p(X)]=∑xp(x)q(x)p(x)=∑xq(x)≤1\quad \mathbb{E}_{P}\!\left[\frac{q(X)}{p(X)}\right]=\sum_x p(x)\frac{q(x)}{p(x)}=\sum_x q(x)\le 1

Derivation hint: Each q(x)q(x) summand survives; the total mass of QQ is at most 1 (exactly 1 if QQ is a genuine distribution over the same support).

Step 4. Therefore −DKL(P∥Q)≤log⁡1=0-D_{\mathrm{KL}}(P\|Q)\le \log 1 = 0, i.e. DKL(P∥Q)≥0D_{\mathrm{KL}}(P\|Q)\ge 0.

Step 5 (equality). Since log⁡\log is strictly concave, Jensen is an equality only if Z=q(X)/p(X)Z=q(X)/p(X) is constant PP-almost-surely. Combined with ∑q=1\sum q=1, this constant must be 1, forcing p(x)=q(x)p(x)=q(x) everywhere PP has mass. ■\blacksquare

∎
mathematician · 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 PP over QQ — which links directly to the Neyman–Pearson Lemma and the theory of Sufficient Statistics.

Building Intuition

🧠 Intuition

Imagine you must design a code to compress messages whose letters truly follow distribution PP. The optimal code (Shannon's source coding) assigns roughly −log⁡2q(x)-\log_2 q(x) bits to letter xx if you believe the distribution is QQ. The true expected code length is then ∑xp(x) (−log⁡2q(x))\sum_x p(x)\,(-\log_2 q(x)), the cross-entropy H(P,Q)H(P,Q). The unavoidable minimum is the entropy H(P)=∑xp(x)(−log⁡2p(x))H(P)=\sum_x p(x)(-\log_2 p(x)). KL divergence is exactly the excess:

8
DKL(P∥Q)=H(P,Q)−H(P)\quad D_{\mathrm{KL}}(P\|Q)=H(P,Q)-H(P)

Derivation hint: Expand log⁡(p/q)=log⁡p−log⁡q\log(p/q)=\log p-\log q inside the sum and recognize the two entropy expressions. 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.

Figure
KL divergence as the gap between cross-entropy and entropy, and a mismatched density pair
KL Divergence = Cross-Entropy − Entropy x density P (true) Q (model) D_KL(P‖Q) = H(P,Q) − H(P) ≥ 0 ; wasted bits using Q's codebook on P-data

Worked Examples

ExampleTwo Coins — Discrete KL in Bits

Given: True coin P=(pH,pT)=(0.5,0.5)P=(p_{\text{H}},p_{\text{T}})=(0.5,0.5). Model coin Q=(0.25,0.75)Q=(0.25,0.75). Compute DKL(P∥Q)D_{\mathrm{KL}}(P\|Q) in bits (base-2 log).

Step 1. Apply [1]:

DKL(P∥Q)=0.5log⁡20.50.25+0.5log⁡20.50.75D_{\mathrm{KL}}(P\|Q)=0.5\log_2\frac{0.5}{0.25}+0.5\log_2\frac{0.5}{0.75}

Step 2. Evaluate each term: log⁡2(0.5/0.25)=log⁡22=1\log_2(0.5/0.25)=\log_2 2 = 1, so first term =0.5×1=0.5=0.5\times 1=0.5. log⁡2(0.5/0.75)=log⁡2(0.6667)=−0.585\log_2(0.5/0.75)=\log_2(0.6667)=-0.585, so second term =0.5×(−0.585)=−0.2925=0.5\times(-0.585)=-0.2925.

Step 3. Sum: 0.5−0.2925=0.20750.5-0.2925=0.2075.

DKL(P∥Q)≈0.2075 bits\boxed{D_{\mathrm{KL}}(P\|Q)\approx 0.2075\ \text{bits}}

Asymmetry check: Reversing, DKL(Q∥P)=0.25log⁡20.250.5+0.75log⁡20.750.5=0.25(−1)+0.75(0.585)=−0.25+0.4387=0.1887 bitsD_{\mathrm{KL}}(Q\|P)=0.25\log_2\frac{0.25}{0.5}+0.75\log_2\frac{0.75}{0.5}=0.25(-1)+0.75(0.585)=-0.25+0.4387=\boxed{0.1887\ \text{bits}}. Different value — confirming asymmetry.

ExampleTwo Gaussians — Continuous KL in Nats

Given: P=N(μ1,σ12)P=\mathcal{N}(\mu_1,\sigma_1^2) and Q=N(μ2,σ22)Q=\mathcal{N}(\mu_2,\sigma_2^2). The closed-form result is

9
DKL(P∥Q)=log⁡σ2σ1+σ12+(μ1−μ2)22σ22−12\quad D_{\mathrm{KL}}(P\|Q)=\log\frac{\sigma_2}{\sigma_1}+\frac{\sigma_1^2+(\mu_1-\mu_2)^2}{2\sigma_2^2}-\frac12

Derivation hint: Plug the two Gaussian densities into [2], expand the log into quadratic terms, and use EP[(X−μ1)2]=σ12\mathbb{E}_P[(X-\mu_1)^2]=\sigma_1^2 and EP[X]=μ1\mathbb{E}_P[X]=\mu_1.

Numbers: Let μ1=0,σ1=1\mu_1=0,\sigma_1=1 (standard normal) and μ2=1,σ2=2\mu_2=1,\sigma_2=2.

Step 1. log⁡(σ2/σ1)=log⁡2=0.6931\log(\sigma_2/\sigma_1)=\log 2=0.6931 nats.

Step 2. Numerator σ12+(μ1−μ2)2=1+(0−1)2=2\sigma_1^2+(\mu_1-\mu_2)^2=1+(0-1)^2=2. Denominator 2σ22=2(4)=82\sigma_2^2=2(4)=8. Ratio =2/8=0.25=2/8=0.25.

Step 3. Subtract 12\tfrac12: total =0.6931+0.25−0.5=0.6931+0.25-0.5.

DKL(P∥Q)≈0.4431 nats\boxed{D_{\mathrm{KL}}(P\|Q)\approx 0.4431\ \text{nats}}

(Multiply by 1/ln⁡2≈1.44271/\ln 2\approx1.4427 to convert to bits: ≈0.639\approx0.639 bits.)

Common Misconceptions

⚠️ Common mistake

KL divergence is NOT a distance metric. It fails two of the three metric axioms: 1. Asymmetry: DKL(P∥Q)≠DKL(Q∥P)D_{\mathrm{KL}}(P\|Q)\neq D_{\mathrm{KL}}(Q\|P) in general (see Example 1). 2. Triangle inequality fails: there is no general DKL(P∥R)≤DKL(P∥Q)+DKL(Q∥R)D_{\mathrm{KL}}(P\|R)\le D_{\mathrm{KL}}(P\|Q)+D_{\mathrm{KL}}(Q\|R). It does satisfy non-negativity and the identity-of-indiscernibles. To get a symmetric, metric-like quantity, use the Jensen–Shannon Divergence (whose square root is a metric) or symmetrize as DKL(P∥Q)+DKL(Q∥P)D_{\mathrm{KL}}(P\|Q)+D_{\mathrm{KL}}(Q\|P) (still not a metric).

CounterexampleInfinite Divergence from Support Mismatch

Let PP be uniform on {1,2,3}\{1,2,3\} and QQ uniform on {1,2}\{1,2\}. Then q(3)=0q(3)=0 but p(3)=13>0p(3)=\tfrac13>0, so the term 13log⁡1/30=+∞\tfrac13\log\frac{1/3}{0}=+\infty. Hence

DKL(P∥Q)=+∞.D_{\mathrm{KL}}(P\|Q)=+\infty.

Yet DKL(Q∥P)D_{\mathrm{KL}}(Q\|P) is finite: QQ assigns no mass to {3}\{3\}, so no division-by-zero occurs, and DKL(Q∥P)=12log⁡1/21/3+12log⁡1/21/3=log⁡32≈0.405D_{\mathrm{KL}}(Q\|P)=\tfrac12\log\frac{1/2}{1/3}+\tfrac12\log\frac{1/2}{1/3}=\log\frac{3}{2}\approx0.405 nats. This dramatic asymmetry shows why the direction of KL matters: DKL(P∥Q)D_{\mathrm{KL}}(P\|Q) harshly punishes QQ for placing zero mass where PP has support, while the reverse is forgiving.

Connections

connection

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: I(X;Y)=DKL(PXY∥PXPY)I(X;Y)=D_{\mathrm{KL}}(P_{XY}\|P_X P_Y). Cross-Entropy equals H(P)+DKL(P∥Q)H(P)+D_{\mathrm{KL}}(P\|Q), which is why minimizing cross-entropy loss is exactly minimizing KL to the data distribution. Minimizing DKL(Pdata∥Pθ)D_{\mathrm{KL}}(P_{\text{data}}\|P_\theta) 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.

TheoremKL and Fisher Information — Local Structure

For a smooth parametric family {Pθ}\{P_\theta\}, the KL divergence between nearby parameters is, to second order,

10
DKL(Pθ∥Pθ+dθ)=12 dθ⊤I(θ) dθ+O(∥dθ∥3)\quad D_{\mathrm{KL}}(P_\theta\|P_{\theta+d\theta})=\tfrac12\, d\theta^{\top} \mathcal{I}(\theta)\, d\theta + O(\|d\theta\|^3)

Derivation hint: Taylor-expand log⁡pθpθ+dθ\log\frac{p_\theta}{p_{\theta+d\theta}} to second order; the first-order term vanishes (score has zero mean) and the second-order term is the Fisher information matrix I(θ)=E[∇θlog⁡p ∇θlog⁡p⊤]\mathcal{I}(\theta)=\mathbb{E}[\nabla_\theta\log p\,\nabla_\theta\log p^{\top}]. This shows KL is locally a (Riemannian) squared distance, even though it is globally asymmetric.

Real-World Applications

🔬 Where this appears

Historical Development

⏳ History
- 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.

Subtleties & Limits

open-problem

Unboundedness and estimation. KL divergence can be infinite under support mismatch, which destabilizes training of generative models (motivating Wasserstein distances in GANs, which remain finite). Estimating KL from samples in high dimensions is notoriously hard: nonparametric estimators suffer the curse of dimensionality and have high variance, and there is no unbiased estimator of DKLD_{\mathrm{KL}} from finite samples in general. The choice of forward (DKL(P∥Q)D_{\mathrm{KL}}(P\|Q), "mean-seeking," covers all modes of PP) versus reverse (DKL(Q∥P)D_{\mathrm{KL}}(Q\|P), "mode-seeking," collapses onto one mode) divergence remains an active design question in variational inference, with no universally optimal answer. Generalizations such as ff-divergences, Rényi divergences, and integral probability metrics each repair different shortcomings, but no single divergence is simultaneously symmetric, metric, finite, and easily estimable.

See Also

Mutual Information · Cross-Entropy · Shannon Entropy · Jensen–Shannon Divergence · Fisher Information Matrix · Maximum Likelihood Estimation · Variational Inference · Jensen's Inequality · Wasserstein Distance