KL Divergence

Volume I, Chapter 3 — Part IV. Kullback-Leibler divergence from cross-entropy and entropy, Gibbs' inequality proof, asymmetry and forward vs reverse KL, multivariate Gaussian closed form, and applications in VAEs, diffusion models, and RLHF.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Prerequisites
  3. Notation
  4. Core Intuition
  5. Cross-Entropy and the Definition of KL Divergence
  6. Step-by-Step Derivation
  7. Gibbs' Inequality: Proof of Non-Negativity
  8. Properties of KL Divergence
  9. Forward KL vs Reverse KL
  10. KL Divergence and Maximum Likelihood
  11. Closed Form for Gaussian Distributions
  12. Conditional and Mutual Information Connections
  13. Connection to Machine Learning
  14. Common Pitfalls and Misconceptions
  15. Research Perspective
  16. Summary of Takeaways
  17. Exercises

Learning Objectives

After reading this chapter, you should be able to:

  1. Derive KL divergence as DKL(PQ)=H(P,Q)H(P)D_{\text{KL}}(P \| Q) = H(P, Q) - H(P) from the definitions of cross-entropy and entropy.
  2. Prove non-negativity (Gibbs' inequality) via Jensen's inequality and identify when equality holds.
  3. Explain why KL divergence is asymmetric and why it is not a metric.
  4. Distinguish forward KL (mean-seeking) from reverse KL (mode-seeking) and predict optimization behavior.
  5. Prove that minimizing cross-entropy is equivalent to minimizing KL divergence and to MLE.
  6. Derive the closed-form KL between multivariate Gaussians.
  7. Identify KL terms in VAEs, diffusion models, variational inference, and RLHF objectives.

Prerequisites


Notation

  • **DKL(P\*Q)D_{\mathrm{KL}}(P \** — Q) — Kullback–Leibler divergence
  • H(P)H(P) — Shannon entropy
  • H(P,Q)H(P, Q) — Cross-entropy
  • p(x),q(x)p(x), q(x) — Probability densities or mass functions
  • EP[]\mathbb{E}_P[\cdot] — Expectation under distribution PP

Core Intuition

How different are two probability distributions PP and QQ? The Kullback–Leibler (KL) divergence answers: If data are generated from PP, how many extra bits per sample do we waste by encoding them with a code optimized for QQ?

KL divergence is not a distance in the geometric sense — it is asymmetric (DKL(PQ)DKL(QP)D_{\text{KL}}(P \| Q) \neq D_{\text{KL}}(Q \| P) in general) and does not satisfy the triangle inequality. Nevertheless, it is the canonical measure of distribution mismatch in machine learning:

  • Training classifiers minimizes DKL(PdataPmodel)D_{\text{KL}}(P_{\text{data}} \| P_{\text{model}}) via cross-entropy.
  • VAEs regularize the encoder with DKL(q(zx)p(z))D_{\text{KL}}(q(\mathbf{z}|\mathbf{x}) \| p(\mathbf{z})).
  • RLHF constrains policy updates with DKL(πθπref)D_{\text{KL}}(\pi_\theta \| \pi_{\text{ref}}).
  • Diffusion model variational bounds decompose into sums of Gaussian KL terms.

This chapter derives KL divergence rigorously, proves its key properties, and connects it to the loss functions used throughout modern AI.

Series context. This is Part IV of Chapter 3 in Volume I. It follows Entropy and precedes Maximum Likelihood Estimation.

Interactive: KL Divergence

P (reference)Q (approximation)

KL(P‖Q)

0.6277

KL(Q‖P)

1.3256

Key: KL is asymmetric — KL(P‖Q) ≠ KL(Q‖P). Make them identical to see KL→0. In VAE/VI, we minimize KL(q‖p) to approximate the posterior.

Cross-Entropy and the Definition of KL Divergence

Definition 1 (Cross-Entropy — Discrete). For distributions PP and QQ on countable X\mathcal{X}:

H(P,Q)=xXP(x)logQ(x).(1)H(P, Q) = -\sum_{x \in \mathcal{X}} P(x) \log Q(x). \tag{1}

Definition 2 (Entropy — Discrete).

H(P)=xP(x)logP(x).(2)H(P) = -\sum_{x} P(x) \log P(x). \tag{2}

Definition 3 (KL Divergence). The Kullback–Leibler divergence from QQ to PP (equivalently, of PP from QQ) is:

DKL(PQ)=H(P,Q)H(P)=xXP(x)logP(x)Q(x).(3)D_{\text{KL}}(P \| Q) = H(P, Q) - H(P) = \sum_{x \in \mathcal{X}} P(x) \log \frac{P(x)}{Q(x)}. \tag{3}

Convention. When P(x)=0P(x) = 0, the term P(x)logP(x)Q(x)=0P(x) \log \frac{P(x)}{Q(x)} = 0. When P(x)>0P(x) > 0 and Q(x)=0Q(x) = 0, the term is ++\infty (divergence is infinite if QQ fails to cover the support of PP).

Definition 4 (KL Divergence — Continuous). For PDFs pp and qq:

DKL(pq)=p(x)logp(x)q(x)dx.(4)D_{\text{KL}}(p \| q) = \int p(x) \log \frac{p(x)}{q(x)}\, dx. \tag{4}

Interpretation. DKL(PQ)D_{\text{KL}}(P \| Q) is the expected log-likelihood ratio ExP[logP(x)logQ(x)]\mathbb{E}_{x \sim P}[\log P(x) - \log Q(x)] — how much more (or less) log-probability PP assigns to typical samples from PP compared to QQ.


Step-by-Step Derivation

We derive (3) from (1) and (2) with no gaps.

Step 1. Write cross-entropy:

H(P,Q)=xP(x)logQ(x).(5)H(P, Q) = -\sum_x P(x) \log Q(x). \tag{5}

Step 2. Write entropy:

H(P)=xP(x)logP(x).(6)H(P) = -\sum_x P(x) \log P(x). \tag{6}

Step 3. Subtract (6) from (5):

H(P,Q)H(P)=xP(x)logQ(x)+xP(x)logP(x).(7)H(P, Q) - H(P) = -\sum_x P(x) \log Q(x) + \sum_x P(x) \log P(x). \tag{7}

Step 4. Combine the sums (same index set):

=xP(x)[logP(x)logQ(x)]=xP(x)logP(x)Q(x).(8)= \sum_x P(x) [\log P(x) - \log Q(x)] = \sum_x P(x) \log \frac{P(x)}{Q(x)}. \tag{8}

Step 5. By Definition 3, this equals DKL(PQ)D_{\text{KL}}(P \| Q). \blacksquare

Remark. The derivation requires only algebra and the definitions — no additional assumptions.


Gibbs' Inequality: Proof of Non-Negativity

Theorem 1 (Gibbs' Inequality). For discrete PP and QQ:

DKL(PQ)0,(9)D_{\text{KL}}(P \| Q) \geq 0, \tag{9}

with equality if and only if P(x)=Q(x)P(x) = Q(x) for all xx with P(x)>0P(x) > 0.

Proof. Consider DKL(PQ)-D_{\text{KL}}(P \| Q):

DKL(PQ)=xP(x)logP(x)Q(x)=xP(x)logQ(x)P(x)=ExP[logQ(x)P(x)].(10)-D_{\text{KL}}(P \| Q) = -\sum_x P(x) \log \frac{P(x)}{Q(x)} = \sum_x P(x) \log \frac{Q(x)}{P(x)} = \mathbb{E}_{x \sim P}\left[\log \frac{Q(x)}{P(x)}\right]. \tag{10}

The function log()\log(\cdot) is concave. By Jensen's inequality for concave ff:

E[f(Z)]f(E[Z]),(11)\mathbb{E}[f(Z)] \leq f(\mathbb{E}[Z]), \tag{11}

with Z=Q(x)P(x)Z = \frac{Q(x)}{P(x)} and expectation over xPx \sim P:

xP(x)logQ(x)P(x)log(xP(x)Q(x)P(x))=log(xQ(x))=log1=0.(12)\sum_x P(x) \log \frac{Q(x)}{P(x)} \leq \log \left(\sum_x P(x) \cdot \frac{Q(x)}{P(x)}\right) = \log \left(\sum_x Q(x)\right) = \log 1 = 0. \tag{12}

Therefore DKL(PQ)0-D_{\text{KL}}(P \| Q) \leq 0, hence DKL(PQ)0D_{\text{KL}}(P \| Q) \geq 0.

Equality condition. Jensen's inequality is tight iff Z=Q(x)/P(x)Z = Q(x)/P(x) is constant for all xx in the support of PP. Since both PP and QQ sum to 1, the constant must be 1, so P(x)=Q(x)P(x) = Q(x) wherever P(x)>0P(x) > 0. \blacksquare

Theorem 2 (Continuous Case). For PDFs p,qp, q with pp absolutely continuous w.r.t. qq:

DKL(pq)=p(x)logp(x)q(x)dx0,(13)D_{\text{KL}}(p \| q) = \int p(x) \log \frac{p(x)}{q(x)}\, dx \geq 0, \tag{13}

with the same equality condition (a.e.).

Proof. Same Jensen argument with the integral in place of the sum. \blacksquare


Properties of KL Divergence

Property 1 (Non-negativity). DKL(PQ)0D_{\text{KL}}(P \| Q) \geq 0 (Theorem 1).

Property 2 (Identity of indiscernibles). DKL(PQ)=0    P=QD_{\text{KL}}(P \| Q) = 0 \iff P = Q (on the support of PP).

Property 3 (Asymmetry). In general, DKL(PQ)DKL(QP)D_{\text{KL}}(P \| Q) \neq D_{\text{KL}}(Q \| P).

Example. Let P=(0.9,0.1)P = (0.9, 0.1) and Q=(0.5,0.5)Q = (0.5, 0.5) on {0,1}\{0, 1\}:

DKL(PQ)=0.9log0.90.5+0.1log0.10.50.252,(14)D_{\text{KL}}(P \| Q) = 0.9 \log \frac{0.9}{0.5} + 0.1 \log \frac{0.1}{0.5} \approx 0.252, \tag{14} DKL(QP)=0.5log0.50.9+0.5log0.50.10.510.(15)D_{\text{KL}}(Q \| P) = 0.5 \log \frac{0.5}{0.9} + 0.5 \log \frac{0.5}{0.1} \approx 0.510. \tag{15}

Property 4 (Not a metric). KL violates the triangle inequality: there exist P,Q,RP, Q, R with DKL(PR)>DKL(PQ)+DKL(QR)D_{\text{KL}}(P \| R) > D_{\text{KL}}(P \| Q) + D_{\text{KL}}(Q \| R).

Property 5 (Chain rule). For joint distributions:

DKL(P(X,Y)Q(X,Y))=DKL(P(X)Q(X))+DKL(P(YX)Q(YX)),(16)D_{\text{KL}}(P(X,Y) \| Q(X,Y)) = D_{\text{KL}}(P(X) \| Q(X)) + D_{\text{KL}}(P(Y|X) \| Q(Y|X)), \tag{16}

where the second term is ExP(X)[DKL(P(YX=x)Q(YX=x))]\mathbb{E}_{x \sim P(X)}[D_{\text{KL}}(P(Y|X=x) \| Q(Y|X=x))].

Property 6 (Data processing / Invariance). If Y=f(X)Y = f(X) deterministically, then DKL(P(Y)Q(Y))DKL(P(X)Q(X))D_{\text{KL}}(P(Y) \| Q(Y)) \leq D_{\text{KL}}(P(X) \| Q(X)) — processing cannot increase divergence (information processing inequality).

Property 7 (Additivity for independent products). If P=P1×P2P = P_1 \times P_2 and Q=Q1×Q2Q = Q_1 \times Q_2, then:

DKL(PQ)=DKL(P1Q1)+DKL(P2Q2).(17)D_{\text{KL}}(P \| Q) = D_{\text{KL}}(P_1 \| Q_1) + D_{\text{KL}}(P_2 \| Q_2). \tag{17}

Forward KL vs Reverse KL

The asymmetry of KL divergence leads to qualitatively different behavior when used as an optimization objective.

Forward KL: DKL(PQ)D_{\text{KL}}(P \| Q) — Mean-Seeking

Minimize over QQ (approximation) with PP (target) fixed:

minQDKL(PQ)=minQxP(x)logP(x)Q(x).(18)\min_Q D_{\text{KL}}(P \| Q) = \min_Q \sum_x P(x) \log \frac{P(x)}{Q(x)}. \tag{18}

Behavior.

  • If P(x)>0P(x) > 0 and Q(x)0Q(x) \to 0, then logP(x)Q(x)+\log \frac{P(x)}{Q(x)} \to +\infty. So Q(x)>0Q(x) > 0 wherever P(x)>0P(x) > 0QQ must cover all modes of PP.
  • QQ tends to be broader than PP (overdispersed) to avoid missing any mass.
  • Used in expectation propagation, variational inference (ELBO), and MLE (via cross-entropy).

Reverse KL: DKL(QP)D_{\text{KL}}(Q \| P) — Mode-Seeking

Minimize over QQ with PP fixed:

minQDKL(QP)=minQxQ(x)logQ(x)P(x).(19)\min_Q D_{\text{KL}}(Q \| P) = \min_Q \sum_x Q(x) \log \frac{Q(x)}{P(x)}. \tag{19}

Behavior.

  • If Q(x)>0Q(x) > 0 and P(x)=0P(x) = 0, then logQ(x)P(x)+\log \frac{Q(x)}{P(x)} \to +\infty. So Q(x)=0Q(x) = 0 wherever P(x)=0P(x) = 0QQ is supported only where PP is positive.
  • QQ can ignore modes of PP and concentrate on one.
  • Used in RLHF (policy vs reference), some GAN objectives, and mode-seeking variational approximations.

Proposition 1. For multimodal PP, the forward KL minimizer argminQDKL(PQ)\arg\min_Q D_{\text{KL}}(P \| Q) typically covers all modes; the reverse KL minimizer often selects a single mode.


KL Divergence and Maximum Likelihood

Theorem 3 (MLE as KL Minimization). Let p^n(x)=1ni=1nδ(xxi)\hat{p}_n(x) = \frac{1}{n}\sum_{i=1}^n \delta(x - x_i) be the empirical distribution. Then:

θ^MLE=argmaxθ  1ni=1nlogpθ(xi)=argminθ  DKL(p^npθ).(20)\hat{\boldsymbol\theta}_{\text{MLE}} = \underset{\boldsymbol\theta}{\text{argmax}} \; \frac{1}{n}\sum_{i=1}^n \log p_\theta(x_i) = \underset{\boldsymbol\theta}{\text{argmin}} \; D_{\text{KL}}(\hat{p}_n \| p_\theta). \tag{20}

Proof. Expand the KL:

DKL(p^npθ)=xp^n(x)logp^n(x)pθ(x)=H(p^n)1ni=1nlogpθ(xi).(21)D_{\text{KL}}(\hat{p}_n \| p_\theta) = \sum_x \hat{p}_n(x) \log \frac{\hat{p}_n(x)}{p_\theta(x)} = -H(\hat{p}_n) - \frac{1}{n}\sum_{i=1}^n \log p_\theta(x_i). \tag{21}

H(p^n)H(\hat{p}_n) is constant in θ\boldsymbol\theta, so minimizing KL \equiv maximizing ilogpθ(xi)\sum_i \log p_\theta(x_i) \equiv MLE. \blacksquare

Corollary 1 (Cross-Entropy Equivalence). For fixed PP:

argminQ  H(P,Q)=argminQ  DKL(PQ).(22)\underset{Q}{\text{argmin}} \; H(P, Q) = \underset{Q}{\text{argmin}} \; D_{\text{KL}}(P \| Q). \tag{22}

Since H(P,Q)=H(P)+DKL(PQ)H(P, Q) = H(P) + D_{\text{KL}}(P \| Q) and H(P)H(P) is constant w.r.t. QQ.


Closed Form for Gaussian Distributions

Theorem 4 (Univariate Gaussian KL). For 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):

DKL(PQ)=logσ2σ1+σ12+(μ1μ2)22σ2212.(23)D_{\text{KL}}(P \| Q) = \log \frac{\sigma_2}{\sigma_1} + \frac{\sigma_1^2 + (\mu_1 - \mu_2)^2}{2\sigma_2^2} - \frac{1}{2}. \tag{23}

Proof. Starting from (4):

DKL=p(x)[logp(x)logq(x)]dx.(24)D_{\text{KL}} = \int p(x) [\log p(x) - \log q(x)]\, dx. \tag{24}

Substitute Gaussian log-densities:

logp(x)=12log(2πσ12)(xμ1)22σ12,(25)\log p(x) = -\frac{1}{2}\log(2\pi\sigma_1^2) - \frac{(x-\mu_1)^2}{2\sigma_1^2}, \tag{25} logq(x)=12log(2πσ22)(xμ2)22σ22.(26)\log q(x) = -\frac{1}{2}\log(2\pi\sigma_2^2) - \frac{(x-\mu_2)^2}{2\sigma_2^2}. \tag{26}

The difference logp(x)logq(x)\log p(x) - \log q(x) is quadratic in xx. Taking expectation under PP:

EP[(xμ1)2]=σ12,EP[(xμ2)2]=σ12+(μ1μ2)2.(27)\mathbb{E}_P[(x-\mu_1)^2] = \sigma_1^2, \quad \mathbb{E}_P[(x-\mu_2)^2] = \sigma_1^2 + (\mu_1 - \mu_2)^2. \tag{27}

Collecting constant and quadratic terms yields (23). \blacksquare

Theorem 5 (Multivariate Gaussian KL). For p=N(μ1,Σ1)p = \mathcal{N}(\boldsymbol\mu_1, \boldsymbol\Sigma_1) and q=N(μ2,Σ2)q = \mathcal{N}(\boldsymbol\mu_2, \boldsymbol\Sigma_2) in Rd\mathbb{R}^d:

DKL(pq)=12[logΣ2Σ1d+tr(Σ21Σ1)+(μ2μ1)TΣ21(μ2μ1)].(28)D_{\text{KL}}(p \| q) = \frac{1}{2}\left[\log\frac{|\boldsymbol\Sigma_2|}{|\boldsymbol\Sigma_1|} - d + \text{tr}(\boldsymbol\Sigma_2^{-1}\boldsymbol\Sigma_1) + (\boldsymbol\mu_2-\boldsymbol\mu_1)^T\boldsymbol\Sigma_2^{-1}(\boldsymbol\mu_2-\boldsymbol\mu_1)\right]. \tag{28}

Proof sketch. Use logp(x)=d2log(2π)12logΣ112(xμ1)TΣ11(xμ1)\log p(\mathbf{x}) = -\frac{d}{2}\log(2\pi) - \frac{1}{2}\log|\boldsymbol\Sigma_1| - \frac{1}{2}(\mathbf{x}-\boldsymbol\mu_1)^T\boldsymbol\Sigma_1^{-1}(\mathbf{x}-\boldsymbol\mu_1) and take expectation under pp, using E[(xμ1)(xμ1)T]=Σ1\mathbb{E}[(\mathbf{x}-\boldsymbol\mu_1)(\mathbf{x}-\boldsymbol\mu_1)^T] = \boldsymbol\Sigma_1 and the trace trick for cross-terms. See The Multivariate Gaussian, equation (33).

Special case (Standard Normal Prior). For p=N(μ,Σ)p = \mathcal{N}(\boldsymbol\mu, \boldsymbol\Sigma) and q=N(0,I)q = \mathcal{N}(\mathbf{0}, \mathbf{I}):

DKL(pq)=12[logΣd+tr(Σ)+μTμ].(29)D_{\text{KL}}(p \| q) = \frac{1}{2}\left[-\log|\boldsymbol\Sigma| - d + \text{tr}(\boldsymbol\Sigma) + \boldsymbol\mu^T\boldsymbol\mu\right]. \tag{29}

This is the VAE regularization term when the prior is standard normal.


Conditional and Mutual Information Connections

Definition 5 (Conditional KL).

DKL(P(YX)Q(YX))=ExP(X)[DKL(P(YX=x)Q(YX=x))].(30)D_{\text{KL}}(P(Y|X) \| Q(Y|X)) = \mathbb{E}_{x \sim P(X)}\left[D_{\text{KL}}(P(Y|X=x) \| Q(Y|X=x))\right]. \tag{30}

Theorem 6 (Mutual Information as KL).

I(X;Y)=DKL(P(X,Y)P(X)P(Y)).(31)I(X; Y) = D_{\text{KL}}(P(X,Y) \| P(X)P(Y)). \tag{31}

Proof. Expand (3) for the joint vs product of marginals; terms telescope to H(X)+H(Y)H(X,Y)=I(X;Y)H(X) + H(Y) - H(X,Y) = I(X;Y). See Entropy. \blacksquare

Theorem 7 (ELBO Decomposition). For latent variable model with p(x)=p(xz)p(z)dzp(\mathbf{x}) = \int p(\mathbf{x}|\mathbf{z})p(\mathbf{z})\, d\mathbf{z} and variational q(zx)q(\mathbf{z}|\mathbf{x}):

logp(x)=LELBO+DKL(q(zx)p(zx)),(32)\log p(\mathbf{x}) = \mathcal{L}_{\text{ELBO}} + D_{\text{KL}}(q(\mathbf{z}|\mathbf{x}) \| p(\mathbf{z}|\mathbf{x})), \tag{32}

where LELBO=Eq[logp(xz)]DKL(q(zx)p(z))\mathcal{L}_{\text{ELBO}} = \mathbb{E}_{q}[\log p(\mathbf{x}|\mathbf{z})] - D_{\text{KL}}(q(\mathbf{z}|\mathbf{x}) \| p(\mathbf{z})). Maximizing ELBO minimizes forward KL to the true posterior.


Connection to Machine Learning

Variational Autoencoders

LVAE=Eqϕ(zx)[logpθ(xz)]DKL(qϕ(zx)p(z)).(33)\mathcal{L}_{\text{VAE}} = \mathbb{E}_{q_\phi(\mathbf{z}|\mathbf{x})}[\log p_\theta(\mathbf{x}|\mathbf{z})] - D_{\text{KL}}(q_\phi(\mathbf{z}|\mathbf{x}) \| p(\mathbf{z})). \tag{33}

The KL term uses (29) when p(z)=N(0,I)p(\mathbf{z}) = \mathcal{N}(\mathbf{0}, \mathbf{I}) and qq is diagonal Gaussian.

Diffusion Models

The variational bound decomposes:

L=DKL(q(xTx0)p(xT))+t=2TDKL(q(xt1xt,x0)pθ(xt1xt))+const.(34)L = D_{\text{KL}}(q(\mathbf{x}_T|\mathbf{x}_0) \| p(\mathbf{x}_T)) + \sum_{t=2}^{T} D_{\text{KL}}(q(\mathbf{x}_{t-1}|\mathbf{x}_t, \mathbf{x}_0) \| p_\theta(\mathbf{x}_{t-1}|\mathbf{x}_t)) + \text{const}. \tag{34}

Each term is a Gaussian KL (Theorem 5) — tractable closed form.

RLHF and PPO

L=Exπθ[R(x)]+βDKL(πθπref).(35)\mathcal{L} = -\mathbb{E}_{x \sim \pi_\theta}[R(x)] + \beta \cdot D_{\text{KL}}(\pi_\theta \| \pi_{\text{ref}}). \tag{35}

Reverse KL (or symmetrized variants) prevents the policy from drifting far from the reference LLM, avoiding reward hacking.

Variational Inference

Approximating posterior p(θD)p(\boldsymbol\theta|\mathcal{D}) with q(θ)q(\boldsymbol\theta) by minimizing DKL(qp(θD))D_{\text{KL}}(q \| p(\boldsymbol\theta|\mathcal{D})) (reverse KL, mode-seeking) or maximizing ELBO equivalent to minimizing DKL(qp(θD))D_{\text{KL}}(q \| p(\boldsymbol\theta|\mathcal{D})) — see Variational Inference.


Common Pitfalls and Misconceptions

Pitfall 1: Calling KL a "distance." It is a divergence or premetric, not a metric — asymmetry and triangle inequality failure matter for interpretation.

Pitfall 2: Swapping argument order. DKL(PQ)D_{\text{KL}}(P \| Q) and DKL(QP)D_{\text{KL}}(Q \| P) optimize differently (mean-seeking vs mode-seeking). VAEs use forward KL in ELBO; RLHF often uses reverse KL.

Pitfall 3: Infinite KL from support mismatch. If Q(x)=0Q(x) = 0 where P(x)>0P(x) > 0, forward KL is ++\infty. Ensure model support covers data support (softmax over all classes, positive variance in Gaussians).

Pitfall 4: Confusing KL with JS divergence. Jensen–Shannon divergence JS(PQ)=12DKL(PM)+12DKL(QM)\text{JS}(P \| Q) = \frac{1}{2}D_{\text{KL}}(P \| M) + \frac{1}{2}D_{\text{KL}}(Q \| M) with M=12(P+Q)M = \frac{1}{2}(P+Q) is symmetric and bounded — different tradeoffs.

Pitfall 5: Ignoring that cross-entropy offset differs from KL scale. H(P,Q)=H(P)+DKL(PQ)H(P,Q) = H(P) + D_{\text{KL}}(P \| Q); training logs cross-entropy, but generalization depends on KL to the true PP, not H(P)H(P) alone.


Research Perspective

Kullback and Leibler (1951) introduced DKLD_{\text{KL}} in the context of sufficient statistics and hypothesis testing. It became central to information theory (I-divergence, Csiszár) and statistics (Bahadur efficiency, large deviations).

In machine learning, KL appears in the EM algorithm (E-step as KL minimization), variational Bayes (Jordan et al., 1999), VAEs (Kingma & Welling, 2014), and modern alignment (RLHF, Ouyang et al., 2022). Research on ff-divergences (generalizing KL), Wasserstein distances (true metrics for generative modeling), and rate-distortion theory continues to refine how we measure distribution mismatch.

Open questions include: optimal divergence choice for generative models, tight KL-based generalization bounds for deep networks, and whether reverse KL penalties in RLHF optimally balance exploration and safety.


Summary of Takeaways

  • KL definitionP(x)logP(x)Q(x)\sum P(x) \log \frac{P(x)}{Q(x)} — Distribution mismatch
  • Cross-entropy relationDKL(P —Q)=H(P,Q)H(P)D_{\text{KL}}(P \ — Q) = H(P,Q) - H(P) — Training loss link
  • Non-negativity — Gibbs' inequality (Jensen) — Valid divergence
  • Forward KL — Mean-seeking, covers modes — MLE, ELBO
  • Reverse KL — Mode-seeking, zero-forcing — RLHF, some VI
  • Gaussian KL — (28), (29) — VAE, diffusion
  • MLE equivalenceargminθDKL(p^n —pθ)\text{argmin}_\theta D_{\text{KL}}(\hat{p}_n \ — p_\theta) — Foundation of learning

Next article: Maximum Likelihood Estimation → — estimating parameters by minimizing KL to the empirical distribution.


Exercises

Exercise 1 (Derivation). Reproduce the step-by-step derivation (5)–(8) and verify for P=(0.5,0.5)P = (0.5, 0.5), Q=(0.25,0.75)Q = (0.25, 0.75).

Exercise 2 (Gibbs). Write out Jensen's inequality step for the continuous case (13).

Exercise 3 (Asymmetry). Compute both DKL(PQ)D_{\text{KL}}(P \| Q) and DKL(QP)D_{\text{KL}}(Q \| P) for P=Bernoulli(0.1)P = \text{Bernoulli}(0.1), Q=Bernoulli(0.5)Q = \text{Bernoulli}(0.5).

Exercise 4 (Gaussian). Derive (23) completely, showing all integral steps.

Exercise 5 (Multivariate). Derive (29) from (28) with μ2=0\boldsymbol\mu_2 = \mathbf{0}, Σ2=I\boldsymbol\Sigma_2 = \mathbf{I}.

Exercise 6 (Chain rule). Prove (16) by expanding the joint KL.

Exercise 7 (Conceptual). Explain why VAEs use forward KL in the ELBO but RLHF uses reverse KL for the policy penalty.

Exercise 8 (ELBO). Prove (32) starting from logp(x)=logq(zx)p(x,z)q(zx)dz\log p(\mathbf{x}) = \log \int q(\mathbf{z}|\mathbf{x}) \frac{p(\mathbf{x},\mathbf{z})}{q(\mathbf{z}|\mathbf{x})} d\mathbf{z} and Jensen's inequality.