Dev.to AI ๐Ÿค– Ai ๐Ÿ‘ 0 ๐Ÿ“– 7 min read

Variational Inference: Turning Posterior Approximation into an Optimization Problem

Knowing how a probabilistic model generates data does not necessarily mean you can efficiently infer the hidden variables behind that data. Even when the prior and likelihood are available, computing the posterior distri

Knowing how a probabilistic model generates data does not necessarily mean you can efficiently infer the hidden variables behind that data. Even when the prior and likelihood are available, computing the posterior distribution can be prohibitively expensive.

Variational inference (VI) addresses this problem by reformulating difficult probabilistic inference as an optimization problem over a restricted family of distributions. Instead of calculating the exact posterior, it searches for a probability distribution that approximates it.

The key idea is to define a manageable family of distributions, parameterize its members, and optimize those parameters to find a suitable approximation.

Why the Exact Posterior Is Difficult to Compute

Consider a photograph of a face with one cheek appearing darker than the other. The observed appearance could result from the orientation of the face, the lighting conditions, or a combination of both.

Different underlying conditions can produce similar observations. Rather than selecting a single explanation, a probabilistic model represents the possible explanations as a distribution.

Let x denote the observed data and z denote the latent variables responsible for generating it.

Bayes' theorem defines the posterior distribution as:

p(zโˆฃx)=p(x)p(xโˆฃz)p(z)โ€‹

Each component has a distinct role:

  • Prior p(z) : Represents the possible latent conditions before observing the photograph.
  • Likelihood p(xโˆฃz) : Describes how likely the observed photograph is under a particular latent configuration.
  • Marginal likelihood p(x) : Represents the probability or probability density of the observation across all possible latent configurations.
  • Posterior p(zโˆฃx) : Describes the updated distribution over latent variables after observing the photograph.

The computational bottleneck is the marginal likelihood in the denominator. For continuous latent variables, it requires evaluating the following integral:

p(x)=โˆซp(xโˆฃz)p(z)dz

This integral accounts for every possible latent configuration that could have contributed to the observation.

For simple probabilistic models, the integral may be evaluated exactly. As the dimensionality of the latent space increases or dependencies between variables become more complex, exact computation becomes difficult.

Variational inference avoids directly computing this posterior by introducing a more manageable approximation.

The Core Idea: Optimize a Distribution

Let q(z) denote a variational distribution used to approximate the true posterior p(zโˆฃx) . When its dependence on the observation needs to be explicit, it can also be written as q(zโˆฃx) .

Suppose we approximate the posterior over face orientation using a Gaussian distribution. Adjusting its mean changes the most probable orientation, while adjusting its variance changes the uncertainty associated with that estimate.

The remaining question is how to measure the quality of the approximation.

Variational inference commonly uses the Kullbackโ€“Leibler divergence (KL divergence):

DKLโ€‹[q(z)โˆฃp(zโˆฃx)]Ezโˆผq(z)โ€‹[logp(zโˆฃx)q(z)โ€‹]

Expanding the logarithm gives:

DKLโ€‹[q(z)โˆฃp(zโˆฃx)]Eqโ€‹[logq(z)]Eqโ€‹[logp(zโˆฃx)]

The divergence measures the expected difference between the log densities of the approximate and true posterior distributions, with the expectation taken under q(z) .

Two properties are particularly important:

  • KL divergence is non-negative and equals zero when the distributions agree almost everywhere.
  • KL divergence is asymmetric. Reversing the order of the distributions can change its value.

Because the expectation is taken under the approximate distribution, the objective emphasizes regions where q(z) assigns substantial probability.

The goal is to find an approximate distribution that minimizes this divergence. This approach is also known as variational Bayes.

However, the true posterior remains difficult to evaluate. Practical variational inference therefore uses an alternative, computable objective that is equivalent to minimizing the posterior KL divergence.

The Variational Family Defines What the Approximation Can Represent

Searching over every possible probability distribution would defeat the purpose of simplifying inference. Instead, variational inference restricts the search to a predefined collection of candidate distributions called the variational family.

For example, the family could contain all univariate Gaussian distributions with different means and positive variances.

Let Q denote the variational family. The optimization problem becomes:

qโˆ—(z)q(z)โˆˆQargminโ€‹DKLโ€‹[q(z)โˆฃp(zโˆฃx)]

The argmin operator identifies the distribution that minimizes the objective. Here, qโˆ—(z) is the best approximation available within the chosen family, according to the KL divergence criterion.

The restriction to Q matters because optimization cannot recover distributional structures that the family cannot represent.

A Single Gaussian vs. a Multimodal Posterior

Return to the face orientation example. Suppose the true posterior has peaks near 0 degrees and 30 degrees.

A single Gaussian has only one peak. No adjustment to its mean or variance can make it exactly represent that bimodal distribution.

This creates a fundamental trade-off:

Variational family Computational efficiency Representational capacity
Simpler family Easier probability calculations and optimization Limited ability to represent complex posteriors
More expressive family Potentially higher computational cost Greater ability to represent complex posterior structures

