Approximating KL Divergence
This post is about Monte-Carlo approximations of KL divergence.
Our options for computing KL depend on what kind of access we have to and . Here, we’ll be assuming that we can compute the probabilities (or probability densities) and for any , but we can’t calculate the sum over analytically. Why wouldn’t we be able to calculate it analytically?
- Computing it exactly requires too much computation or memory.
- There’s no closed form expression.
- We can simplify code by just storing the log-prob, not the whole distribution. This is a reasonable choice if KL is just being used as a diagnostic, as is often the case in reinforcement learning.
The most common strategy for estimating sums or integrals is to use a Monte-Carlo estimate. Given samples (x_1, x_2, \dots \sim q), how can we construct a good estimate?
A good estimator is unbiased (it has the right mean) and has low variance. We know that one unbiased estimator (under samples from ) is (\log \iratio). However, it has high-variance, as it’s negative for half of the samples, whereas KL is always positive. Let’s call this naive estimator (k_1 = \log \iratio = - \log r), where we’ve defined the ratio that’ll appear frequently in the subsequent calculations.
An alternative estimator, which has lower variance but is biased, is (\frac{1}{2}(\log \ratio)^2 = \half (\log r)^2). Let’s call this estimator . Intuitively, seems to be better because each sample tells you how far apart and are, and it’s always positive. Empirically, does indeed have much lower variance than , and also has remarkably low bias. (We’ll show this in an experiment below.)
There’s a good reason why estimator has low bias: its expectation is an f-divergence. An f-divergence is defined as (D_f(p,q) = E_{x \sim q}[f(\ratio)]) for a convex function . KL divergence and various other well-known probability distances are f-divergences. Now here’s the key non-obvious fact: all f-divergences with differentiable look like KL divergence up to second order when is close to . Namely, for a parametrized distribution ,
where is the Fisher information matrix for evaluated at .
is the f-divergence where (f(x)=\half (\log x)^2), whereas corresponds to . It’s easy to check that both have , so both look like the same quadratic distance function for .
Is it possible to write down a KL divergence estimator that is unbiased but also low variance? The general way to lower variance is with a control variate. I.e., take and add something that has expectation zero but is negatively correlated with . The only interesting quantity that’s guaranteed to have zero expectation is . So for any , the expression is an unbiased estimator of . We can do a calculation to minimize the variance of this estimator and solve for . But unfortunately we get an expression that depends on and and is hard to calculate analytically.
However, we can choose a good using a simpler strategy. Note that since log is concave, (\log(x) \le x - 1). Therefore, if we let , the expression above is guaranteed to be positive. It measures the vertical distance between and its tangent. This leaves us with the estimator (k_3 = (r - 1) - \log r).
The idea of measuring distance by looking at the difference between a convex function and its tangent plane appears in many places. It’s called a Bregman divergence and has many beautiful properties.
We can generalize the above idea to get a good, always-positive estimator for any f-divergence, most notably the other KL divergence (note that and are switched here). Since is by convex, and and , the following is an estimator of the f-divergence: (f(r) - f'(1)(r-1)). This is always positive because it’s the distance between and its tangent at , and convex functions lie above their tangent lines. Now corresponds to , which has , leaving us with the estimator .
In summary, we have the following estimators (for samples , and (r = \ratio)):
Now let’s compare the bias and variance of the three estimators for . Suppose , . Here, the true KL is 0.005.
| bias/true | stdev/true | |
| k1 | 0 | 20 |
| k2 | 0.002 | 1.42 |
| k3 | 0 | 1.42 |
Note that the bias of k2 is incredibly low here: it’s 0.2%.
Now let’s try for a larger true KL divergence. gives us a true KL divergence of 0.5.
| bias/true | stdev/true | |
| k1 | 0 | 2 |
| k2 | 0.25 | 1.73 |
| k3 | 0 | 1.7 |
Here, the bias of k2 is much larger. k3 has even lower standard deviation than k2 while being unbiased, so it appears to be a strictly better estimator.
Here’s the code I used to get these results:
import torch.distributions as dis
p = dis.Normal(loc=0, scale=1)
q = dis.Normal(loc=0.1, scale=1)
x = q.sample(sample_shape=(10_000_000,))
truekl = dis.kl_divergence(p, q)
print("true", truekl)
logr = p.log_prob(x) - q.log_prob(x)
k1 = -logr
k2 = logr ** 2 / 2
k3 = (logr.exp() - 1) - logr
for k in (k1, k2, k3):
print((k.mean() - truekl) / truekl, k.std() / truekl)Thanks to Jacob Hilton and Nisan Stiennon for helpful feedback.