25.3 f-Divergences

Throughout this book, whenever we fit a model by maximum likelihood, we measure how well it matches the data with a single number: the KL divergence. As we saw at the end of Section 25.1, however, KL divergence has a glaring blind spot: it jumps to infinity the moment a model assigns zero probability to an event that actually occurs. A classifier rarely runs into this problem, because a softmax layer never outputs an exact zero. A generative network that maps a low-dimensional noise vector into image space runs into it immediately, because its samples lie on a thin surface in image space that can miss the real data altogether. How else can we compare two distributions, and which comparisons can still distinguish a model that is nearly right from one that is far off?

The simplest possible example already reveals what can go wrong. Suppose the data sit at a single point, P=δ0P = \delta_0, while the model places all its mass at a single point a distance dd away, Q=δdQ = \delta_d. Intuitively, a model at d=0.01d = 0.01 is nearly right and one at d=100d = 100 is far off, so a useful loss should tell them apart. Table 25.3.1 shows what five standard comparisons report.

Table 25.3.1. Five comparisons of the point masses δ0\delta_0 and δd\delta_d. The MMD row uses the RBF kernel with length scale 11.
comparisond=0.01d = 0.01d=1d = 1d=100d = 100
KL divergence DKL(P∥Q)D_{\textrm{KL}}(P\|Q)∞\infty∞\infty∞\infty
Jensen--Shannon divergencelog⁡2\log 2log⁡2\log 2log⁡2\log 2
total variation distance111111
maximum mean discrepancy MMD2\textrm{MMD}^20.00010.00010.790.792.002.00
Wasserstein distance W1W_10.010.0111100100

Look closely at the table: three of the five rows do not budge as dd varies. A model trained with any of those three receives the same loss at d=0.01d = 0.01 as at d=100d = 100, and therefore zero gradient with respect to dd. The original generative adversarial network (GAN) of Chapter 16 sat in this predicament, since its objective reduces to the Jensen--Shannon row up to constants.

Those three frozen rows belong to a single family. KL divergence compares PP and QQ through the ratio p/qp/q of their densities. If we keep that density ratio and vary the convex function applied to it, we obtain a broad family called the f-divergences. All of its members share one proof of non-negativity, one method for estimation from samples, and one structural reason for being blind to the distance dd. The remaining two rows, which do move with dd, avoid the density ratio altogether; we will study them in Section 25.4.

Once the family is in place, we will address three questions that recur throughout deep learning. First, does it matter which distribution comes first in KL, the data as in maximum likelihood or the model as in variational inference (Section 25.3.2)? Second, what does a small divergence actually guarantee to someone trying to tell PP from QQ (Section 25.3.3)? Third, what can we still compute when both distributions are known only through samples (Section 25.3.4)? Section 25.2 ended on that last question, and adversarial training rests on its answer.

Throughout this section, all logarithms are natural, so KL and Jensen--Shannon divergences are measured in nats. We write PP for the data or target distribution and QQ for the model, pp and qq for their probability mass functions or densities, and u=p/qu = p/q for their density ratio.

from scipy import integrate, optimize

We can check every entry of Table 25.3.1 directly in code. The next cell represents δ0\delta_0 and δd\delta_d as the probability vectors (1,0)(1, 0) and (0,1)(0, 1) on the two-point support {0,d}\{0, d\} and evaluates each comparison from its definition. Three of these definitions follow in this section and the other two in the next.

def kl_div(p, q):
    """D_KL(P||Q) on a finite support: 0 log 0 = 0, and inf if q = 0 < p."""
    s = p > 0
    with np.errstate(divide='ignore'):
        return np.sum(p[s] * np.log(p[s] / q[s]))

def five_comparisons(atoms, p, q, ell=1.0):
    """KL, JS, TV, RBF MMD^2 and W1 between pmfs p, q on sorted atoms."""
    m = (p + q) / 2                                   # the mixture for JS
    K = np.exp(-(atoms[:, None] - atoms[None, :]) ** 2 / (2 * ell ** 2))
    F_gap = np.abs(np.cumsum(p) - np.cumsum(q))[:-1]  # |F_P - F_Q| between atoms
    return {'KL': kl_div(p, q), 'JS': (kl_div(p, m) + kl_div(q, m)) / 2,
            'TV': np.abs(p - q).sum() / 2, 'MMD^2': (p - q) @ K @ (p - q),
            'W1': np.sum(F_gap * np.diff(atoms))}

delta_0, delta_d = np.array([1.0, 0.0]), np.array([0.0, 1.0])  # on atoms (0, d)
for d in (0.01, 1.0, 100.0):
    vals = five_comparisons(np.array([0.0, d]), delta_0, delta_d)
    print(f'd = {d:6}: ' + '  '.join(f'{k} {v:.4g}' for k, v in vals.items()))
d =   0.01: KL inf  JS 0.6931  TV 1  MMD^2 0.0001  W1 0.01
d =    1.0: KL inf  JS 0.6931  TV 1  MMD^2 0.7869  W1 1
d =  100.0: KL inf  JS 0.6931  TV 1  MMD^2 2  W1 100

25.3.1 The f-Divergence Family#

Before generalizing KL divergence, let us pin down the minimum properties that any comparison between two distributions should satisfy. The f-divergence family then emerges naturally when we ask which ingredients the non-negativity proof of KL truly uses.

25.3.1.1 Divergences and Metrics#

At a minimum, a "distance between distributions" should vanish when the two distributions coincide and stay strictly positive when they differ. A divergence is any function of two distributions with this property:

D(P,Q)≥0,D(P,Q)=0if and only ifP=Q.D(P, Q) \geq 0, \qquad D(P, Q) = 0 \quad \textrm{if and only if} \quad P = Q.

Nothing else is required. A metric, like the Euclidean distance ∥x−y∥\|\mathbf{x} - \mathbf{y}\| between two vectors (Section 1.3), must additionally be symmetric, D(P,Q)=D(Q,P)D(P, Q) = D(Q, P), and obey the triangle inequality, D(P,R)≤D(P,Q)+D(Q,R)D(P, R) \leq D(P, Q) + D(Q, R). KL divergence is a divergence: Gibbs' inequality (Section 25.1) guarantees that DKL(P∥Q)≥0D_{\textrm{KL}}(P\|Q) \geq 0, with equality only when P=QP = Q. It fails both metric axioms, however, and a pair of univariate Gaussians shows both failures immediately. As we computed in that section, the KL divergence between two Gaussians is

DKL(N(μ1,σ12) ∥ N(μ2,σ22))=log⁡σ2σ1+σ12+(μ1−μ2)22σ22−12.(25.3.1)D_{\textrm{KL}}\big(\mathcal{N}(\mu_1, \sigma_1^2) \,\big\|\, \mathcal{N}(\mu_2, \sigma_2^2)\big) = \log\frac{\sigma_2}{\sigma_1} + \frac{\sigma_1^2 + (\mu_1 - \mu_2)^2}{2\sigma_2^2} - \frac{1}{2}. \tag{25.3.1}

For P=N(0,1)P = \mathcal{N}(0, 1) and Q=N(0,4)Q = \mathcal{N}(0, 4), this formula gives DKL(P∥Q)≈0.318D_{\textrm{KL}}(P\|Q) \approx 0.318 nats in one direction and DKL(Q∥P)≈0.807D_{\textrm{KL}}(Q\|P) \approx 0.807 nats in the other, so KL is not symmetric. The triangle inequality fails for a different reason. Take three unit-variance Gaussians centered at 00, 11, and 22: the formula gives DKL=12D_{\textrm{KL}} = \tfrac{1}{2} for each adjacent pair, yet 22 for the outer pair, and 2>12+122 > \tfrac{1}{2} + \tfrac{1}{2}. Because KL grows like the square of the distance between the means, it behaves like a squared Euclidean distance rather than a true length.

Neither failure needs repair. Minimizing a divergence uses neither symmetry nor the triangle inequality, and the asymmetry of KL turns out to be a genuine modeling choice (Section 25.3.2). Still, whenever a proof needs to chain comparisons from PP to QQ and from QQ to RR, the triangle inequality becomes essential. Fortunately, the family we are about to construct includes true metrics as well.

25.3.1.2 The f-Divergence Template#

Gibbs' inequality is what makes KL a valid divergence. Let us inspect its proof closely, because any other function that shares the same two properties will yield a divergence too.

First, rewrite KL as an expectation under QQ of a function of the density ratio u(x)=p(x)/q(x)u(x) = p(x)/q(x):

DKL(P∥Q)=∑xp(x)log⁡p(x)q(x)=∑xq(x) p(x)q(x)log⁡p(x)q(x)=Ex∼Q[u(x)log⁡u(x)].D_{\textrm{KL}}(P\|Q) = \sum_x p(x) \log\frac{p(x)}{q(x)} = \sum_x q(x)\, \frac{p(x)}{q(x)} \log\frac{p(x)}{q(x)} = \mathbb{E}_{x \sim Q}\big[u(x) \log u(x)\big].

Viewed this way, KL weights each outcome by QQ and penalizes its density ratio through the function ulog⁡uu \log u. Let us check this formula on our running example from Section 25.1, the discrete pair P=(0.6,0.3,0.1)P = (0.6, 0.3, 0.1) and Q=(0.2,0.5,0.3)Q = (0.2, 0.5, 0.3). Here the ratios are u=3u = 3, 0.60.6, and 13\tfrac13, weighted by 0.20.2, 0.50.5, and 0.30.3:

0.2 (3log⁡3)+0.5 (0.6log⁡0.6)+0.3 (13log⁡13)=0.659−0.153−0.110=0.396 nats,\begin{aligned} 0.2\,(3 \log 3) &+ 0.5\,(0.6 \log 0.6) + 0.3\,\big(\tfrac13 \log \tfrac13\big) \\ &= 0.659 - 0.153 - 0.110 = 0.396 \textrm{ nats}, \end{aligned}

matching the KL divergence we found earlier.

