SECTION 07 / 7

Examples: KL, JS and total variation

3 min read
On this page 8 headings

KL divergence is generated by

f(u)=uln⁡uf(u)=u\ln u

(with

f(0)=0f(0)=0

by continuity), giving

DKL(P ∥ Q)=∫pln⁡ ⁣(pq)\begin{aligned}&D_{\mathrm{KL}}(P\,\|\,Q)\\&=\int p\ln\!\left(\frac{p}{q}\right)\end{aligned}

KL generally changes when P and Q are swapped; it is not a metric.

Figure 21. Divergence versus metric distance.
Figure 21. Divergence versus metric distance.
Open image ↗
Illustration: Examples: KL, JS and total variation

Jensen–Shannon divergence is based on the mixture M=P+Q2M=\frac{P+Q}{2}:

JS⁡(P,Q)=12KL⁡(P∥M)+12KL⁡(Q∥M)\begin{aligned}&\operatorname{JS}(P,Q)\\&=\frac12\operatorname{KL}(P\|M)\\&+\frac12\operatorname{KL}(Q\|M)\end{aligned}

It is symmetric. A standard generating function for the corresponding f-divergence is

f(u)=12[uln⁡2u1+u+ln⁡21+u]\begin{aligned}&f(u)\\&=\frac12\left[u\ln\frac{2u}{1+u}+\ln\frac{2}{1+u}\right]\end{aligned}

Total variation is

TV⁡(P,Q)=12∫∣p−q∣\operatorname{TV}(P,Q)=\frac12\int|p-q|

One generating function is

f(u)=∣u−1∣2f(u)=\frac{|u-1|}{2}

Different conventions sometimes absorb the factor 12\frac{1}{2}, so always check the definition used.

Asymmetry of KL

Figure 22. Forward and reverse KL.
Figure 22. Forward and reverse KL.
Open image ↗
Illustration: Examples: KL, JS and total variation
  1. Let P=(0.9,0.1)P=(0.9,0.1), Q=(0.5,0.5)Q=(0.5,0.5).

  2. KL⁡(P∥Q)=0.9ln⁡(1.8)+0.1ln⁡(0.2)≈0.3681\begin{aligned}&\operatorname{KL}(P\|Q)\\&=0.9 \ln (1.8)\\&+0.1 \ln (0.2)\\&\approx 0.3681\end{aligned}
  3. KL⁡(Q∥P)=0.5ln⁡(0.50.9)+0.5ln⁡(0.50.1)≈0.5108\begin{aligned}&\operatorname{KL}(Q\|P)\\&=0.5 \ln (\frac{0.5}{0.9})\\&+0.5 \ln (\frac{0.5}{0.1})\\&\approx 0.5108\end{aligned}

    The values differ.

KL divergence in both directions

Figure 23. KL for two coins.
Figure 23. KL for two coins.
Open image ↗
Illustration: Examples: KL, JS and total variation

Take P=(0.8,0.2)P=(0.8,0.2), Q=(0.5,0.5)Q=(0.5,0.5). Using natural logarithms,

KL⁡(P∥Q)=0.8ln⁡(0.80.5)+0.2ln⁡(0.20.5)=0.8ln⁡(1.6)+0.2ln⁡(0.4)≈0.1927 nats\begin{aligned}&\operatorname{KL}(P\|Q)\\&=0.8 \ln (\frac{0.8}{0.5})\\&+0.2 \ln (\frac{0.2}{0.5})\\&=0.8 \ln (1.6)\\&+0.2 \ln (0.4)\\&\approx 0.1927\,\text{nats}\end{aligned}

Reverse the roles:

KL⁡(Q∥P)=0.5ln⁡(0.50.8)+0.5ln⁡(0.50.2)=0.5ln⁡(0.625)+0.5ln⁡(2.5)≈0.2231 nats\begin{aligned}&\operatorname{KL}(Q\|P)\\&=0.5 \ln (\frac{0.5}{0.8})\\&+0.5 \ln (\frac{0.5}{0.2})\\&=0.5 \ln (0.625)\\&+0.5 \ln (2.5)\\&\approx 0.2231\,\text{nats}\end{aligned}

The log ratios and the probability weights both change, so the two directions need not agree.

Jensen–Shannon divergence

Figure 24. Jensen–Shannon mixture.
Figure 24. Jensen–Shannon mixture.
Open image ↗
Illustration: Examples: KL, JS and total variation

Use the same P=(0.8,0.2)P=(0.8,0.2) and Q=(0.5,0.5)Q=(0.5,0.5). Step 1: form M=P+Q2=(0.65,0.35)M=\frac{P+Q}{2}=(0.65,0.35). Step 2:

KL⁡(P∥M)=0.8ln⁡(0.80.65)+0.2ln⁡(0.20.35)≈0.0542\begin{aligned}&\operatorname{KL}(P\|M)\\&=0.8 \ln (\frac{0.8}{0.65})\\&+0.2 \ln (\frac{0.2}{0.35})\\&\approx 0.0542\end{aligned}

Step 3:

KL⁡(Q∥M)=0.5ln⁡(0.50.65)+0.5ln⁡(0.50.35)≈0.0472\begin{aligned}&\operatorname{KL}(Q\|M)\\&=0.5 \ln (\frac{0.5}{0.65})\\&+0.5 \ln (\frac{0.5}{0.35})\\&\approx 0.0472\end{aligned}

Step 4:

JS⁡(P,Q)=0.0542+0.04722≈0.0507 nats\begin{aligned}&\operatorname{JS}(P,Q)\\&=\frac{0.0542+0.0472}{2}\\&\approx 0.0507\,\text{nats}\end{aligned}

