Jensen-Shannon Divergence Calculator

Statistics

JS divergence0.0488 bits
In nats0.0338 nats
JS distance (square root)0.2209
Midpoint probability M0.375
How this was calculated
  1. Jensen-Shannon divergence: average the two KL divergences to the midpoint M = (P + Q) / 2, here M = 0.375.
  2. D(P||M) = 0.0323 nats and D(Q||M) = 0.0354 nats.
  3. JSD = 0.5 x 0.0323 + 0.5 x 0.0354 = 0.0338 nats = 0.0488 bits.
  4. Jensen-Shannon result: 0.0488 bits of a possible 1 bit, a true distance of 0.2209 after the square root.

KL divergence is the standard way to price the difference between two probability distributions, but it has two awkward traits: it is asymmetric, so the answer depends on which distribution you start from, and it is unbounded, so there is no scale for judging whether a value is large. This jensen shannon divergence calculator offers the standard fix, applied to the simplest case of two Bernoulli, or binary, distributions.

You enter the probability of an event under P and under Q. The calculator forms the midpoint distribution M, averages the two KL divergences to that midpoint, and reports the Jensen-Shannon divergence in bits and in nats, the Jensen-Shannon distance obtained from its square root, and the midpoint probability itself. The result is symmetric by construction and bounded, which makes it far easier to compare across problems.

Worked examples

Mild mismatch, 0.5 against 0.25

(0.5 · 0.25)

JS divergence0.0488 bits
In nats0.0338 nats
JS distance (square root)0.2209
Midpoint probability M0.375

Wide mismatch, 0.9 against 0.5

(0.9 · 0.5)

JS divergence0.1468 bits
In nats0.1017 nats
JS distance (square root)0.3831
Midpoint probability M0.700

Near-opposite laws, 0.99 against 0.01

(0.99 · 0.01)

JS divergence0.9192 bits
In nats0.6371 nats
JS distance (square root)0.9588
Midpoint probability M0.500

The midpoint fix for KL's asymmetry

The construction is simple. Given the two distributions P and Q, form their average, the midpoint M = (P + Q) / 2. Then measure how far each distribution sits from that common midpoint, using KL divergence, and average the two measurements: JSD = 0.5 x D(P||M) + 0.5 x D(Q||M). Because both distributions are judged against the same referee, swapping P and Q changes nothing, and the asymmetry of KL disappears.

The midpoint trick buys a second property as well. Neither distribution can be infinitely far from the midpoint, because the midpoint always assigns at least half the probability that either distribution assigns to any outcome. The explosive log-ratios that make raw KL divergence hard to interpret cannot occur, and the resulting measure stays within fixed bounds instead of running off to infinity.

A third property makes the measure especially well behaved: the square root of the Jensen-Shannon divergence is a true metric. It satisfies the triangle inequality, so distances between distributions can be compared, chained and reasoned about geometrically in a way that raw divergences do not allow. The calculator reports this square root as the JS distance alongside the divergence itself.

Using the calculator takes two numbers: the probability of the event under P and its probability under Q, entered as decimals. The defaults, 0.5 against 0.25, reproduce the first worked example below, so the divergence, the distance and the midpoint can all be inspected before you substitute your own pair. Because the construction is symmetric, the order in which you enter the two distributions makes no difference to any of the reported values, a small convenience that KL-based tools cannot offer.

  • JSD(P, Q) always equals JSD(Q, P)
  • Each distribution is compared with the shared midpoint, never directly with the other
  • The divergence is zero only when the two distributions are identical
  • The square root of the divergence is a true metric distance

Worked examples: mild, wide and near-opposite laws

The default example sets P at 0.5 against Q at 0.25, so the midpoint is M = 0.375. The two KL divergences to the midpoint are D(P||M) = 0.0323 nats and D(Q||M) = 0.0354 nats, and averaging gives JSD = 0.5 x 0.0323 + 0.5 x 0.0354 = 0.0338 nats = 0.0488 bits. That is 0.0488 bits of a possible 1 bit, and the square root gives a JS distance of 0.2209. Two distributions this close together barely register on the scale.

The second example widens the gap: P at 0.9 against Q at 0.5, with midpoint M = 0.700. Here D(P||M) = 0.1163 nats and D(Q||M) = 0.0872 nats, so JSD = 0.5 x 0.1163 + 0.5 x 0.0872 = 0.1017 nats = 0.1468 bits, and the JS distance is 0.3831. Treating a near-certain event as a coin flip is a serious modelling error, yet on the bounded scale it still uses only a modest share of the available range.

The third example pushes the pair near the limit: P at 0.99 against Q at 0.01, two laws that almost completely disagree, with midpoint M = 0.500. By symmetry the two divergences to the midpoint are equal, D(P||M) = 0.6371 nats and D(Q||M) = 0.6371 nats, so JSD = 0.5 x 0.6371 + 0.5 x 0.6371 = 0.6371 nats = 0.9192 bits, with a JS distance of 0.9588. The table below collects all three examples.