Second, look at the proof of non-negativity. In Section 25.1, we applied Jensen's inequality once to −log⁡(q/p)-\log(q/p) under PP. Run instead under QQ on the function ulog⁡uu \log u, that same one-step argument uses only two facts: the function is convex, and it equals zero at u=1u = 1. Nothing stops us from replacing ulog⁡uu \log u by any function with those two properties. Let f:(0,∞)→Rf : (0, \infty) \to \mathbb{R} be convex with f(1)=0f(1) = 0, and define f(0)=lim⁡u↓0f(u)f(0) = \lim_{u \downarrow 0} f(u), which may be +∞+\infty. Assume for now that p(x)=0p(x) = 0 wherever q(x)=0q(x) = 0, written P≪QP \ll Q, so that an expectation under QQ sees all of PP. The f-divergence of PP with respect to QQ is (Csiszár, 1967)

Df(P∥Q)=Ex∼Q ⁣[f ⁣(p(x)q(x))].(25.3.2)D_f(P\|Q) = \mathbb{E}_{x \sim Q}\!\left[ f\!\left( \frac{p(x)}{q(x)} \right) \right]. \tag{25.3.2}

Point by point, the ratio u=p/qu = p/q measures how much PP over- or under-represents xx relative to QQ, and ff assigns a penalty that vanishes when u=1u = 1. We call ff the divergence function. Much of the literature calls it the generator of the divergence, and our code below stores these functions in a dictionary named generators. In the prose, however, we reserve the word generator for a network that produces samples, as in Section 16.2.

To pin down when the divergence equals zero, we need one mild refinement of convexity: we call ff strictly convex at 11 if some line through (1,0)(1, 0) lies strictly below ff everywhere else, meaning f(u)>c (u−1)f(u) > c\,(u - 1) for all u≠1u \neq 1 and some slope cc.

Proposition (non-negativity). For any convex ff with f(1)=0f(1) = 0 and P≪QP \ll Q,

Df(P∥Q)≥0,D_f(P\|Q) \geq 0,

and if ff is strictly convex at 11, then Df(P∥Q)=0D_f(P\|Q) = 0 if and only if P=QP = Q.

Proof. Jensen's inequality (Section 23.3.3.1) states that E[f(U)]≥f(E[U])\mathbb{E}[f(U)] \geq f(\mathbb{E}[U]) for any convex ff and random variable UU. Apply it to U=u(x)U = u(x) with x∼Qx \sim Q. Everything rests on a single identity: the density ratio averages to one under QQ, since EQ[u]=∑xq(x) p(x)/q(x)=∑xp(x)=1\mathbb{E}_Q[u] = \sum_x q(x)\, p(x)/q(x) = \sum_x p(x) = 1. Therefore

Df(P∥Q)=EQ[f(u)]≥f(EQ[u])=f(1)=0.D_f(P\|Q) = \mathbb{E}_Q[f(u)] \geq f(\mathbb{E}_Q[u]) = f(1) = 0.

For the equality case, take the supporting line c (u−1)c\,(u - 1) lying strictly below ff away from 11. Then Df(P∥Q)≥EQ[c (u−1)]=0D_f(P\|Q) \geq \mathbb{E}_Q[c\,(u - 1)] = 0, and the gap is the expectation of f(u)−c (u−1)f(u) - c\,(u - 1), which is strictly positive wherever u≠1u \neq 1. Hence Df(P∥Q)=0D_f(P\|Q) = 0 forces u(x)=1u(x) = 1 at every xx with q(x)>0q(x) > 0, meaning P=QP = Q. ■\blacksquare

The identity EQ[u]=1\mathbb{E}_Q[u] = 1 that powered this proof has a second handy consequence we will use repeatedly: adding any multiple c (u−1)c\,(u - 1) to ff leaves every divergence unchanged, because EQ[c (u−1)]=0\mathbb{E}_Q[c\,(u - 1)] = 0. The functions ulog⁡uu \log u and ulog⁡u−u+1u \log u - u + 1, for example, define the same KL divergence.

So far we have assumed P≪QP \ll Q, yet our opening point-mass example violates that condition. A generative model starts training in the same regime, with its samples and the data occupying disjoint regions, so we must extend DfD_f to handle zeros in qq. Outcomes with p(x)=q(x)=0p(x) = q(x) = 0 contribute nothing. Where p(x)=0<q(x)p(x) = 0 \lt q(x), the contribution is simply q(x) f(0)q(x)\, f(0), as the definition already says. Where q(x)=0<p(x)q(x) = 0 \lt p(x), the ratio blows up, so we define the contribution as the limit of q f(p/q)q\, f(p/q) as q↓0q \downarrow 0. Substituting t=p/qt = p/q rewrites this as p f(t)/tp\, f(t)/t, whose limit is p(x) f′(∞)p(x)\, f'(\infty) with f′(∞):=lim⁡t→∞f(t)/tf'(\infty) := \lim_{t \to \infty} f(t)/t. For a convex function ff, the secant f(t)/tf(t)/t and the derivative f′(t)f'(t) share the same limit at infinity, which explains the notation. Either f(0)f(0) or f′(∞)f'(\infty) may be +∞+\infty.

Now apply this rule to the point masses δ0\delta_0 and δd\delta_d for any d≠0d \neq 0. At x=0x = 0 we have q=0<p=1q = 0 \lt p = 1, and at x=dx = d we have p=0<q=1p = 0 \lt q = 1, so

Df(δ0∥δd)=f(0)+f′(∞)for every d≠0.D_f(\delta_0 \| \delta_d) = f(0) + f'(\infty) \qquad \textrm{for every } d \neq 0.

Notice that dd has vanished from the right-hand side. This tells us something far stronger than Table 25.3.1 alone showed: every f-divergence is constant on disjoint supports. It cannot distinguish a near miss from a distant one, so a model trained on it gets zero gradient from the distance separating the supports. For KL, f(u)=ulog⁡uf(u) = u \log u gives f(0)=0f(0) = 0 and f′(∞)=lim⁡t→∞log⁡t=∞f'(\infty) = \lim_{t \to \infty} \log t = \infty, reproducing the first row of Table 25.3.1. The other two frozen rows follow as soon as we write down their divergence functions.

25.3.1.3 Common Divergence Functions#

Each row of Table 25.3.2 corresponds to one choice of ff, and Figure 25.3.1 plots six of these curves side by side. The column f′′(1)f''(1), recording the curvature at u=1u = 1, will play a central role in the next subsection.

Table 25.3.2. Common divergence functions. Each row gives the function ff of one divergence, its curvature at u=1u = 1, whether the divergence is symmetric, and whether it (or its square root) is a metric.
Divergencef(u)f(u)f′′(1)f''(1)Symmetric?Metric?
Kullback--Leibler (forward)ulog⁡uu \log u11nono
reverse KL−log⁡u-\log u11nono
Pearson χ2\chi^2(u−1)2(u - 1)^222nono
squared Hellinger dH2d_{\mathrm{H}}^2(u−1)2(\sqrt{u} - 1)^212\tfrac12yesdHd_{\mathrm{H}} is a metric
total variation12∣u−1∣\tfrac{1}{2}\lvert u - 1 \rvertnone (corner)yesyes
Jensen--Shannonu2log⁡u−u+12log⁡u+12\tfrac{u}{2} \log u - \tfrac{u+1}{2} \log \tfrac{u+1}{2}14\tfrac14yesJS\sqrt{\textrm{JS}} is a metric
α\alpha-divergence (α≠0,1\alpha \neq 0, 1)uα−αu+α−1α(α−1)\dfrac{u^\alpha-\alpha u+\alpha-1}{\alpha(\alpha-1)}11only at α=12\alpha = \tfrac{1}{2}square root at α=12\alpha = \tfrac{1}{2}
Figure 25.3.1:

Six divergence functions, with the smooth curves shifted by f′(1)(u−1)f'(1)(u-1) so that each touches zero tangentially at u=1u=1 without changing its divergence. Near u=1u=1 the smooth curves are parabolas with the curvatures 1,1,2,12,141, 1, 2, \tfrac12, \tfrac14 listed in the table, while total variation has a sharp corner instead. Away from u=1u=1 they penalize disagreement very differently: reverse KL blows up as u→0u \to 0, KL and χ2\chi^2 grow superlinearly, and total variation grows linearly.

Reverse KL uses f(u)=−log⁡uf(u) = -\log u, because EQ[−log⁡(p/q)]=EQ[log⁡(q/p)]=DKL(Q∥P)\mathbb{E}_Q[-\log(p/q)] = \mathbb{E}_Q[\log(q/p)] = D_{\textrm{KL}}(Q\|P). Swapping the arguments of KL is therefore just another choice of ff, so both directions sit side by side in the same table. Exercise 1a gives the general recipe for swapping the arguments of any f-divergence.

Total variation uses f(u)=12∣u−1∣f(u) = \tfrac{1}{2}|u - 1|, which simplifies to ∑xq(x) 12∣p(x)/q(x)−1∣=12∑x∣p(x)−q(x)∣\sum_x q(x)\, \tfrac12 |p(x)/q(x) - 1| = \tfrac{1}{2}\sum_x |p(x) - q(x)|. We write it as TV(P,Q)\textrm{TV}(P, Q). It is half the ℓ1\ell_1 distance between the probability vectors; on our running pair, TV=12(0.4+0.2+0.2)=0.4\textrm{TV} = \tfrac12(0.4 + 0.2 + 0.2) = 0.4. We will unpack what this number means for distinguishing PP from QQ in Section 25.3.3. On the point masses, its divergence function gives f(0)+f′(∞)=12+12=1f(0) + f'(\infty) = \tfrac12 + \tfrac12 = 1, accounting for the third row of Table 25.3.1.

Pearson's χ2\chi^2 divergence uses the parabola f(u)=(u−1)2f(u) = (u - 1)^2, giving χ2(P∥Q)=∑xq(x) (p(x)/q(x)−1)2=∑x(p(x)−q(x))2/q(x)\chi^2(P\|Q) = \sum_x q(x)\,\big(p(x)/q(x) - 1\big)^2 = \sum_x (p(x) - q(x))^2 / q(x). On the running pair, it evaluates to 0.16/0.2+0.04/0.5+0.04/0.3=1.01330.16/0.2 + 0.04/0.5 + 0.04/0.3 = 1.0133. Notice how heavily a mismatch is weighted where QQ is small: the first outcome alone, where q=0.2q = 0.2, contributes 0.80.8 of the total.

The Jensen--Shannon divergence (Lin, 1991) symmetrizes KL by comparing both distributions against their even mixture M=12(P+Q)M = \tfrac{1}{2}(P + Q) (with mass function mm):

