Quant Memo
Core

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 qq, but it actually follows pp. 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 pp and a model qq over the same outcomes, the KL divergence from qq to pp is

DKL(pq)=ipilog2piqi.D_{\mathrm{KL}}(p \,\|\, q) = \sum_i p_i \log_2 \frac{p_i}{q_i}.

Each pip_i is the true probability of outcome ii, each qiq_i is your model's probability for it, and log2(pi/qi)\log_2(p_i/q_i) 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 pip_i out front) and summing gives the average extra cost. Two facts pin it down: it is always 0\ge 0, and it equals zero only when q=pq = p exactly. The bigger it is, the worse your model.

p (true) q (model) A B C D KL measures the total gap between the green and amber bars
The true distribution p (green) versus a model q (amber, here a flat guess). Wherever the bars disagree, KL divergence accumulates a cost — weighted by how often each outcome actually happens. Only when every green bar matches its amber partner does the divergence hit zero.

DKL(pq)=pilog2(pi/qi)D_{\mathrm{KL}}(p \,\|\, q) = \sum p_i \log_2(p_i/q_i) is the extra bits of surprise from using model qq when the truth is pp. It is always 0\ge 0 and equals zero only when qq matches pp exactly.

Worked example: the wrong coin

The truth is a fair coin, p=(0.5,0.5)p = (0.5, 0.5). Your model wrongly thinks it is biased, q=(0.25,0.75)q = (0.25, 0.75). How costly is that mistake?

DKL(pq)=0.5log20.50.25+0.5log20.50.75=0.5(1)+0.5(0.585)=0.208 bits.D_{\mathrm{KL}}(p \,\|\, q) = 0.5\log_2\frac{0.5}{0.25} + 0.5\log_2\frac{0.5}{0.75} = 0.5(1) + 0.5(-0.585) = 0.208 \text{ bits}.

Now reverse the roles — pretend qq is the truth and pp the model:

DKL(qp)=0.25log20.250.5+0.75log20.750.5=0.25(1)+0.75(0.585)=0.188 bits.D_{\mathrm{KL}}(q \,\|\, p) = 0.25\log_2\frac{0.25}{0.5} + 0.75\log_2\frac{0.75}{0.5} = 0.25(-1) + 0.75(0.585) = 0.188 \text{ bits}.

The two numbers differ — 0.208 versus 0.188. That is the point: KL divergence is not symmetric. "How wrong is qq about pp" and "how wrong is pp about qq" are genuinely different questions, and the direction you write matters.

KL divergence is not a true distance. It is asymmetric (D(pq)D(qp)D(p\|q) \ne D(q\|p)) and violates the triangle inequality, so never treat it as a metric. Worse, if your model qq assigns probability zero to something pp 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)
ShareTwitterLinkedIn