Foundations to Frontier
Tests
A. Mathematical Foundations › A7 Information Theory

KL Divergence: Properties, Forward vs Reverse

The Kullback–Leibler divergence measures how badly one distribution approximates another: the expected extra bits paid for coding p-data with a q-code. It is non-negative, zero only when p = q, asymmetric, and infinite when q misses p's support. You will prove these properties, derive closed forms for Gaussians, use the chain rule, estimate KL by Monte Carlo, contrast mass-covering forward KL with mode-seeking reverse KL, and see KL as the rate at which evidence accumulates in a change detector for spacecraft sensors.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

How wrong is my map?

Your map says the river is to the east; reality says it’s to the north. How wrong are you? The KL divergence measures the average extra surprise you’ll experience if you believe qq but the world actually behaves like pp. It’s zero if your beliefs are right. It’s huge, even infinite, if you’re sure something can’t happen and then it does.

Unlike ordinary distance, it depends on direction: “how wrong is q about p” differs from “how wrong is p about q”.

KL divergence: a distance that depends on direction
p (shaded) · q (orange)integrand of KL(p‖q) (area = the divergence)
KL(p‖q) = 2.624 nats · KL(q‖p) = 1.073 nats (different!)
KL(p‖q) = ∫ p log(p/q) averages over p. It explodes where p has mass but q is tiny, so it punishes q for missing any part of p (mass-covering). KL(q‖p) averages over q and punishes q for putting mass where p is tiny (mode-seeking). With a two-mode p, try a narrow q on one mode: KL(q‖p) is small while KL(p‖q) is large. The integrand can be negative locally, but the total is always ≥ 0, and zero only when p = q.
Why engineers care

KL divergence is the loss behind maximum likelihood and variational inference, the trust-region constraint in PPO/TRPO, the drift measure in fault detection, and the regularizer in VAEs. Choosing the direction (forward or reverse) changes what an approximation gets right.

📝 Quick check (Level 0)
0/3 answered
  1. 1. KL(p ‖ q) is zero when…
  2. 2. Is KL(p ‖ q) always equal to KL(q ‖ p)?
  3. 3. If q says an event is impossible but p says it can happen, KL(p ‖ q) is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Definition

KL⁡(p ∥ q)=∑xp(x)log⁡p(x)q(x)=Ep[log⁡pq]=H(p,q)−H(p)\operatorname{KL}(p\,\|\,q) = \sum_xp(x)\log\frac{p(x)}{q(x)} = \mathbb E_p\Big[\log\frac pq\Big] = H(p, q) - H(p)

For densities, replace the sum by an integral.

Properties

  • Non-negative: KL⁡≥0\operatorname{KL}\ge0, with equality iff p=qp = q (Gibbs).
  • Asymmetric: KL⁡(p ∥ q)≠KL⁡(q ∥ p)\operatorname{KL}(p\,\|\,q)\ne\operatorname{KL}(q\,\|\,p) in general. It’s not a metric, and there’s no triangle inequality.
  • Infinite if q(x)=0q(x) = 0 where p(x)>0p(x) > 0.
  • Invariant under invertible reparameterizations of xx (unlike differential entropy).

Gaussians

KL⁡(N(μ0,σ02) ∥ N(μ1,σ12))=log⁡σ1σ0+σ02+(μ0−μ1)22σ12−12\operatorname{KL}(\mathcal N(\mu_0, \sigma_0^2)\,\|\,\mathcal N(\mu_1, \sigma_1^2)) = \log\frac{\sigma_1}{\sigma_0} + \frac{\sigma_0^2 + (\mu_0 - \mu_1)^2}{2\sigma_1^2} - \frac12

For a pure mean shift δ\delta with equal variances: δ2/(2σ2)\delta^2/(2\sigma^2).

📝 Quick check (Level 1)
0/3 answered
  1. 1. KL(N(1, 1) ‖ N(0, 1)) =
  2. 2. KL(p ‖ q) = H(p, q) − …
  3. 3. KL satisfies the triangle inequality?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

Forward vs reverse KL

When fitting an approximation qθq_\theta to a target pp:

  • Forward min⁡θKL⁡(p ∥ qθ)\min_\theta\operatorname{KL}(p\,\|\,q_\theta) is mass-covering. qq must put probability everywhere pp does. It equals maximum likelihood on samples from pp. For exponential families it gives moment matching.
  • Reverse min⁡θKL⁡(qθ ∥ p)\min_\theta\operatorname{KL}(q_\theta\,\|\,p) is mode-seeking. qq avoids regions where pp is small and may lock onto one mode. It only needs pp up to a constant, which is why variational inference uses it (A5.2-9).

Chain rule and convexity

KL⁡(pXY ∥ qXY)=KL⁡(pX ∥ qX)+EpXKL⁡(pY∣X ∥ qY∣X)\operatorname{KL}(p_{XY}\,\|\,q_{XY}) = \operatorname{KL}(p_X\,\|\,q_X) + \mathbb E_{p_X}\operatorname{KL}(p_{Y\mid X}\,\|\,q_{Y\mid X})

KL is jointly convex in (p,q)(p, q). For Markov chains and time series, KL decomposes into a sum of per-step conditional KLs.

Estimating KL

  • Monte Carlo: KL⁡≈1n∑log⁡p(xi)q(xi)\operatorname{KL}\approx\frac1n\sum\log\frac{p(x_i)}{q(x_i)} with xi∼px_i\sim p, when both densities can be evaluated.
  • From samples only: kk-NN estimators, or density-ratio and classifier-based estimators.