JS(P,Q)=12DKL(P ∥ M)+12DKL(Q ∥ M).(25.3.3)\textrm{JS}(P, Q) = \frac{1}{2} D_{\textrm{KL}}(P \,\|\, M) + \frac{1}{2} D_{\textrm{KL}}(Q \,\|\, M). \tag{25.3.3}

Because MM puts mass wherever either PP or QQ does, the ratios p/mp/m and q/mq/m can never blow up: p/m=2p/(p+q)≤2p/m = 2p/(p + q) \leq 2, so each KL term is at most log⁡2\log 2. Hence JS is symmetric and bounded, 0≤JS(P,Q)≤log⁡20 \leq \textrm{JS}(P, Q) \leq \log 2, and it hits its ceiling log⁡2\log 2 only when PP and QQ have disjoint supports (where p/m=2p/m = 2 wherever p>0p > 0). Remarkably, its square root is a true metric (Endres and Schindelin, 2003). Writing (25.3.3) in terms of the ratio uu yields the divergence function in the table (Exercise 1c), which satisfies f(0)=f′(∞)=12log⁡2f(0) = f'(\infty) = \tfrac12 \log 2. Their sum is log⁡2\log 2, the second row of Table 25.3.1. Our single identity Df(δ0∥δd)=f(0)+f′(∞)D_f(\delta_0\|\delta_d) = f(0) + f'(\infty) has now explained all three frozen rows.

The squared Hellinger divergence is dH2(P,Q)=∑x(p(x)−q(x))2d_{\mathrm{H}}^2(P, Q) = \sum_x \big(\sqrt{p(x)} - \sqrt{q(x)}\big)^2. Its square root dHd_{\mathrm{H}} is simply the Euclidean distance between the vectors p\sqrt{p} and q\sqrt{q}, which immediately makes dHd_{\mathrm{H}} a metric (Exercise 2b).

The final row of the table is a continuous family inside the family, the α\alpha-divergences (Amari, 2016), generated by

fα(u)=uα−αu+α−1α(α−1).(25.3.4)f_\alpha(u)=\frac{u^\alpha-\alpha u+\alpha-1}{\alpha(\alpha-1)}. \tag{25.3.4}

