Search

Search papers and lessons

Jensen–Shannon Divergence

Level 3 · 12 min read

Jensen–Shannon divergence scores how different two distributions are by comparing each one to their average, giving a symmetric value that is always finite and never above log 2.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Discrete math
  • Information theory
  • Sign in to see your progress

TL;DR

Jensen–Shannon divergence measures how different two probability distributions are. It compares each distribution to their average, and it averages the two resulting KL divergences. The result is symmetric, always finite, and never larger than log⁡2\log 2 (one bit).

Its square root is a true distance metric. That makes it a convenient, well-behaved score when KL divergence is too one-sided or infinite.

Where you'll see it: in the original analysis of generative adversarial networks, where training is shown to minimise it, and in comparing word or topic distributions, where a bounded and symmetric score is easy to read.

Intuition

KL divergence asks how badly you are surprised if you believe QQ when the truth is PP. The question is one-sided. Swap the roles and you get a different number, and if QQ puts zero probability on something PP can produce, the answer is infinite. That is awkward when you want a plain "how different are these two?"

Jensen–Shannon fixes both problems with one idea. Make a neutral reference point that is halfway between the two distributions, then measure how far each of them sits from that midpoint. Neither distribution is privileged, so the score is symmetric. The midpoint has both distributions' outcomes in it, so it is never zero where either one is nonzero, so nothing blows up.

flowchart LR
  P[Distribution P] --> M[Midpoint M = average of P and Q]
  Q[Distribution Q] --> M
  P --> D1[KL of P from M]
  Q --> D2[KL of Q from M]
  M --> D1
  M --> D2
  D1 --> S[Average of the two = JS divergence]
  D2 --> S

Think of two bags of coloured marbles. If the bags contain the same colours in the same proportions, both are close to their mixture and the score is zero. If one bag holds only red and the other only blue, each is far from the 50-50 mixture, and the score is at its maximum.

The name has a second meaning. The midpoint's entropy is at least the average of the two entropies. This is a case of Jensen's inequality, because entropy is a concave function. The gap between those two quantities is the divergence.

The math

Let PP and QQ be probability distributions over the same outcomes. Their midpoint, or mixture, is

M=12(P+Q),M(x)=12(P(x)+Q(x)).M = \tfrac{1}{2}(P + Q), \qquad M(x) = \tfrac{1}{2}\big(P(x) + Q(x)\big).
Definition: Jensen–Shannon divergence
JSD(P ∥ Q)=12 DKL(P ∥ M)+12 DKL(Q ∥ M).\mathrm{JSD}(P \,\|\, Q) = \tfrac{1}{2}\, D_{\mathrm{KL}}(P \,\|\, M) + \tfrac{1}{2}\, D_{\mathrm{KL}}(Q \,\|\, M).

Here DKL(P ∥ M)=∑xP(x)log⁡P(x)M(x)D_{\mathrm{KL}}(P \,\|\, M) = \sum_x P(x) \log \frac{P(x)}{M(x)} is the KL divergence of PP from MM, and the sum is replaced by an integral for continuous distributions. The logarithm sets the unit. With natural logs the answer is in nats, and with base-2 logs it is in bits.

Equivalent entropy form

Expand the logarithms and the definition becomes a statement about Entropy. Write H(P)=−∑xP(x)log⁡P(x)H(P) = -\sum_x P(x) \log P(x) for Shannon entropy. Then

JSD(P ∥ Q)=H(M)−12H(P)−12H(Q).\mathrm{JSD}(P \,\|\, Q) = H(M) - \tfrac{1}{2} H(P) - \tfrac{1}{2} H(Q).

This form is often the fastest way to compute the divergence by hand, and it shows what the score measures. It is the entropy gained by mixing the two distributions, relative to the average entropy before mixing. Mixing can only make a distribution more spread out, so the score is never negative.

Bounds and zero

Three properties follow from the definition.

  1. Symmetry. JSD(P ∥ Q)=JSD(Q ∥ P)\mathrm{JSD}(P \,\|\, Q) = \mathrm{JSD}(Q \,\|\, P), because swapping PP and QQ leaves MM unchanged and just swaps the two terms.
  2. Finiteness. Wherever P(x)>0P(x) \gt 0, we have M(x)≥P(x)/2>0M(x) \ge P(x)/2 \gt 0. So each KL term in the definition is finite, even when PP and QQ have disjoint supports. This is the key difference from KL divergence, which is infinite whenever Q(x)=0Q(x) = 0 and P(x)>0P(x) \gt 0.
  3. Bounds and zero. 0≤JSD(P ∥ Q)≤log⁡20 \le \mathrm{JSD}(P \,\|\, Q) \le \log 2, and the value is zero exactly when P=QP = Q. The upper bound is reached when PP and QQ have disjoint supports. In bits, the maximum is 1.

The square root JSD(P ∥ Q)\sqrt{\mathrm{JSD}(P \,\|\, Q)} is a metric. It is symmetric, zero only for identical distributions, and satisfies the triangle inequality. This result was shown by Endres and Schindelin in 2003, and it is why the square root is often used as a distance.

