Files never leave your device

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

Ready. Processing stays in this browser.

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.

  1. Check the example inputs: Distribution P, Distribution Q.
  2. Change the inputs for your problem. Results update as you type, so finish entering the values before copying an answer.
  3. 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

Year 3: Advanced and applied maths

Last updated