Algorithm Search⌘K

Definitioncore

Kullback-Leibler divergence

Also called KL divergence, relative entropy, information divergence.

The expected log ratio of two densities under the first one. It is never negative, is zero only for equal laws, is not symmetric, and the MLE minimizes its empirical version.

The Kullback-Leibler divergence D(P ∥ Q)D(P \,\|\, Q) measures how much a probability law QQ differs from a law PP, seen from the side of PP. It is the expected log likelihood ratio of PP to QQ when the data come from PP. It is zero only when the two laws are equal and is positive otherwise, but it is not symmetric and it does not obey the triangle inequality, so it is not a distance. In statistics it is the target that maximum likelihood estimates: the MLE minimizes an empirical version of D(P ∥ Qθ)D(P \,\|\, Q_\theta) over the model, and its local curvature is the Fisher information.

Definition

Let PP and QQ be probability laws on the same space.

Discrete case. If PP and QQ have probability mass functions pp and qq on a countable set,

D(P ∥ Q)=∑xp(x) log⁡p(x)q(x),D(P \,\|\, Q) = \sum_{x} p(x)\,\log\frac{p(x)}{q(x)},

with the conventions 0log⁡(0/q)=00\log(0/q) = 0 for every q≥0q \ge 0 and plog⁡(p/0)=+∞p\log(p/0) = +\infty for p>0p > 0.

Densities. If PP and QQ have densities pp and qq with respect to a common measure μ\mu (Lebesgue measure, counting measure, or μ=P+Q\mu = P + Q),

D(P ∥ Q)=∫p(x) log⁡p(x)q(x) μ(dx)=EP ⁣[log⁡p(X)q(X)].D(P \,\|\, Q) = \int p(x)\,\log\frac{p(x)}{q(x)}\,\mu(dx) = \mathrm E_P\!\left[\log\frac{p(X)}{q(X)}\right].

General case. If PP is absolutely continuous with respect to QQ (every event with Q(A)=0Q(A) = 0 also has P(A)=0P(A) = 0), then D(P ∥ Q)=EP[log⁡(dP/dQ)]D(P \,\|\, Q) = \mathrm E_P[\log (dP/dQ)], where dP/dQdP/dQ is the Radon-Nikodym derivative. Otherwise D(P ∥ Q)=+∞D(P \,\|\, Q) = +\infty.

The value lies in [0,+∞][0, +\infty]. With the natural log the unit is the nat; with log⁡2\log_2 it is the bit. For a parametric family f(x;θ)f(x; \theta) we write D(θ0 ∥ θ)D(\theta_0 \,\|\, \theta) for D(f(⋅;θ0) ∥ f(⋅;θ))D(f(\cdot; \theta_0) \,\|\, f(\cdot; \theta)).

Properties

Gibbs inequality. D(P ∥ Q)≥0D(P \,\|\, Q) \ge 0, with equality if and only if P=QP = Q.

Proof. It suffices to treat the case where D(P ∥ Q)D(P \,\|\, Q) is finite, so q(x)>0q(x) > 0 wherever p(x)>0p(x) > 0. Let A={x:p(x)>0}A = \{x : p(x) > 0\}. For u>0u > 0, log⁡u≤u−1\log u \le u - 1, with equality only at u=1u = 1. Put u=q(x)/p(x)u = q(x)/p(x) for x∈Ax \in A:

−D(P ∥ Q)=∫Ap log⁡qp dμ≤∫Ap(qp−1)dμ=Q(A)−1≤0.-D(P \,\|\, Q) = \int_A p\,\log\frac{q}{p}\,d\mu \le \int_A p\left(\frac{q}{p} - 1\right)d\mu = Q(A) - 1 \le 0.

Equality in the first step needs q=pq = p almost everywhere on AA, and equality in the second needs Q(A)=1Q(A) = 1. Together they give q=pq = p almost everywhere, so P=QP = Q. ■\blacksquare

The integral in the first line is well defined, because its integrand is bounded above by the integrable function q−pq - p.

Not symmetric. In general D(P ∥ Q)≠D(Q ∥ P)D(P \,\|\, Q) \ne D(Q \,\|\, P); one can be finite while the other is infinite. The symmetric sum D(P ∥ Q)+D(Q ∥ P)D(P \,\|\, Q) + D(Q \,\|\, P) is what Kullback and Leibler called the divergence in 1951; today "KL divergence" means the one-sided quantity.

