KL Divergence Calculator
Statistics
- Kullback-Leibler divergence for Bernoulli laws: D(P||Q) = p ln(p/q) + (1-p) ln((1-p)/(1-q)), with p = 0.50 and q = 0.25.
- First term: 0.50 x ln(0.50 / 0.25) = 0.3466; second term: 0.50 x ln(0.50 / 0.75) = -0.2027.
- D(P||Q) = 0.3466 + -0.2027 = 0.1438 nats = 0.2075 bits.
- KL result: 0.1438 nats one way, 0.1308 nats back - the asymmetry is the point of the measure.
Every model, code, forecast and summary is a stand-in for reality. This kl divergence calculator prices that substitution for the simplest case, a binary outcome: you enter the probability of an event under the true distribution P and its probability under the approximating distribution Q, and the calculator returns the Kullback-Leibler divergence between the two Bernoulli laws.
The divergence is reported in nats and in bits, together with the reverse divergence D(Q||P) and the symmetrised Jeffreys sum of both directions. Together these four numbers show not only how much information is lost when Q stands in for P, but also that the loss depends on which distribution plays which role, because KL divergence is deliberately, informatively asymmetric.
Worked examples
Fair coin modelled as 0.25
(0.5 · 0.25)
| KL divergence | 0.1438 nats |
| In bits | 0.2075 bits |
| Reverse KL D(Q||P) | 0.1308 nats |
| Jeffreys (symmetrised) | 0.2747 nats |
Confident model, P 0.9 against Q 0.5
(0.9 · 0.5)
| KL divergence | 0.3681 nats |
| In bits | 0.5310 bits |
| Reverse KL D(Q||P) | 0.5108 nats |
| Jeffreys (symmetrised) | 0.8789 nats |
Perfect stand-in: Q equals P
(0.3 · 0.3)
| KL divergence | 0.0000 nats |
| In bits | 0.0000 bits |
| Reverse KL D(Q||P) | 0.0000 nats |
| Jeffreys (symmetrised) | 0.0000 nats |
Information lost by a stand-in distribution
KL divergence prices the information lost when Q approximates P. For binary laws the formula is D(P||Q) = p ln(p/q) + (1-p) ln((1-p)/(1-q)), where p is the probability of the event under P and q is its probability under Q. Each outcome contributes its probability under the true law multiplied by the logarithm of the ratio between the two laws, so outcomes that Q badly misprices dominate the total.
The unit of the answer depends on the logarithm base. With natural logarithms the divergence is measured in nats; with base-2 logarithms it is measured in bits. The calculator reports both, because information theory papers tend to work in nats while coding and compression contexts tend to work in bits. The two scales describe the same loss, and converting between them never changes which of two approximations is better.
One property anchors all interpretation: divergence is zero only for a perfect stand-in. When p and q are equal, every ratio inside the logarithms is one, every logarithm is zero, and the cost vanishes. Any mismatch at all, in either direction, produces a positive cost. KL divergence is therefore a measure of discrepancy with a hard floor at zero, not a signed score that can cancel out.
Using the calculator takes two numbers: the probability under P and the probability under Q, entered as decimals. The default pair, 0.5 against 0.25, reproduces the first worked example below, so you can see every reported quantity in context before substituting your own values. The formula handles the complementary outcome automatically through the (1-p) and (1-q) terms, which is why a binary divergence always has exactly two contributions, one for the event and one for its absence.
- D(P||Q) is zero only when Q matches P exactly
- Individual terms can be negative; the total never is
- Nats use natural logarithms, bits use base-2 logarithms
- A probability of zero in Q where P is positive is the costliest mismatch
Worked examples in nats and bits
The default example sets p = 0.50 against q = 0.25: the stand-in halves the true probability of the event. The first term is 0.50 x ln(0.50 / 0.25) = 0.3466 and the second term is 0.50 x ln(0.50 / 0.75) = -0.2027. Adding them, D(P||Q) = 0.3466 + -0.2027 = 0.1438 nats = 0.2075 bits. The reverse divergence D(Q||P) is 0.1308 nats, and the Jeffreys symmetrised sum of the two directions is 0.2747 nats.
The second example sets p = 0.90 against q = 0.50, a stand-in that treats a near-certain event as a coin flip. The first term is 0.90 x ln(0.90 / 0.50) = 0.5290 and the second term is 0.10 x ln(0.10 / 0.50) = -0.1609, giving D(P||Q) = 0.5290 + -0.1609 = 0.3681 nats = 0.5310 bits. Here the reverse direction is the expensive one: D(Q||P) is 0.5108 nats, and the Jeffreys sum reaches 0.8789 nats.
The third example is the control case: p = 0.30 against q = 0.30. The first term is 0.30 x ln(0.30 / 0.30) = 0.0000 and the second term is 0.70 x ln(0.70 / 0.70) = 0.0000, so D(P||Q) = 0.0000 + 0.0000 = 0.0000 nats = 0.0000 bits, with the reverse direction and the Jeffreys sum also at 0.0000 nats. The table below collects all three examples.
| P | Q | D(P||Q) | In bits | Reverse KL D(Q||P) | Jeffreys (symmetrised) |
|---|---|---|---|---|---|
| 0.5 | 0.25 | 0.1438 nats | 0.2075 bits | 0.1308 nats | 0.2747 nats |
| 0.9 | 0.5 | 0.3681 nats | 0.5310 bits | 0.5108 nats | 0.8789 nats |
| 0.3 | 0.3 | 0.0000 nats | 0.0000 bits | 0.0000 nats | 0.0000 nats |
Why the direction matters
KL divergence is not a distance, and its asymmetry is the point of the measure. D(P||Q) asks: if the world follows P, how costly is it to describe or predict that world using Q? D(Q||P) asks the reverse question, and there is no reason the two answers should agree. In the first worked example they are 0.1438 nats and 0.1308 nats; in the second they are 0.3681 nats and 0.5108 nats, with the ordering between the directions flipped.
The asymmetry has a concrete meaning. The forward direction weights each outcome by its probability under the true law P, so it punishes Q most heavily for underpricing outcomes that actually happen often. A stand-in that assigns a tiny probability to a common event is ruinous in the forward direction, because the logarithm of the ratio explodes exactly where the weight is largest.
When a single symmetric number is genuinely needed, the Jeffreys sum adds both directions: 0.2747 nats for the first example and 0.8789 nats for the second. That total is symmetric by construction, but it also blends two different questions into one figure. In modelling work it is usually better to report the direction that matches your use of the approximation, and to let the calculator's reverse and Jeffreys values serve as context rather than as replacements.
It helps to read the two directions as two different modelling commitments. The forward direction, D(P||Q), is the cost that matters when P is the reality your system will actually face and Q is the model you ship: reality generates the data, and the model pays for every surprise. The reverse direction matters when the roles swap, for instance when samples drawn from the approximating law are used to judge the true one. Reporting both, as this calculator does, prevents a common sleight of hand in which the cheaper direction is quoted and the expensive one quietly ignored.
From KL to cross-entropy
KL divergence sits one step away from two quantities machine learning practitioners meet daily. The cross-entropy of Q relative to P equals the entropy of P plus D(P||Q), so for a fixed true distribution, minimising cross-entropy and minimising KL divergence are the same optimisation. This is why training a classifier by cross-entropy loss is, in information terms, an attempt to drive a divergence like the one on this page towards zero.
The Bernoulli case on this page is the atomic version of that story. A single binary prediction, a predicted probability q for an event whose true rate is p, already carries the full structure: the two weighted log-ratio terms, the asymmetry, and the zero floor at a perfect match. Multi-category and continuous versions replace the two terms with sums and integrals, but the interpretation survives unchanged.
A practical caution closes the loop. Because the divergence weights outcomes by the true law, estimates built from small samples can be badly skewed when a rare outcome is mispriced, and a stand-in that rules out an outcome the true law allows is the most expensive error of all. Check the direction, check both units, and treat any divergence near zero, like the 0.0000 nats of the matched example, as a claim that the two laws agree, not merely that they are close.
Magnitudes become intuitive with a little practice on the examples here. A divergence of 0.1438 nats describes a stand-in that is recognisably wrong but serviceable: it misstates the odds, yet rarely produces a shocking outcome. A divergence of 0.3681 nats, with a reverse of 0.5108 nats and a Jeffreys sum of 0.8789 nats, describes a stand-in that will be caught out regularly, because outcomes it treats as unlikely are in fact routine. Between those poles, the Jeffreys sum offers a quick single-figure summary when a symmetric headline number is unavoidable, while the pair of directional values remains the fuller report.
FAQs
What is D(P||Q) for P = 0.5 and Q = 0.25?
The divergence is 0.1438 nats. The first term is 0.3466 and the second term is -0.2027, and adding them gives D(P||Q) = 0.1438 nats.
The same in bits?
In bits the same example is 0.2075 bits. Bits simply re-express the divergence on a base-2 logarithm scale, so the ordering of approximations is unchanged.
And the reverse direction?
The reverse direction is 0.1308 nats - KL is not symmetric. D(Q||P) prices the loss of using P as the stand-in for Q, which is a different question from the forward direction.
P = 0.9 against Q = 0.5?
The divergence is 0.3681 nats, or 0.5310 bits. The reverse direction is larger still at 0.5108 nats, and the Jeffreys sum is 0.8789 nats.
What if Q equals P, say both 0.3?
The divergence is 0.0000 nats. When Q equals P every logarithm in the formula is the logarithm of one, so both terms vanish and the stand-in loses no information at all.
What is the symmetrised Jeffreys sum for the first example?
The Jeffreys sum for the first example is 0.2747 nats. It adds the forward divergence 0.1438 nats and the reverse divergence 0.1308 nats into a single symmetric total.