For every α≠0,1\alpha \neq 0, 1, this function is convex with fα(1)=0f_\alpha(1) = 0 and curvature fα′′(1)=1f_\alpha''(1) = 1. As α→1\alpha \to 1 it approaches ulog⁡u−u+1u\log u - u + 1, and as α→0\alpha \to 0 it approaches −log⁡u+u−1-\log u + u - 1. Since adding a multiple of u−1u - 1 changes no divergence, the two limits recover forward and reverse KL. Halfway between them, at α=12\alpha = \tfrac12, we get 2(u−1)22(\sqrt{u} - 1)^2 (twice squared Hellinger), while α=2\alpha = 2 gives half of χ2\chi^2. A single parameter α\alpha therefore interpolates smoothly from reverse KL through Hellinger to forward KL and on to χ2\chi^2 (Exercise 1d connects this family to Rényi's divergence).

Let us evaluate the first six rows on our running pair in both argument orders. KL should print 0.3960.396 and 0.3650.365 nats, matching Section 25.1, while TV and χ2(P∥Q)\chi^2(P\|Q) should match the hand calculations above.

def f_divergence(f, p, q):
    """D_f(P||Q) = sum_x q(x) f(p(x)/q(x)); nats for KL/JS, else unitless."""
    return np.sum(q * f(p / q))

generators = {
    'KL':          lambda u: u * np.log(u),
    'reverse KL':  lambda u: -np.log(u),
    'chi^2':       lambda u: (u - 1) ** 2,
    'Hellinger^2': lambda u: (np.sqrt(u) - 1) ** 2,
    'TV':          lambda u: 0.5 * np.abs(u - 1),
    'JS':          lambda u: u / 2 * np.log(u) - (u + 1) / 2 * np.log((u + 1) / 2),
}

p = np.array([0.6, 0.3, 0.1])
q = np.array([0.2, 0.5, 0.3])
for name, f in generators.items():
    print(f'{name:12s} D_f(P||Q) = {f_divergence(f, p, q):.4f}   '
          f'D_f(Q||P) = {f_divergence(f, q, p):.4f}')
KL           D_f(P||Q) = 0.3961   D_f(Q||P) = 0.3653
reverse KL   D_f(P||Q) = 0.3653   D_f(Q||P) = 0.3961
chi^2        D_f(P||Q) = 1.0133   D_f(Q||P) = 0.8000
Hellinger^2  D_f(P||Q) = 0.1862   D_f(Q||P) = 0.1862
TV           D_f(P||Q) = 0.4000   D_f(Q||P) = 0.4000
JS           D_f(P||Q) = 0.0911   D_f(Q||P) = 0.0911

As expected, the KL row is asymmetric, and the reverse-KL row is simply the KL row with its columns swapped. The χ2\chi^2 row is asymmetric too (1.01331.0133 against 0.80000.8000). By contrast, squared Hellinger (0.18620.1862), TV (0.40.4), and JS are symmetric, with JS at 0.09110.0911 nats sitting well below its ceiling of log⁡2≈0.693\log 2 \approx 0.693. Six divergences have looked at the same pair of distributions and returned values spanning an order of magnitude, from 0.090.09 to 1.011.01. Yet if we multiply KL by 22, squared Hellinger by 44, and JS by 88, the three rescaled values are 0.790.79, 0.740.74, and 0.730.73. Why would three quite different functions of the density ratio nearly agree, and where did the factors 22, 44, and 88 come from?

25.3.1.4 The Local Quadratic#

The curves in Figure 25.3.1 behave very differently far from u=1u = 1. Near u=1u = 1, however, they collapse into a single function up to a constant factor, which is perhaps the most surprising property of the entire family. Nothing later in this section depends on this subsection, so you can skip it on a first reading.

Because adding a linear term c (u−1)c\,(u - 1) leaves every divergence unchanged, neither the value f(1)=0f(1) = 0 nor the slope f′(1)f'(1) affects DfD_f. The first property that distinguishes one smooth divergence function from another is its curvature f′′(1)f''(1). Near u=1u = 1, every smooth ff looks like the parabola 12f′′(1)(u−1)2\tfrac12 f''(1)(u - 1)^2, and the expectation of (u−1)2(u - 1)^2 under QQ is none other than the χ2\chi^2 divergence, EQ[(u−1)2]=∑x(p(x)−q(x))2/q(x)\mathbb{E}_Q[(u - 1)^2] = \sum_x (p(x) - q(x))^2/q(x). Let r=max⁡x∣u(x)−1∣r = \max_x |u(x) - 1| measure how far the density ratio strays from 11.

Proposition (local quadratic). Let P≪QP \ll Q, let 0<r<10 \lt r \lt 1, let ff be three times differentiable on [1−r,1+r][1 - r, 1 + r] with ∣f′′′∣≤B|f'''| \leq B there, and let ∣u(x)−1∣≤r|u(x) - 1| \leq r for every xx with q(x)>0q(x) > 0. Then

∣Df(P∥Q)−f′′(1)2 χ2(P∥Q)∣≤Br6 χ2(P∥Q).(25.3.5)\left| D_f(P\|Q) - \frac{f''(1)}{2}\, \chi^2(P\|Q) \right| \leq \frac{B r}{6}\, \chi^2(P\|Q). \tag{25.3.5}

Proof. Since f(1)=0f(1) = 0, Taylor's theorem with remainder gives f(u)=f′(1)(u−1)+12f′′(1)(u−1)2+R(u)f(u) = f'(1)(u - 1) + \tfrac12 f''(1)(u - 1)^2 + R(u) with ∣R(u)∣≤B∣u−1∣3/6|R(u)| \leq B |u - 1|^3 / 6. Take EQ\mathbb{E}_Q term by term. The linear term vanishes because EQ[u−1]=0\mathbb{E}_Q[u - 1] = 0, eliminating the only first-order difference between one ff and another. The quadratic term becomes 12f′′(1) χ2(P∥Q)\tfrac12 f''(1)\, \chi^2(P\|Q), and the remainder satisfies EQ∣R∣≤B6 EQ∣u−1∣3≤Br6 EQ(u−1)2=Br6 χ2\mathbb{E}_Q|R| \leq \tfrac{B}{6}\,\mathbb{E}_Q|u - 1|^3 \leq \tfrac{B r}{6}\, \mathbb{E}_Q(u - 1)^2 = \tfrac{B r}{6}\,\chi^2. ■\blacksquare

In short, Df≈12f′′(1) χ2D_f \approx \tfrac12 f''(1)\, \chi^2 with a relative error proportional to rr: whenever PP is close to QQ, all smooth f-divergences are scalar multiples of one another. The factors 22, 44, and 88 we applied to KL, squared Hellinger, and JS are simply 2/f′′(1)2/f''(1), and the target all three products approximate is χ2(P∥Q)=1.013\chi^2(P\|Q) = 1.013. On our running pair they still fall about a quarter short, which is no surprise: the largest ratio is 33, so r=2r = 2, outside the proposition's hypothesis r<1r \lt 1. To watch the quadratic approximation tighten, let us slide PP toward QQ along the line segment Pt=Q+t (P−Q)P_t = Q + t\,(P - Q). This scales every u−1u - 1 by tt, giving r=2tr = 2t. For four smooth divergences, we print the ratio Df(Pt∥Q)/(12f′′(1) χ2(Pt∥Q))D_f(P_t\|Q) / \big(\tfrac12 f''(1)\, \chi^2(P_t\|Q)\big), which should converge to 11 at a rate proportional to tt.

curvature = {'KL': 1.0, 'reverse KL': 1.0, 'Hellinger^2': 0.5, 'JS': 0.25}
for t in (0.5, 0.1, 0.01):
    p_t = q + t * (p - q)                     # moves P_t toward Q as t -> 0
    chi2 = f_divergence(generators['chi^2'], p_t, q)
    ratios = [f_divergence(generators[name], p_t, q) / (c / 2 * chi2)
              for name, c in curvature.items()]
    print(f't = {t:4}: ' + '  '.join(f'{name} {r:.3f}'
                                     for name, r in zip(curvature, ratios)))
t =  0.5: KL 0.844  reverse KL 0.747  Hellinger^2 0.789  JS 0.783
t =  0.1: KL 0.956  reverse KL 0.917  Hellinger^2 0.936  JS 0.935
t = 0.01: KL 0.995  reverse KL 0.990  Hellinger^2 0.993  JS 0.993

At t=0.5t = 0.5, the four divergences sit at roughly 75%75\% to 85%85\% of their quadratic approximations; at t=0.1t = 0.1 they exceed 91%91\%, and at t=0.01t = 0.01 they exceed 99%99\%. Between the last two rows tt drops tenfold and each shortfall drops nearly tenfold too, matching the linear factor rr in the proposition. The bound itself is conservative: for KL at t=0.1t = 0.1 we have r=0.2r = 0.2 and ∣f′′′(u)∣=1/u2≤1/0.82|f'''(u)| = 1/u^2 \leq 1/0.8^2, which allows a relative error of about 10%10\%, whereas the actual gap is only 4%4\%. The takeaway for optimization is practical: smooth f-divergences disagree meaningfully only on distant distributions, which is the regime early in training when the model is still far from the data.

Inside a parametric family, this common local quadratic is governed by the Fisher information matrix I(θ)=E[∇θlog⁡qθ ∇θlog⁡qθ⊤]\mathbf{I}(\boldsymbol{\theta}) = \mathbb{E}\big[\nabla_{\boldsymbol{\theta}} \log q_{\boldsymbol{\theta}}\, \nabla_{\boldsymbol{\theta}} \log q_{\boldsymbol{\theta}}^\top\big] of Section 24.6. For a small parameter step h\mathbf{h}, the density ratio of qθ+hq_{\boldsymbol{\theta} + \mathbf{h}} to qθq_{\boldsymbol{\theta}} expands as u≈1+h⊤∇θlog⁡qθu \approx 1 + \mathbf{h}^\top \nabla_{\boldsymbol{\theta}} \log q_{\boldsymbol{\theta}}. Therefore χ2(qθ+h∥qθ)=E[(u−1)2]≈h⊤I(θ) h\chi^2(q_{\boldsymbol{\theta} + \mathbf{h}}\| q_{\boldsymbol{\theta}}) = \mathbb{E}[(u - 1)^2] \approx \mathbf{h}^\top \mathbf{I}(\boldsymbol{\theta})\, \mathbf{h}, so every smooth DfD_f has Hessian f′′(1) I(θ)f''(1)\, \mathbf{I}(\boldsymbol{\theta}) at h=0\mathbf{h} = \mathbf{0}. The natural gradient of Section 23.5, though usually motivated by KL divergence, would yield the same update under any smooth f-divergence. Total variation is the lone exception in our table: its divergence function has a corner at u=1u = 1 and no second derivative there, and Exercise 4b explores how that changes its behavior as PP approaches QQ.

25.3.2 Forward and Reverse KL#

Maximum likelihood, the training criterion behind most models in this book, minimizes KL divergence with the data in the first argument. Variational inference minimizes KL divergence with the model in the first argument. Both orders appear as separate rows in Table 25.3.2, and when we fit a model QθQ_{\boldsymbol{\theta}} (with density qθq_{\boldsymbol{\theta}}) to a target PP that lies outside the model family, they push the model in opposite directions:

  • Forward KL, DKL(P ∥ Qθ)=EP[log⁡(p/qθ)]D_{\textrm{KL}}(P \,\|\, Q_{\boldsymbol{\theta}}) = \mathbb{E}_P[\log(p/q_{\boldsymbol{\theta}})], takes its expectation over the truth. Whenever qθ(x)→0q_{\boldsymbol{\theta}}(x) \to 0 at a point where p(x)>0p(x) > 0, the divergence blows up. Forward KL is therefore zero-avoiding, or mode-covering: the model must stretch to cover every mode of the data, that is, every region where pp concentrates, even if that forces it to spill mass across empty regions PP never visits.
  • Reverse KL, DKL(Qθ ∥ P)=EQθ[log⁡(qθ/p)]D_{\textrm{KL}}(Q_{\boldsymbol{\theta}} \,\|\, P) = \mathbb{E}_{Q_{\boldsymbol{\theta}}}[\log(q_{\boldsymbol{\theta}}/p)], takes its expectation over the model. Here the model is penalized heavily for placing mass where the truth has none, while any mode the model ignores contributes nothing to the objective. Reverse KL is therefore zero-forcing, or mode-seeking: the model locks onto the portion of the target it can explain well.

In practice, the choice of direction is often settled by what we can actually compute. Forward KL requires samples from PP and the density of the model, and minimizing it is just maximum likelihood: Section 24.6.2.1 shows that the average negative log-likelihood equals the cross-entropy from the empirical distribution to the model, which differs from forward KL only by a constant independent of θ\boldsymbol{\theta}. Reverse KL requires samples from the model and the density of the target, and it works even when the target density is known only up to a normalizing constant. Variational inference (Section 24.7) sits in this setting: its target PP is a posterior with an intractable normalizer, so it minimizes DKL(Qθ∥P)D_{\textrm{KL}}(Q_{\boldsymbol{\theta}}\|P) over tractable approximations QθQ_{\boldsymbol{\theta}}.

When samples and densities are available on both sides, however, the direction becomes a genuine design choice. A student network distilled from a teacher spreads out to cover everything the teacher might say when the teacher comes first, and concentrates on the teacher's likeliest outputs when the student comes first. Similarly, the KL penalty in Section 15.3 places the learned policy first, keeping it away from actions that the reference policy never takes.

If the model family contains PP, both directions recover PP. They part ways when the model is misspecified, and the clearest illustration is fitting a single Gaussian to a bimodal mixture. For forward KL, no optimization is even needed. Because forward KL equals a constant minus EP[log⁡q(X)]\mathbb{E}_P[\log q(X)], its minimizer is the maximum-likelihood Gaussian with PP replacing the empirical distribution, and maximum likelihood in an exponential family matches the expected sufficient statistics (Section 24.4.3.2): μ∗=EP[X]\mu^* = \mathbb{E}_P[X] and (σ∗)2=VarP(X)(\sigma^*)^2 = \textrm{Var}_P(X), provided the variance is finite. Reverse KL has no such closed-form solution, and it can have a separate local minimum near each mode.

For the bimodal mixture P=0.7 N(−2,0.62)+0.3 N(2,0.62)P = 0.7\,\mathcal{N}(-2, 0.6^2) + 0.3\,\mathcal{N}(2, 0.6^2), we can predict the forward fit in advance. Its mean is 0.7(−2)+0.3(2)=−0.80.7(-2) + 0.3(2) = -0.8. Its variance is the within-component variance plus the between-component variance, which for weights w1w_1, w2w_2 and means separated by Δ\Delta equals w1w2Δ2w_1 w_2 \Delta^2, giving 0.36+0.7⋅0.3⋅42=3.720.36 + 0.7 \cdot 0.3 \cdot 4^2 = 3.72. For the reverse fit, we run a derivative-free optimizer over θ=(μ,log⁡σ)\boldsymbol{\theta} = (\mu, \log\sigma) from two starting points, once at the origin and once near the smaller mode, evaluating each KL by numerical quadrature on a grid. For each fit, the next cell reports the mean, standard deviation, and both divergences.

w, mus, sigmas = np.array([0.7, 0.3]), np.array([-2.0, 2.0]), np.array([0.6, 0.6])
x = np.linspace(-8.0, 8.0, 4001)

def log_components(x, w, mus, sigmas):
    """log(w_k N(x; mu_k, sigma_k^2)) for every component k; shape (k, len(x))."""
    return (np.log(w)[:, None] - 0.5 * np.log(2 * np.pi * sigmas[:, None] ** 2)
            - (x - mus[:, None]) ** 2 / (2 * sigmas[:, None] ** 2))

def log_mix(x, w, mus, sigmas):
    """Log-density of the mixture sum_k w_k N(mu_k, sigma_k^2), computed stably."""
    c = log_components(x, w, mus, sigmas)
    c_max = c.max(axis=0)
    return c_max + np.log(np.exp(c - c_max).sum(axis=0))

log_p = lambda x: log_mix(x, w, mus, sigmas)

def log_q(x, theta):
    mu, log_sigma = theta
    return (-0.5 * np.log(2 * np.pi) - log_sigma
            - (x - mu) ** 2 / (2 * np.exp(2 * log_sigma)))

def kl(log_a, log_b, x):
    """D_KL(A||B) by quadrature on the grid x; in nats."""
    return integrate.trapezoid(np.exp(log_a) * (log_a - log_b), x)

fwd = lambda th: kl(log_p(x), log_q(x, th), x)     # D_KL(P || Q_theta)
rev = lambda th: kl(log_q(x, th), log_p(x), x)     # D_KL(Q_theta || P)

th_fwd = optimize.minimize(fwd, x0=[0.0, 0.0], method='Nelder-Mead').x
th_rev = optimize.minimize(rev, x0=[0.0, 0.0], method='Nelder-Mead').x
th_rev2 = optimize.minimize(rev, x0=[2.5, -0.5], method='Nelder-Mead').x

print(f'{"fit":26s}{"mu":>8s}{"sigma":>7s}'
      f'{"forward KL":>12s}{"reverse KL":>12s}')
for name, th in [('forward-KL fit', th_fwd), ('reverse-KL fit', th_rev),
                 ('reverse-KL fit, 2nd start', th_rev2)]:
    print(f'{name:26s}{th[0]:+8.3f}{np.exp(th[1]):7.3f}'
          f'{fwd(th):12.3f}{rev(th):12.3f}')

d2l.plot(x, [np.exp(log_p(x)), np.exp(log_q(x, th_fwd)),
             np.exp(log_q(x, th_rev))], 'x', 'density',
         legend=['target P (mixture)', 'forward-KL fit', 'reverse-KL fit'],
         figsize=(6, 3))
fit                             mu  sigma  forward KL  reverse KL
forward-KL fit              -0.800  1.929       0.558       1.274
reverse-KL fit              -1.998  0.603       5.981       0.356
reverse-KL fit, 2nd start   +1.995  0.607      14.534       1.202

The forward fit matches our calculation: μ=−0.800\mu = -0.800 and σ=1.929≈3.72\sigma = 1.929 \approx \sqrt{3.72}. It is a broad bell curve draped over both modes, putting substantial mass in the valley between them where PP has almost none. The first reverse fit lands at μ=−1.998\mu = -1.998, σ=0.603\sigma = 0.603: it hugs the dominant component and leaves almost no mass on the minor one. Its reverse KL, 0.3560.356 nats, is close to log⁡(1/0.7)≈0.357\log(1/0.7) \approx 0.357, the cost of treating the 70%70\% component as if it were the entire distribution. When started near the minor mode, the optimizer settles instead on μ=+1.995\mu = +1.995, with reverse KL 1.2021.202 nats, close to log⁡(1/0.3)≈1.204\log(1/0.3) \approx 1.204. Which mode a variational approximation captures therefore depends on its initialization.

Because reverse KL averages under the model, any mode the model fails to visit is never inspected. The objective only notices that the chosen mode carries weight 0.70.7 (or 0.30.3) instead of 11. That is why variational posteriors notoriously underestimate uncertainty: here the reverse fit has standard deviation 0.600.60, whereas the target's is 1.931.93.

The last two columns highlight how sharply the two criteria disagree on the same model. Forward KL, the maximum-likelihood criterion, penalizes the dominant-mode fit at 5.985.98 nats (more than ten times the 0.5580.558 nats of the broad fit) and the minor-mode fit at 14.514.5 nats. In both cases, the dropped mode sits more than six of the model's standard deviations away from its mean, where log⁡q\log q is deeply negative (Exercise 3c). Reverse KL ranks the first two fits in the opposite order, 0.3560.356 nats against 1.271.27: what it cannot forgive is the broad fit's mass sitting in the empty valley between the modes.

Our boundary rules for vanishing densities capture this contrast in the limit. For forward KL, f(u)=ulog⁡uf(u) = u \log u has f′(∞)=∞f'(\infty) = \infty, so placing zero model mass where PP has positive mass costs infinity, while f(0)=0f(0) = 0 means placing model mass where PP has none adds no boundary term at all. For reverse KL, f(u)=−log⁡uf(u) = -\log u, those two values swap places.

25.3.3 Total Variation, Pinsker's Inequality, and Data Processing#

Knowing that a divergence equals 0.40.4 tells us very little on its own until we know what that number actually guarantees. For total variation, the operational meaning is completely transparent, and Pinsker's inequality passes much of that meaning on to KL divergence.

25.3.3.1 Total Variation#

The divergence function 12∣u−1∣\tfrac12|u - 1| is the simplest entry in Table 25.3.2: it penalizes only the absolute distance of the ratio from 11. Its divergence, half the ℓ1\ell_1 distance between pp and qq, admits a second, equivalent form that makes the number immediately interpretable. Total variation is the largest gap between the probabilities that PP and QQ assign to any event AA:

TV(P,Q)=12∑x∣p(x)−q(x)∣=sup⁡A ∣P(A)−Q(A)∣.(25.3.6)\textrm{TV}(P, Q) = \frac{1}{2} \sum_x \lvert p(x) - q(x) \rvert = \sup_{A} \,\lvert P(A) - Q(A) \rvert. \tag{25.3.6}

If TV(P,Q)=0.03\textrm{TV}(P, Q) = 0.03, no event whatsoever can separate the two distributions by more than three percentage points. The supremum is achieved by the event A⋆={x:p(x)>q(x)}A^\star = \{x : p(x) > q(x)\} of outcomes that PP favors over QQ. To verify this, pick any event AA. Then P(A)−Q(A)=∑x∈A(p(x)−q(x))≤∑x∈A⋆(p(x)−q(x))P(A) - Q(A) = \sum_{x \in A} (p(x) - q(x)) \leq \sum_{x \in A^\star} (p(x) - q(x)), because switching from AA to A⋆A^\star only adds non-negative terms and drops non-positive ones. Since ∑x(p(x)−q(x))=0\sum_x (p(x) - q(x)) = 0, the positive and negative parts of p−qp - q have equal total magnitude, so the right-hand side equals 12∑x∣p(x)−q(x)∣\tfrac12 \sum_x |p(x) - q(x)|; the same bound applies to Q(A)−P(A)Q(A) - P(A).

On our running pair, A⋆A^\star consists of the first outcome alone, giving TV=0.6−0.2=0.4\textrm{TV} = 0.6 - 0.2 = 0.4, as we found earlier. Figure 25.3.2 illustrates the same geometry for two continuous densities. Total variation is also a true metric: both forms are manifestly symmetric, and the triangle inequality follows directly from the ℓ1\ell_1 formula (Exercise 2a).

Figure 25.3.2:

Two equal-variance densities and the regions where they disagree. The lobe where p>qp > q and the lobe where q>pq > p have equal area, and that common area is the total variation distance. The event attaining the supremum is A⋆={p>q}A^\star = \{p > q\}, here the half-line left of the crossing: guessing PP there and QQ elsewhere is the optimal single-sample test.

The supremum form gives TV a concrete statistical meaning. Imagine handing a tester a single sample, drawn with equal probability 12\tfrac12 from PP or from QQ, and asking which distribution generated it. Any deterministic test answers "PP" when the sample falls in some region AA, and "QQ" otherwise, succeeding with probability

12P(A)+12(1−Q(A))=12(1+P(A)−Q(A))≤12(1+TV(P,Q)),\tfrac12 P(A) + \tfrac12 \big(1 - Q(A)\big) = \tfrac12 \big(1 + P(A) - Q(A)\big) \leq \tfrac12 \big(1 + \textrm{TV}(P, Q)\big),

with equality at A=A⋆A = A^\star. Every test takes this form for some AA (a randomized test merely averages deterministic ones and cannot do better), so even the optimal tester beats a fair coin flip by at most TV/2\textrm{TV}/2. On our running pair, the optimal test answers "PP" on the first outcome and is right 70%70\% of the time. Both the discriminator of a GAN (trying to tell real data from generated samples) and a detector of machine-generated text are testers of this kind, and total variation sets a hard ceiling on how accurate either can be from a single sample.

25.3.3.2 Pinsker's Inequality#

Total variation bounds every statistical test, while KL divergence is what we can usually compute or control in practice because neural network losses are built from log-probabilities. Pinsker's inequality bridges the two: a small KL divergence certifies that no test can reliably tell PP from QQ. Policy trust regions in Section 15.2 are a classic example: the monotonic improvement guarantee is proved in terms of total variation, yet enforced through a bound on KL.

Proposition (Pinsker's inequality). (Pinsker, 1964; Csiszár, 1967) For distributions PP and QQ on a finite or countable set,

TV(P,Q)  ≤  12 DKL(P∥Q).(25.3.7)\textrm{TV}(P, Q) \;\leq\; \sqrt{ \tfrac{1}{2}\, D_{\textrm{KL}}(P\|Q) }. \tag{25.3.7}

Proof. Step 1: two outcomes. Write Ber(a)\textrm{Ber}(a) for a coin with heads probability aa. For a∈[0,1]a \in [0, 1] and b∈(0,1)b \in (0, 1), we have DKL(Ber(a)∥Ber(b))=alog⁡ab+(1−a)log⁡1−a1−bD_{\textrm{KL}}(\textrm{Ber}(a)\|\textrm{Ber}(b)) = a \log\frac{a}{b} + (1-a) \log\frac{1-a}{1-b} and TV(Ber(a),Ber(b))=∣a−b∣\textrm{TV}(\textrm{Ber}(a), \textrm{Ber}(b)) = |a - b|. We therefore need to show that ρ(a)=DKL(Ber(a)∥Ber(b))−2(a−b)2\rho(a) = D_{\textrm{KL}}(\textrm{Ber}(a)\|\textrm{Ber}(b)) - 2(a - b)^2 is non-negative. Fix bb. Clearly ρ(b)=0\rho(b) = 0, the first derivative ρ′(a)=log⁡ab−log⁡1−a1−b−4(a−b)\rho'(a) = \log\frac{a}{b} - \log\frac{1-a}{1-b} - 4(a - b) also vanishes at a=ba = b, and for 0<a<10 \lt a \lt 1 the second derivative is

ρ′′(a)=1a(1−a)−4≥0,\rho''(a) = \frac{1}{a(1-a)} - 4 \geq 0,

because a(1−a)≤14a(1-a) \leq \tfrac{1}{4}. A convex function with both value and slope zero at a=ba = b is non-negative everywhere on (0,1)(0, 1), and by continuity (with 0log⁡0=00 \log 0 = 0) at the endpoints a=0a = 0 and a=1a = 1 as well. If bb is 00 or 11, the KL divergence is infinite unless a=ba = b, so the inequality holds trivially.

Step 2: reduction to two outcomes. Let A⋆={p>q}A^\star = \{p > q\}, and set a=P(A⋆)a = P(A^\star) and b=Q(A⋆)b = Q(A^\star), so that TV(P,Q)=a−b\textrm{TV}(P, Q) = a - b. Coarsening the space by merging all outcomes inside A⋆A^\star into one bucket and all outcomes outside A⋆A^\star into another can only decrease KL. That follows from the log-sum inequality: for non-negative numbers viv_i and wiw_i,

∑ivilog⁡viwi≥(∑ivi)log⁡∑ivi∑iwi,\sum_i v_i \log\frac{v_i}{w_i} \geq \big(\sum_i v_i\big) \log \frac{\sum_i v_i}{\sum_i w_i},

which is itself one application of Jensen's inequality to t↦tlog⁡tt \mapsto t\log t. Applying it separately to A⋆A^\star and its complement gives

DKL(P∥Q)  ≥  DKL(Ber(a)∥Ber(b))  ≥  2(a−b)2=2 TV(P,Q)2,D_{\textrm{KL}}(P\|Q) \;\geq\; D_{\textrm{KL}}(\textrm{Ber}(a)\|\textrm{Ber}(b)) \;\geq\; 2(a - b)^2 = 2\,\textrm{TV}(P, Q)^2,

which rearranges to (25.3.7). ■\blacksquare

On our running pair, Pinsker's inequality reads 0.4≤0.396/2=0.4450.4 \leq \sqrt{0.396/2} = 0.445, and we can trace Step 2 by hand. Merging the second and third outcomes collapses the pair to two coins, (0.6,0.4)(0.6, 0.4) and (0.2,0.8)(0.2, 0.8). Their TV distance remains 0.40.4, their KL divergence drops from 0.3960.396 to 0.6log⁡3+0.4log⁡12=0.3820.6 \log 3 + 0.4 \log \tfrac12 = 0.382 nats, and Step 1 lower bounds that by 2 (0.4)2=0.322\,(0.4)^2 = 0.32.

For a coin of bias 0.60.6 compared with a fair coin, KL is 0.6log⁡1.2+0.4log⁡0.8=0.02010.6 \log 1.2 + 0.4 \log 0.8 = 0.0201 nats, while 2 (0.1)2=0.022\,(0.1)^2 = 0.02: for nearly equal coins, Pinsker's inequality is almost tight. Crucially, however, there is no useful converse. A model that puts q=0q = 0 on a single rare outcome has infinite KL even when its TV distance is tiny. The bound also becomes vacuous for well-separated distributions, since TV can never exceed 11: two unit-variance Gaussians separated by 5050 have TV≈1\textrm{TV} \approx 1, whereas (25.3.1) gives KL =502/2=1250= 50^2/2 = 1250 nats.

How tight is Pinsker's bound on typical distributions, and can the constant 12\tfrac12 be improved? To check the first question, we sample 10,00010{,}000 random pairs of distributions on five outcomes uniformly from the simplex. To check the second, we let two coins approach each other at a=b=12a = b = \tfrac12: Step 1 lost nothing to second order there, so the ratio TV/KL/2\textrm{TV}/\sqrt{\textrm{KL}/2} should climb all the way to 11.

rng = np.random.default_rng()
pr, qr = rng.dirichlet(np.ones(5), 10000), rng.dirichlet(np.ones(5), 10000)
tv = 0.5 * np.abs(pr - qr).sum(axis=1)
kl_pq = np.sum(pr * np.log(pr / qr), axis=1)
ratio = tv / np.sqrt(kl_pq / 2)
print(f'TV / sqrt(KL/2) over 10,000 random pairs: median {np.median(ratio):.2f}, '
      f'max {ratio.max():.3f}')
for delta in (0.1, 0.01, 0.001):
    a, b = np.array([0.5, 0.5]), np.array([0.5 + delta, 0.5 - delta])
    print(f'coins 1/2 vs 1/2 + {delta:5}: TV / sqrt(KL/2) = '
          f'{delta / np.sqrt(kl_div(a, b) / 2):.6f}')
grid = np.linspace(0, 3, 100)
d2l.plot([kl_pq[:2000], grid], [tv[:2000], np.sqrt(grid / 2)], 'KL (nats)',
         'TV', legend=['random pairs', 'sqrt(KL/2)'], fmts=('.', '-'),
         xlim=[0, 3], figsize=(4.5, 3))
TV / sqrt(KL/2) over 10,000 random pairs: median 0.78, max 0.986
coins 1/2 vs 1/2 +   0.1: TV / sqrt(KL/2) = 0.989881
coins 1/2 vs 1/2 +  0.01: TV / sqrt(KL/2) = 0.999900
coins 1/2 vs 1/2 + 0.001: TV / sqrt(KL/2) = 0.999999

Every point in the scatter plot falls beneath the curve KL/2\sqrt{\textrm{KL}/2}. The median pair sits near 0.80.8 of the bound, and the tightest of the 10,00010{,}000 random pairs comes within a few hundredths of 11. Once KL>2\textrm{KL} > 2, the curve rises above 11 and the bound becomes vacuous. For nearly fair coins, the ratio climbs through 0.98990.9899, 0.99990.9999, and 0.9999990.999999, confirming that the constant 12\tfrac{1}{2} cannot be tightened further (Exercise 4c). Pinsker's original proof had a weaker constant; the sharp factor 12\tfrac{1}{2} is due to Csiszár (1967) and, independently, to Kullback and Kemperman.

25.3.3.3 Data Processing#

Can a feature extractor, a quantizer, or any other post-processing step make two distributions easier to tell apart? In Pinsker's proof, merging outcomes could only decrease KL, and the chain rule of Section 25.1.2.4 showed the same for discarding one variable of a pair. The same principle holds for every f-divergence and every form of processing. A channel κ(y∣x)\kappa(y \mid x) is any rule, deterministic or randomized, that transforms an input xx into an output yy. Passing PP through the channel produces the output distribution (Pκ)(y)=∑xκ(y∣x) p(x)(P\kappa)(y) = \sum_x \kappa(y \mid x)\, p(x), and passing QQ produces QκQ\kappa analogously.

Proposition (data processing for f-divergences). For any channel κ\kappa, convex ff with f(1)=0f(1) = 0, and P≪QP \ll Q on a finite or countable set,

Df(Pκ ∥ Qκ)  ≤  Df(P∥Q).(25.3.8)D_f(P\kappa \,\|\, Q\kappa) \;\leq\; D_f(P\|Q). \tag{25.3.8}

Proof. Since ∑yκ(y∣x)=1\sum_y \kappa(y \mid x) = 1, we can expand Df(P∥Q)=∑y∑xκ(y∣x) q(x) f(p(x)/q(x))D_f(P\|Q) = \sum_y \sum_x \kappa(y \mid x)\, q(x)\, f\big(p(x)/q(x)\big). Fix an output yy with (Qκ)(y)>0(Q\kappa)(y) > 0. The normalized weights κ(y∣x) q(x)/(Qκ)(y)\kappa(y \mid x)\, q(x) / (Q\kappa)(y) sum to one over xx, and averaging the density ratios p(x)/q(x)p(x)/q(x) under these weights yields (Pκ)(y)/(Qκ)(y)(P\kappa)(y)/(Q\kappa)(y). Applying Jensen's inequality to the inner sum therefore bounds it below by (Qκ)(y) f((Pκ)(y)/(Qκ)(y))(Q\kappa)(y)\, f\big((P\kappa)(y)/(Q\kappa)(y)\big), which is the yy-th term of Df(Pκ∥Qκ)D_f(P\kappa\|Q\kappa). Any output yy with (Qκ)(y)=0(Q\kappa)(y) = 0 must have κ(y∣x)=0\kappa(y \mid x) = 0 wherever q(x)>0q(x) > 0, hence (Pκ)(y)=0(P\kappa)(y) = 0 as well, contributing nothing to either side. Summing over yy completes the proof. ■\blacksquare

No matter which divergence function we pick, processing data can never make two distributions more distinguishable. In deep learning, this inequality is often read in reverse: a divergence measured on learned features, quantized outputs, or any other summary of the data is always a lower bound on the divergence between the raw inputs. Merging outcomes in Pinsker's proof is simply the deterministic channel x↦1(x∈A⋆)x \mapsto \mathbf{1}(x \in A^\star). On our running pair, it lowered KL from 0.3960.396 to 0.3820.382, and it lowers χ2\chi^2 from 1.0131.013 to 0.16/0.2+0.16/0.8=10.16/0.2 + 0.16/0.8 = 1. The KL special case is also the engine behind the data-processing inequality for mutual information in Section 25.6.2.2.

An invertible transformation acts as a channel in both directions, forcing equality in (25.3.8): every f-divergence is invariant to relabeling outcomes or rescaling the axes. By contrast, the last two rows of Table 25.3.1 do change when you rescale the axis. What those distances gain in geometric sensitivity to dd, they give up in scale invariance.

25.3.4 The Fenchel Conjugate and the Critic Bound#

Suppose we have a generative network that produces samples from a distribution QQ, alongside a dataset of samples from the true distribution PP. Neither collection of samples gives us access to the density ratio p(x)/q(x)p(x)/q(x) required by (25.3.2). Can we still estimate DfD_f from samples alone? As it turns out, we can lower-bound any f-divergence using only expectations under PP and QQ, and that lower bound turns any divergence into an adversarial training objective.

25.3.4.1 Derivation of the Bound#

The key tool is the convex conjugate from Section 23.7.4. For a convex function ff, its conjugate is defined by

f∗(t)=sup⁡u (ut−f(u)),f^*(t) = \sup_{u}\, \big( ut - f(u) \big),

so that −f∗(t)-f^*(t) is the vertical intercept of the tangent line to ff with slope tt (Figure 25.3.3). For f(u)=ulog⁡uf(u) = u \log u, the supremum occurs where log⁡u+1=t\log u + 1 = t, giving f∗(t)=et−1f^*(t) = e^{t-1}. Because a convex function always lies above every one of its tangent lines, we obtain the Fenchel--Young inequality:

f(u)≥ut−f∗(t)for all t,(25.3.9)f(u) \geq ut - f^*(t) \quad \textrm{for all } t, \tag{25.3.9}

with equality whenever tt matches the slope of ff at uu, namely t=f′(u)t = f'(u).

Figure 25.3.3:

A convex divergence function, here f(u)=ulog⁡uf(u)=u\log u, is the upper envelope of its tangent lines. The tangent of slope tt has vertical intercept −f∗(t)-f^*(t), here −et−1-e^{t-1}. Keeping a single tangent gives the Fenchel--Young lower bound, and taking the supremum over slopes recovers f(u)f(u). Applying this inequality pointwise to the density ratio yields the critic bound.

Now apply (25.3.9) pointwise inside the divergence, letting the slope vary with xx. Specifically, pick any function T(x)T(x), called the critic, and substitute u=p(x)/q(x)u = p(x)/q(x) and t=T(x)t = T(x).

Proposition (critic bound). Let ff be convex and differentiable with f(1)=0f(1) = 0, let P≪QP \ll Q, and let TT be any critic with EP∣T∣<∞\mathbb{E}_P|T| \lt \infty and EQ∣f∗(T)∣<∞\mathbb{E}_Q|f^*(T)| \lt \infty. Then

Df(P∥Q)  ≥  Ex∼P[T(x)]−Ex∼Q[f∗(T(x))].(25.3.10)D_f(P\|Q) \;\geq\; \mathbb{E}_{x \sim P}[T(x)] - \mathbb{E}_{x \sim Q}[f^*(T(x))]. \tag{25.3.10}

Moreover, the supremum of the right-hand side over all critics equals Df(P∥Q)D_f(P\|Q), and it is attained by T⋆(x)=f′(p(x)/q(x))T^\star(x) = f'\big(p(x)/q(x)\big) wherever T⋆T^\star is finite (Nguyen et al., 2010; Nowozin et al., 2016).

Proof. Multiply (25.3.9) at u=p(x)/q(x)u = p(x)/q(x) and t=T(x)t = T(x) by q(x)≥0q(x) \geq 0:

q(x) f ⁣(p(x)q(x))  ≥  p(x) T(x)−q(x) f∗(T(x)).q(x)\, f\!\left(\frac{p(x)}{q(x)}\right) \;\geq\; p(x)\, T(x) - q(x)\, f^*(T(x)).

Summing (or integrating) over xx yields (25.3.10). Because Fenchel--Young holds with equality at t=f′(u)t = f'(u), choosing T⋆(x)=f′(p(x)/q(x))T^\star(x) = f'(p(x)/q(x)) turns the inequality into an equality at every xx, and therefore in expectation as well. ■\blacksquare

Both terms on the right-hand side of (25.3.10) are simple expectations, of TT over real data and of f∗(T)f^*(T) over generated samples, requiring no explicit densities at all. We can therefore parameterize TT as a neural network and train it on minibatches to maximize the bound. For KL divergence, the bound reads DKL(P∥Q)≥EP[T]−EQ[eT−1]D_{\textrm{KL}}(P\|Q) \geq \mathbb{E}_P[T] - \mathbb{E}_Q[e^{T - 1}] for every critic TT. The f-GAN of Nowozin et al. (2016) (after which (25.3.10) is also called the f-GAN bound) turns this variational form into a two-player game: the critic maximizes the bound so that it tracks the divergence, while the generator producing QQ minimizes it. Section 16.2 implements and trains such pairs.

25.3.4.2 The Original GAN#

The original GAN of Goodfellow et al. (2014) falls right out of this framework when we choose f(u)=ulog⁡u−(u+1)log⁡(u+1)f(u) = u\log u - (u+1)\log(u+1). Its convex conjugate is f∗(t)=−log⁡(1−et)f^*(t) = -\log(1 - e^t) for t<0t \lt 0. If we write the critic as the logarithm of a discriminator D(x)∈(0,1)D(x) \in (0, 1), setting T=log⁡DT = \log D, the critic bound becomes the familiar binary cross-entropy game:

EP[T]−EQ[f∗(T)]=Ex∼P[log⁡D(x)]+Ex∼Q[log⁡(1−D(x))].\mathbb{E}_P[T] - \mathbb{E}_Q[f^*(T)] = \mathbb{E}_{x \sim P}[\log D(x)] + \mathbb{E}_{x \sim Q}[\log(1 - D(x))].

The optimal critic T⋆=f′(p/q)=log⁡(p/(p+q))T^\star = f'(p/q) = \log\big(p/(p+q)\big) corresponds to the Bayes-optimal discriminator D⋆=p/(p+q)D^\star = p/(p + q). Comparing with the Jensen--Shannon divergence function fJSf_{\textrm{JS}} in Table 25.3.2, we find that f(u)=2fJS(u)−(u−1)log⁡2−log⁡4f(u) = 2 f_{\textrm{JS}}(u) - (u - 1)\log 2 - \log 4: twice the Jensen--Shannon function, plus a linear term in u−1u - 1 (which changes no divergence) and the constant f(1)=−log⁡4f(1) = -\log 4 (which only shifts the value). At D⋆D^\star, the minimax game therefore achieves 2 JS(P,Q)−log⁡42\,\textrm{JS}(P, Q) - \log 4, which on our running pair equals 2(0.0911)−1.386=−1.2042(0.0911) - 1.386 = -1.204.

25.3.4.3 Limits of the Bound#

In practice, the critic bound runs into two limitations, and we can measure the first one directly. Because the bound is tight only at T⋆T^\star, an undertrained critic reports less than the true divergence. Let us watch how a critic closes that gap as it trains.

On our discrete running pair, expectations are simple sums and a critic is just a vector of three numbers, one per outcome. Take the χ2\chi^2 divergence, with f(u)=(u−1)2f(u) = (u-1)^2, conjugate f∗(t)=t+t2/4f^*(t) = t + t^2/4, and optimal critic T⋆=2(p/q−1)T^\star = 2(p/q - 1). (Here the conjugate takes the supremum over all real uu; Exercise 6a derives it and checks that this loses nothing at T⋆T^\star.) Starting from T=0T = 0, where the bound is 00, we run gradient ascent with step size 11 on the bound, whose derivative with respect to T(x)T(x) is p(x)−q(x) (1+T(x)/2)p(x) - q(x)\,\big(1 + T(x)/2\big). By the proposition, the ascent must climb from below toward χ2(P∥Q)=1.0133\chi^2(P\|Q) = 1.0133. As a sanity check, the final line also evaluates an overconfident critic 2T⋆2T^\star that points in the right direction but overshoots by a factor of two.

def fgan_bound(T, p, q, f_star):
    """E_P[T] - E_Q[f*(T)]: a lower bound on D_f(P||Q) for any critic T."""
    return np.sum(p * T) - np.sum(q * f_star(T))

chi2_star = lambda t: t + t ** 2 / 4    # conjugate of f(u) = (u - 1)^2
exact = f_divergence(generators['chi^2'], p, q)
T, bounds = np.zeros(3), []             # untrained critic: one number per outcome
for step in range(51):
    bounds.append(fgan_bound(T, p, q, chi2_star))
    T = T + (p - q * (1 + T / 2))       # ascent step 1 along p - q f*'(T)
shown = (0, 1, 2, 5, 10, 20, 50)
print(f'exact chi^2(P||Q) = {exact:.4f}')
print('steps ' + ''.join(f'{s:8d}' for s in shown))
print('bound ' + ''.join(f'{bounds[s]:8.4f}' for s in shown))
T_star = 2 * (p / q - 1)                # the optimal critic f'(p/q)
print(f'overconfident critic 2 T*: bound = '
      f'{fgan_bound(2 * T_star, p, q, chi2_star):.4f}')
d2l.plot(np.arange(51), [bounds, [exact] * 51], 'ascent steps', 'value',
         legend=['bound', 'chi^2(P||Q)'], figsize=(4.5, 2.8))
exact chi^2(P||Q) = 1.0133
steps        0       1       2       5      10      20      50
bound   0.0000  0.2240  0.3935  0.7036  0.9107  1.0013  1.0133
overconfident critic 2 T*: bound = 0.0000

After five steps, the critic certifies 0.700.70 out of 1.011.01 (about 70%70\% of the true divergence); after ten steps it reaches 90%90\%, and after twenty steps 99%99\%. At no point does the bound overshoot the true value. Even the overconfident critic 2T⋆2T^\star reports zero, no better than an untrained critic. With exact expectations, any adversarial divergence estimate is an underestimate, and the number itself never reveals how much is missing. When expectations are replaced by finite-sample averages, it remains a valid lower bound (in expectation) only on fresh samples that were not used to train the critic.

The gradient update also reveals where the missing divergence hides. Because T(x)T(x) moves at a rate proportional to q(x)q(x), the critic learns slowest where the model's samples are rarest. On our running pair, that is the first outcome, where the density ratio is largest and most of the divergence lives (Exercise 6b).

The second limitation brings us back to disjoint supports, now viewed from the critic's perspective. Extending T⋆=f′(p/q)T^\star = f'(p/q) formally beyond the hypothesis P≪QP \ll Q, the ratio equals ∞\infty on the data support and 00 on the model support, so T⋆T^\star takes the constant value f′(∞)f'(\infty) on one and f′(0)f'(0) on the other. Because the generator updates its parameters using the gradient of the critic evaluated at its own samples, a critic that is locally constant there provides zero gradient. The generator receives no signal telling it which way to move.

Both limitations stem from the same root: the density ratio p/qp/q. Finite samples never hand us p/qp/q directly, and whenever the supports separate, the ratio collapses to 00 or ∞\infty and erases all trace of how far apart the distributions sit. In Section 25.4, we turn to comparisons that do not rely on density ratios at all.

25.3.5 Summary#

KL divergence is one member of a broad family: an f-divergence averages a convex function of the density ratio p/qp/q under QQ. Jensen's inequality guarantees non-negativity for every member, all smooth members agree up to their curvature f′′(1)f''(1) when PP and QQ are close, and no post-processing of the data can increase any f-divergence. Choosing the direction of KL is a modeling decision: forward KL (maximum likelihood) stretches to cover every mode, whereas reverse KL (variational inference) can lock onto a single one. Operationally, the best single-sample test distinguishing PP from QQ succeeds with probability 12(1+TV)\tfrac12(1 + \textrm{TV}), and Pinsker's inequality bounds TV by DKL/2\sqrt{D_{\textrm{KL}}/2}. Using the convex conjugate, any critic lower-bounds DfD_f via expectations over PP and QQ alone, which is how adversarial training estimates divergences from samples. Yet every member shares the same blind spot of the density ratio: on disjoint supports, DfD_f freezes at the constant f(0)+f′(∞)f(0) + f'(\infty), regardless of how near or far the supports lie.

25.3.6 Exercises#

  1. Divergence functions and argument order. Each row of Table 25.3.2 is one choice of ff in the template (25.3.2).

    1. For a divergence function ff, define f⋄(u)=u f(1/u)f^\diamond(u) = u\, f(1/u). Show that f⋄f^\diamond is convex with f⋄(1)=0f^\diamond(1) = 0 and that Df(Q∥P)=Df⋄(P∥Q)D_f(Q\|P) = D_{f^\diamond}(P\|Q). Compute f⋄f^\diamond for the KL function ulog⁡uu \log u and for the χ2\chi^2 function (u−1)2(u - 1)^2, and write the divergence that the second one defines as a sum over xx.
    2. Adding c (u−1)c\,(u - 1) to ff does not change the divergence. Show that DfD_f is symmetric whenever f⋄(u)=f(u)+c (u−1)f^\diamond(u) = f(u) + c\,(u - 1) for some constant cc. Check this condition for the rows of Table 25.3.2 that are marked symmetric, and show that Dfα(Q∥P)=Df1−α(P∥Q)D_{f_\alpha}(Q\|P) = D_{f_{1-\alpha}}(P\|Q) for the function (25.3.4). Use this identity to confirm the entry of the α\alpha-divergence row in the "Symmetric?" column.
    3. Expand (25.3.3) in terms of the ratio u=p/qu = p/q and recover the Jensen--Shannon function of Table 25.3.2.
    4. Rényi divergence. For α>0\alpha > 0, α≠1\alpha \neq 1, define Dα(P∥Q)=1α−1log⁡∑xp(x)α q(x)1−αD_\alpha(P\|Q) = \frac{1}{\alpha - 1} \log \sum_x p(x)^\alpha\, q(x)^{1-\alpha} (Rényi, 1961). Show that ∑xpαq1−α=1+α(α−1) Dfα(P∥Q)\sum_x p^\alpha q^{1-\alpha} = 1 + \alpha(\alpha - 1)\, D_{f_\alpha}(P\|Q) and that DαD_\alpha is an increasing function of DfαD_{f_\alpha}. Deduce that Dα≥0D_\alpha \geq 0 and that DαD_\alpha satisfies the data-processing inequality (25.3.8). Find lim⁡α→0Dα(P∥Q)\lim_{\alpha \to 0} D_\alpha(P\|Q), and explain why it is 00 whenever pp and qq are positive on the same outcomes. Why does the Rényi family therefore not reach reverse KL as α→0\alpha \to 0, while DfαD_{f_\alpha} does?
  2. Metrics among divergences. The last column of Table 25.3.2 records which divergences are metrics.

    1. Prove the triangle inequality for TV from its half-ℓ1\ell_1 form.
    2. Show that the Hellinger distance dH=dH2d_{\mathrm{H}} = \sqrt{d_{\mathrm{H}}^2} is the Euclidean distance between the vectors (p(x))x\big(\sqrt{p(x)}\big)_x and (q(x))x\big(\sqrt{q(x)}\big)_x, so that dHd_{\mathrm{H}} is a metric. Find three distributions on two outcomes for which both dH2d_{\mathrm{H}}^2 and JS violate the triangle inequality.
  3. Cost of dropping a mode. A model QQ misses part of a data distribution PP.

    1. Let P=(1−w) Q+wRP = (1 - w)\, Q + w R with 0<w<10 \lt w \lt 1, where RR is a distribution on a set to which QQ assigns probability zero. Compute DKL(P∥Q)D_{\textrm{KL}}(P\|Q), DKL(Q∥P)D_{\textrm{KL}}(Q\|P), TV(P,Q)\textrm{TV}(P, Q) and dH2(P,Q)d_{\mathrm{H}}^2(P, Q) as functions of ww. Which direction of KL penalizes the missing mass? How do the finite divergences behave as w→0w \to 0?
    2. Now let P=w1N(m1,s2)+w2N(m2,s2)P = w_1 \mathcal{N}(m_1, s^2) + w_2 \mathcal{N}(m_2, s^2) with separation Δ=∣m2−m1∣\Delta = |m_2 - m_1|, and let the model Q=N(m1,s2)Q = \mathcal{N}(m_1, s^2) keep only the first component. Show that DKL(Q∥P)≤log⁡(1/w1)D_{\textrm{KL}}(Q\|P) \leq \log(1/w_1) for every Δ\Delta, with equality in the limit Δ→∞\Delta \to \infty, and relate the limit to (a). Use the bound to explain the two reverse-KL values, 0.3560.356 and 1.2021.202 nats, that the fit of a Gaussian to the mixture prints.
    3. Assume that the components are well separated, so that almost every xx has non-negligible density under at most one of them. Show that DKL(P∥Q)≈w2Δ2/(2s2)+w1log⁡w1+w2log⁡w2D_{\textrm{KL}}(P\|Q) \approx w_2 \Delta^2 / (2 s^2) + w_1 \log w_1 + w_2 \log w_2. Evaluate the approximation for the mixture of that experiment, and check it with kl(log_p(x), log_q(x, [-2.0, np.log(0.6)]), x). The experiment prints two values in its forward-KL column to compare with: 5.9815.981 nats at the reverse-KL fit, whose parameters (−1.998,0.603)(-1.998, 0.603) are not quite (−2,0.6)(-2, 0.6), and 0.5580.558 nats at the forward fit.
  4. Local quadratic regime. The proposition (25.3.5) bounds the gap between DfD_f and 12f′′(1) χ2\tfrac12 f''(1)\, \chi^2.

    1. Exercise 7b of Section 25.1 finds the order at which the two directions of the Gaussian KL divergence first differ. Which property of the KL and reverse-KL functions explains that order? Carry the Taylor expansion of the proof one term further, and show that the ratio which the local-quadratic experiment prints is 1+c t f′′′(1)/f′′(1)+O(t2)1 + c\, t\, f'''(1)/f''(1) + O(t^2) with c=EQ[(u−1)3]/(3χ2(P∥Q))c = \mathbb{E}_Q[(u - 1)^3] / \big(3 \chi^2(P\|Q)\big), about 0.490.49 for the running pair. Compute f′′′(1)/f′′(1)f'''(1)/f''(1) for the four functions. Why is the shortfall of reverse KL at t=0.01t = 0.01 twice that of KL, and why do squared Hellinger and JS print the same value there?
    2. The total-variation function 12∣u−1∣\tfrac{1}{2}|u - 1| has no second derivative at 11. Let Pt=Q+t (P−Q)P_t = Q + t\,(P - Q) for t∈(0,1]t \in (0, 1]. How do TV(Pt,Q)\textrm{TV}(P_t, Q) and χ2(Pt∥Q)\chi^2(P_t\|Q) scale with tt? Explain from this why Pinsker's inequality (25.3.7) bounds TV by the square root of KL and not by a multiple of KL.
    3. Let PP and QQ be coins with heads probabilities bb and b+δb + \delta. Use the proposition to show that DKL(P∥Q)=δ2/(2b(1−b))+O(δ3)D_{\textrm{KL}}(P\|Q) = \delta^2 / \big(2 b (1 - b)\big) + O(\delta^3). Exercise 5a of Section 25.2 obtains the same expansion directly from the Bernoulli KL divergence. The factor 1/(b(1−b))1/\big(b(1-b)\big) is the Fisher information of the coin (Section 24.6). Find the limit of TV/DKL/2\textrm{TV}/\sqrt{D_{\textrm{KL}}/2} as δ→0\delta \to 0, and the values of bb for which Pinsker's inequality is tight in this limit. The coin experiment that follows Pinsker's inequality takes b=12b = \tfrac{1}{2} and prints a ratio of 0.9999990.999999 at δ=10−3\delta = 10^{-3}. Why does the ratio approach 11 at the rate δ2\delta^2 and not δ\delta?
  5. Hellinger bounds and testing. ● Pinsker's inequality is vacuous once DKL>2D_{\textrm{KL}} > 2. Let BC(P,Q)=∑xp(x)q(x)=1−12dH2(P,Q)\textrm{BC}(P, Q) = \sum_x \sqrt{p(x) q(x)} = 1 - \tfrac{1}{2} d_{\mathrm{H}}^2(P, Q) be the Bhattacharyya coefficient.

    1. Prove that 12dH2(P,Q)≤TV(P,Q)\tfrac{1}{2} d_{\mathrm{H}}^2(P, Q) \leq \textrm{TV}(P, Q), using ∣p−q∣=∣p−q∣ (p+q)|p - q| = |\sqrt{p} - \sqrt{q}|\,(\sqrt{p} + \sqrt{q}).
    2. Prove that TV(P,Q)≤dH(P,Q)1−14dH2(P,Q)\textrm{TV}(P, Q) \leq d_{\mathrm{H}}(P, Q) \sqrt{1 - \tfrac{1}{4} d_{\mathrm{H}}^2(P, Q)} with the Cauchy--Schwarz inequality of Section 21.1, and rewrite the bound as TV2≤1−BC2\textrm{TV}^2 \leq 1 - \textrm{BC}^2.
    3. Use Jensen's inequality to show that BC(P,Q)≥e−DKL(P∥Q)/2\textrm{BC}(P, Q) \geq e^{-D_{\textrm{KL}}(P\|Q)/2}, and conclude the Bretagnolle--Huber inequality TV(P,Q)≤1−e−DKL(P∥Q)\textrm{TV}(P, Q) \leq \sqrt{1 - e^{-D_{\textrm{KL}}(P\|Q)}} (Bretagnolle and Huber, 1978; Canonne, 2022). For which values of DKLD_{\textrm{KL}} is it tighter than (25.3.7)?
    4. A test sees nn independent samples, all from PP or all from QQ, each case with probability 12\tfrac{1}{2}. Use (c) and the additivity of KL over independent samples from Section 25.1.2.4 to show that every test errs with probability at least 14e−nDKL(P∥Q)\tfrac{1}{4} e^{-n D_{\textrm{KL}}(P\|Q)}. How many samples does an error probability of at most η\eta require? Why can Pinsker's inequality not give a bound of this kind?
  6. Conjugates and critics. The critic bound (25.3.10) needs the conjugate f∗f^* of the divergence function.

    1. Derive the conjugate f∗(t)=t+t2/4f^*(t) = t + t^2/4 of the χ2\chi^2 function f(u)=(u−1)2f(u) = (u - 1)^2, taking the supremum over u∈Ru \in \mathbb{R}, and its optimal critic T⋆=2(p/q−1)T^\star = 2(p/q - 1). Redo the computation over the function's domain u∈(0,∞)u \in (0, \infty), and show that the supremum is t+t2/4t + t^2/4 for t≥−2t \geq -2 and −1-1 for t≤−2t \leq -2.
    2. Use the R\mathbb{R}-conjugate. Show that for every critic TT the gap between χ2(P∥Q)\chi^2(P\|Q) and the bound equals 14EQ[(T−T⋆)2]\tfrac{1}{4} \mathbb{E}_Q\big[(T - T^\star)^2\big]. Deduce that a scaled critic T=c T⋆T = c\,T^\star certifies χ2(P∥Q) (2c−c2)\chi^2(P\|Q)\,(2c - c^2), which explains the value that the experiment prints for 2T⋆2T^\star. Show that one ascent step of the experiment multiplies T(x)−T⋆(x)T(x) - T^\star(x) by 1−q(x)/21 - q(x)/2. Which outcome of the running pair accounts for most of the gap after ten steps, and why? For which step sizes does the ascent converge?
    3. Compute the conjugate of the total-variation function f(u)=12∣u−1∣f(u) = \tfrac{1}{2}|u - 1| over u∈(0,∞)u \in (0, \infty). Show that critics with values in [−12,12][-\tfrac{1}{2}, \tfrac{1}{2}] suffice to maximize the bound, and conclude that TV(P,Q)=sup⁡g(EP[g]−EQ[g])\textrm{TV}(P, Q) = \sup_g \big(\mathbb{E}_P[g] - \mathbb{E}_Q[g]\big) over all functions gg with ∣g∣≤12|g| \leq \tfrac{1}{2}, a supremum of the kind that Section 25.4.1 studies. Which gg attains it?

Discussions