KL Divergence
A measure of how far one probability distribution sits from another — the extra "surprise" you pay for modeling the world with the wrong distribution. Always non-negative, zero only when the two match, and deliberately not symmetric.
Prerequisites: Entropy and Information
Once you can measure the uncertainty in a single distribution with entropy, the natural next question is: how different are two distributions? Kullback-Leibler (KL) divergence answers it. Think of it as the penalty for being wrong: you believe the world follows a model , but it actually follows . KL divergence counts the extra bits of surprise you rack up, on average, by using the wrong distribution — the inefficiency of your model. It is the workhorse "distance" between distributions across statistics and machine learning, even though, as we'll see, it isn't quite a distance.
The formula
For a true distribution and a model over the same outcomes, the KL divergence from to is
Each is the true probability of outcome , each is your model's probability for it, and is the per-outcome "extra surprise" — positive where your model under-weights an outcome, negative where it over-weights it. Weighting those by how often each outcome truly occurs (the out front) and summing gives the average extra cost. Two facts pin it down: it is always , and it equals zero only when exactly. The bigger it is, the worse your model.
is the extra bits of surprise from using model when the truth is . It is always and equals zero only when matches exactly.
Worked example: the wrong coin
The truth is a fair coin, . Your model wrongly thinks it is biased, . How costly is that mistake?
Now reverse the roles — pretend is the truth and the model:
The two numbers differ — 0.208 versus 0.188. That is the point: KL divergence is not symmetric. "How wrong is about " and "how wrong is about " are genuinely different questions, and the direction you write matters.
KL divergence is not a true distance. It is asymmetric () and violates the triangle inequality, so never treat it as a metric. Worse, if your model assigns probability zero to something says can happen, the divergence is infinite — one impossible-according-to-you event that actually occurs makes the model infinitely surprised.
Why it is everywhere
- It is what maximum likelihood minimizes. Fitting a model by maximum likelihood is exactly minimizing the KL divergence from your model to the data's empirical distribution. The "cross-entropy loss" that trains classifiers is the same quantity in disguise.
- It scores Bayesian updates. In Bayesian inference, the information a dataset provides is the KL divergence from the prior to the posterior, and variational methods and MCMC both lean on KL to measure how close an approximation is to the target.
- It underlies test power. The ability of a hypothesis test to tell two distributions apart grows with the KL divergence between them — more divergence, easier to distinguish.
Remember the decomposition cross-entropy = entropy + KL divergence. The entropy is the irreducible surprise you'd pay even with a perfect model; the KL term is the avoidable extra you pay for using the wrong one. Minimizing cross-entropy and minimizing KL are the same thing.
KL divergence is the bridge from entropy to model-fitting: entropy measures the surprise inside one distribution, KL measures the surprise of swapping one distribution for another. That single idea shows up in nearly every corner of statistics, machine learning, and quantitative modeling.
Related concepts
Practice in interviews
Further reading
- Cover & Thomas, Elements of Information Theory (ch. 2)
- Kullback & Leibler, On Information and Sufficiency (1951)