Part III — Probability, Information Theory & Bayesian Inference · Chapter 9

KL divergence & mutual information

Hook

Chapter 9 measured surprise in bits for a single distribution. What if you have two distributions — the real one, and your model's guess at it — and want to measure how far apart they are?

Intuition
40%A40%B20%C
P = (0.50, 0.30, 0.20) | D_KL(P‖Q) = 0.0365 bits

P is fixed. Drag Q's bars around and watch the readout shrink toward zero as Q starts to look like P — and grow the more they disagree.

Formalize

The KL divergence — short for Kullback–Leibler divergence — measures the extra bits it costs to describe data from PP using a code built for QQ instead of PP's own optimal code:

DKL(PQ)=xP(x)log2P(x)Q(x)D_{KL}(P \Vert Q) = \sum_x P(x) \log_2 \frac{P(x)}{Q(x)}
  • DKL(PQ)D_{KL}(P\Vert Q) — the divergence itself, in bits: how many extra bits QQ's code wastes on data actually drawn from PP.
  • P(x)P(x) — the true probability of outcome xx.
  • Q(x)Q(x) — the model's (or approximate) probability of that same outcome xx.
  • xx — one outcome, ranging over everything either distribution assigns probability to.
  1. Never negative, zero only at a perfect match

    DKL(PQ)0D_{KL}(P\Vert Q) \geq 0 always, and it hits exactly 00 only when P=QP=Q everywhere — the only way to waste zero bits on QQ's code is for QQ to already be the right code.

  2. Not symmetric — direction matters

    DKL(PQ)DKL(QP)D_{KL}(P\Vert Q) \neq D_{KL}(Q\Vert P) in general. Despite measuring how "far apart" two distributions are, swapping which one is the reference changes the answer, so it isn't a true distance.

  3. Related to cross-entropy

    Cross-entropy H(P,Q)=xP(x)log2Q(x)H(P,Q) = -\sum_x P(x)\log_2 Q(x) always decomposes as H(P,Q)=H(P)+DKL(PQ)H(P,Q) = H(P) + D_{KL}(P\Vert Q) — Part II's cross-entropy loss chapter is exactly this H(P,Q)H(P,Q), with PP the true one-hot label.

Play
40%A40%B20%C
D_KL(P‖Q) = 0.0365 | D_KL(Q‖P) = 0.0372 — not the same number

Now watch both directions at once. DKL(PQ)D_{KL}(P\Vert Q) and DKL(QP)D_{KL}(Q\Vert P) move together, roughly, but they're never quite equal — drag until they're close and notice they still don't match exactly.

Worked example

With P=(0.5,0.3,0.2)P=(0.5,0.3,0.2) and Q=(0.4,0.4,0.2)Q=(0.4,0.4,0.2):

  1. KL divergence, one direction

    Compute each outcome's term separately:

    • 0.5log20.50.4=0.5log2(1.25)0.5(0.3219)0.16100.5\log_2\frac{0.5}{0.4}=0.5\log_2(1.25)\approx0.5(0.3219)\approx0.1610
    • 0.3log20.30.4=0.3log2(0.75)0.3(0.4150)0.12450.3\log_2\frac{0.3}{0.4}=0.3\log_2(0.75)\approx0.3(-0.4150)\approx-0.1245
    • The third term is exactly 00 since PP and QQ agree there (log2(1)=0\log_2(1)=0)

    Summing:

    DKL(PQ)0.16100.1245+00.0365 bitsD_{KL}(P\Vert Q) \approx 0.1610 - 0.1245 + 0 \approx 0.0365 \text{ bits}
  2. The reverse direction gives a different number

    Same process, swapped:

    • 0.4log20.40.5=0.4log2(0.8)0.4(0.3219)0.12880.4\log_2\frac{0.4}{0.5}=0.4\log_2(0.8)\approx0.4(-0.3219)\approx-0.1288
    • 0.4log20.40.3=0.4log2(1.333)0.4(0.4150)0.16600.4\log_2\frac{0.4}{0.3}=0.4\log_2(1.333)\approx0.4(0.4150)\approx0.1660
    • The third term is again 00

    Summing:

    DKL(QP)0.1288+0.1660+00.0372 bitsD_{KL}(Q\Vert P) \approx -0.1288+0.1660+0 \approx 0.0372 \text{ bits}

    — close to the forward value here, but not identical, confirming the asymmetry.

  3. Mutual information: KL applied to independence itself

    For a joint distribution over two binary variables, P(X=0,Y=0)=P(X=1,Y=1)=0.4P(X{=}0,Y{=}0){=}P(X{=}1,Y{=}1){=}0.4 and P(X=0,Y=1)=P(X=1,Y=0)=0.1P(X{=}0,Y{=}1){=}P(X{=}1,Y{=}0){=}0.1XX and YY agree far more than chance. Both marginals are uniform, (0.5,0.5)(0.5,0.5), so H(X)=H(Y)=1H(X)=H(Y)=1 bit.

    The joint entropy sums the same plog2p-p\log_2 p recipe over all four outcomes:

    • 0.4log2(0.4)0.5288-0.4\log_2(0.4)\approx0.5288, for each of the two 0.40.4 outcomes
    • 0.1log2(0.1)0.3322-0.1\log_2(0.1)\approx0.3322, for each of the two 0.10.1 outcomes
    H(X,Y)2(0.5288)+2(0.3322)1.7219 bitsH(X,Y) \approx 2(0.5288) + 2(0.3322) \approx 1.7219 \text{ bits}

    Mutual information is exactly the gap: I(X;Y)=H(X)+H(Y)H(X,Y)1+11.72190.2781I(X;Y)=H(X)+H(Y)-H(X,Y)\approx 1+1-1.7219\approx 0.2781 bits — equivalently, it's the KL divergence between the real joint distribution and the independent one its own marginals would predict. Zero mutual information means knowing XX tells you nothing about YY; here, it tells you a fair amount.

Checkpoint

Drag Q until D_KL(P‖Q) drops under 0.02 bits — i.e., reshape it to closely match P = (0.5, 0.3, 0.2).

40%A40%B20%C
D_KL(P‖Q) = 0.0365 bits
Drag a bar to try it
Summary
DKL(PQ)=xP(x)log2P(x)Q(x)I(X;Y)=x,yP(x,y)log2P(x,y)P(x)P(y)D_{KL}(P\Vert Q) = \sum_x P(x)\log_2\frac{P(x)}{Q(x)} \qquad\qquad I(X;Y) = \sum_{x,y} P(x,y)\log_2\frac{P(x,y)}{P(x)P(y)}

KL divergence is entropy's natural extension to comparing two distributions instead of describing one; mutual information is what happens when you apply that same comparison to a joint distribution against the independent version of itself. Both show up constantly downstream — in cross-entropy loss, in variational autoencoders' regularizer, and in feature-selection methods that rank inputs by how much information they share with the target.