In one of my courses this semester, Bayesian Models for Machine Learning, we’ve been spending quite a bit of time on a technique called “Variational Inference”. I’ve spent the last few days working on an assigment using this technique, so I thought this would be a good occasion to test knowledge by attempting to describe the method. Much credit to John Paisley for teaching me all this in the first place. The Columbia faculty are really top-notch.

High-level Overview

First, a brief overview of Bayesian statistics. We begin with a distribution on our data , paramaterized by :

Using basic rules of joint and conditional probability, we derive the theorem:

For those unfamiliar with Bayes Theorem, our goal is to use the data to learn a better distribution on . In other words, Bayes Theorem gives us a formal way to update our predictions, given our experience of the world. In particular,

is known as the posterior distribution of . This distribution is our goal.

Now, the formulas given above are in terms of probability distributions. If we actually look under the hood at what these probabilities actually look like… well, it looks like a lot of calculus. Using Bayes Theorem involves working with those integrals. Fortunately, over the years statisticians have developed a pretty sophisticated body of knowledge around manipulating these probability distributions, so that (if you make smart choices about which distributions you pick) you can skip basically all of the calculus. This is convenient.

However, for more complicated models, things aren’t always guaranteed to work out so nicely. Sometimes, when we try to model something, we find that calculating the posteriors directly is impossible. What can we do?

Fortunately, statisticians have developed techniques for handling this. Variational Inference is one of those techniques.

Now, we will derive the VI master equation.

Recall some basic rules of probability:

Now, we introduce an entirely new distribution, , and take the expectation with regards to this distribution:

Observing that the left-hand term is constant with respect to :

We then add and subtract the entropy of :

And reorganize:

Let’s take a moment to understand what was just derived. We have shown that the log probability of the random variable is equal to the involved-looking equation on the right-hand side. This right-hand term is the sum of two terms. The first, which we call , we will refer to as the “Variational Objective Function”. The second is the equation of something known as the Kullback–Leibler divergence, or KL divergence for short.

Recall that our goal is to learn

We see that this term appears in the KL divergence, next to this new distribution we are calling . Conveniently, the KL divergence is a measure of the difference between two distributions. When the distributions are equal, the KL divergence equals 0. The more the distributions differ, the larger the term becomes. We’re not sure what this distribution is, but let’s assume that we can control it. The closer it comes to approximating the posterior, the smaller the KL divergence will become.

We see also that the left-hand term, , is a constant (just the probability of the data). So we have a constant equal to an equation plus a term we want to minimize. Therefore, if we can find a way to maximize the term, we will necessarily minimize the KL divergence. Therefore, the problem becomes one of finding a distribution which maximizes !

In Context

Let’s consider the model in the context of a very interesting problem. Say we have a non-linear function (such as or similar) and we would like to approximate this function via Bayesian Linear Regression. Approximating a non-linear function via a naive linear regression is not really feasible. However, if we expand the data into higher dimensions, it may be possible to learn a linear regression in the higher-dimensional space that corresponds to a non-linear function in the original dimension. This is what we will attempt in this problem.

We will project into , by projecting each into a vector of distances defined by the Gaussian kernel. In other words, every will become a vector representing that point’s distance from every other point in the set – and , the data, becomes a matrix of distances, with and for all . Adding a column of ones to represent any intercept term, we can now interpret the problem as a regression. Our goal is then to find a coefficient vector which, given the vector of difference, map the point to its correct location in the original space, .

This transformation is quite a profound. We have many tools for working with linear functions (i.e. linear algebra), but fewer for working with non-linear functions. We found ourselves facing a problem, and rather than attempt to solve the problem using a limited toolset, we simply transformed the problem into one that we can approach skillfully. For any Ender’s Game fans out there, this is an “enemy’s gate is down” kind of moment. Anyway.

Complicating the problem further, we would like to encourage sparsity in – in other words, we would like to identify a small subset of which are sufficiently discriminative to allow us to correctly place the other points. We can think of these points as “vantage points”, and they are especially good at creating distance between points and illuminating their differences.

With that setup, we turn to the actual model, which looks like this:

The joint probability distribution is as follows:

And the log joint probability is as follows:

We model the distribution as a joint probability of independent distributions, one per variable:

Now, with the model defined, we plug these values in to the VI maste requation we derived earlier:

Learning q

Recall, our goal is to maximize:

We will do this by finding better values for . To show how this is done, let’s first consider in isolation (the process will be the same for each variable). First, we reorganize the equation to remove all terms which are constant with respect to (in other words, which won’t change regardless of , so aren’t important when it comes to maximizing the equation with regards to ):

