Skip to content
AI360Xpert
Beta

Jensen-Shannon Divergence

KL divergence points one way and explodes on zeros. Jensen-Shannon meets in the middle: symmetric, always finite, capped at one bit.

Fair and 0.75-biased coins meet at their 0.625 midpoint, and the averaged KLs total 0.049 bits
Fair and 0.75-biased coins meet at their 0.625 midpoint, and the averaged KLs total 0.049 bits

Why Does This Exist?

DKL(P∣Q)D_{KL}(P\\|Q) is directed and infinite when QQ misses PP's support: useless for comparing disjoint generator outputs or text distributions with different vocabularies. Evaluation needs a symmetric, finite ruler that still respects information geometry.

JSD =tfrac12DKL(P∣M)+tfrac12DKL(Q∣M)= \\tfrac{1}{2}D_{KL}(P\\|M) + \\tfrac{1}{2}D_{KL}(Q\\|M) with M=(P+Q)/2M = (P+Q)/2 is that ruler: symmetric, in [0,1][0, 1] bits ([0,ln2][0, \\ln 2] nats), zero exactly at equality, with sqrtmathrmJSD\\sqrt{\\mathrm{JSD}} a metric. It symmetrizes KL divergence for the comparisons in mutual information (MI is a KL; JSD is its tamed cousin).

The one idea you need first

KL DKL(P∣Q)=sumPlog(P/Q)D_{KL}(P\\|Q) = \\sum P\\log(P/Q) prices extra bits using QQ where PP rules. The midpoint MM covers both supports, so neither KL explodes. Averaging removes direction.

Think of It Like This

Meeting halfway

Two hikers argue whose trailhead is better. Instead of pricing each from the other's start (KL, possibly impossible), both walk to the midpoint camp and price from there. Same camp both ways: symmetric, reachable, bounded effort.

Midpoint effort caps at the trail length (1 bit). Where it stops: halfway flattens real asymmetries. If direction matters (coding cost one way), keep KL.

How It Actually Works

Properties: 0leqmathrmJSDleq10 \\leq \\mathrm{JSD} \\leq 1 bit, symmetric, finite for all pairs, sqrtmathrmJSD\\sqrt{\\mathrm{JSD}} satisfies triangle inequality. Identity: I(X;Y)I(X;Y) with XX a fair mixture bit equals mathrmJSD(P∣Q)\\mathrm{JSD}(P\\|Q): JSD is the information a sample reveals about its source. Original GANs minimize JSD implicitly; evaluation uses it for stability where KL would be infinite. Estimate via plug-in on shared support (midpoint covers union) with smoothing for small samples.

Worked example

P=[0.5,0.5]P = [0.5, 0.5], Q=[0.75,0.25]Q = [0.75, 0.25], M=[0.625,0.375]M = [0.625, 0.375]. DKL(P∣M)=0.5log2(0.5/0.625)times2=log2(0.8)times−1D_{KL}(P\\|M) = 0.5\\log_2(0.5/0.625) \\times 2 = \\log_2(0.8) \\times -1... compute: 0.5log2(0.8)+0.5log2(1.3333)=0.5(−0.3219)+0.5(0.4150)=0.046550.5\\log_2(0.8) + 0.5\\log_2(1.3333) = 0.5(-0.3219) + 0.5(0.4150) = 0.04655. DKL(Q∣M)=0.75log2(1.2)+0.25log2(0.6667)=0.75(0.2630)+0.25(−0.5850)=0.1973−0.1462=0.05105D_{KL}(Q\\|M) = 0.75\\log_2(1.2) + 0.25\\log_2(0.6667) = 0.75(0.2630) + 0.25(-0.5850) = 0.1973 - 0.1462 = 0.05105. JSD =(0.04655+0.05105)/2approx0.0488= (0.04655 + 0.05105)/2 \\approx 0.0488... recompute precisely: first term 0.5times−0.321928=−0.1609640.5 \\times -0.321928 = -0.160964; second 0.5times0.415037=0.2075190.5 \\times 0.415037 = 0.207519; sum 0.0465550.046555. Second KL: 0.75times0.263034=0.1972760.75 \\times 0.263034 = 0.197276; 0.25times−0.584963=−0.1462410.25 \\times -0.584963 = -0.146241; sum 0.0510350.051035. Average 0.048795approx0.0490.048795 \\approx 0.049 bits. far under the 1-bit cap.

Code

import numpy as np
p = np.array([0.5, 0.5])q = np.array([0.75, 0.25])m = 0.5 * (p + q)jsd = 0.5 * (p * np.log2(p / m)).sum() + 0.5 * (q * np.log2(q / m)).sum()print(round(float(jsd), 3))  # -> 0.049

Code 0.049 pins the hand arithmetic. Prose, program, and diagram agree.

Watch Out For

Reading JSD saturation as progress

Near-disjoint distributions all score ~1 bit: JSD saturates and its gradient vanishes. GAN and embedding comparisons stall with "everything maximally different" while shapes differ wildly.

Use Wasserstein or MMD for geometry among far-apart laws. If JSD sits at 0.99+ across variants, the ruler is pegged.

Comparing JSD across different support sizes

Vocabulary or binning changes move the 1-bit cap's meaning: finer bins inflate attainable JSD without real change. Cross-tokenizer JSD leaderboards compare rulers, not models.

Fix the support and smoothing first. If bins differ, renormalize or switch metric.

The Quick Version

  • JSD midpoints KL: symmetric, finite, capped at 1 bit, square-root metric.
  • Fair versus 0.75-biased coins score about 0.049 bits.
  • MI with a mixture bit is exactly a JSD; original GANs optimize it implicitly.
  • Saturates near 1 for disjoint laws; use geometric metrics there.
  • Fix support and smoothing before comparing across setups.
  • Keep directed KL when coding direction is the question.