Generalisation to several distributions

For distributions P1,…,PnP_1, \dots, P_n with weights πi≥0\pi_i \ge 0 summing to 1, the same idea gives

JSDπ(P1,…,Pn)=H ⁣(∑iπiPi)−∑iπiH(Pi).\mathrm{JSD}_\pi(P_1, \dots, P_n) = H\!\left(\sum_i \pi_i P_i\right) - \sum_i \pi_i H(P_i).

Its maximum is H(π)H(\pi), the entropy of the weights, so with equal weights on two distributions the maximum is log⁡2\log 2 as before.

Worked examples

Example
Two distributions on three outcomes, by hand

Let P=(0.5, 0.5, 0)P = (0.5,\ 0.5,\ 0) and Q=(0, 0.5, 0.5)Q = (0,\ 0.5,\ 0.5) over outcomes a,b,ca, b, c. Use natural logs.

  1. Midpoint: M=(0.25, 0.5, 0.25)M = (0.25,\ 0.5,\ 0.25).
  2. KL of PP from MM: 0.5ln⁡0.50.25+0.5ln⁡0.50.5+0⋅ln⁡00.25=0.5ln⁡2≈0.346570.5 \ln\frac{0.5}{0.25} + 0.5 \ln\frac{0.5}{0.5} + 0 \cdot \ln\frac{0}{0.25} = 0.5 \ln 2 \approx 0.34657. The last term is zero by the convention 0ln⁡0=00 \ln 0 = 0.
  3. KL of QQ from MM: by symmetry of the example, the same value, 0.346570.34657.
  4. JSD: 12(0.34657)+12(0.34657)=0.34657\tfrac{1}{2}(0.34657) + \tfrac{1}{2}(0.34657) = 0.34657 nats.
  5. Check with the entropy form: H(M)=0.25ln⁡4+0.5ln⁡2+0.25ln⁡4≈1.03972H(M) = 0.25 \ln 4 + 0.5 \ln 2 + 0.25 \ln 4 \approx 1.03972, and H(P)=H(Q)=ln⁡2≈0.69315H(P) = H(Q) = \ln 2 \approx 0.69315. Then 1.03972−0.5(0.69315)−0.5(0.69315)=0.346571.03972 - 0.5(0.69315) - 0.5(0.69315) = 0.34657, which matches.

Interpretation: the two distributions share half their mass on bb and disagree on the rest. The score is 12ln⁡2\tfrac{1}{2}\ln 2, or exactly half a bit, which is half the maximum of one bit. Half of the mass is in disagreement, and the score reflects that.

Example
Disjoint supports reach the maximum

Let P=(1, 0)P = (1,\ 0) and Q=(0, 1)Q = (0,\ 1). The midpoint is M=(0.5, 0.5)M = (0.5,\ 0.5).

  1. DKL(P ∥ M)=1⋅ln⁡10.5=ln⁡2D_{\mathrm{KL}}(P \,\|\, M) = 1 \cdot \ln\frac{1}{0.5} = \ln 2.
  2. DKL(Q ∥ M)=ln⁡2D_{\mathrm{KL}}(Q \,\|\, M) = \ln 2 by the same reasoning.
  3. JSD=12ln⁡2+12ln⁡2=ln⁡2≈0.693\mathrm{JSD} = \tfrac{1}{2}\ln 2 + \tfrac{1}{2}\ln 2 = \ln 2 \approx 0.693 nats, which is exactly 1 bit.

Interpretation: the score is at its ceiling. It does not matter whether the two outcomes are next to each other or on opposite ends of a long scale. The score says only that they do not overlap, and this is the property that makes JSD a poor training signal in some settings, as discussed below.

Example
The JSD inside a GAN training objective

A GAN trains a discriminator DD to tell real samples, drawn from PP, from generated samples, drawn from a generator distribution QQ. The standard value function is V(D)=EP[log⁡D(x)]+EQ[log⁡(1−D(x))]V(D) = \mathbb{E}_P[\log D(x)] + \mathbb{E}_Q[\log(1 - D(x))].

For a fixed generator, the best possible discriminator is D∗(x)=P(x)/(P(x)+Q(x))D^*(x) = P(x)/(P(x) + Q(x)). Substituting it in and writing M=12(P+Q)M = \tfrac{1}{2}(P+Q) gives

V(D∗)=−log⁡4+2 JSD(P ∥ Q).V(D^*) = -\log 4 + 2\,\mathrm{JSD}(P \,\|\, Q).

Using the example above, where JSD=0.34657\mathrm{JSD} = 0.34657, we get V(D∗)=−1.38629+0.69315=−0.69315V(D^*) = -1.38629 + 0.69315 = -0.69315.

