SafeZone AI Learn
Learn/ Mathematical Foundations/ Information Theory · lesson 2 of 3

KL divergence and mutual information

The gap between believed and true, made rigorous — its famous asymmetry felt by fitting one Gaussian to two humps both ways — plus mutual information, and the reveal that half this curriculum's objectives were KL all along.

Last lesson ended on a decomposition: coding with wrong beliefs costs H(p)+KL(pq)H(p) + \mathrm{KL}(p\|q) — the truth’s own entropy plus a penalty for being wrong. This lesson is about the penalty. Kullback–Leibler divergence is the curriculum’s most-used and least-explained quantity: it has appeared in the MLE lesson (in disguise), t-SNE’s objective, the RLHF leash, DPO’s derivation, speculative decoding’s proof and the scaling-laws lesson — always with an IOU for a real definition. Today the IOU is paid, and the asymmetry that most treatments mumble past becomes something you have felt with a slider.

The definition, and the three readings

KL(pq)  =  xp(x)logp(x)q(x)  =  Exp ⁣[logp(x)q(x)]\mathrm{KL}(p \,\|\, q) \;=\; \sum_x p(x) \log \frac{p(x)}{q(x)} \;=\; \mathbb{E}_{x \sim p}\!\left[\log \frac{p(x)}{q(x)}\right]

Three equivalent readings, each doing work somewhere in the curriculum: the coder’s (last lesson): extra bits per symbol for using code qq on source pp; the gambler’s: the expected log-loss of betting with beliefs qq against reality pp; the statistician’s: the expected log-likelihood-ratio between truth and model — the currency of hypothesis testing. Two properties are load-bearing everywhere: KL0\mathrm{KL} \ge 0 with equality iff p=qp = q (Gibbs’ inequality — via Jensen, the same convexity trick as the EM lesson), and it is not a distance: KL(pq)KL(qp)\mathrm{KL}(p\|q) \neq \mathrm{KL}(q\|p), no triangle inequality, no symmetry. That asymmetry is not a defect to apologize for — it encodes which mistakes you fear, and choosing a direction is choosing a failure mode:

Drag Q around and watch the two numbers move independently — already proof of asymmetry. Now press min KL(P‖Q): the fit spreads into one broad Gaussian covering BOTH humps, because P‖Q is an expectation under the TRUTH — any region where truth has mass but your model doesn’t (q ≈ 0, log p/q → ∞) is catastrophic, so the optimum is mass-covering: never assign near-zero to something real, even at the cost of believing in the empty valley between the humps. Press min KL(Q‖P): the fit snaps tight onto ONE hump — this direction is an expectation under YOUR MODEL, punished only where the model puts mass that truth lacks, so the optimum is mode-seeking: commit to one plausible reality, ignore the other. Neither is “correct”; they fear different errors. MLE trains with the first (cover the data); variational inference and RLHF’s leash use flavors of the second (stay where the reference has mass) — and now, when the alignment lesson’s β dial or t-SNE’s KL(P‖Q) choice comes up, you know exactly which philosophy was purchased.

The curriculum, re-read through KL

The reveal this module exists for — objectives you have already met, unmasked as divergences:

  • Maximum likelihood minimizes KL(p^datapθ)\mathrm{KL}(\hat p_{\text{data}} \| p_\theta) — the MLE lesson’s closing identity, now with its information-theoretic meaning attached: fit = compress.
  • Every loss curve in the scaling-laws lesson is H(text)+KL(textmodel)H(\text{text}) + \mathrm{KL}(\text{text} \| \text{model}) descending toward the irreducible floor.
  • t-SNE minimizes KL(PQ)\mathrm{KL}(P\|Q) over layouts — mass-covering, which is why it refuses to separate points that are neighbors in the data (truth-mass must be covered) while feeling free to invent gaps.
  • RLHF’s leash βKL(ππref)\beta\,\mathrm{KL}(\pi \| \pi_{\text{ref}}) is reverse-KL-shaped — mode-seeking: stay inside the reference’s mass, the exact philosophy the widget’s second button demonstrated. DPO’s derivation solved this objective in closed form.
  • EM’s ELBO gap is exactly KL(qposterior)\mathrm{KL}(q \| \text{posterior}) — the bound is tight when the divergence is zero.
  • Speculative decoding’s acceptance rule works because the correction distribution is built from where draft and target disagree — a KL-flavored accounting of disagreement.

One derived quantity earns its keep so often it needs its own name. Mutual information is the KL between a joint distribution and the independent world it would be if its variables ignored each other:

I(X;Y)  =  KL(p(x,y)p(x)p(y))  =  H(X)H(XY)I(X; Y) \;=\; \mathrm{KL}\big(p(x,y) \,\|\, p(x)\,p(y)\big) \;=\; H(X) - H(X \mid Y)

— the bits of uncertainty about XX that observing YY removes. Zero iff independent, and unlike correlation it catches any dependence, not just linear (the U-shaped relationship that correlation scores at zero, MI sees plainly). It appears across the field as the honest currency of “how much does this tell me about that”: feature selection, the information bottleneck view of deep nets, decision-tree splits (information gain is exactly II between split and label — the trees lesson’s Gini was a cheap cousin), and channel capacity, where Shannon defined it first.

Practical fine print, learned the hard way by everyone: KL is infinite when qq misses pp‘s support — the reason label smoothing exists, why zero probabilities are clamped with ϵ\epsilon everywhere in this curriculum’s code, and why the widget clamps at 101210^{-12}; estimating KL and MI from samples is genuinely hard in high dimensions (naive histogram estimators fail; the field uses variational bounds); and when you need a symmetric, bounded compromise, Jensen–Shannon divergence (the symmetrized mixture form) is the standard — it is what the original GAN objective secretly minimized.

import numpy as np

# the asymmetry, computed: bimodal truth, one-Gaussian model
xs = np.linspace(-6, 6, 4001); dx = xs[1] - xs[0]
g = lambda x, m, s: np.exp(-(x - m)**2 / (2 * s * s)) / (s * np.sqrt(2 * np.pi))
P = 0.6 * g(xs, -1.6, 0.45) + 0.4 * g(xs, 1.8, 0.55)

def kl(a, b):
    a, b = np.maximum(a, 1e-12), np.maximum(b, 1e-12)
    return float(np.sum(a * np.log(a / b) * dx))

wide  = g(xs, -0.2, 1.75)   # ~the mass-covering optimum
tight = g(xs, -1.6, 0.45)   # ~the mode-seeking optimum
print(kl(P, wide),  kl(P, tight))   # P‖Q: wide wins (tight leaves a hump uncovered)
print(kl(wide, P),  kl(tight, P))   # Q‖P: tight wins (wide bets on the empty valley)

Exercises

Closing the module — and the Foundations track

  1. Prove Gibbs’ inequality KL(p‖q) ≥ 0 via Jensen on the convex −log (the EM lesson’s move). Where exactly does equality force p = q?
    Solution

    Worked solutions are part of Premiumunlock all of them for £5/month →

  2. 5 more exercises — each with a worked solution — are part of Premium. Unlock everything for £5/month →

References

  • S. Kullback & R. Leibler, “On Information and Sufficiency”, 1951.
  • T. Cover & J. Thomas, Elements of Information Theory, ch. 2, 8 — KL and mutual information, rigorously.
  • T. Minka, “Divergence measures and message passing”, 2005 — the mass-covering/mode-seeking taxonomy this lesson’s widget demonstrates.
  • D. MacKay, Information Theory, Inference, and Learning Algorithms, ch. 8 — dependence and MI, with the curriculum’s own spirit.