Additive over independent coordinates. If P=P1×P2P = P_1 \times P_2 and Q=Q1×Q2Q = Q_1 \times Q_2, then D(P ∥ Q)=D(P1 ∥ Q1)+D(P2 ∥ Q2)D(P \,\|\, Q) = D(P_1 \,\|\, Q_1) + D(P_2 \,\|\, Q_2), because the log of a product is a sum. For nn iid observations, D(Pn ∥ Qn)=n D(P ∥ Q)D(P^n \,\|\, Q^n) = n\,D(P \,\|\, Q).

Local form. If the family f(x;θ)f(x; \theta) satisfies the regularity conditions of Fisher information (enough smoothness to differentiate twice under the integral sign), then

D(θ ∥ θ+h)=12 hTI(θ) h+o(∥h∥2),D(\theta \,\|\, \theta + h) = \tfrac12\,h^{\mathsf T} I(\theta)\,h + o(\|h\|^2),

where I(θ)I(\theta) is the Fisher information of one observation. The first-order term vanishes because the score has mean zero, and the second-order term is the mean of minus the Hessian of log⁡f\log f.

Pinsker's inequality. The total variation distance satisfies sup⁡A∣P(A)−Q(A)∣≤D(P ∥ Q)/2\sup_A |P(A) - Q(A)| \le \sqrt{D(P \,\|\, Q)/2}, with DD in nats (Cover and Thomas, Section 11.6). So a small divergence forces the two laws to give nearly equal probabilities to every event.

Entropy and cross entropy. For discrete laws, D(P ∥ Q)=H(P,Q)−H(P)D(P \,\|\, Q) = H(P, Q) - H(P), where H(P)=−∑plog⁡pH(P) = -\sum p\log p is the Shannon entropy and H(P,Q)=−∑plog⁡qH(P, Q) = -\sum p\log q is the cross entropy. Minimizing cross-entropy loss over QQ with PP fixed is the same as minimizing D(P ∥ Q)D(P \,\|\, Q).

Maximum likelihood minimizes an empirical divergence. Let X1,…,XnX_1, \ldots, X_n be iid from a law with density gg, and let {f(⋅;θ)}\{f(\cdot; \theta)\} be a model that may not contain gg. Then

D(g ∥ fθ)=Eg[log⁡g(X)]−Eg[log⁡f(X;θ)].D(g \,\|\, f_\theta) = \mathrm E_g[\log g(X)] - \mathrm E_g[\log f(X; \theta)].

The first term does not depend on θ\theta, so minimizing D(g ∥ fθ)D(g \,\|\, f_\theta) is the same as maximizing Eg[log⁡f(X;θ)]\mathrm E_g[\log f(X; \theta)], and the MLE maximizes the sample mean n−1∑log⁡f(Xi;θ)n^{-1}\sum \log f(X_i; \theta) of that expectation. Under regularity conditions the MLE converges almost surely to the minimizer θ∗\theta^* of D(g ∥ fθ)D(g \,\|\, f_\theta) (White 1982). When the model is correct, θ∗=θ0\theta^* = \theta_0 by the Gibbs inequality, which is the core of consistency proofs for the MLE.

Examples and non-examples

Two Bernoulli laws. D(Bern(0.5) ∥ Bern(0.1))=0.5log⁡5+0.5log⁡(5/9)=log⁡(5/3)=0.51083D(\mathrm{Bern}(0.5) \,\|\, \mathrm{Bern}(0.1)) = 0.5\log 5 + 0.5\log(5/9) = \log(5/3) = 0.51083 nats, while D(Bern(0.1) ∥ Bern(0.5))=0.36806D(\mathrm{Bern}(0.1) \,\|\, \mathrm{Bern}(0.5)) = 0.36806 nats. The two directions differ.

An exact asymmetric example in bits. On four points let p=(1/2,1/4,1/8,1/8)p = (1/2, 1/4, 1/8, 1/8) and q=(1/8,1/2,1/4,1/8)q = (1/8, 1/2, 1/4, 1/8). Every ratio is a power of 2, so the logs in base 2 are integers and the sums are exact rationals:

D(p ∥ q)=12⋅2+14⋅(−1)+18⋅(−1)+18⋅0=58 bit,D(q ∥ p)=18⋅(−2)+12⋅1+14⋅1+18⋅0=12 bit.D(p \,\|\, q) = \tfrac12\cdot 2 + \tfrac14\cdot(-1) + \tfrac18\cdot(-1) + \tfrac18\cdot 0 = \tfrac58 \text{ bit}, \qquad D(q \,\|\, p) = \tfrac18\cdot(-2) + \tfrac12\cdot 1 + \tfrac14\cdot 1 + \tfrac18\cdot 0 = \tfrac12 \text{ bit}.