Interpretation: the generator is trying to make JSD(P ∥ Q)\mathrm{JSD}(P \,\|\, Q) small. The minimum is zero, reached when Q=PQ = P, and then the value is −log⁡4-\log 4. That is the result Goodfellow and colleagues used to show that the generator's objective is tied to a divergence between the real and generated distributions.

Tradeoffs and failure modes

JSD is a good choice when you want a symmetric, bounded score and both distributions are discrete, or when you can estimate densities. Its bounded range makes thresholds easy to set, and the square root gives a metric you can cluster with.

Its weak point is the flat region. Because the score sits at log⁡2\log 2 whenever supports are disjoint, moving one distribution a little without making it overlap the other gives no change in the score. For a learner that updates by gradients, this means no signal. This happened in early GAN training, where the generator's distribution and the real distribution sit on lower-dimensional manifolds with little overlap. The Wasserstein distance, which keeps changing as the distributions move closer, was proposed partly to fix this.

It also does not say which way a mismatch goes. KL divergence in either direction tells you that QQ puts mass where PP does not, or misses mass where PP has it, and the two directions behave differently. JSD mixes both penalties into one number.

SituationProblemBetter choice
Two discrete distributions, symmetric comparison neededKL is one-sided and can be infiniteJSD, or its square root as a metric
Continuous distributions with disjoint supportsJSD is flat at log⁡2\log 2 and gives no gradientWasserstein distance
Need to know which side is missing massJSD blends both directionsOne-sided KL in the direction you care about
Fitting a model by maximum likelihoodLikelihood already equals KL up to a constant[[maximum-likelihood-estimation
Estimating from samples in high dimensionsDensities are hard to estimateClassifier-based estimates, such as the GAN discriminator

History

Shannon introduced entropy in 1948, and Kullback and Leibler defined their divergence in 1951. Jianhua Lin's 1991 paper introduced the symmetrised, bounded version and named it after Jensen, because the construction uses the concavity of entropy, which is the same property that drives Jensen's inequality.

The divergence then sat mostly in information theory and signal processing until the 2010s, when it became central to deep learning. Goodfellow and colleagues' 2014 paper on generative adversarial networks showed that the optimal discriminator turns the generator's objective into a constant plus twice the JSD between the real and generated distributions. That made JSD the working explanation for GAN training, and it also exposed the flat-region problem that later work tried to fix. Endres and Schindelin's 2003 result, that the square root is a metric, had already made it a usable distance for clustering and similarity search.

Check yourself

QuestionWrite the Jensen–Shannon divergence of PP and QQ in terms of MM and KL divergences, and say what MM is.Show answer

M=12(P+Q)M = \tfrac{1}{2}(P+Q) is the midpoint of the two distributions. Then JSD(P ∥ Q)=12DKL(P ∥ M)+12DKL(Q ∥ M)\mathrm{JSD}(P \,\|\, Q) = \tfrac{1}{2} D_{\mathrm{KL}}(P \,\|\, M) + \tfrac{1}{2} D_{\mathrm{KL}}(Q \,\|\, M). Each distribution is compared to the midpoint, and the two comparisons are averaged.

QuestionLet P=(1,0)P = (1, 0) and Q=(0.5,0.5)Q = (0.5, 0.5). Compute JSD(P ∥ Q)\mathrm{JSD}(P \,\|\, Q) in nats and in bits.Show answer

The midpoint is M=(0.75, 0.25)M = (0.75,\ 0.25). DKL(P ∥ M)=ln⁡(1/0.75)≈0.28768D_{\mathrm{KL}}(P \,\|\, M) = \ln(1/0.75) \approx 0.28768. DKL(Q ∥ M)=0.5ln⁡(0.5/0.75)+0.5ln⁡(0.5/0.25)≈0.14384D_{\mathrm{KL}}(Q \,\|\, M) = 0.5 \ln(0.5/0.75) + 0.5 \ln(0.5/0.25) \approx 0.14384. So JSD=12(0.28768+0.14384)≈0.21576\mathrm{JSD} = \tfrac{1}{2}(0.28768 + 0.14384) \approx 0.21576 nats, which is about 0.3110.311 bits.

QuestionWhy is JSD always finite, even when KL divergence from PP to QQ is infinite?Show answer

KL from PP to QQ is infinite when P(x)>0P(x) \gt 0 but Q(x)=0Q(x) = 0 for some outcome. In JSD, each KL is taken to the midpoint MM, and M(x)≥P(x)/2>0M(x) \ge P(x)/2 \gt 0 wherever P(x)>0P(x) \gt 0. So the logarithms never see a zero denominator, and both terms stay finite.

QuestionA GAN generator produces samples that lie on a completely different region of space from the real data. Explain why the JSD-based objective gives little signal to improve, and name a distance that avoids the problem.Show answer

When the supports are disjoint, the JSD is at its maximum log⁡2\log 2, and small moves of the generator's distribution that do not create overlap leave the value unchanged. The gradient is therefore zero or uninformative. The Wasserstein distance keeps changing with how far apart the distributions are, so it still gives a useful gradient in this case.

Sources