🐍 Exercise 1: KL properties and estimators
Implement discrete KL (with infinite cases), multivariate Gaussian KL and a Monte Carlo estimator. Verify non-negativity and asymmetry, the support condition, the chain rule, and agreement between Monte Carlo and the closed form.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Maximum likelihood minimizes…
  2. 2. Variational inference typically minimizes reverse KL because…
  3. 3. The KL between two joints decomposes into…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Proof of non-negativity (Gibbs’ inequality)

By Jensen with the convex −log⁡-\log: KL⁡(p ∥ q)=Ep[−log⁡(q/p)]≥−log⁡Ep[q/p]=−log⁡∑x:p>0q(x)≥−log⁡1=0\operatorname{KL}(p\,\|\,q) = \mathbb E_p[-\log(q/p)]\ge-\log\mathbb E_p[q/p] = -\log\sum_{x:p>0}q(x)\ge-\log1 = 0. Equality requires q/pq/p to be constant on the support, i.e. q=pq = p. ∎

Deriving the Gaussian KL (1-D)

log⁡p0p1=log⁡σ1σ0−(x−μ0)22σ02+(x−μ1)22σ12\log\frac{p_0}{p_1} = \log\frac{\sigma_1}{\sigma_0} - \frac{(x - \mu_0)^2}{2\sigma_0^2} + \frac{(x - \mu_1)^2}{2\sigma_1^2}. Under p0p_0, E(x−μ0)2=σ02\mathbb E(x - \mu_0)^2 = \sigma_0^2 and E(x−μ1)2=σ02+(μ0−μ1)2\mathbb E(x - \mu_1)^2 = \sigma_0^2 + (\mu_0 - \mu_1)^2. Substitute. ∎

Chain rule

log⁡p(x,y)q(x,y)=log⁡p(x)q(x)+log⁡p(y∣x)q(y∣x)\log\frac{p(x, y)}{q(x, y)} = \log\frac{p(x)}{q(x)} + \log\frac{p(y\mid x)}{q(y\mid x)}. Take Ep\mathbb E_p. ∎

Exercise 3.1: KL(N(0, 4) ‖ N(0, 1))

log⁡12+42−12=−0.693+1.5=0.807\log\frac12 + \frac42 - \frac12 = -0.693 + 1.5 = 0.807 nats. The reverse, KL⁡(N(0,1) ∥ N(0,4))=log⁡2+18−12=0.318\operatorname{KL}(\mathcal N(0, 1)\,\|\,\mathcal N(0, 4)) = \log2 + \frac18 - \frac12 = 0.318, is different.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Gibbs' inequality is proved with…
  2. 2. KL(N(0, 4) ‖ N(0, 1)) ≈
  3. 3. KL is invariant under…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Detecting a gyro bias jump: KL as evidence rate

A gyro’s bias suddenly shifts by δ\delta. The CUSUM detector accumulates the log-likelihood ratio log⁡p1(x)p0(x)\log\frac{p_1(x)}{p_0(x)}, resets at zero, and alarms when the sum exceeds hh. After the change, each sample adds on average Ep1log⁡p1p0=KL⁡(p1 ∥ p0)=δ2/(2σ2)\mathbb E_{p_1}\log\frac{p_1}{p_0} = \operatorname{KL}(p_1\,\|\,p_0) = \delta^2/(2\sigma^2). So the mean detection delay is about h/KL⁡h/\operatorname{KL} (Lorden, 1971). Halving the shift quarters the KL and quadruples the delay. Simulations match h/KL⁡h/\operatorname{KL} closely.

🐍 Exercise 2 (research): CUSUM detection delay vs KL
Simulate CUSUM detection of gyro bias jumps of different sizes. Compare the mean detection delay with h/KL and check the inverse-square dependence on the shift.

KL divergence in AI, robotics and aerospace

  • Fault detection and integrity monitoring: CUSUM and GLR tests for navigation-sensor faults and GNSS integrity (Basseville and Nikiforov, 1993).
  • Policy optimization: TRPO constrains the KL between successive policies; PPO penalizes or clips it (Schulman et al., 2015, 2017).
  • VAEs and variational inference: the KL term regularizes the latent posterior toward the prior (Kingma and Welling, 2014).
  • Knowledge distillation: train a small on-board model to match a large model’s predictive distribution (Hinton, Vinyals and Dean, 2015).

References

  1. S. Kullback, R. Leibler (1951). “On information and sufficiency.” Annals of Mathematical Statistics, 22(1).
  2. T. Cover, J. Thomas (2006). Elements of Information Theory, 2nd ed. Wiley. Ch. 2, 11.
  3. G. Lorden (1971). “Procedures for reacting to a change in distribution.” Annals of Mathematical Statistics, 42(6).
  4. E. S. Page (1954). “Continuous inspection schemes.” Biometrika, 41 (CUSUM).
  5. M. Basseville, I. Nikiforov (1993). Detection of Abrupt Changes: Theory and Application. Prentice Hall.
  6. T. Minka (2005). “Divergence measures and message passing.” Microsoft Research Technical Report MSR-TR-2005-173.
  7. J. Schulman, S. Levine, P. Moritz, M. Jordan, P. Abbeel (2015). “Trust region policy optimization.” ICML.
  8. D. Kingma, M. Welling (2014). “Auto-encoding variational Bayes.” ICLR.
  9. G. Hinton, O. Vinyals, J. Dean (2015). “Distilling the knowledge in a neural network.” arXiv:1503.02531.
  10. Q. Wang, S. Kulkarni, S. Verdú (2009). “Divergence estimation for multidimensional densities via k-nearest-neighbor distances.” IEEE Transactions on Information Theory, 55(5).