One direction infinite. Let p=(1/2,1/2,0)p = (1/2, 1/2, 0) and q=(1/4,1/4,1/2)q = (1/4, 1/4, 1/2). Then D(p ∥ q)=1D(p \,\|\, q) = 1 bit, but D(q ∥ p)=+∞D(q \,\|\, p) = +\infty, because qq puts mass 1/21/2 on a point where pp is zero. A model that gives probability zero to an event that can happen is infinitely far from the truth in this direction.

Normal laws. For N(μ1,σ12)N(\mu_1, \sigma_1^2) and N(μ2,σ22)N(\mu_2, \sigma_2^2),

D=log⁡σ2σ1+σ12+(μ1−μ2)22σ22−12.D = \log\frac{\sigma_2}{\sigma_1} + \frac{\sigma_1^2 + (\mu_1 - \mu_2)^2}{2\sigma_2^2} - \frac12.

With equal variances σ2\sigma^2 this is (μ1−μ2)2/(2σ2)(\mu_1 - \mu_2)^2/(2\sigma^2), which is exactly 12h2I(μ)\tfrac12 h^2 I(\mu) with h=μ2−μ1h = \mu_2 - \mu_1 and I=1/σ2I = 1/\sigma^2. The local form is exact here.

Local form for a Bernoulli law. For Bern(0.3)\mathrm{Bern}(0.3) the Fisher information is 1/(0.3⋅0.7)1/(0.3 \cdot 0.7). At h=0.1h = 0.1, D(Bern(0.3) ∥ Bern(0.4))=0.021601D(\mathrm{Bern}(0.3) \,\|\, \mathrm{Bern}(0.4)) = 0.021601 nats against 12h2I=0.023810\tfrac12 h^2 I = 0.023810; at h=0.01h = 0.01 the values are 0.00023517 and 0.00023810.

Pinsker's inequality. For P=Bern(0.5)P = \mathrm{Bern}(0.5) and Q=Bern(0.75)Q = \mathrm{Bern}(0.75), D(P ∥ Q)=0.143841D(P \,\|\, Q) = 0.143841 nats and the total variation distance is 0.25, below the bound 0.143841/2=0.26818\sqrt{0.143841/2} = 0.26818.

A misspecified model. Let the data come from a Gamma law with shape 2 and rate 1 (mean 2), and fit the exponential model f(x;λ)=λe−λxf(x; \lambda) = \lambda e^{-\lambda x}. Then E[log⁡f(X;λ)]=log⁡λ−2λ\mathrm E[\log f(X; \lambda)] = \log\lambda - 2\lambda, which is largest at λ∗=1/2\lambda^* = 1/2. The exponential MLE 1/Xˉ1/\bar X converges almost surely to 1/21/2, the rate of the exponential law closest to the Gamma law in Kullback-Leibler divergence.

Non-example: not a metric. The divergence is not symmetric (see above), and it does not satisfy the triangle inequality. For Bernoulli laws with success probabilities 0.10.1, 0.50.5 and 0.90.9, D(0.1 ∥ 0.5)+D(0.5 ∥ 0.9)=0.36806+0.51083=0.87889D(0.1 \,\|\, 0.5) + D(0.5 \,\|\, 0.9) = 0.36806 + 0.51083 = 0.87889, but D(0.1 ∥ 0.9)=0.8log⁡9=1.75778D(0.1 \,\|\, 0.9) = 0.8\log 9 = 1.75778 nats.

Non-example: a difference of entropies. D(P ∥ Q)D(P \,\|\, Q) is not H(Q)−H(P)H(Q) - H(P). Two laws with the same entropy, such as pp and a permutation of pp, have equal entropies but positive divergence when the permutation moves mass.

References

  1. S. Kullback and R. A. Leibler, On information and sufficiency, Annals of Mathematical Statistics 22(1), 1951, pages 79 to 86 (definition of the mean information for discrimination and the divergence, and their properties)
  2. Thomas M. Cover and Joy A. Thomas, Elements of Information Theory, second edition, Wiley, 2006, Chapter 2, Section 2.3 (relative entropy), Section 2.5 (chain rule) and Theorem 2.6.3 (information inequality); Chapter 11, Section 11.6 (Pinsker's inequality)
  3. H. White, Maximum likelihood estimation of misspecified models, Econometrica 50(1), 1982, pages 1 to 25 (the quasi-maximum likelihood estimator converges to the minimizer of the Kullback-Leibler divergence)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/51K26E/text.yaml