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.
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:
- 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.
- 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
jensenshannondoes. - 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.
- 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
Frequently asked questions
Sources & references
- Lin, J. (1991), “Divergence Measures Based on the Shannon Entropy”, IEEE Trans. Inf. Theory 37(1):145–151
- Endres & Schindelin (2003), “A new metric for probability distributions”, IEEE Trans. Inf. Theory 49(7):1858–1860
- Cover & Thomas, Elements of Information Theory (2nd ed., 2006), ch. 2
- SciPy — scipy.spatial.distance.jensenshannon (returns the JS distance √JSD)
The formulas and conventions on this page were last cross-checked against Lin (1991), Endres & Schindelin (2003) and SciPy on 2026-07-10. The worked examples reproduce to the displayed precision against scipy.spatial.distance.jensenshannon.
Related tools
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.