Observe next that we can interpret the first integral as an expectation, where , the expectation over all other variables.

We will now pull off some slick math. First, observe that . Now:

This is looking an awful lot like our friend the KL divergence. If only the numerator were a probability distribution! Fortunately we can make it one, by introducing a new term :

Here, Z can be interpreted as the normalizing constant for the distribution . By adding and subtracting , which is constant with respect to , we witness some more slick math:

Where are we now? We have succesfully transformed the integral into a KL divergence between , our distribution of interest, and , which is an expression involving terms we know. Specifically, we have:

We want to maximize this expression, which is equivalent to minimizing -KL. We minimize -KL when the two distributions are equal. Therefore, we know that:

Sweet! Now we just need to solve for the left hand term. It’s worth pausing and noting how much fancy math it took to get us here. We relied on properties of logarithms, expectations, KL divergence, and the mechanics of probability distributions to derive this expression.

To actually evaluate this expression and figure out what should be, we’ll rewrite things to remove terms not involving , by absorbing them in the normalizing constant. To see why this is the case, let’s first rewrite the expectation:

Recalling the log joint probability we derived earlier:

Putting this back into context, we can rewrite the distribution:

Bringing outside of the integral all terms constant with respect to :

And then cancelling:

All that is left to do is to evaluate the expression

to learn the distribution. We will not go through the specific derivation here, which involves evaluating the expectation of the log of the distributions on and ; instead we will skip to the final result and claim that:


The key observation to make here is that involves the expected values of the other model variables. This will be true for the other variables as well. To show this, here are and :

Finally, we give the expectations:

Now we are prepared to discuss the value of this technique. Note how each distribution is a function of the expectations of the other random variables, with respect to their distributions. This means that as one distribution changes, the others change… causing the first to change, causing the others to change, over and over again in a loop. The insight is that each change brings the closer to the true posterior that we are trying to approximate. In other words, each iteration through this update loop gives us a better set of , as improved values for one give improved values for the others. Also note that since we have solved for the various distributions solely in terms of the data and the expecatations , we can implement the algorithm efficiently using only basic arithmetic operations, without having to do any calculus or derive anything!

Assessing Convergence

The last thing we will need to do is discuss the process for assessing convergence – calculating how much and how quickly our distributions are closing in on the true posteriors. To do this, we will have to evaluate the entire equation using the new distributions. Recall the equation:

Which can be written as follows:

And interpreted as a sum of expectations:

Which breaks down as follows:

There is an important subtlety in evaluating these expectations. To understand this subtlety, let’s look at two of the terms:

Observe how both are expectations over . However the probability distributions are not the same. To see how this works out, let’s write out the actual log probabilities (both distributions are Gamma).

Now, passing the expectations through:

Writing in terms of the difference (note that the sign changes in the second expectation):

And combining terms:

Notice how for both distributions, is identical. This is because is a function of the distribution with which we are taking the expectation, . Therefore, even though the paramaters for don’t change (they remain always), evaluates to a different result as changes. For , on the other hand, we always use the latest values of .

At first I found it counterintuitive that should be constant through every iteration – the Bayesian insight is that priors are constantly being updated as information comes in. The reason why, in this case, are constant (and this is true for the priors on the other distributions as well) is that the entire Variational Inference algorithm is meant to approximate a single Bayesian update. Thus, it is wrong to interpret the distribution learned from iteration as the new prior on the model variables for iteration . In the context of the single update (regardless of current iteration), is always the initial prior, and is the best posterior-so-far. The VI concept is that we can iteratively improve the posteriors , but always in the context of a single Bayesian update.

Final Thoughts

We have presented Variational Inference, in a hopefully accessible manner. It is a very slick technique that I am excited to continue to gain skill in applying. VI is the inference technique which underlies Latent Dirichlet Allocation, a very popular learning algorithm developed by David Blei (now at Columbia!), Andrew Ng, and Michael Jordan, all Machine Learning heavyweights, while the former was at Cal (Go Bears)!

Earlier, we mentioned that we wanted to encourage sparsity in . This can be accomplished (so I am told), by setting the priors on to . Tiny priors here will limit the dimensions of which are signicantly non-zero. I’m not entirely sure why (something I still need to look into), but I can assure you that my model was super sparse :).

Variational Inference is a fairly sophisticated technique (the most complex algorithm I have encountered, but that might not count for much), and allows for the formal definition and learning of complex posteriors otherwise intractable using normal Bayesian methods.