Kullback–Leibler Divergence Calculator

JJ Ben-Joseph headshot JJ Ben-Joseph

Introduction to KL divergence between discrete distributions

This KL divergence calculator compares two discrete probability distributions, or probability vectors, P and Q over the same outcomes. Enter each list as comma-separated values such as 0.6, 0.4. The calculator normalizes each list before it measures the gap between them, so raw counts and weights are acceptable as long as every entry is non-negative and each position still refers to the same outcome.

Choose ln for results in nats, or choose base 2 when you want results in bits and prefer the binary-log view of the same divergence calculation.

That normalization step makes the comparison shape-based rather than size-based. If P and Q are meant to describe a classifier, a language model, a survey response mix, or any other discrete table, the values should be lined up first. A small KL result means the shapes are close; a larger result usually means one list gives much less probability to an outcome that the other list considers important.

Definitions and formulas for discrete KL comparisons

For this KL divergence calculator, P and Q are discrete distributions over outcomes i=1,n, with Pi0, Qi0, and with both lists normalized to sum to 1 before the divergences are computed. In practice, that means the calculator can accept probabilities, counts, or weights, provided the two lists have the same length and describe the same ordered outcomes.

After normalization, the totals satisfy i=1nPi=1 and i=1nQi=1, so the output depends on the distribution shape rather than the raw scale of the input numbers.

KL divergence

The Kullback–Leibler divergence from P to Q measures how costly it is to use Q as the stand-in for data generated by P:

DKL (PQ) = i=1 n Pi · log ( Pi Qi )

If you choose ln, then log is ln and the unit is nats. If you choose log2, the unit is bits.

Cross-entropy

For KL divergence and log-loss calculations, cross-entropy is the average cost of encoding outcomes from P with probabilities supplied by Q:

Formula: H(P, Q) = − ∑ i = 1 n P_i · log(Q_i)

H(P,Q)=i=1nPi·log(Qi)

It relates to KL divergence via:

H(P,Q)=H(P)+DKL(PQ), where H(P)=i=1nPi·log(Pi) is the entropy of P.

Jensen–Shannon divergence (JSD)

In this calculator, Jensen–Shannon divergence is built from the midpoint distribution M=12(P+Q) so both inputs contribute evenly:

JSD(P, Q) = 1/2 · D_KL(P‖M) + 1/2 · D_KL(Q‖M)

JSD(P,Q)=12·DKL(PM)+12·DKL(QM)

With base-2 logs, JSD is bounded between 0 and 1 bit for discrete distributions.

How to interpret KL divergence, cross-entropy, and JSD

In practice, the most useful result is often not the single KL number but the pattern across the two directions. If forward KL is large while reverse KL is modest, Q may be missing an outcome that P treats as important. If both are small, the tables are broadly similar after scaling. That makes this calculator handy for checking model outputs, comparing empirical frequencies, or deciding whether two discrete summaries can reasonably be treated as the same shape.

Worked example: comparing P = 0.6, 0.4 to Q = 0.5, 0.5

For this KL divergence example, let:

Using natural logs for the KL calculation:

D_KL(P‖Q) = 0.6·ln(0.6/0.5) + 0.4·ln(0.4/0.5)

= 0.6·ln(1.2) + 0.4·ln(0.8) ≈ 0.6·0.1823 + 0.4·(-0.2231) ≈ 0.0201 nats

This small positive value means the two discrete distributions are close, but not identical, on these two outcomes. The same idea applies when the lists have more categories: the calculator is looking for where the probability mass shifts, not just whether the totals add up.

Metric comparison for KL divergence at a glance

Metric Discrete formula Symmetric? Range / behavior Notes
KL(P‖Q) ∑ P(i) log(P(i)/Q(i)) No ≥ 0; can be ∞ Undefined/infinite if Q(i)=0 where P(i)>0
KL(Q‖P) ∑ Q(i) log(Q(i)/P(i)) No ≥ 0; can be ∞ Highlights different failure modes than KL(P‖Q)
Cross-entropy H(P,Q) −∑ P(i) log Q(i) No ≥ H(P); can be ∞ Common in classification/log-loss settings
JSD(P,Q) ½·KL(P‖M)+½·KL(Q‖M), M=(P+Q)/2 Yes Finite; bounded (≤ 1 bit with log2) More stable and interpretable for “distance-like” comparison

KL divergence limitations and assumptions

These assumptions matter most when you compare real data to a model prediction. Before trusting the number, make sure the categories are in the same order, because KL cannot tell whether two labels have been swapped. If an outcome appears in P but not in Q, forward KL will blow up, which often means you should smooth the inputs or reconsider the binning scheme.

KL divergence references

How to use this KL divergence calculator

  1. Enter Probabilities P as a comma-separated list for the reference distribution.
  2. Enter Probabilities Q with the same number of outcomes, and keep the order aligned with P.
  3. Choose Log base as Natural (ln) for nats or Base 2 for bits.
  4. Click Compute Divergences, then compare how much probability mass shifts between P and Q before you rely on the numbers in a model, report, or explanation. The summary and table show the main divergences together so you can read the forward and reverse directions side by side.
Enter P and Q.

Arcade Mini-Game: Kullback–Leibler Divergence Calculator Calibration Run

Use this quick arcade run to practice separating useful scenario inputs from common planning mistakes before you rely on the calculator output.

Score: 0 Timer: 30s Best: 0

Start the game, then use your pointer or arrow keys to catch useful inputs and avoid bad assumptions.