Formula: KL & Jensen-Shannon Divergence Formula
- trueDistributionP1: True Distribution (P(1))
- modelDistributionQ1: Model Distribution (Q(1))
Compute H(P), Cross-Entropy H(P,Q), D_KL(P||Q), and symmetric Jensen-Shannon Divergence.
Because the model puts 0.45 on an outcome that really happens 70% of the time. The two log-ratio terms are 0.4462 and minus 0.2623, which add to 0.1839 bits. It is the extra coding cost per outcome of using the model's probabilities instead of the true ones. Moving Q(1) closer to 0.7 shrinks it quickly.
Put the reference, usually the true or observed distribution, in P, and the approximation or model in Q. The forward divergence then measures how badly Q represents P. Swapping them gives the reverse divergence, which is a different quantity and usually a different number. Most model-fitting work uses the true distribution as P.
No. Individual terms in the sum can be negative, as the outcome 0 term is on the default, but the total is always zero or positive. If you calculate a negative KL by hand, check that both distributions sum to 1 and that the logs use the same base. Identical distributions give exactly zero.
Not quite. The divergence is the value this tool reports, 0.0467 bits on the default. The distance is its square root, which satisfies the triangle inequality and is a proper metric. Libraries differ on which one they return, so check the documentation before comparing figures.
Measured in bits, the Jensen-Shannon divergence between any two distributions lies between 0 and 1. Zero means the distributions are identical, and 1 means they put probability on completely separate outcomes. The bound changes to ln 2 if you measure in nats. That fixed range makes JSD values easy to compare across problems.
Multiply the bit value by the natural log of 2, about 0.6931. The default forward KL of 0.1839 bits becomes about 0.1274 nats. Going the other way, divide nats by 0.6931. The tool itself only shows bits. Convert before comparing with library loss values, which are often in nats.
Because one direction of KL divergence becomes infinite there. If P rules an outcome out completely while Q allows it, D_KL(Q || P) has no finite value, and the reverse holds for Q. The calculator shows an error instead of a misleading number. Use a value such as 0.95 to study near-certain cases.
Binary log loss averaged over many predictions is a cross-entropy between the observed outcomes and the predicted probabilities, usually in nats. The cross-entropy here, 1.0652 bits on the default, is the same idea for one fixed true distribution and one fixed model distribution. Subtracting the entropy of 0.8813 bits leaves the divergence.
Neither distribution is near 0 or 1, so the weighting by P or by Q makes little difference: 0.184 bits forward against 0.194 bits reverse. The gap widens sharply near the edges. With P(1) at 0.9 and Q(1) at 0.5 it becomes 0.531 against 0.737 bits.
Not directly. The tool only takes P(1) and Q(1) and derives the second outcome from each. For more outcomes, apply the same formula by hand or in code, summing p log₂(p/q) over every outcome, and check that both distributions add up to 1 first. Zero probabilities make one direction infinite there too.
P(1) of 0.5 and Q(1) of 0.45 are very close, so the model's code is nearly right. The forward KL is only 0.0072 bits and the Jensen-Shannon divergence 0.0018 bits. KL rises roughly with the square of the gap for close distributions, so small errors produce very small divergences.
Not in the strict sense. A distance must be symmetric and satisfy the triangle inequality, and KL does neither, as the different forward and reverse figures on the default show. It is better read as an expected coding cost. The square root of the Jensen-Shannon divergence is the usual choice when you need a true metric.
This calculator works on the simplest distributions there are: two outcomes. You enter P(1), the true probability of outcome 1, and Q(1), the probability your model assigns to it. The other outcome takes whatever is left, so P(0) is 1 minus P(1) and Q(0) is 1 minus Q(1).
The default compares a true distribution of 0.7 and 0.3 with a model of 0.45 and 0.55. Think of a coin that lands heads 70% of the time and a model that believes heads comes up 45% of the time. The headline says the forward KL divergence is 0.184 bits.
Everything is reported in bits, which means base-2 logarithms. The supporting lines add the cross-entropy, the reverse KL divergence and the Jensen-Shannon divergence. The interpretation line also gives the Shannon entropy of P, which for the default is 0.8813 bits.
Binary distributions are limiting, but they are also where the behaviour of these measures is easiest to see. Every property described below holds for larger distributions too.
The forward divergence is D_KL(P || Q) = Σ p log₂(p / q), summed over both outcomes. For the default there are two terms.
Outcome 1 contributes 0.7 × log₂(0.7 / 0.45). The ratio is about 1.556 and its base-2 log is about 0.637, so the term is 0.4462. Outcome 0 contributes 0.3 × log₂(0.3 / 0.55). The ratio is about 0.545, its log is negative, and the term is minus 0.2623. Adding them gives 0.1839 bits, the figure in the interpretation line, shown as 0.184 in the headline.
One term is negative and that is normal. Individual terms can go either way. The sum is never negative, and it is zero only when the two distributions are identical. Enter 0.5 for both and every divergence on the page reads 0.
The tool actually computes the forward divergence as cross-entropy minus entropy, which is the same thing rearranged. The cross-entropy H(P, Q) is 1.0652 bits and the entropy H(P) is 0.8813 bits. Their difference is 0.1839, matching the hand result.
If you are checking a library, this is the quickest test. Compute the two terms with a calculator, add them, and compare with the headline. If your figure is larger by a factor of about 1.44, your library is using natural logs and you are comparing nats with bits.
Shannon entropy H(P) is the average number of bits needed to encode outcomes from P using a code designed for P. A fair coin needs a full bit, and a 0.7 and 0.3 coin needs 0.8813 bits on average because it is somewhat predictable.
Cross-entropy H(P, Q) is the average number of bits you need when the outcomes really come from P but your code was built for Q. On the default that is 1.0652 bits, more than the entropy, because the code is tuned to the wrong probabilities.
The KL divergence is the gap between the two: the extra bits per outcome you pay for using Q instead of P. That reading makes the headline concrete. A model that believes 0.45 when the truth is 0.7 wastes about 0.184 bits on every outcome it encodes.
This is also why minimising cross-entropy loss in machine learning is the same as minimising KL divergence from the data. The entropy of the data does not depend on the model, so only the divergence part can change.
KL divergence is not symmetric. On the default the forward value is 0.184 bits and the reverse, D_KL(Q || P), is 0.194 bits. Swap the inputs, setting P(1) to 0.45 and Q(1) to 0.7, and the two figures change places.
The difference comes from which distribution does the weighting. The forward version weights the log ratio by P, so it punishes the model most where the truth puts a lot of mass and the model puts little. The reverse version weights by Q, so it punishes most where the model puts mass that the truth does not support.
On the default the gap is small because neither distribution is close to 0 or 1. Move toward the edges and it grows quickly. With P(1) at 0.9 and Q(1) at 0.5, the forward divergence is 0.531 bits while the reverse is 0.737 bits.
In practice, the direction you want depends on the job. Fitting a model to data usually minimises the forward direction, D_KL(data || model). Some approximate inference methods minimise the reverse direction instead, which tends to produce approximations that concentrate on one region rather than spreading out.
Setting P(1) to 0.7 and Q(1) to 0.3 gives a mirror-image pair, and there the forward and reverse values are both 0.489 bits. Symmetry in the inputs produces symmetry in the divergence, which is a neat check that the tool is weighting each direction correctly.
The Jensen-Shannon divergence fixes the asymmetry by comparing each distribution with their average. The mixture M is the midpoint of P and Q. For the default, M(1) is 0.575 and M(0) is 0.425.
Work it through. D_KL(P || M) is 0.7 × log₂(0.7 / 0.575) plus 0.3 × log₂(0.3 / 0.425), which is 0.1987 minus 0.1508, or 0.0479 bits. D_KL(Q || M) is 0.45 × log₂(0.45 / 0.575) plus 0.55 × log₂(0.55 / 0.425), which is minus 0.1591 plus 0.2046, or 0.0454 bits. Half of each, added, is 0.0467 bits, as the tool reports.
Because both distributions are compared with the same midpoint, swapping P and Q leaves the result unchanged. It is also bounded: in bits it can never exceed 1, which is why the supporting line is marked 0 to 1. A value of 0 means identical distributions and 1 means they share no outcomes at all.
That bound makes JSD easier to compare across problems than raw KL, which has no upper limit. Its square root is a true distance metric, which matters if you want to cluster distributions or use triangle-inequality arguments.
Both inputs must lie strictly between 0 and 1. Typing 0, 1 or anything outside that range produces an error message rather than a result, and the reason is mathematical rather than cosmetic.
If P gives an outcome zero probability while Q does not, the reverse divergence D_KL(Q || P) is infinite, because Q pays an unbounded coding cost for an outcome P says can never happen. The same happens to the forward divergence when Q is 0 or 1 and P is not. No finite number on the screen could represent that honestly, so the calculator declines to give one.
You can still explore near-certain cases. The far preset sets P(1) to 0.95 against the same 0.45 model. The entropy of P drops to 0.2864 bits because the outcome is fairly predictable, the forward KL rises to 0.851 bits, and the reverse KL is larger still at 1.418 bits. Push P closer to 1 and the reverse figure keeps climbing, which is the infinite limit showing through.
The Jensen-Shannon line behaves better at the edges. In that far case it reads 0.242 bits, comfortably inside its 0 to 1 range, because the mixture never puts zero mass on an outcome either distribution uses.
Many machine learning libraries report divergence and cross-entropy in nats, using natural logarithms. The tool reports bits only. To convert, multiply any figure on the page by the natural log of 2, about 0.6931.
The default forward KL of 0.1839 bits is about 0.1274 nats. The Jensen-Shannon value of 0.0467 bits is about 0.0324 nats. In nats the upper bound for JSD is ln 2 rather than 1.
Mixing units is a common source of confusion when comparing a hand calculation with a library's loss value. If your numbers are off by a factor close to 0.69 or 1.44, a unit mismatch is the likely cause.
A binary classifier's predicted probability against the observed rate is a natural fit. If an event happens 70% of the time in your data and a model predicts 45% on average, the default numbers describe that miscalibration, and 0.184 bits is its cost per prediction.
Small differences give small divergences. The close preset, P(1) of 0.5 against Q(1) of 0.45, gives a forward KL of 0.0072 bits and a JSD of 0.0018 bits. Keeping P at 0.7 and moving Q to 0.6 gives a forward KL of 0.031 bits, and moving Q to 0.65 gives 0.008 bits. KL grows roughly with the square of the gap when the distributions are close, so halving the error from 0.1 to 0.05 cuts the divergence by about three quarters.
For distributions with more than two outcomes, this page will not do the sum for you, but the hand method above works the same way with more terms. Make sure each distribution sums to 1 before you start.
That square-law behaviour is worth remembering when you read divergences in a report. A model that is twice as far off is not twice as bad by this measure, it is roughly four times as bad, at least while both distributions stay away from the edges.