12. Bayesian Latent Variable Models and Variational Inference#
This chapter is adapted from AM207 (Harvard, Fall 2021). Watch the recorded lecture.
In this chapter
Bayesian Latent Variable Models
Coordinate Ascent Variational Inference
Bayesian Gaussian Mixture Models
12.1. Bayesian Latent Variable Models#
Overfitting is an always a concern when using MLE model parameters. We can mitigate the effect of outliers in the data on the model we learn by treating the parameters as random variables and placing priors on them.
In a latent variable model, maximum likelihood inference treats parameters \(\theta\), \(\phi\) as unknown constants and produces point-estimates for them. In a Bayesian latent variable model, \(\theta\), \(\phi\) are random variables and we derive the posterior distribution over them.
That is, we want to infer
Challenges in Bayesian Inference#
Unfortunately, most Bayesian models with multiple types of random variables (like Bayesian latent variable models) have complex posteriors that do not match known distributions. Exact inference is not possible.
Sampling from the posterior may not always be the best option because:
Convergence of samplers may be slow (due to high dimensionality of the distribution or multimodality)
Samplers like Metropolis-Hastings requires evaluating the likelihood \(\prod_n p(Y_n | Z_n, \phi)\) in each iteration, if the observed data is large (\(N\) is in the millions), this computation is expensive.
The Idea of Variational Inference#
Idea: (Approximate Inference) Approximate the hard posterior \(p(\theta, \phi, Z_1, \ldots, Z_N|Y_1, \ldots, Y_N)\) with a distribution \(q\) that is easy to sample from (like a Gaussian). Any computation involving the posterior can now be done with \(q\).
This approximation of \(p(\theta, \phi, Z_1, \ldots, Z_N|Y_1, \ldots, Y_N)\) with a distribution \(q\) is called variational inference.
The Design of the Variational Objective#
Goal: given a target posterior distribution \(p(\psi | Y_1, \ldots, Y_N)\), \(\psi \in \mathbb{R}^I\) we want to find a distribution \(q(\psi |\lambda^*)\) in a family of distributions \(Q = \{q(\psi |\lambda) | \lambda \in \Lambda \}\) such that \(q(\psi |\lambda^*)\) best approximates \(p\).
Design Choices: we need to choose:
A. (Variational family) a family \(Q\) of candidate distributions for approximating \(p\). The members of \(Q\) are called the variational distributions.
Our Choice: we assume that the joint \(q(\psi)\) factorizes completely over each dimension of \(\psi\), i.e. \(q(\psi)= \prod_{i=1}^I q(\psi_i | \lambda_i)\). This is called the mean field assumption. What can go wrong with this design choice?
B. (Divergence measure) a divergence measure to quantify the difference between \(p\) and \(q\).
Our Choice:
What can go wrong with this design choice?
Variational Inference as Optimization#
We now formalize variational inference for a target \(p(\psi)\): find \(q(\psi|\lambda^*)\) where
Recall that for EM, we had proved that minimizing the KL is equivalent to maximizing the ELBO (for which it is easier to compute the gradient). We will do the same here:
Thus, the variational objective can be rephrased as maximizing the \(ELBO\).
Gradients of the ELBO#
Unfortunately, the ELBO for variational inference of the posterior does not have easy gradients,
In particular, the issue is that the gradient taken is with respect to the parameter \(\psi\) of the distribution over which we are taking the expectation - i.e. we cannot push the gradient into the expectation.
In this chapter, we will maximize the \(ELBO\) using coordinate ascent (just as in the case of EM). But you’ll see that coordinate ascent variational inference requires that we perform model specific computations (often in closed form). This restrict the class of Bayesian models for which we can perform variational inference.
Two of the major developments we will cover in Chapter 22 address how to estimate this gradient efficiently and without bias.
Coordinate Ascent Variational Inference#
Going deeper: Aside
Maximizing the ELBO via Coordinate Ascent#
The coordinate ascent algorithm maximizes an objective function \(ELBO(\lambda)\) by iteratively maximizing over \(\lambda_i\), holding constant \(\lambda_{-i} = [\lambda_1\; \ldots\; \lambda_{i-1}\; \lambda_{i+1}\; \ldots\; \lambda_{I}]\).
The coordinate ascent variational inference algorithm:
Initialization: pick an initial value \(\lambda^{(0)}\)
Coordinate-wise maximization:
Repeat for \(j=1, \ldots, J\) iterations:
\(\quad\quad\) Cycle thru \(i=1, \ldots, I\) coordinates:
where \(\psi_{-i} = [\psi_1\; \ldots\; \psi_{i-1}\; \psi_{i+1}\; \ldots\; \psi_{I}]\).
Going deeper: Proof of the Update Rule for \(q(\psi_i | \lambda^{\text{new}}_i)\)
We want to show that \(q(\psi_i | \lambda^{*}_i) \propto \exp\left\{ \mathbb{E}_{\psi_{-i} \sim q(\phi_{-i} | \lambda_{-i})}\left[\log p(Y_1, \ldots, Y_N, \psi_i | \psi_{-i})\right]\right\}\), where
To maximize the \(ELBO\), we will
use the mean-field assumption to break up the expectation \(\mathbb{E}_{\psi \sim q(\psi|\lambda)}\) into an iterated expectation \(\mathbb{E}_{\psi_i \sim q(\psi_i|\lambda_i)}\mathbb{E}_{\psi_{-i} \sim q(\psi_{-i}|\lambda_{-i})}\)
we will rewrite the outer expectation \(\mathbb{E}_{\psi_i \sim q(\psi_i|\lambda_i)}\) as a negative KL-divergence
we will maximize the negative KL-divergence by setting the two arguments of the divergence equal to each other
For the following, we will use the following notation:
Step 1: Show that \(\mathbb{E}_{\psi \sim q(\psi|\lambda)}[\ldots] = \mathbb{E}_{\psi_i \sim q(\psi_i|\lambda_i)}\left[\mathbb{E}_{\psi_{-i} \sim q(\psi_{-i}|\lambda_{-i})}[\ldots] \right]\) For step 1, we rewrite the ELBO:
Now we decompose the ELBO into terms containing \(\lambda_i\), over which we are going to optimize, and terms not containing \(\lambda_i\):
Note that we don’t need to consider the constant term when optimizing with respect to \(\lambda_i\).
Step 2: Show that \(\underset{\lambda_i}{\max}\mathbb{E}_{\psi_i \sim q(\psi_i|\lambda_i)}[\ldots] \equiv \underset{\lambda_i}{\min} D_{\text{KL}}[\ldots]\) We show that maximizing the \(ELBO\) with respect to \(\lambda_{i}\) is equivalent to minimizing a KL-divergence:
Step 3: Minimize the KL-divergence We see that
is minimized when
This is also exactly where the ELBO is maximized.
We can further rewrite the update rule for \( q(\psi_{i}|\lambda_{i})\) as:
The rational for rewriting the update for \(q(\psi_{i}|\lambda_{i})\) in terms of the joint \(p(Y_1, \ldots, Y_N, \psi)\) is that the joint is easy to compute.
Bayesian Gaussian Mixture Models#
Variational Inference for Bayesian Gaussian Mixture Models#
We consider a Bayesian model for a mixture of \(K\) number of univariate Gaussians:
The hyperparameters of the models are \(\pi, \sigma^2, m_0, s^2_0\), which are constants that must be selected prior to inference. For example, to simplify our computations we selected \(\pi = [1/K, \ldots, 1/K]\), \(m_0 = 0\), \(\sigma=1\).
We make the mean field assumption – that our variational posterior factorizes completely:
Going deeper: Coordinate Ascent Variational Inference Updates for Bayesian GMM
In coordinate ascent variational inference, we update the variational distribution for each variable in turn. For the Bayesian GMM this means:
where
Update rule for \(q(Z_n | \phi_n)\) To update \(q(Z_n | \phi_n)\), we rewrite \(exp\left\{ \mathbb{E}_{\mu \sim q(\mu | m, s^2), Z_{-n} \sim q(Z_{-n} | \phi_{-n})}\left[\log p(Y_1, \ldots, Y_N, Z_1, \ldots, Z_N, \mu)\right]\right\}\), dropping all terms that do not involve \(Z_n\) and \(\mu\):
The likelihood \(p(Y_n | \mu, Z_n)\) is a normal distribution given the value of \(Z_n\), we can express this by treating each \(Z_n\) as an indicator variable – a \(K\)-dimensional binary vector with \(Z_{nk}=1\) if \(Y_n\) is in class \(k\) and 0 otherwise. Thus, we can write
Finally, we can write the update for \(q(Z_n | \phi_n)\) as follows, again dropping all terms not involving \(Z_n\) and \(\mu\):
Since \(q(Z_n | \phi_n)\) is a categorical distribution, we can use the above to compute the \(k\)-th component as:
Update rule for \(q(\mu_k | m_k, s_k^2)\) To update \(q(\mu_k | m_k, s_k^2)\), we compute:
Thus, we see that \(q(\mu_k | m_k, s_k^2) \propto \exp\left\{ - B\left(\mu_k^2 - \frac{A}{2B}\right)^2\right\} \) is a Gaussian with mean
and variance
Implementation of CAVI for Bayesian GMM#
fig, ax = plt.subplots(1, 1, figsize=(10, 5))
ax.hist(y, bins=60, density=True, color='gray', alpha=0.7, label='histogram of birth weights')
posterior_predictive_samples = posterior_predictive_sampling(m_current, s_sq_current, 100)
ax.hist(posterior_predictive_samples, bins=60, density=True, color='red', alpha=0.3, label='variational posterior predictive')
ax.set_title('GMM for Birth Weights')
ax.legend(loc='best')
plt.show()
Sanity Check: ELBO During Training#
Remember that plotting the posterior predictive against actual data is not always an option (e.g. high-dimensional data).
A sanity check for that your CAVI algorithm has been implemented correctly is to plot the ELBO (or alternatively, the observed data log-likelihood) over the iterations of the algorithm:
fig, ax = plt.subplots(1, 1, figsize=(10, 2))
ax.plot(range(len(ELBOs)), ELBOs, color='red', alpha=0.5)
ax.set_title('ELBO over iterations of CAVI')
plt.show()
12.2. Exercises#
These problems were originally assigned as homework. Data files are in the book’s data/ folder. Worked solutions, where available, are collapsed beneath the questions they answer; try each problem before expanding them.
Problem Set: Understanding EM and Variational Inference#
In this problem, we will draw concrete connections between EM and variational inference by applying both methods to a certain class of latent variable models. You’ll need to refer to the derivation of EM in Chapter 11 and the derivation of the variational inference objective earlier in this chapter. This is an essay question that requires you to engage with complex derivations at a productive but still high level. No implementation is required.
Non-Bayesian Latent Variable Model#
Recall the class of latent variable models we studied in Chapter 11:
Bayesian Latent Variable Model#
A Bayesian version of the same class of models involve adding priors for the model parameters:
(Comparing ELBOs) For the above type of Bayesian latent variable model, write down the ELBO for variational inference with a mean field variational family. Compare the variational inference ELBO for the Bayesian model to the expectation maximization ELBO for the non-Bayesian model. What are the differences and similarities between these two ELBOs?
In both EM and variational inference we optimize the ELBO. Compare the update steps in EM to the update steps in Coordinate Ascent Variational Inference, draw a concrete analogy between them.
Hint: To make both ELBO’s comparable, make sure that both are in terms of \(z, y, \theta, \phi\).
(Comparing ELBOs and KL-divergences) Recall that the original objective of variational inference is to minimize a KL-divergence, we rewrote the objective to be that of maximizing the ELBO. Why is directly minimizing the KL-divergence in the original objective difficult (be specific about wherein the difficulty lies)?
In the derivation of the E-step of EM, we reframed an maximization of the ELBO problem as a minimization of a KL-divergence problem. In this case, why was the KL-divergence easier to minimize and the ELBO harder to maximize (use the instantiation of the E-step for Gaussian Mixture Models in Chapter 11 to help support your answer)?
Earlier in this chapter, we introduced a way to maximize the variational inference ELBO – through coordinate ascent. In the derivation of the updates for coordinate ascent, there is a place where we reframed an maximization of the ELBO problem as an equivalent minimization of a KL-divergence problem. Write down the exact form of this equivalence (the two expressions are separated in the derivation by a bunch of lines, you’ll need to identify both parts that you need). In this case, why was the KL-divergence easier to minimize and the ELBO harder to maximization (use the instantiation of the update for Gaussian Mixture Models earlier in this chapter to help support your answer)?
Based on this analysis, can you draw some general conclusions about when we’d prefer to minimize the KL-divergence versus when we’d prefer to maximize the ELBO?
(The Mean Field Assumption and Coordinate Ascent) Describe exactly when and how the mean field assumption is used in the derivation of the coordinate ascent updates.
(Generalizability of CAVI) Summarize what kind of derivations/math is needed in order instantiate Coordinate Ascent Variational Inference (CAVI) for a given new model (look at what we did for Gaussian Mixture Models earlier in this chapter and predict what you’d need to do for a new model). Based on this, discuss the potential draw backs of using CAVI for Bayesian inference in general. What do these draw backs imply about the practicality of variational inference as an inference method?
(Generalizability of EM) Summarize what kind of derivations/math is needed in order instantiate Expectation Maximization (EM) for a given new model (look at what we did for Gaussian Mixture Models in Chapter 11 and predict what you’d need to do for a new model). Based on this, discuss the potential draw backs of using EM for MLE inference in general. What do these draw backs imply about the practicality of EM as an inference method?
Solution: Question 1
Recall that the EM ELBO for this type of latent variable model is
whereas the variational inference ELBO for the Bayesian version of this type of latent variable model is
The similarity between these two ELBO is that they are both expected log ratios between a joint distribution \(p\) and an auxiliary or approximating distribution \(q\). In the case of EM, we are measuring the expected (under \(q\)) log ratio between the auxiliary distribution \(q(z)\) and the joint \(p(y, z|\theta, \phi)\); in the case of variational inference we are measuring the expected log ratio between the variational distribution \(q(z, \phi, \theta |\lambda_1, \lambda_2, \lambda_3)\) and the joint \(p(y, z, \theta, \phi|a, b)\).
The major difference is how the parameters \(\theta\) and \(\phi\) are treated in the two ELBOs. For EM, we treat \(\theta\) and \(\phi\) as point masses, in variational inference we treat them as random variables with distributions. In EM, we optimize the ELBO with respect to \(\theta\) and \(\phi\) as point-masses where as in variational inference we optimize the distribution of \(\theta\) and \(\phi\). Note that the random variable \(z\) plays the same role in both ELBOs.
For those interested in a deeper connection: We can actually prove that EM is a special case of variational inference, by:
imposing flat priors for \(\theta\) and \(\phi\)
performing variational inference on this model with delta distributions for \(q(\theta)\) and \(q(\phi)\)
You’ll derive the same ELBO for variational inference as that for the EM estimation of the MLE of \(\theta, \phi\).
Not covered here: a comparison of the EM update steps with the CAVI update steps.
Solution: Question 2
Variational Objective In variational inference, we are interested in finding a tractable distribution \(q\) that best approximates a complex distribution \(p\). For the latent variable model considered in this problem, the variational objective is
Recall that the posterior \(p(z, \theta, \phi|y, a, b)\) is actually equal to
So when we substitute this into the expression of the KL-divergence, we can write:
Note that the second term in the KL-divergence above is hard to compute:
Now, when we work with the ELBO for variational inference, we have that
That is, the problematic term \(\mathbb{E}_{z, \phi, \theta \sim q(z, \phi, \theta |\lambda_1, \lambda_2, \lambda_3)}\left[\log p(y | a, b)\right]\) is gone!
This is why we say that the ELBO is easier to work with than the KL for variational inference.
The E-step in EM In the E-step of EM we had to compute
where \(\theta^*, \phi^*\) are the optimal values we found in the \(M\)-step.
In order to optimize this ELBO, we needed to take the “gradient” with respect to \(q\)
But since the expectation is also with respect to \(q\), this gradient is hard to compute!
Converting the problem of maximizing the ELBO into one of minimizing a KL-divergence
meant that we can solve the optimization problem immediately – by setting \(q(z)\) equal to \(p(z|y, \theta, \phi)\). In the case, of the Gaussian Mixture Model, the posterior \(p(z|y, \theta, \phi)\) was not difficult to compute. Since the latent variable \(z\) is a categorical variable, we could compute the probability of each class membership \(z=k\) independently. Each computation was very tractable.
The Derivation of the Update for CAVI Let \(\phi = [\phi_1,\ldots, \phi_I]\) be the set of parameters and latent variables for which we want to infer a posterior \(p(\phi | y_1, \ldots y_N)\). We approximate this posterior with a mean field variational family:
In CAVI, we update the variational distribution for each \(\phi_j\), conditioned on the other \(\phi\)’s by the rule:
where \(\psi_{-i} = [\psi_1\; \ldots\; \psi_{i-1}\; \psi_{i+1}\; \ldots\; \psi_{I}]\).
To prove that this is the update that would maximize the ELBO for the coordinate \(i\), we transformed the problem of maximizing the variational inference ELBO with respect to \(q(\phi_i | \lambda_i)\) into a problem of minimizing a KL-divergence between \(q(\phi_i | \lambda_i)\) and some other distribution:
The optimization problem involving the KL-divergence was again easy to solve! We just set \(q(\psi_{i}|\lambda_{i})\) equal to \(\mathcal{Z}\exp\left\{\mathbb{E}_{\psi_{-i} \sim q(\psi_{-i}|\lambda_{-i})} \left[\log \left(p(Y_1, \ldots, Y_N, \psi_i | \psi_{-i}) \right)\right]\right\}\).
So why do we prefer to evaluate this complicated expression \(q(\psi_{i}|\lambda_{i})\) equal to \(\mathcal{Z}\exp\left\{\mathbb{E}_{\psi_{-i} \sim q(\psi_{-i}|\lambda_{-i})} \left[\log \left(p(Y_1, \ldots, Y_N, \psi_i | \psi_{-i}) \right)\right]\right\}\)?
For one, maximizing the ELBO requires taking a gradient with respect to a variable that the expectation depends on: \(\nabla_{\lambda_i} \mathbb{E}_{\psi_i \sim q(\psi_i|\lambda_i)}\left[ \ldots \right]\). This, again, is a hard problem to solve.
Secondly, the expectation \(\mathbb{E}_{\psi_{-i} \sim q(\psi_{-i}|\lambda_{-i})} \left[\log \left(p(Y_1, \ldots, Y_N, \psi_i | \psi_{-i}) \right)\right]\) is not intractable to compute for nice models like Gaussian Mixture Models. Due to the way that these models factorize – in each update, we end up working with one term involving \(\phi_i\), since the other terms involving \(\phi_{-i}\) become constants after taking the expectation \(\mathbb{E}_{\psi_{-i} \sim q(\psi_{-i}|\lambda_{-i})}\).
When to Work with the ELBO and When to Work with a KL? The general rule of thumb is intuitive: if the posterior \(p(A|B)\) is known and tractable then minimizing the KL-divergence \(D_{\text{KL}}[q(A)\|p(A|B)]\) by setting \(q\) equal to \(p\) is preferred. If \(p(A|B)\) is not known or tractable, but the joint \(p(A, B)\) is tractable then it’s preferable to work with the ELBO \(\mathbb{E}_{q(A)}\left[\log \frac{p(A, B)}{q(A)}\right]\).
Solution: Question 3
The mean field assumption allows us to rewrite the expectation in the ELBO over all \(\psi_i\)’s jointly as iterated expectation:
When we modeled \(\psi\)’s as independent under the variational distribution \(q\), we were able to apply Fubini’s theorem to the integral that defined \(\mathbb{E}_{\psi \sim q(\psi|\lambda)}\) – i.e. we turned a integral over multiple variables into an iterated integral.
Solution: Question 4
Given a model with a posterior over variable \(\psi_1, \ldots, \psi_I\), we’d need to set \(q(\psi_{i}|\lambda_{i})\) equal to \(\mathcal{Z}\exp\left\{\mathbb{E}_{\psi_{-i} \sim q(\psi_{-i}|\lambda_{-i})} \left[\log \left(p(Y_1, \ldots, Y_N, \psi_i | \psi_{-i}) \right)\right]\right\}\) for every \(i\), in order to iteratively maximize the ELBO.
For example, in the Bayesian Gaussian mixture case, we had to derive the update for \(q(z|\phi)\) and \(q(\mu_k|m_k, s_k^2)\) for each component \(k\) in the mixture. Each derivation required an immense amount of algebra. Further more each derivation was different in nature – for \(q(z|\phi)\) we used the fact that \(z\) is a categorical variable to compute the probability of \(z=k\) individually, for \(q(\mu_k|m_k, s_k^2)\) we had to complete the square to make the expression
look like the pdf of a Gaussian.
This is a process that is highly specialized to the specific model we’re given.
One can imagine that if you wanted to implement CAVI for a new model, you’d have to do the entire set of derivations from scratch!
This prevents us from applying variational inference easily to new models and may prevent us from applying variational inference entirely if we are unable to complete these derivations. In order to make variational inference broadly applicable to a large class of Bayesian models we need an automatic and efficient way of
computing the gradient of the ELBO
finding the stationary points of this gradient
12.3. Further Reading#
Variational Inference
(Introductory) Coordinate Ascent Mean-field Variational Inference (Univariate Gaussian Example)
(In-Depth) Variational Inference: A Review for Statisticians
(Introductory) Kullback-Leibler divergence