Examples: KL, JS and total variation
On this page 8 headings
KL divergence is generated by
(with
by continuity), giving
KL generally changes when P and Q are swapped; it is not a metric.
Jensen–Shannon divergence is based on the mixture :
It is symmetric. A standard generating function for the corresponding f-divergence is
Total variation is
One generating function is
Different conventions sometimes absorb the factor , so always check the definition used.
Asymmetry of KL
Let , .
The values differ.
KL divergence in both directions
Take , . Using natural logarithms,
Reverse the roles:
The log ratios and the probability weights both change, so the two directions need not agree.
Jensen–Shannon divergence
Use the same and . Step 1: form . Step 2:
Step 3:
Step 4:
Swapping P and Q leaves M unchanged and merely swaps the two KL terms, so JS is symmetric.
Total variation
For and ,
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
KL: multiply each P probability by and add. JS: calculate the midpoint distribution M first, then average and . TV: take absolute differences, sum and multiply by . All three can compare the same P and Q, but their numerical answers are not interchangeable.
Practice with complete answers
Question 1: , . Find TV.
Show solution
.
Question 2: For the same P,Q, find .
Show solution
nats.
Question 3: Compute JS. ,
has the same value, so nats.
Zero probabilities — careful calculation
For discrete KL, the term with contributes 0 by the standard limiting convention, provided . If but , is infinite. This explains why blindly entering 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 and . Find , , , and .
Here the two directions happen to be equal; KL is not symmetric in general.
.
By symmetry of this particular example, is the same. Hence nats.
Takeaway: the same P and Q can give different numerical divergences; always use the formula named in the question.
