# Variational Inference¶

Suppose we have data \(x\) that we assume are generated via latent variables \(z\). In the case of a simplified `scvi.model.SCVI`

, the data would
be gene expression counts and the latent variable would be the low-dimensional representation that corresponds to cellular state.

For many interesting models, including all the models in this package, the posterior distribution of \(z\)

is intractable because the denominator, which is defined via integral cannot be computed analytically:

## Approximating the posterior¶

With variational inference 1, we cast the problem of finding the posterior distribution into an optimization problem by minimizing the KL divergence
between an approximate posterior distribution \(q\) that belongs to some *family* of approximate densities
\(Q\) and the true posterior. As an example, if \(Q\) is the family of multivariate Gaussians, then we would like to find the a
mean and covariance matrix that best approximate the true posterior.

Using Bayes Rule, we may rewrite this problem into something we can actually compute.

Manipulation of the last line allows us to rewrite the optimization problem as

where the first term is often called the “reconstruction error” and the second term as the KL divergence or “regularizer”. Presenting the problem this way reveals that the approximate posterior should provide samples that results in high probability of the data (reconstruction) while also being “close” to the prior \(p(z)\) (KL divergence).

## End to end learning¶

So far we have an optimization problem that enables us to learn an approximation of the true posterior. It is often also the case that we have model parameters \(\theta\), which may be unknown, such that we can write the probability of the data as \(p_\theta(x)\). In the case of scVI, \(\theta\) would include the parameters of the decoder neural networks.

In addition to the model parameters \(\theta\), let \(\phi\) be the parameters of the approximate posterior (e.g., mean and covariance matrix if \(Q\) is the family of multivariate Gaussians). Using the derivation above, we can express the log probability of the data as

where the Evidence Lower Bound is the quantity we derived previously:

This quantity is aptly named as it lower bounds the log probability of the data, or the evidence due to the fact that the KL divergence is non-negative by definition:

It turns out that we can maximize the ELBO with respect to both the model parameters \(\theta\) and the variational parameters \(\phi\), which gives us both estimates of \(\theta\) and an approximate posterior on the latent variables. This can also be stated as minimizing our original KL divergence \(\mathrm{KL}(\,q_\phi(z)\, \| \,p_\theta(z \mid x)\,)\), but with a true posterior that is a moving target (as it is a function of \(\theta\)).

## Amortizing inference¶

Let’s think about our high-dimensional single-cell data. If we have a model where we want to learn a posterior for a low-dimensional latent variable \(z\), then there will be an approximate posterior mean and variance parameter associated with each single cell. This scales linearly with the number of cells in our dataset.

To amortize the cost of inference, we can tie together all cells using a neural network. Now suppose that we feed each cell’s gene expression \(x\) through a neural network that outputs the mean and variance
of \(q(z)\) for that cell. Inference will tend to be more scalable as the variational parameters are global (shared by all cells). On the other hand, we may pay a cost called the *amortization gap* 2 for this sharing.

Overall, this technique is typically called *amortized variational inference* and in some cases *auto-encoding variational bayes* 3.

References:

- 1
Blei, David M., Alp Kucukelbir, and Jon D. McAuliffe (2017),

*Variational inference: A review for statisticians.*, Journal of the American statistical Association.- 2
Cremer, Chris, Xuechen Li, and David Duvenaud.

*Inference suboptimality in variational autoencoders.*International Conference on Machine Learning. PMLR, 2018.- 3
Kingma, Diederik P., and Max Welling (2019),

*An introduction to variational autoencoders.*, arXiv preprint arXiv:1906.02691.