KL divergence calculator
Measure how much extra information is needed when Q models data from P. Your inputs stay on your device.
Set up your task
Starting local processing
Ready. Processing stays in this browser.
2 Your result
Your result updates in this browser. Check your input if the result looks unexpected.
How to use KL divergence calculator
Measure how much extra information is needed when Q models data from P. The pre-filled problem gives you a worked starting point. Compare the response with your own working before trying the self-check below.
- Check the example inputs: Distribution P, Distribution Q.
- Change the inputs for your problem. Results update as you type, so finish entering the values before copying an answer.
- Use the working to check an intermediate value or assumption. Then change the example as suggested in the self-check and compare the result.
Supported inputs and limits
Use two probability lists for the same ordered outcomes, each summing to one. Enter up to 64 outcomes.
How it works
Kullback–Leibler divergence measures a mismatch between a reference distribution P and a model Q. It averages the log probability ratio using probabilities from P. In information terms, it is the expected extra number of bits when samples follow P but the code-length model is Q.
Match the outcome ordering before comparing probability lists. An outcome that occurs under P but has zero mass under Q makes the divergence infinite. An outcome impossible under P contributes zero even when Q assigns it probability. Individual contributions can be negative, while the full divergence is non-negative.
Example
Run the pre-filled example. Its first result is “KL(P ∥ Q): 1 bit”.
- Distribution P
- 1 0
- Distribution Q
- 0.5 0.5
KL(P ∥ Q): 1 bit Contributions: 1 0 The order matters: this measures P relative to Q.
Questions about KL divergence calculator
When is the divergence zero?
For equal distributions it is zero. The tool’s approximate display can also round a very small difference to zero.
Can I swap the two probability lists?
You can compute the reverse quantity, but KL divergence is generally asymmetric. Swapping the distributions can change both the value and whether it is finite.
Reverse the example distributions.
The result is infinite because Q would assign zero mass to the second outcome, while P would give it probability one half.
Related tools
Last updated