~Variational inference
but not via the evidence lower bound
In this short note, I recall variational inference and the evidence lower bound (ELBO). In contrast to the introductions I have seen, I start by describing variational inference as a loss minimisation problem in its own right without reference to the ELBO, and then derive the ELBO afterwards.
§Bayesian inference
Let’s briefly recall the set-up of Bayesian inference.
Let be a parameter space, indexing a parametric statistical model class of interest. (In the following, I will refer to each as parameters or models interchangeably.)
Let be a prior distribution encoding our initial belief as to which parameter is the true data-generating model.
Let be the likelihood function, encoding the probability of observing the given data conditional on each parameter/model.
Bayesian inference says that, given this set-up, our updated posterior distribution over the parameters, denoted , should be given by the conditional probability of the parameters given the data. That is, by Bayes’ rule, where is a normalisation constant.
§Evidence
The normalisation constant can easily be expressed as the thing that makes integrate to 1,
This quantity is known to different communities by several different names.
In probabilistic terms, represents the marginal probability of the data under the given model class (with the given prior). Accordingly, it is called the marginal likelihood.
In the context of model comparison, this marginal likelihood would be a key factor in selecting which of two separate model classes best explains the data. This earns the alternative name of model evidence (or, simply evidence, which is the term I adopt in this note).
The use of the symbol derives from an analogous quantity in statistical mechanics, which in the original German was called the Zustandssumme (literally, “state sum”). In this analogy, the parameters are the potential states or configurations of the statistical problem.
While it’s easy to express the evidence as an integral, computing that integral for high-dimensional parameter spaces with non-trivial likelihood functions is often intractable.
This is the main barrier to using exact Bayesian inference in practice. Without normalisation, we can’t make any absolute statements about our posterior beliefs about particular parameters in our model class, or comparisons between the suitability of different model classes (we can only make comparisons between parameters within the one model class).
§Variational inference
That’s where variational inference comes in: it’s a method of constructing an approximate posterior that is as similar as possible to the Bayesian posterior, avoiding having to normalise the Bayesian posterior itself.
At a high level, the method works as follows:
We first fix some variational family, , a set of distributions over parameter space which we have efficient access to (for example, we should know how to compute their normalised densities, and how to sample from them).
Then, we somehow pick from this family a single distribution, , of “best fit” to the posterior. That is, should be maximally similar to the Bayesian posterior according to some measure of divergence between distributions.
This distribution becomes our variational posterior and serves as our approximation for the Bayesian posterior . The process of going from a prior to a variational posterior is called variational inference.
In detail, the standard measure of divergence used is the Kullback–Leibler (KL) divergence from the Bayesian posterior to the candidate variational posterior, This KL divergence is lower if the approximations are intuitively more similar, and zero if and only if they are equal.
Computing the KL divergence itself without knowing the Bayesian posterior would be challenging, but it turns out we can get away without it for the purposes of optimisation. Observe:
Take a look at the three terms we ended up with:
The first term here is the expected negative log likelihood of the data under the candidate variational posterior . This is easy enough to optimise using stochastic gradient descent as long as we can sample parameters from (some care may be needed to autodiff through the sampler if you use PyTorch).
The second term is just the KL divergence of the candidate variational posterior from the prior . This is likely easy enough to optimise assuming we can sample parameters from . Or, if we choose and carefully, we might even be able to write the expectation in closed form.
The third term is the log evidence, which is hard to compute. But notice it doesn’t depend on the candidate variational posterior . So, it’s actually trivial to optimise since the gradient is always zero.
Equivalently, the objective we end up minimising is shifted down by the log evidence, The right-hand side of the above equation is a tractable objective that can be optimised at scale using stochastic gradient descent.
§Evidence lower bound
A property of the KL divergence between any two distributions is that it is always non-negative. We can therefore rearrange the above decomposition of the KL divergence from the Bayesian posterior to produce the following inequality:
The final expression, , is named the evidence lower bound (ELBO). Formally, define such that for we have
To spell it out, we have, for all , This explains the name “evidence lower bound,” except that we have produced a bound on the logarithm of the evidence (rather than the evidence itself).
The gap in the bound is the term we dropped, , which is smaller the closer we make to , and zero if and only if .
Notice that the bound holds regardless of which variational posterior distribution we plug in. This suggests the following methodology for approximating by trying to make the bound as tight as possible.
First, fix some variational family, , a set of distributions over parameter space which we have efficient access to (for example, we should know how to compute their normalised densities, and how to sample from them).
Then, we somehow pick from this family a single distribution, , giving the best bound. That is, should be chosen to maximise the evidence lower bound .
We can apply the bound to reveal that , this being the tightest possible bound available using our chosen variational family. Assuming we have a sufficiently expressive variational family, we could even use as an approximation of for purposes of model selection.
How do we maximise the ELBO over a variational family? Well, that’s just the same as minimising the negative ELBO over the variational family. That is, finding so as to minimise . Wait a second, that’s just the same as the variational inference objective. So, we’re done.
§Conclusion
In both variational inference and evidence lower bound maximisation, we discovered equivalent optimisation problems: optimising an expectation over a class of distributions. That we are optimising over a space of distributions is the “variational” part of the method.
In both cases, the quality of the approximation we get (how good is the variational posterior as an approximation of the Bayesian posterior, or how tight is the maximised evidence lower bound) comes down to how small we can get . In theory, this is just about having a sufficiently expressive variational family, so that somewhere there is a distribution that is close to . In practice, if we make the variational family very complex, we also have to worry about whether we can find a good fit using practical optimisation methods such as stochastic gradient descent and friends.
If our parametric statistical model class is based on a neural network, the posteriors can be very thorny indeed, even in simple cases. These posteriors defy approximation by unimodal Gaussian posteriors that are commonly studied in the literature. It remains to be seen whether we can find variational families that can better approximate these neural posteriors while still being tractable to work with.