induwara.lk
induwara.lkAI · Information Theory

Jensen-Shannon Divergence Calculator

Compute the Jensen–Shannon divergence and the Jensen–Shannon distance between two discrete distributions, in bits or nats. It shows the mixture M = ½(P+Q), both KL terms, the entropy decomposition and the full per-bin working — all in your browser, no signup, matched to SciPy.

By Induwara AshinsanaUpdated Jul 10, 2026
Jensen–Shannon divergence

Two or more non-negative numbers. Comma, space, or newline separated. Auto-normalised to sum to 1.

Same number of bins as P. JSD is symmetric, so the order of P and Q doesn't change the result.

Log base
Auto-normalise
Presets
JS divergence — bits
0.1468
0 ≤ JSD ≤ 1.0000
JS distance √JSD
0.3831
True metric (Endres–Schindelin)
Mixture entropy H(M)
0.8813
½[H(P)+H(Q)] = 0.7345
Bins
2
Sums to 1 as entered
Decimals
JSD in every base0.1468 bits0.1017 nats0.0442 dits(1 nat = 1/ln2 ≈ 1.4427 bits)

Cross-check. The entropy form H(M) − ½[H(P)+H(Q)] = 0.8813 0.7345 = 0.1468 bits; the independent KL form ½·D(P‖M) + ½·D(Q‖M) = ½(0.1258 + 0.1678) = 0.1468. Both derivations reconcile, as they must.

P, Q and mixture M by bin

PQM

Per-bin working

ipᵢqᵢmᵢJSD term (bits)
10.50000.90000.70000.0418
20.50000.10000.30000.1050
JSD total (bits)0.1468
JSD = 0.1468 bits, JS distance = 0.3831 (over 2 bins, base bits)

Method: JSD(P‖Q) = ½·D(P‖M) + ½·D(Q‖M), M = ½(P+Q); distance d = √JSD. Base-2 → bits (bounded 0–1), base-e → nats, base-10 → dits, with 0·log 0 = 0. Sources: Lin (1991), Endres & Schindelin (2003), cross-checked against SciPy. No data leaves this page.

How it works

The Jensen–Shannon divergence measures how different two probability distributions P and Q are. It is the symmetric, always-finite, bounded counterpart to the Kullback–Leibler divergence, introduced by Jianhua Lin in 1991. Instead of comparing P directly to Q, it compares each of them to their average — the mixture distribution M = ½(P+Q):

JSD(P‖Q) = ½·D(P‖M) + ½·D(Q‖M),  M = ½(P+Q)

where D(A‖B) = Σᵢ aᵢ·log_b(aᵢ/bᵢ) is the KL divergence. Because mᵢ = (pᵢ+qᵢ)/2 is zero only when both pᵢ and qᵢ are zero, every term stays finite — so unlike raw KL divergence, JSD never blows up to +∞ when one distribution has a zero bin. The calculation runs like this:

  1. Parse P and Q into numeric arrays. Reject the input if the lengths differ, if either has fewer than two bins, or if any entry is negative — a probability mass cannot be below zero.
  2. If auto-normalise is on, divide each vector by its own sum so ΣP = ΣQ = 1. This lets you paste raw counts like 1, 2, 3, exactly as SciPy's jensenshannon does.
  3. Build the mixture M = ½(P+Q) bin by bin, then compute the two KL terms D(P‖M) and D(Q‖M) using the convention 0·log 0 = 0.
  4. Average them: JSD = ½·D(P‖M) + ½·D(Q‖M). Take the square root to get the Jensen–Shannon distance d = √JSD, which Endres & Schindelin (2003) proved is a true metric — it obeys the triangle inequality.

There is an equivalent form in terms of Shannon entropy H(X) = −Σ xᵢ·log_b(xᵢ): JSD(P‖Q) = H(M) − ½·[H(P) + H(Q)]. The calculator computes JSD both ways — the KL form and the entropy form — and shows them reconciling, a built-in correctness check. The log base sets the unit: base 2 gives bits (with the clean bound 0 ≤ JSD ≤ 1), base e gives nats (maximum ln 2 ≈ 0.6931), base 10 gives dits. JSD is 0 exactly when P = Q, and hits its maximum when P and Q have disjoint support.

Worked examples

Partial overlap (bits)

P = [0.5, 0.5], Q = [0.9, 0.1], base 2

  1. M = ½(P+Q) = [0.7, 0.3]
  2. H(P) = 1.0000; H(Q) = 0.4690; H(M) = 0.8813 bits
  3. Entropy form: JSD = 0.8813 − ½(1.0000 + 0.4690) = 0.1468 bits
  4. KL form: D(P‖M) = 0.1258, D(Q‖M) = 0.1678 → ½(0.1258+0.1678) = 0.1468 ✓
  5. JS distance = √0.1468 = 0.3831 (matches SciPy base=2)

Zero bin — still finite (edge case)

P = [0.5, 0.5], Q = [1, 0], base 2

  1. M = [0.75, 0.25] — positive everywhere P or Q is
  2. H(P) = 1, H(Q) = 0, H(M) = 0.8113 bits
  3. JSD = 0.8113 − ½(1 + 0) = 0.3113 bits (finite — KL would be +∞)
  4. JS distance = √0.3113 = 0.5579

Disjoint support — the maximum

P = [1, 0], Q = [0, 1], base 2

  1. M = [0.5, 0.5], H(M) = 1, H(P) = H(Q) = 0
  2. JSD = 1 − ½(0 + 0) = 1.0000 bits (upper bound)
  3. JS distance = √1 = 1.0000 — the two distributions share no support

Frequently asked questions

Sources & references

Related tools

Rate this tool
Be the first to rate

Comments & feedback

Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.

Found a bug, edge case, or want to suggest an improvement?

Email me at [email protected] — most fixes ship within 24 hours.