PQJS divergenceIn natsJS distance (square root)Midpoint probability M
0.50.250.0488 bits0.0338 nats0.22090.375
0.90.50.1468 bits0.1017 nats0.38310.700
0.990.010.9192 bits0.6371 nats0.95880.500
Even near-opposite distributions stay inside the scale: 0.99 against 0.01 gives 0.9192 bits of a possible 1 bit.

Bounds: why bits cap at 1

Measured in bits, the Jensen-Shannon divergence between two distributions can never exceed 1 bit. The intuition is that the divergence measures how much knowing which of the two distributions a draw came from would tell you, and a single binary choice carries at most one bit of information. When the two distributions are identical there is nothing to learn and the divergence is zero; when they are completely disjoint, the source is fully revealed and the divergence reaches its ceiling.

The worked examples show how the scale fills up in practice. A mild disagreement, 0.5 against 0.25, scores 0.0488 bits, near the floor. A wide disagreement, 0.9 against 0.5, scores 0.1468 bits. Only the near-opposite pair, 0.99 against 0.01, at 0.9192 bits, comes close to the 1 bit maximum. The scale is therefore demanding: values that look small in absolute terms can still represent substantial disagreement between distributions.

In nats the same values read 0.0338 nats, 0.1017 nats and 0.6371 nats. The calculator reports both units because the literature uses both, but the bounded interpretation is easiest to keep in mind in bits: read the answer as a fraction of the 1 bit ceiling, and read the JS distance, 0.2209, 0.3831 and 0.9588 across the examples, as the corresponding position on a distance scale that tops out at one as well.

The bounded scale also changes how differences between values should be read. On an unbounded measure, a gap between two divergences has no fixed meaning, because the same numerical gap can be trivial in one region of the scale and decisive in another. Here, every value is a share of the same 1 bit ceiling, so the step from 0.0488 bits to 0.1468 bits and the step from there towards 0.9192 bits can be compared directly: each is movement along one shared ruler, and the JS distance values 0.2209, 0.3831 and 0.9588 mark positions on the matching distance ruler.

JSD versus total variation

Total variation distance is the other bounded, symmetric way to compare two distributions, and it answers a different question. It looks at the single largest gap between the probabilities the two laws assign, in effect asking how much probability mass would have to move to turn one distribution into the other. It is intuitive, but it notices only the biggest discrepancy and is blind to how the remaining mass is arranged.

Jensen-Shannon divergence, by contrast, accumulates discrepancy across the whole distribution through its log-ratio terms, weighted towards outcomes the distributions themselves consider likely. Two pairs of distributions can share the same total variation while differing in Jensen-Shannon terms, because the shape of the disagreement matters to one measure and not the other. For comparing models, forecasts and generators, that sensitivity to shape is usually what you want.

The choice between measures is a choice of question. If the direction of approximation matters, as when a code or a model must stand in for a specific truth, KL divergence remains the right tool despite its asymmetry. If you need a symmetric discrepancy with a fixed scale and a genuine distance hiding inside it, the Jensen-Shannon pair reported here, divergence and square-root distance together, is the more convenient instrument.

In applied work the Jensen-Shannon divergence appears wherever two estimated distributions must be scored against each other: comparing a generative model's output distribution with the data it imitates, checking whether a forecast distribution has drifted from a reference, or measuring how far apart two groups' response patterns sit. In each setting the same features do the work: symmetry, so the comparison has no arbitrary direction; boundedness, so scores from different problems share a scale; and a square root that behaves as an honest distance when results need to be ranked, clustered or plotted.

  • Use JSD when you need one symmetric number for a pair of distributions
  • Use the JS distance when distances must chain or be compared geometrically
  • Use KL divergence when the direction of approximation is the whole point
  • Use total variation when the worst single event gap is what matters

FAQs

What JSD do P = 0.5 and Q = 0.25 give?

The Jensen-Shannon divergence is 0.0488 bits. In nats it is 0.0338 nats, the midpoint probability is 0.375, and the Jensen-Shannon distance is 0.2209.

And 0.9 against 0.5?

The divergence is 0.1468 bits, or 0.1017 nats. The midpoint probability is 0.700 and the Jensen-Shannon distance is 0.3831.

How large can JSD get?

In bits the divergence can reach 1 bit at most in this scale. Near-opposite laws, 0.99 against 0.01, already reach 0.9192 bits, close to that ceiling.

What is the midpoint for the first example?

The midpoint for the first example is M = 0.375, the average of 0.5 and 0.25. Each distribution is compared against this midpoint rather than directly against the other.

What distance does the square root give for the first example?

Taking the square root of the bit value gives 0.2209 for the first example. That square root, the Jensen-Shannon distance, behaves as a true metric distance between the two distributions.

In nats, the first example?

In nats, the first example is 0.0338 nats. It comes from averaging D(P||M) = 0.0323 nats and D(Q||M) = 0.0354 nats.

Sources

Related calculators

Shannon Entropy Calculator
Shannon entropy of a probability distribution in bits, with the maximum entropy for the outcome count, efficiency and perplexity.
Normal Distribution Calculator
Probabilities below, above and between values, and the value at a percentile, from mean and standard deviation.
Beta Distribution Calculator
CDF, PDF, mean and variance of the beta distribution on [0, 1] for any alpha and beta.