Swapping P and Q leaves M unchanged and merely swaps the two KL terms, so JS is symmetric.

Total variation

Figure 25. Total variation area.
Figure 25. Total variation area.
Open image ↗
Illustration: Examples: KL, JS and total variation

For P=(0.8,0.2)P=(0.8,0.2) and Q=(0.5,0.5)Q=(0.5,0.5),

TV⁡(P,Q)=12(∣0.8−0.5∣+∣0.2−0.5∣)=12(0.3+0.3)=0.3\begin{aligned}&\operatorname{TV}(P,Q)\\&=\frac{1}{2} (|0.8-0.5|+|0.2-0.5|)\\&=\frac{1}{2} (0.3+0.3)\\&=0.3\end{aligned}

The absolute values prevent positive and negative differences from canceling. Unlike KL and JS, the result here is not measured in nats.

How to recognize which formula to use

Figure 26. Four divergence comparisons.
Figure 26. Four divergence comparisons.
Open image ↗
Illustration: Examples: KL, JS and total variation

KL: multiply each P probability by ln⁡PQ\ln\frac{P}{Q} and add. JS: calculate the midpoint distribution M first, then average KL⁡(P∥M)\operatorname{KL}(P\|M) and KL⁡(Q∥M)\operatorname{KL}(Q\|M). TV: take absolute differences, sum and multiply by 12\frac{1}{2}. All three can compare the same P and Q, but their numerical answers are not interchangeable.

Practice with complete answers

Question 1: P=(0.6,0.4)P=(0.6,0.4), Q=(0.4,0.6)Q=(0.4,0.6). Find TV.

Show solution

TV⁡=12(∣0.6−0.4∣+∣0.4−0.6∣)=0.2\operatorname{TV}=\frac{1}{2}(|0.6-0.4|+|0.4-0.6|)=0.2.

Question 2: For the same P,Q, find KL⁡(P∥Q)\operatorname{KL}(P\|Q).

Show solution

0.6ln⁡(1.5)+0.4ln⁡(23)≈0.08110.6 \ln (1.5)+0.4 \ln (\frac{2}{3})\approx 0.0811 nats.

Question 3: Compute JS. M=(0.5,0.5)M=(0.5,0.5),

KL⁡(P∥M)=0.6ln⁡(1.2)+0.4ln⁡(0.8)≈0.02014\begin{aligned}&\operatorname{KL}(P\|M)\\&=0.6 \ln (1.2)\\&+0.4 \ln (0.8)\\&\approx 0.02014\end{aligned}

KL⁡(Q∥M)\operatorname{KL}(Q\|M) has the same value, so JS⁡≈0.02014\operatorname{JS}\approx 0.02014 nats.

Zero probabilities — careful calculation

For discrete KL, the term with pi=0p_{i}=0 contributes 0 by the standard limiting convention, provided qi>0q_{i}>0. If pi>0p_{i}>0 but qi=0q_{i}=0, KL⁡(P∥Q)\operatorname{KL}(P\|Q) is infinite. This explains why blindly entering ln⁡(0)\ln (0) or dividing by zero on a calculator gives misleading results. For JS, the mixture M avoids this particular infinite-KL issue when comparing finite discrete distributions.

All three on the same distributions

Question. Let P=(0.75,0.25)P=(0.75,0.25) and Q=(0.25,0.75)Q=(0.25,0.75). Find KL⁡(P∥Q)\operatorname{KL}(P\|Q), KL⁡(Q∥P)\operatorname{KL}(Q\|P), JS⁡(P,Q)\operatorname{JS}(P,Q), and TV⁡(P,Q)\operatorname{TV}(P,Q).

KL⁡(P∥Q)=0.75ln⁡(3)+0.25ln⁡(13)=(0.75−0.25)ln⁡3=0.5ln⁡3≈0.5493 nats\begin{aligned}&\operatorname{KL}(P\|Q)\\&=0.75 \ln (3)\\&+0.25 \ln (\frac{1}{3})\\&=(0.75-0.25)\ln 3\\&=0.5 \ln 3\\&\approx 0.5493\,\text{nats}\end{aligned}
KL⁡(Q∥P)=0.25ln⁡(13)+0.75ln⁡3=0.5ln⁡3≈0.5493 nats\begin{aligned}&\operatorname{KL}(Q\|P)\\&=0.25 \ln (\frac{1}{3})\\&+0.75 \ln 3\\&=0.5 \ln 3\\&\approx 0.5493\,\text{nats}\end{aligned}

Here the two directions happen to be equal; KL is not symmetric in general.

M=(0.5,0.5)M=(0.5,0.5).

KL⁡(P∥M)=0.75ln⁡(1.5)+0.25ln⁡(0.5)≈0.1308\begin{aligned}&\operatorname{KL}(P\|M)\\&=0.75 \ln (1.5)\\&+0.25 \ln (0.5)\\&\approx 0.1308\end{aligned}

By symmetry of this particular example, KL⁡(Q∥M)\operatorname{KL}(Q\|M) is the same. Hence JS⁡(P,Q)≈0.1308\operatorname{JS}(P,Q)\approx 0.1308 nats.

TV⁡(P,Q)=12(∣0.75−0.25∣+∣0.25−0.75∣)=12(0.5+0.5)=0.5\begin{aligned}&\operatorname{TV}(P,Q)\\&=\frac12\bigl(|0.75-0.25|\\&\qquad+|0.25-0.75|\bigr)\\&=\frac12(0.5+0.5)\\&=0.5\end{aligned}

Takeaway: the same P and Q can give different numerical divergences; always use the formula named in the question.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