16 min read
KL divergence — why the direction you minimize changes what you get
KL divergence looks like a distance but fails the triangle inequality and isn't symmetric: . Maximum likelihood minimizes one direction; variational inference is forced into the other. The same formula, pointed backward, will happily ignore an entire mode of the distribution you're trying to model.
By the end you should be able to compute for two discrete distributions, predict whether forward or reverse KL covers or ignores a mode of , and diagnose which direction a trained model minimized from its mode-covering or mode-seeking behavior.
Contents
Cost of distributional mismatch
What does it cost, in expected bits, to believe when the true distribution is ?
The gambler models the die with , a single peak stacked on face 1. The die itself follows , which splits its mass almost evenly between face 1 and face 4 — matching at face 1 but leaving face 4 almost untouched. Every roll the gambler bets according to instead of , something is lost, but nothing yet turns that loss into a number.
A first guess is to line up and face by face and sum the plain differences. That number treats every face as interchangeable; it never asks what a wrong belief actually costs. The cost shows up in coding. An optimal code built for spends about bits on outcome , the shortest expected length any code tuned to can achieve [1].
On the die, this overspend is largest exactly on face 4, the face favors and neglects; face 1 costs little because already tracks there. To turn the per-outcome overspend into one number, average it the way rolls actually happen, expectation under the true distribution the true , not under the gambler's mistaken .
Summing this weighted excess over all six faces gives one scalar mismatch score for the whole die. But face 1's overspend and some other face's possible undershoot could in principle offset each other, and nothing said so far rules out the total landing below zero, which would mean the wrong code somehow saves bits on average.
KL divergence definition
How do we formalize the mismatch cost as ?
Take the excess-bit cost for one face of the die, say face 4: . That is a single number tied to a single outcome. The die has six faces, and does not visit them equally — it puts almost half its weight on two distant faces and almost none on the rest. Do you add the six per-face costs straight up, or weight each one by how often that face actually comes up under ? Get this wrong and the total either treats a face never sees as equally important, or lets a face cares about vanish into the average.
Weight each per-face cost by its true frequency and sum: . This sum has a name, the forward KL divergence.
On the die, suppose puts on face 1 and on face 4, with spread over the remaining four faces. The gambler's is unimodal: on face 1, small residual probability elsewhere. The face-4 term is ; if is small this single log-ratio is large, and it still gets multiplied by because that is what actually assigns to face 4. The face-1 term stays small because . Adding all six terms produces one scalar, the mismatch cost of using on a die genuinely distributed as .
Nothing in that sum treats and interchangeably. Every weight in front of a log-ratio term comes from , never from ; swap which distribution supplies the weights and the total changes for a structural reason, not from rounding. Whether this number can ever come out negative — letting a badly mismatched score as though it cost nothing — is still open.
Why KL is never negative
Why is for every pair of distributions, with equality only when ?
Suppose the gambler's happens to overlap one of the die's two heavy faces while missing the other one entirely. At the overlapping face the log-ratio turns negative, since now assigns that face even more probability than does. At the missed face the log-ratio is a large positive number. Could a clever choice of push the -weighted sum of these terms below zero, making the mismatch score negative?
Try it directly. Let , , and put , . The face-1 term is , negative as hoped. But the face-4 term is , and it overwhelms the negative one. Shuffle the twelve numbers however you like and the same thing happens: mass steals from an underweighted face produces a positive log-ratio large enough to cancel whatever negative term the overlap bought. That is not a coincidence of this particular [1].
Jensen's inequality is tight exactly when the quantity being averaged, , is constant across every face with . Since both and sum to 1 over the same six faces, that constant can only be 1, forcing everywhere. This is why the two-peak, one-peak setup above never actually reaches zero: cannot equal while restricted to a single mode, so some positive residual is guaranteed no matter how the gambler tunes it.
So is a legitimate non-negative mismatch score, zero only when the gambler's matches face for face. It is still not a distance in the usual sense: relabel which distribution on this same die gets called and which gets called , and the number the formula returns changes, not just cosmetically.
Forward vs reverse KL
How different are and for the same pair, and why?
You already have for the die: true splits its weight almost evenly across two distant faces, and the gambler's piles almost everything onto one face sitting between them. Flip the formula — write first, second — and recompute. A number that behaves like a distance between two objects shouldn't care which one you call first. Swap and everywhere in and check whether the result matches what you already found.
It doesn't match. forward KL divergence weights every log-ratio by : the two distant faces the gambler barely touches carry heavy weight, because that's where lives. reverse KL divergence weights the same kind of ratio by instead: the single face the gambler favors carries almost all the weight, because that's where lives [1]. Swapping and doesn't just flip a sign — it moves the averaging measure onto a different set of faces. On a face where is large and is tiny, pays a heavy -weighted penalty for that mismatch; barely notices, because is tiny there and the term gets almost no weight. The two sums are built from the same log-ratios but averaged under different distributions, so in general .
How swapping the averaging measure changes the KL value
Forward dominates right at the edges of the range, where is near 0 or 1 and abandons a face still visits, while reverse stays bounded there. But in the interior the ordering can flip: at , reverse KL exceeds forward KL at every sampled from 0.275 to 0.725, so which direction is larger depends on where sits relative to , not just on the distance between them.
This collapses the six-face die to two outcomes: the face under the gambler's peak, with true weight and gambler weight , versus every other face lumped together and held fixed once lumped. It isolates the asymmetry; it is not the full six-face computation.
Notice what the swap actually cost you: it silently changed which mistakes matter. A modeler who fits by minimizing is telling the procedure to worry most about faces where the true die rolls often. A modeler who minimizes is telling it to worry most about faces where its own guess rolls often. Choosing which distribution sits on which side of the bar is a modeling decision with consequences, not free notation.
Mode-covering forward KL
What does minimizing over do to 's shape, and why does maximum likelihood use this direction?
Force 's single hump onto face : soaks up most of the mass, and is left near zero. But is close to , so the per-outcome excess bit cost term at face , , explodes as , and takes on a huge value from that one face alone. Put the hump on face instead and the same disaster happens at face . Neither placement is cheap, because is an average taken under : any face visits often but nearly ignores gets charged, repeatedly, at whatever rate actually visits it.
Minimizing over therefore refuses to let sit near zero at any face where is not already near zero, call it zero-avoiding. With restricted to one hump, the cheapest single location is the one that keeps both heavy faces at moderate distance rather than starving either one: somewhere between face and face . The optimal one-peak lands in that middle ground, spreading mass toward both true peaks instead of committing to one. This is exactly what maximum likelihood does on real data: fitting by maximizing likelihood over samples drawn from minimizes up to a constant that does not depend on , so a unimodal model fit this way to bimodal data blurs between the two modes rather than picking one and abandoning the other.
That coverage is not free, it is just an expense forward KL never bills. To keep both peaks fed, 's single hump also has to cover the faces between them, faces through , where places almost no mass. Nothing in penalizes for spending probability where rarely visits, since near zero there makes that term's weight near zero too. A direction that instead averages under would never let pay for outcomes it does not itself predict.
Mode-seeking reverse KL
What does minimizing over do instead, and why is variational inference forced into this direction?
Try repeating the forward-KL fix here: average the log-ratio under , the true posterior over which die face actually generated the data. That posterior is exactly the object variational inference cannot compute — its normalizing constant sums over every latent configuration, the reason the gambler needs an approximation in the first place. Averaging under requires already knowing . The only distribution the gambler can sample from and average under is itself.
Swap which distribution does the averaging: , the reverse KL divergence. Every term is now weighted by , so a face where contributes nothing to the sum no matter how large is there. The gambler's single-peak is free to ignore one of the two true peaks entirely, as long as it puts all its remaining mass on the other. Minimizing over means picking whichever peak makes the log-ratio smallest at the mass actually assigns — collapsing onto one peak of , the cheapest one to sit on, and treating the second peak as though it does not exist.
This is mode-seeking behavior, and it is not a modeling preference — it is forced by tractability. Variational inference optimizes a bound built from expectations under alone, never under the true posterior , which is exactly why the direction flips [2]. The same asymmetric formula, the same two-peaked die, produces opposite failures depending on which distribution does the averaging: forward KL spreads across both peaks and pays for faces it barely visits, while reverse KL refuses to pay for any face it does not already own and starves the second peak completely.
Maximum likelihood and variational inference therefore inherit opposite failure modes from the same die and the same formula — one blurs across both peaks, the other truncates to one — and telling which direction produced a given fitted from its shape alone is the open diagnostic problem.
Diagnosing the KL direction
Given a fitted model that either blurs across modes or commits to one, how do you diagnose which KL direction produced it?
Hand someone a fitted for the die and hide the training procedure. All they see is a histogram: either mass smeared across faces barely visits, or a tight spike on one face while a second, equally plausible face gets nothing. Which loss produced which picture?
Run the causal chain from each direction backward. Forward averages the log-ratio under , so it charges for every face actually visits, including both of the die's true peaks. A single-peak minimizing that cost has to compromise, sliding its one mode toward the midpoint of the two peaks and picking up nonzero probability on the faces in between. The symptom is diffusion: mass leaking onto outcomes considers implausible. That is the signature of maximum likelihood, which is exactly a forward-KL fit to the data-generating [1].
Reverse averages the same log-ratio under instead, so any face never visits contributes nothing to the cost regardless of how much probability puts there. A single-peak minimizing that cost has no incentive to compromise; it commits entirely to whichever peak is cheaper and reports zero probability for the other, real, peak. The symptom is confident omission, not diffusion. That is the signature of variational inference, forced into this direction because the true posterior is intractable to average under [2].
So the diagnostic reads backward from the shape of : spread mass on low-probability outcomes implicates an average taken under ; a confident peak with a starved alternative implicates an average taken under . The direction of averaging is not a technicality inside the formula, it is the whole difference between a model that hedges across every mode of the truth and one that bets everything on the cheapest mode it can find. Two log-ratios, two directions of averaging, two failure modes visible in any KL-trained model without ever seeing its loss function.
Transfer set
Put the pieces together
These questions combine mechanisms from more than one section. Work from the causal chain before opening the answer.
- 01
Suppose the gambler is allowed to drop the unimodal restriction and match exactly to 's two-peak shape. Using the equality condition from non-negativity together with the forward/reverse weighting mechanism from asymmetry, what happens to and , and does the blur-versus-collapse distinction from forward-kl and reverse-kl still apply?
Show answer
Non-negativity showed equality requires constant everywhere , which forces face for face. If is free to match 's two peaks exactly, that condition is satisfied, so both and, by the same equality condition applied with the roles swapped, . With no mismatch left, the -weighting versus -weighting distinction from asymmetry has nothing left to punish differently, so forward KL's mode-covering blur and reverse KL's mode-seeking collapse both vanish. The blur/collapse split from forward-kl and reverse-kl is therefore a symptom of 's model class being too restrictive to equal , not an inherent property of the KL formula itself.
- 02
Change the die so has three roughly equal distant peaks (faces 1, 3, 6) instead of two, with still restricted to one unimodal peak. Using the mechanisms from forward-kl and reverse-kl, predict how the forward-KL-fit and the reverse-KL-fit each change from the two-peak case.
Show answer
Forward KL averages the log-ratio under , so it still refuses to let sit near zero at any face visits with substantial weight. With three peaks instead of two, the single hump minimizing must now spread enough mass to avoid starving any of the three, so it widens further, straddling all three peak regions rather than just nesting between two, at the cost of even more wasted mass on the faces between them. Reverse KL still averages under , so any face assigns zero mass to costs nothing regardless of how large is there; the cost-minimizing single peak still collapses onto whichever one of the three peaks is cheapest, now ignoring two real peaks instead of one. The same -weighted-versus--weighted mechanism produces a wider blur on one side and a deeper omission on the other.
- 03
A trained model's approximate distribution has exactly zero probability on a mode that the true distribution assigns substantial weight to. Using the diagnosis mechanism together with the reverse-kl mechanism, explain causally why this happened, and connect it back to what the hook means by the formula 'pointed backward' ignoring an entire mode.
Show answer
By the diagnosis section, a confident peak that starves a real alternative implicates reverse , the direction variational inference is forced into because the true posterior is intractable to average under. The reverse-kl mechanism explains why: weights every log-ratio term by , so a face where contributes exactly zero to the sum no matter how large is there, giving the optimizer no cost, and hence no pressure, to cover that mode. This is the concrete mechanism behind the hook's claim: pointing the same log-ratio formula backward, so it averages under instead of , creates a zero-cost path to dropping any mode doesn't already occupy, while forward , which weights by , would have charged an unbounded cost for exactly that omission.
References
- [1]Cover & Thomas, Elements of Information Theory — source coding chapterUnverified model-knowledge reference; verify independently.
- [2]Blei, Kucukelbir & McAuliffe, Variational Inference: A Review for StatisticiansUnverified model-knowledge reference; verify independently.
- [3]Boyd & Vandenberghe, Convex Optimization — Jensen's inequality backgroundUnverified model-knowledge reference; verify independently.