If the true posterior falls outside the chosen family, approximation error remains even when the optimization reaches its best possible solution.

Conversely, choosing a more expressive family does not guarantee a better practical result if its parameters are difficult to optimize.

Mean-Field Approximation: Simplifying the Joint Distribution

When a model contains multiple latent variables, the posterior must represent not only individual uncertainties but also dependencies between those variables.

For a photograph, the latent variables might include face orientation, lighting, and facial expression. Representing and computing their joint distribution becomes increasingly difficult as the number of variables grows.

The mean-field approximation simplifies this structure by assuming independence between latent variables within the approximate distribution.

For m latent variables, the factorized distribution is:

q(z)=j=1โˆmโ€‹qjโ€‹(zjโ€‹)

For three latent variables, this becomes:

q(z1โ€‹,z2โ€‹,z3โ€‹)q1โ€‹(z1โ€‹)q2โ€‹(z2โ€‹)q3โ€‹(z3โ€‹)

If z1โ€‹ , z2โ€‹ , and z3โ€‹ represent face orientation, lighting, and facial expression, respectively, each factor represents the approximate distribution of its corresponding variable.

Expressing a joint distribution as a product of individual factors is called factorization.

Choosing a Distribution for Each Factor

The factors do not need to use the same distribution type. Each factor can be chosen according to the nature of its latent variable.

Latent variable type Distribution Parameterization
Continuous Gaussian Mean and positive variance
Binary Bernoulli Probability, which can be computed using a sigmoid function
Categorical Categorical Category probabilities, which can be computed using a softmax function

For example, face orientation can be represented as a continuous variable, whether a light is on or off as a binary variable, and facial expression as a categorical variable.

Softmax ensures that the probabilities assigned to the available categories sum to one.

What the Independence Assumption Actually Means

Mean-field approximation simplifies computation, but it cannot directly represent dependencies between independently factorized latent variables.

If a particular face orientation and lighting condition are strongly associated in the true posterior, the factorized approximation may fail to capture that relationship.

Independence is a constraint imposed on the approximate distribution, not a claim about the true posterior or the underlying latent variables.

The factors also share an overall optimization objective. Although the approximate distribution is factorized, the optimization of individual factors can still influence one another.

Parameterization: From Distribution Search to Parameter Optimization

Rather than adjusting a probability density independently at every possible point, we can represent a distribution using a set of adjustable parameters.

This process is called parameterization. The parameters controlling the approximate distribution are known as variational parameters, commonly denoted by ฯ• .

A parameterized variational distribution can be written as:

q(z;ฯ•)=qฯ•โ€‹(z)

For a univariate Gaussian approximation:

qฯ•โ€‹(z)=N(z;ฮผ,ฯƒ2)

The variational parameters are:

ฯ•=(ฮผ,ฯƒ2)

The mean ฮผ controls the center of the distribution, while the positive variance ฯƒ2 controls its spread.

With this parameterization, the original search over probability distributions becomes an optimization problem over their parameters:

ฯ•โˆ—ฯ•argminโ€‹DKLโ€‹[qฯ•โ€‹(z)โˆฃp(zโˆฃx)]

Instead of constructing an arbitrary probability distribution, the optimization procedure adjusts the parameters of a predefined distribution.

What Variational Inference Looks Like in Practice

From an implementation perspective, variational inference involves three related design decisions:

  • Factorization: Determines how the approximate joint distribution is structured.
  • Parameterization: Determines which adjustable values control the distribution.
  • Optimization: Determines how those values are updated to improve the approximation.

Once the distribution structure and its parameterization have been selected, the optimization process follows a general iterative flow:

Define the variational family
|
Initialize variational parameters
|
Construct the approximate distribution
|
Evaluate a computable objective
|
Update variational parameters
|
Repeat objective evaluation and parameter updates
|
Return the resulting approximate posterior

This formulation makes it possible to apply numerical optimization techniques to probabilistic inference.

Because the true posterior is difficult to evaluate directly, variational inference maximizes the Evidence Lower Bound (ELBO) rather than directly minimizing the posterior KL divergence. For fixed observed data and a fixed probabilistic model, these two optimization objectives are equivalent.

How This Connects to Variational Autoencoders

A Variational Autoencoder (VAE) combines variational inference with neural networks.

Its encoder computes the parameters of an approximate posterior conditioned on the input data. The neural network is trained using an ELBO-based objective, allowing it to approximate the distribution over latent variables without directly computing the exact posterior.

Takeaway

Variational inference makes difficult posterior inference more manageable by reformulating it as an optimization problem over a restricted family of distributions.

Even successful optimization cannot recover posterior structures that the chosen family cannot represent. The central design challenge is balancing computational efficiency against the expressive capacity needed for an accurate approximation.

Originally published at zeromathai.com.

Original article: https://zeromathai.com/en/variational-inference-approximate-posterior-course-en/

๐Ÿ“ฐ Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.