Variational Bayesian methods explained

Variational Bayesian methods are a family of techniques for approximating intractable integrals arising in Bayesian inference and machine learning. They are typically used in complex statistical models consisting of observed variables (usually termed "data") as well as unknown parameters and latent variables, with various sorts of relationships among the three types of random variables, as might be described by a graphical model. As typical in Bayesian inference, the parameters and latent variables are grouped together as "unobserved variables". Variational Bayesian methods are primarily used for two purposes:

  1. To provide an analytical approximation to the posterior probability of the unobserved variables, in order to do statistical inference over these variables.
  2. To derive a lower bound for the marginal likelihood (sometimes called the evidence) of the observed data (i.e. the marginal probability of the data given the model, with marginalization performed over unobserved variables). This is typically used for performing model selection, the general idea being that a higher marginal likelihood for a given model indicates a better fit of the data by that model and hence a greater probability that the model in question was the one that generated the data. (See also the Bayes factor article.)

In the former purpose (that of approximating a posterior probability), variational Bayes is an alternative to Monte Carlo sampling methods—particularly, Markov chain Monte Carlo methods such as Gibbs sampling—for taking a fully Bayesian approach to statistical inference over complex distributions that are difficult to evaluate directly or sample. In particular, whereas Monte Carlo techniques provide a numerical approximation to the exact posterior using a set of samples, variational Bayes provides a locally-optimal, exact analytical solution to an approximation of the posterior.

Variational Bayes can be seen as an extension of the expectation–maximization (EM) algorithm from maximum likelihood (ML) or maximum a posteriori (MAP) estimation of the single most probable value of each parameter to fully Bayesian estimation which computes (an approximation to) the entire posterior distribution of the parameters and latent variables. As in EM, it finds a set of optimal parameter values, and it has the same alternating structure as does EM, based on a set of interlocked (mutually dependent) equations that cannot be solved analytically.

For many applications, variational Bayes produces solutions of comparable accuracy to Gibbs sampling at greater speed. However, deriving the set of equations used to update the parameters iteratively often requires a large amount of work compared with deriving the comparable Gibbs sampling equations. This is the case even for many models that are conceptually quite simple, as is demonstrated below in the case of a basic non-hierarchical model with only two parameters and no latent variables.

Mathematical derivation

Problem

In variational inference, the posterior distribution over a set of unobserved variables

Z=\{Z1...Zn\}

given some data

X

is approximated by a so-called variational distribution,

Q(Z):

P(Z\midX)Q(Z).

The distribution

Q(Z)

is restricted to belong to a family of distributions of simpler form than

P(Z\midX)

(e.g. a family of Gaussian distributions), selected with the intention of making

Q(Z)

similar to the true posterior,

P(Z\midX)

.

The similarity (or dissimilarity) is measured in terms of a dissimilarity function

d(Q;P)

and hence inference is performed by selecting the distribution

Q(Z)

that minimizes

d(Q;P)

.

KL divergence

The most common type of variational Bayes uses the Kullback–Leibler divergence (KL-divergence) of Q from P as the choice of dissimilarity function. This choice makes this minimization tractable. The KL-divergence is defined as

DKL(Q\parallelP)\triangleq\sumZQ(Z)log

Q(Z)
P(Z\midX)

.

Note that Q and P are reversed from what one might expect. This use of reversed KL-divergence is conceptually similar to the expectation–maximization algorithm. (Using the KL-divergence in the other way produces the expectation propagation algorithm.)

Intractability

Variational techniques are typically used to form an approximation for:

P(Z\midX)=

P(X\midZ)P(Z)
P(X)

=

P(X\midZ)P(Z)
\intZP(X,Z')dZ'

The marginalization over

Z

to calculate

P(X)

in the denominator is typically intractable, because, for example, the search space of

Z

is combinatorially large. Therefore, we seek an approximation, using

Q(Z)P(Z\midX)

.

Evidence lower bound

See main article: Evidence lower bound.

Given that

P(Z\midX)=

P(X,Z)
P(X)
, the KL-divergence above can also be written as

DKL(Q\parallelP)=\sumZQ(Z)\left[log

Q(Z)
P(Z,X)

+logP(X)\right] =\sumZQ(Z)\left[logQ(Z)-logP(Z,X)\right]+\sumZQ(Z)\left[logP(X)\right]

Because

P(X)

is a constant with respect to

Z

and

\sumZQ(Z)=1

because

Q(Z)

is a distribution, we have

DKL(Q\parallelP)=\sumZQ(Z)\left[logQ(Z)-logP(Z,X)\right]+logP(X)

which, according to the definition of expected value (for a discrete random variable), can be written as follows

DKL(Q\parallelP)=EQ\left[logQ(Z)-logP(Z,X)\right]+logP(X)

which can be rearranged to become

logP(X)= DKL(Q\parallelP)-EQ\left[logQ(Z)-logP(Z,X)\right]=DKL(Q\parallelP)+l{L}(Q)

As the log-evidence

logP(X)

is fixed with respect to

Q

, maximizing the final term

l{L}(Q)

minimizes the KL divergence of

Q

from

P

. By appropriate choice of

Q

,

l{L}(Q)

becomes tractable to compute and to maximize. Hence we have both an analytical approximation

Q

for the posterior

P(Z\midX)

, and a lower bound

l{L}(Q)

for the log-evidence

logP(X)

(since the KL-divergence is non-negative).

The lower bound

l{L}(Q)

is known as the (negative) variational free energy in analogy with thermodynamic free energy because it can also be expressed as a negative energy

\operatorname{E}Q[logP(Z,X)]

plus the entropy of

Q

. The term

l{L}(Q)

is also known as Evidence Lower Bound, abbreviated as ELBO, to emphasize that it is a lower (worst-case) bound on the log-evidence of the data.

Proofs

By the generalized Pythagorean theorem of Bregman divergence, of which KL-divergence is a special case, it can be shown that:[1]

DKL(Q\parallelP)\geqDKL(Q\parallelQ*)+DKL(Q*\parallelP),\forallQ*\inl{C}

where

l{C}

is a convex set and the equality holds if:

Q=Q*\triangleq\argminQ\inl{C

}D_(Q\parallel P).

In this case, the global minimizer

Q*(Z)=q*(Z1\midZ

*
2)q

(Z2)=q*(Z2\midZ

*
1)q

(Z1),

with
Z=\{Z1,Z2\},
can be found as follows:[1]

q*(Z2)=

P(X)
\zeta(X)
P(Z2\midX)
\exp(DKL(q*(Z1\midZ2)\parallelP(Z1\midZ2,X)))

=

1
\zeta(X)
\expE\left(log
q*(Z1\midZ2)
P(Z,X)
q*(Z1\midZ2)

\right),

in which the normalizing constant is:

\zeta(X)

=P(X)\int
Z2
P(Z2\midX)
\exp(DKL(q*(Z1\midZ2)\parallelP(Z1\midZ2,X)))

=

\int
Z2
\expE\left(log
q*(Z1\midZ2)
P(Z,X)
q*(Z1\midZ2)

\right).

The term

\zeta(X)

is often called the evidence lower bound (ELBO) in practice, since

P(X)\geq\zeta(X)=\exp(l{L}(Q*))

,[1] as shown above.

By interchanging the roles of

Z1

and

Z2,

we can iteratively compute the approximated

q*(Z1)

and

q*(Z2)

of the true model's marginals

P(Z1\midX)

and

P(Z2\midX),

respectively. Although this iterative scheme is guaranteed to converge monotonically,[1] the converged

Q*

is only a local minimizer of

DKL(Q\parallelP)

.

If the constrained space

l{C}

is confined within independent space, i.e.

q*(Z1\midZ2)=q*

(Z1),
the above iterative scheme will become the so-called mean field approximation

Q*(Z)=q*

*
(Z
1)q

(Z2),

as shown below.

Mean field approximation

The variational distribution

Q(Z)

is usually assumed to factorize over some partition of the latent variables, i.e. for some partition of the latent variables

Z

into

Z1...ZM

,

Q(Z)=

M
\prod
i=1

qi(Zi\midX)

It can be shown using the calculus of variations (hence the name "variational Bayes") that the "best" distribution

*
q
j
for each of the factors

qj

(in terms of the distribution minimizing the KL divergence, as described above) satisfies:[2]
*
q
j

(Zj\midX)=

\operatorname{E
e[lnp(Z,X)]
*
q
-j
}

where

\operatorname{E}
*
q
-j

[lnp(Z,X)]

is the expectation of the logarithm of the joint probability of the data and latent variables, taken with respect to

q*

over all variables not in the partition: refer to Lemma 4.1 of[3] for a derivation of the distribution
*
q
j

(Zj\midX)

.

In practice, we usually work in terms of logarithms, i.e.:

ln

*
q
j

(Zj\midX)=

\operatorname{E}
*
q
-j

[lnp(Z,X)]+constant

The constant in the above expression is related to the normalizing constant (the denominator in the expression above for

*
q
j
) and is usually reinstated by inspection, as the rest of the expression can usually be recognized as being a known type of distribution (e.g. Gaussian, gamma, etc.).

Using the properties of expectations, the expression

\operatorname{E}
*
q
-j

[lnp(Z,X)]

can usually be simplified into a function of the fixed hyperparameters of the prior distributions over the latent variables and of expectations (and sometimes higher moments such as the variance) of latent variables not in the current partition (i.e. latent variables not included in

Zj

). This creates circular dependencies between the parameters of the distributions over variables in one partition and the expectations of variables in the other partitions. This naturally suggests an iterative algorithm, much like EM (the expectation–maximization algorithm), in which the expectations (and possibly higher moments) of the latent variables are initialized in some fashion (perhaps randomly), and then the parameters of each distribution are computed in turn using the current values of the expectations, after which the expectation of the newly computed distribution is set appropriately according to the computed parameters. An algorithm of this sort is guaranteed to converge.[4]

In other words, for each of the partitions of variables, by simplifying the expression for the distribution over the partition's variables and examining the distribution's functional dependency on the variables in question, the family of the distribution can usually be determined (which in turn determines the value of the constant). The formula for the distribution's parameters will be expressed in terms of the prior distributions' hyperparameters (which are known constants), but also in terms of expectations of functions of variables in other partitions. Usually these expectations can be simplified into functions of expectations of the variables themselves (i.e. the means); sometimes expectations of squared variables (which can be related to the variance of the variables), or expectations of higher powers (i.e. higher moments) also appear. In most cases, the other variables' distributions will be from known families, and the formulas for the relevant expectations can be looked up. However, those formulas depend on those distributions' parameters, which depend in turn on the expectations about other variables. The result is that the formulas for the parameters of each variable's distributions can be expressed as a series of equations with mutual, nonlinear dependencies among the variables. Usually, it is not possible to solve this system of equations directly. However, as described above, the dependencies suggest a simple iterative algorithm, which in most cases is guaranteed to converge. An example will make this process clearer.

A duality formula for variational inference

The following theorem is referred to as a duality formula for variational inference.[3] It explains some important properties of the variational distributions used in variational Bayes methods.

(\Theta,l{F},P)

and

(\Theta,l{F},Q)

with

Q\llP

. Assume that there is a common dominating probability measure

λ

such that

P\llλ

and

Q\llλ

. Let

h

denote any real-valued random variable on

(\Theta,l{F},P)

that satisfies

h\inL1(P)

. Then the following equality holds

logEP[\exph]=supQ\{EQ[h]-DKL(Q\parallelP)\}.

Further, the supremum on the right-hand side is attained if and only if it holds

q(\theta)
p(\theta)

=

\exph(\theta)
EP[\exph]

,

almost surely with respect to probability measure

Q

, where

p(\theta)=dP/dλ

and

q(\theta)=dQ/dλ

denote the Radon–Nikodym derivatives of the probability measures

P

and

Q

with respect to

λ

, respectively.

A basic example

Consider a simple non-hierarchical Bayesian model consisting of a set of i.i.d. observations from a Gaussian distribution, with unknown mean and variance.[5] In the following, we work through this model in great detail to illustrate the workings of the variational Bayes method.

For mathematical convenience, in the following example we work in terms of the precision — i.e. the reciprocal of the variance (or in a multivariate Gaussian, the inverse of the covariance matrix) — rather than the variance itself. (From a theoretical standpoint, precision and variance are equivalent since there is a one-to-one correspondence between the two.)

The mathematical model

We place conjugate prior distributions on the unknown mean

\mu

and precision

\tau

, i.e. the mean also follows a Gaussian distribution while the precision follows a gamma distribution. In other words:

\begin{align} \tau&\sim\operatorname{Gamma}(a0,b0)\\ \mu|\tau&\siml{N}(\mu0,(λ0\tau)-1)\\ \{x1,...,xN\}&\siml{N}(\mu,\tau-1)\\ N&=numberofdatapoints \end{align}

The hyperparameters

\mu0,λ0,a0

and

b0

in the prior distributions are fixed, given values. They can be set to small positive numbers to give broad prior distributions indicating ignorance about the prior distributions of

\mu

and

\tau

.

We are given

N

data points

X=\{x1,\ldots,xN\}

and our goal is to infer the posterior distribution

q(\mu,\tau)=p(\mu,\tau\midx1,\ldots,xN)

of the parameters

\mu

and

\tau.

The joint probability

The joint probability of all variables can be rewritten as

p(X,\mu,\tau)=p(X\mid\mu,\tau)p(\mu\mid\tau)p(\tau)

where the individual factors are

\begin{align} p(X\mid\mu,\tau)&=

N
\prod
n=1

l{N}(xn\mid\mu,\tau-1)\\ p(\mu\mid\tau)&=l{N}\left(\mu\mid\mu0,(λ0\tau)-1\right)\\ p(\tau)&=\operatorname{Gamma}(\tau\mida0,b0) \end{align}

where

\begin{align} l{N}(x\mid\mu,\sigma2)&=

1
\sqrt{2\pi\sigma2
} e^ \\\operatorname(\tau\mid a,b) & = \frac b^a \tau^ e^\end

Factorized approximation

Assume that

q(\mu,\tau)=q(\mu)q(\tau)

, i.e. that the posterior distribution factorizes into independent factors for

\mu

and

\tau

. This type of assumption underlies the variational Bayesian method. The true posterior distribution does not in fact factor this way (in fact, in this simple case, it is known to be a Gaussian-gamma distribution), and hence the result we obtain will be an approximation.

Derivation of

Then

\begin{align} ln

*(\mu)
q
\mu

&=\operatorname{E}\tau\left[lnp(X\mid\mu,\tau)+lnp(\mu\mid\tau)+lnp(\tau)\right]+C\\ &=\operatorname{E}\tau\left[lnp(X\mid\mu,\tau)\right]+\operatorname{E}\tau\left[lnp(\mu\mid\tau)\right]+\operatorname{E}\tau\left[lnp(\tau)\right]+C\\ &=\operatorname{E}\tau\left[ln

N
\prod
n=1

l{N}\left(xn\mid\mu,\tau-1\right)\right]+\operatorname{E}\tau\left[lnl{N}\left(\mu\mid\mu0,(λ0\tau)-1\right)\right]+C2\\ &=\operatorname{E}\tau\left[ln

N
\prod\sqrt{
n=1
\tau
2\pi
} e^\right] + \operatorname_\left[\ln \sqrt{\frac{\lambda_0 \tau}{2\pi}} e^{-\frac{(\mu-\mu_0)^2\lambda_0 \tau}{2}}\right] + C_2 \\ &= \operatorname_\left[\sum_{n=1}^N \left(\frac{1}{2}(\ln\tau - \ln 2\pi) - \frac{(x_n-\mu)^2\tau}{2}\right)\right] + \operatorname_\left[\frac{1}{2}(\ln \lambda_0 + \ln \tau - \ln 2\pi) - \frac{(\mu-\mu_0)^2\lambda_0 \tau}{2}\right] + C_2 \\ &= \operatorname_\left[\sum_{n=1}^N -\frac{(x_n-\mu)^2\tau}{2}\right] + \operatorname_\left[-\frac{(\mu-\mu_0)^2\lambda_0 \tau}{2}\right] + \operatorname_\left[\sum_{n=1}^N \frac{1}{2}(\ln\tau - \ln 2\pi)\right] + \operatorname_\left[\frac{1}{2}(\ln \lambda_0 + \ln \tau - \ln 2\pi)\right] + C_2 \\ &= \operatorname_\left[\sum_{n=1}^N -\frac{(x_n-\mu)^2\tau}{2}\right] + \operatorname_\left[-\frac{(\mu-\mu_0)^2\lambda_0 \tau}{2}\right] + C_3 \\ &= - \frac \left\ + C_3\end

In the above derivation,

C

,

C2

and

C3

refer to values that are constant with respect to

\mu

. Note that the term

\operatorname{E}\tau[lnp(\tau)]

is not a function of

\mu

and will have the same value regardless of the value of

\mu

. Hence in line 3 we can absorb it into the constant term at the end. We do the same thing in line 7.

The last line is simply a quadratic polynomial in

\mu

. Since this is the logarithm of
*(\mu)
q
\mu
, we can see that
*(\mu)
q
\mu
itself is a Gaussian distribution.

With a certain amount of tedious math (expanding the squares inside of the braces, separating out and grouping the terms involving

\mu

and

\mu2

and completing the square over

\mu

), we can derive the parameters of the Gaussian distribution:

\begin{align} ln

*(\mu)
q
\mu

&=-

\operatorname{E
\tau

[\tau]}{2}\left\{

N
\sum
n=1
2
(x
n-\mu)

+λ0(\mu-\mu

2
0)

\right\}+C3\\ &=-

\operatorname{E
\tau

[\tau]}{2}\left\{

N
\sum
n=1
2-2x
(x
n\mu

+\mu2)+

2-2\mu
λ
0\mu

+

2)
\mu
0

\right\}+C3\\ &=-

\operatorname{E
\tau

[\tau]}{2}\left\{

N
\left(\sum
n=1
N
x
n=1

xn\right)\mu+\left(

N
\sum
n=1

\mu2\right)+

2-2λ
λ
0\mu

0\mu+λ0\mu

2
0

\right\}+C3\\ &=-

\operatorname{E
\tau

[\tau]}{2}\left\{

2
(λ
0+N)\mu

-2\left(λ0\mu0+

N
\sum
n=1

xn\right)\mu+

N
\left(\sum
n=1
2\right)
x
n

+λ0\mu

2
0

\right\}+C3\\ &=-

\operatorname{E
\tau

[\tau]}{2}\left\{

2
(λ
0+N)\mu

-2\left(λ0\mu0+

N
\sum
n=1

xn\right)\mu\right\}+C4\\ &=-

\operatorname{E
\tau

[\tau]}{2}\left\{

2
(λ-2\left(
0+N)\mu
λ0+
N
\sum
n=1
xn
0\mu
λ0+N

\right)(λ0+N)\mu\right\}+C4\\ &=-

\operatorname{E
\tau

[\tau]}{2}\left\{

2
(λ-2\left(
0+N)\left(\mu
λ0+
N
\sum
n=1
xn
0\mu
λ0+N

\right)\mu\right)\right\}+C4\\ &=-

\operatorname{E
\tau

[\tau]}{2}\left\{

2
(λ-2\left(
0+N)\left(\mu
λ0+
N
\sum
n=1
xn
0\mu
λ0+N

\right)\mu+\left(

λ0+
N
\sum
n=1
xn
0\mu
λ0+N

\right)2-\left(

λ0+
N
\sum
n=1
xn
0\mu
λ0+N

\right)2\right)\right\}+C4\\ &=-

\operatorname{E
\tau

[\tau]}{2}\left\{

2
(λ-2\left(
0+N)\left(\mu
λ0+
N
\sum
n=1
xn
0\mu
λ0+N

\right)\mu+\left(

λ0+
N
\sum
n=1
xn
0\mu
λ0+N

\right)2\right)\right\}+C5\\ &=-

\operatorname{E
\tau

[\tau]}{2}\left\{

(λ
0+N)\left(\mu-
λ0+
N
\sum
n=1
xn
0\mu
λ0+N

\right)2\right\}+C5\\ &=-

1
2

(λ0+N)\operatorname{E}\tau[\tau]\left(\mu-

λ0+
N
\sum
n=1
xn
0\mu
λ0+N

\right)2+C5 \end{align}

Note that all of the above steps can be shortened by using the formula for the sum of two quadratics.

In other words:

*(\mu)
\begin{align} q
\mu

&\siml{N}(\mu\mid\muN,λ

-1
N

)\\ \muN&=

λ0\mu0+N\bar{x
} \\\lambda_N &= (\lambda_0 + N) \operatorname_[\tau] \\\bar &= \frac\sum_^N x_n\end

Derivation of

The derivation of

*(\tau)
q
\tau
is similar to above, although we omit some of the details for the sake of brevity.

\begin{align} ln

*(\tau)
q
\tau

&=\operatorname{E}\mu[lnp(X\mid\mu,\tau)+lnp(\mu\mid\tau)]+lnp(\tau)+constant\\ &=(a0-1)ln\tau-b0\tau+

1
2

ln\tau+

N
2

ln\tau-

\tau
2

\operatorname{E}\mu\left[

N
\sum
n=1
2
(x
n-\mu)

+λ0(\mu-

2
\mu
0)

\right]+constant \end{align}

Exponentiating both sides, we can see that

*(\tau)
q
\tau
is a gamma distribution. Specifically:
*(\tau)
\begin{align} q
\tau

&\sim\operatorname{Gamma}(\tau\midaN,bN)\\ aN&=a0+

N+1
2

\\ bN&=b0+

1
2

\operatorname{E}\mu

N
\left[\sum
n=1
2
(x
n-\mu)

+λ0(\mu-

2\right] \end{align}
\mu
0)

Algorithm for computing the parameters

Let us recap the conclusions from the previous sections:

*(\mu)
\begin{align} q
\mu

&\siml{N}(\mu\mid\muN,λ

-1
N

)\\ \muN&=

λ0\mu0+N\bar{x
} \\\lambda_N &= (\lambda_0 + N) \operatorname_[\tau] \\\bar &= \frac\sum_^N x_n\end

and

*(\tau)
\begin{align} q
\tau

&\sim\operatorname{Gamma}(\tau\midaN,bN)\\ aN&=a0+

N+1
2

\\ bN&=b0+

1
2

\operatorname{E}\mu

N
\left[\sum
n=1
2
(x
n-\mu)

+λ0(\mu-

2\right] \end{align}
\mu
0)

In each case, the parameters for the distribution over one of the variables depend on expectations taken with respect to the other variable. We can expand the expectations, using the standard formulas for the expectations of moments of the Gaussian and gamma distributions:

\begin{align} \operatorname{E}[\tau\midaN,bN]&=

aN
bN

\\ \operatorname{E}\left[\mu\mid\muN,λ

-1
N

\right]&=\muN\\ \operatorname{E}\left[X2\right]&=\operatorname{Var}(X)+(\operatorname{E}[X])2\\ \operatorname{E}\left

2\mid\mu
[\mu
N,λ
-1
N

\right]&=

-1
λ
N

+

2 \end{align}
\mu
N

Applying these formulas to the above equations is trivial in most cases, but the equation for

bN

takes more work:

\begin{align} bN&=b0+

1
2

\operatorname{E}\mu

N
\left[\sum
n=1
2
(x
n-\mu)

+λ0(\mu-

2\right]
\mu
0)

\\ &=b0+

1
2

\operatorname{E}\mu\left[

2
(λ
0+N)\mu

-2\left(λ0\mu0+

N
\sum
n=1

xn\right)\mu+

N
\left(\sum
n=1
2
x
n

\right)+λ0\mu

2
0

\right]\\ &=b0+

1
2

\left[(λ0+N)\operatorname{E}

2]
\mu[\mu

-2\left(λ0\mu0+

N
\sum
n=1

xn\right)\operatorname{E}\mu[\mu]+\left

N
(\sum
n=1
2
x
n

\right)+λ0\mu

2
0

\right]\\ &=b0+

1
2

\left[(λ0+N)\left

-1
(λ
N

+

2
\mu
N

\right)-2\left(λ0\mu0+

N
\sum
n=1

xn\right)\muN+

N
\left(\sum
n=1
2
x
n

\right)+λ0\mu

2
0

\right]\\ \end{align}

We can then write the parameter equations as follows, without any expectations:

\begin{align} \muN&=

λ0\mu0+N\bar{x
} \\\lambda_N &= (\lambda_0 + N) \frac \\\bar &= \frac\sum_^N x_n \\a_N &= a_0 + \frac \\b_N &= b_0 + \frac \left[(\lambda_0+N) \left (\lambda_N^{-1} + \mu_N^2 \right) -2 \left (\lambda_0\mu_0 + \sum_{n=1}^N x_n \right)\mu_N + \left (\sum_{n=1}^N x_n^2 \right) + \lambda_0\mu_0^2 \right]\end

Note that there are circular dependencies among the formulas for

λN

and

bN

. This naturally suggests an EM-like algorithm:
  1. Compute
N
\sum
n=1

xn

and
N
\sum
n=1
2.
x
n
Use these values to compute

\muN

and

aN.

  1. Initialize

λN

to some arbitrary value.
  1. Use the current value of

λN,

along with the known values of the other parameters, to compute

bN

.
  1. Use the current value of

bN,

along with the known values of the other parameters, to compute

λN

.
  1. Repeat the last two steps until convergence (i.e. until neither value has changed more than some small amount).

We then have values for the hyperparameters of the approximating distributions of the posterior parameters, which we can use to compute any properties we want of the posterior — e.g. its mean and variance, a 95% highest-density region (the smallest interval that includes 95% of the total probability), etc.

It can be shown that this algorithm is guaranteed to converge to a local maximum.

Note also that the posterior distributions have the same form as the corresponding prior distributions. We did not assume this; the only assumption we made was that the distributions factorize, and the form of the distributions followed naturally. It turns out (see below) that the fact that the posterior distributions have the same form as the prior distributions is not a coincidence, but a general result whenever the prior distributions are members of the exponential family, which is the case for most of the standard distributions.

Further discussion

Step-by-step recipe

The above example shows the method by which the variational-Bayesian approximation to a posterior probability density in a given Bayesian network is derived:

  1. Describe the network with a graphical model, identifying the observed variables (data)

X

and unobserved variables (parameters

\boldsymbol\Theta

and latent variables

Z

) and their conditional probability distributions. Variational Bayes will then construct an approximation to the posterior probability

p(Z,\boldsymbol\Theta\midX)

. The approximation has the basic property that it is a factorized distribution, i.e. a product of two or more independent distributions over disjoint subsets of the unobserved variables.
  1. Partition the unobserved variables into two or more subsets, over which the independent factors will be derived. There is no universal procedure for doing this; creating too many subsets yields a poor approximation, while creating too few makes the entire variational Bayes procedure intractable. Typically, the first split is to separate the parameters and latent variables; often, this is enough by itself to produce a tractable result. Assume that the partitions are called

Z1,\ldots,ZM

.
  1. For a given partition

Zj

, write down the formula for the best approximating distribution
*
q
j

(Zj\midX)

using the basic equation

ln

*
q
j

(Zj\midX)=\operatorname{E}i[lnp(Z,X)]+constant

.
  1. Fill in the formula for the joint probability distribution using the graphical model. Any component conditional distributions that don't involve any of the variables in

Zj

can be ignored; they will be folded into the constant term.
  1. Simplify the formula and apply the expectation operator, following the above example. Ideally, this should simplify into expectations of basic functions of variables not in

Zj

(e.g. first or second raw moments, expectation of a logarithm, etc.). In order for the variational Bayes procedure to work well, these expectations should generally be expressible analytically as functions of the parameters and/or hyperparameters of the distributions of these variables. In all cases, these expectation terms are constants with respect to the variables in the current partition.
  1. The functional form of the formula with respect to the variables in the current partition indicates the type of distribution. In particular, exponentiating the formula generates the probability density function (PDF) of the distribution (or at least, something proportional to it, with unknown normalization constant). In order for the overall method to be tractable, it should be possible to recognize the functional form as belonging to a known distribution. Significant mathematical manipulation may be required to convert the formula into a form that matches the PDF of a known distribution. When this can be done, the normalization constant can be reinstated by definition, and equations for the parameters of the known distribution can be derived by extracting the appropriate parts of the formula.
  2. When all expectations can be replaced analytically with functions of variables not in the current partition, and the PDF put into a form that allows identification with a known distribution, the result is a set of equations expressing the values of the optimum parameters as functions of the parameters of variables in other partitions.
  3. When this procedure can be applied to all partitions, the result is a set of mutually linked equations specifying the optimum values of all parameters.
  4. An expectation–maximization (EM) type procedure is then applied, picking an initial value for each parameter and the iterating through a series of steps, where at each step we cycle through the equations, updating each parameter in turn. This is guaranteed to converge.

Most important points

Due to all of the mathematical manipulations involved, it is easy to lose track of the big picture. The important things are:

  1. The idea of variational Bayes is to construct an analytical approximation to the posterior probability of the set of unobserved variables (parameters and latent variables), given the data. This means that the form of the solution is similar to other Bayesian inference methods, such as Gibbs sampling — i.e. a distribution that seeks to describe everything that is known about the variables. As in other Bayesian methods — but unlike e.g. in expectation–maximization (EM) or other maximum likelihood methods — both types of unobserved variables (i.e. parameters and latent variables) are treated the same, i.e. as random variables. Estimates for the variables can then be derived in the standard Bayesian ways, e.g. calculating the mean of the distribution to get a single point estimate or deriving a credible interval, highest density region, etc.
  2. "Analytical approximation" means that a formula can be written down for the posterior distribution. The formula generally consists of a product of well-known probability distributions, each of which factorizes over a set of unobserved variables (i.e. it is conditionally independent of the other variables, given the observed data). This formula is not the true posterior distribution, but an approximation to it; in particular, it will generally agree fairly closely in the lowest moments of the unobserved variables, e.g. the mean and variance.
  3. The result of all of the mathematical manipulations is (1) the identity of the probability distributions making up the factors, and (2) mutually dependent formulas for the parameters of these distributions. The actual values of these parameters are computed numerically, through an alternating iterative procedure much like EM.

Compared with expectation–maximization (EM)

Variational Bayes (VB) is often compared with expectation–maximization (EM). The actual numerical procedure is quite similar, in that both are alternating iterative procedures that successively converge on optimum parameter values. The initial steps to derive the respective procedures are also vaguely similar, both starting out with formulas for probability densities and both involving significant amounts of mathematical manipulations.

However, there are a number of differences. Most important is what is being computed.

A more complex example

Imagine a Bayesian Gaussian mixture model described as follows:[2]

\begin{align} \pi&\sim\operatorname{SymDir}(K,\alpha0)\\ Λi=1&\siml{W}(W0,\nu0)\\ \mui=1&\siml{N}(\mu0,(\beta0

-1
Λ
i)

)\\ z[i=1...N]&\sim\operatorname{Mult}(1,\pi)\\ xi=1&\sim

l{N}(\mu
zi

,

{Λ
zi
}^) \\K &= \text \\N &= \text\end

Note:

K

, with the hyperparameter for each component set to

\alpha0

. The Dirichlet distribution is the conjugate prior of the categorical distribution or multinomial distribution.

l{W}

is the Wishart distribution, which is the conjugate prior of the precision matrix (inverse covariance matrix) for a multivariate Gaussian distribution.

K

-dimensional vector in which one of the elements is 1 (specifying the identity of the observation) and all other elements are 0.

l{N}

is the Gaussian distribution, in this case specifically the multivariate Gaussian distribution.

The interpretation of the above variables is as follows:

X=\{x1,...,xN\}

is the set of

N

data points, each of which is a

D

-dimensional vector distributed according to a multivariate Gaussian distribution.

Z=\{z1,...,zN\}

is a set of latent variables, one per data point, specifying which mixture component the corresponding data point belongs to, using a "one-of-K" vector representation with components

znk

for

k=1...K

, as described above.

\pi

is the mixing proportions for the

K

mixture components.

\mui=1

and

Λi=1

specify the parameters (mean and precision) associated with each mixture component.

The joint probability of all variables can be rewritten as

p(X,Z,\pi,\mu,Λ)=p(X\midZ,\mu,Λ)p(Z\mid\pi)p(\pi)p(\mu\midΛ)p(Λ)

where the individual factors are

\begin{align} p(X\midZ,\mu,Λ)&=

N
\prod
n=1
K
\prod
k=1

l{N}(xn\mid\muk,Λ

-1
k
znk
)

\\ p(Z\mid\pi)&=

N
\prod
n=1
K
\prod
k=1
znk
\pi
k

\\ p(\pi)&=

\Gamma(K\alpha0)
K
\Gamma(\alpha
0)
K
\prod
k=1
\alpha0-1
\pi
k

\\ p(\mu\midΛ)&=

K
\prod
k=1

l{N}(\muk\mid\mu0,(\beta0

-1
Λ
k)

)\\ p(Λ)&=

K
\prod
k=1

l{W}(Λk\midW0,\nu0) \end{align}

where

\begin{align} l{N}(x\mid\mu,\Sigma)&=

1
(2\pi)D/2
1
|\Sigma|1/2

\exp\left\{-

1
2

(x-\mu)\rm\Sigma-1(x-\mu)\right\}\\ l{W}(Λ\midW,\nu)&=B(W,\nu)|Λ|(\nu-D-1)/2\exp\left(-

1
2

\operatorname{Tr}(W-1Λ)\right)\\ B(W,\nu)&=|W|-\nu/2\left\{2\nu\piD(D-1)/4

D
\prod\Gamma\left(
i=1
\nu+1-i
2

\right)\right\}-1\\ D&=dimensionalityofeachdatapoint \end{align}

Assume that

q(Z,\pi,\mu,Λ)=q(Z)q(\pi,\mu,Λ)

.

Then[2]

\begin{align} lnq*(Z)&=\operatorname{E}\pi,\mu,Λ[lnp(X,Z,\pi,\mu,Λ)]+constant\\ &=\operatorname{E}\pi[lnp(Z\mid\pi)]+\operatorname{E}\mu,Λ[lnp(X\midZ,\mu,Λ)]+constant\\ &=

N
\sum
n=1
K
\sum
k=1

znkln\rhonk+constant \end{align}

where we have defined

ln\rhonk=\operatorname{E}[ln\pik]+

1
2

\operatorname{E}[ln|Λk|]-

D
2

ln(2\pi)-

1
2
\operatorname{E}
\muk,Λk

[(xn-

\rmT
\mu
k)

Λk(xn-\muk)]

Exponentiating both sides of the formula for

lnq*(Z)

yields

q*(Z)\propto

N
\prod
n=1
K
\prod
k=1
znk
\rho
nk

Requiring that this be normalized ends up requiring that the

\rhonk

sum to 1 over all values of

k

, yielding

q*(Z)=

N
\prod
n=1
K
\prod
k=1
znk
r
nk

where

rnk=

\rhonk
K
\sum\rhonj
j=1

In other words,

q*(Z)

is a product of single-observation multinomial distributions, and factors over each individual

zn

, which is distributed as a single-observation multinomial distribution with parameters

rnk

for

k=1...K

.

Furthermore, we note that

\operatorname{E}[znk]=rnk

which is a standard result for categorical distributions.

Now, considering the factor

q(\pi,\mu,Λ)

, note that it automatically factors into

q(\pi)

K
\prod
k=1

q(\muk,Λk)

due to the structure of the graphical model defining our Gaussian mixture model, which is specified above.

Then,

\begin{align} lnq*(\pi)&=lnp(\pi)+\operatorname{E}Z[lnp(Z\mid\pi)]+constant\\ &=(\alpha0-1)

K
\sum
k=1

ln\pik+

N
\sum
n=1
K
\sum
k=1

rnkln\pik+constant \end{align}

Taking the exponential of both sides, we recognize

q*(\pi)

as a Dirichlet distribution

q*(\pi)\sim\operatorname{Dir}(\alpha)

where

\alphak=\alpha0+Nk

where

Nk=

N
\sum
n=1

rnk

Finally

ln

*(\mu
q
k,Λ

k)=lnp(\muk,Λk)+

N
\sum
n=1

\operatorname{E}[znk]lnl{N}(xn\mid\muk,Λ

-1
k

)+constant

Grouping and reading off terms involving

\muk

and

Λk

, the result is a Gaussian-Wishart distribution given by
*(\mu
q
k,Λ

k)=l{N}(\muk\midmk,(\betak

-1
Λ
k)

)l{W}(Λk\midWk,\nuk)

given the definitions

\begin{align} \betak&=\beta0+Nk\\ mk&=

1
\betak

(\beta0\mu0+Nk{\bar{x

}}_k) \\\mathbf_k^ &= \mathbf_0^ + N_k \mathbf_k + \frac (_k - \mathbf_0)(_k - \mathbf_0)^ \\\nu_k &= \nu_0 + N_k \\N_k &= \sum_^N r_ \\_k &= \frac \sum_^N r_ \mathbf_n \\\mathbf_k &= \frac \sum_^N r_ (\mathbf_n - _k) (\mathbf_n - _k)^\end

Finally, notice that these functions require the values of

rnk

, which make use of

\rhonk

, which is defined in turn based on

\operatorname{E}[ln\pik]

,

\operatorname{E}[ln|Λk|]

, and
\operatorname{E}
\muk,Λk

[(xn-

\rmT
\mu
k)

Λk(xn-\muk)]

. Now that we have determined the distributions over which these expectations are taken, we can derive formulas for them:
\begin{align} \operatorname{E}
\muk,Λk

[(xn-

\rmT
\mu
k)

Λk(xn-\muk)]&=

-1
D\beta
k

+\nuk(xn-

\rmT
m
k)

Wk(xn-mk)\\ ln{\widetilde{Λ}}k&\equiv\operatorname{E}[ln|Λk|]=

D
\sum
i=1

\psi\left(

\nuk+1-i
2

\right)+Dln2+ln|Wk|\\ ln{\widetilde{\pi}}k&\equiv\operatorname{E}\left[ln|\pik|\right]=\psi(\alphak)-

K
\psi\left(\sum
i=1

\alphai\right) \end{align}

These results lead to

rnk\propto{\widetilde{\pi}}k

1/2
{\widetilde{Λ}}
k

\exp\left\{-

D
2\betak

-

\nuk
2

(xn-

\rmT
m
k)

Wk(xn-mk)\right\}

These can be converted from proportional to absolute values by normalizing over

k

so that the corresponding values sum to 1.

Note that:

  1. The update equations for the parameters

\betak

,

mk

,

Wk

and

\nuk

of the variables

\muk

and

Λk

depend on the statistics

Nk

,

{\bar{x

}}_k, and

Sk

, and these statistics in turn depend on

rnk

.
  1. The update equations for the parameters

\alpha1

of the variable

\pi

depend on the statistic

Nk

, which depends in turn on

rnk

.
  1. The update equation for

rnk

has a direct circular dependence on

\betak

,

mk

,

Wk

and

\nuk

as well as an indirect circular dependence on

Wk

,

\nuk

and

\alpha1

through

{\widetilde{\pi}}k

and

{\widetilde{Λ}}k

.

This suggests an iterative procedure that alternates between two steps:

  1. An E-step that computes the value of

rnk

using the current values of all the other parameters.
  1. An M-step that uses the new value of

rnk

to compute new values of all the other parameters.

Note that these steps correspond closely with the standard EM algorithm to derive a maximum likelihood or maximum a posteriori (MAP) solution for the parameters of a Gaussian mixture model. The responsibilities

rnk

in the E step correspond closely to the posterior probabilities of the latent variables given the data, i.e.

p(Z\midX)

; the computation of the statistics

Nk

,

{\bar{x

}}_k, and

Sk

corresponds closely to the computation of corresponding "soft-count" statistics over the data; and the use of those statistics to compute new values of the parameters corresponds closely to the use of soft counts to compute new parameter values in normal EM over a Gaussian mixture model.

Exponential-family distributions

Note that in the previous example, once the distribution over unobserved variables was assumed to factorize into distributions over the "parameters" and distributions over the "latent data", the derived "best" distribution for each variable was in the same family as the corresponding prior distribution over the variable. This is a general result that holds true for all prior distributions derived from the exponential family.

See also

External links

Notes and References

  1. Copula Variational Bayes inference via information geometry. Viet Hung. Tran. 2018. 1803.10998. cs.IT.
  2. Web site: Nguyen. Duy. AN IN DEPTH INTRODUCTION TO VARIATIONAL BAYES NOTE. 15 August 2023 . 10.2139/ssrn.4541076 . 4541076 . 15 August 2023.
  3. Lee. Se Yoon. Gibbs sampler and coordinate ascent variational inference: A set-theoretical review. Communications in Statistics - Theory and Methods. 2021. 51 . 6 . 1–21. 10.1080/03610926.2021.1921214. 2008.01006. 220935477.
  4. Book: Convex Optimization. Stephen P.. Boyd. Lieven. Vandenberghe. 2004. Cambridge University Press. 978-0-521-83378-3. October 15, 2011.
  5. Book: Bishop, Christopher M. . Chapter 10 . Pattern Recognition and Machine Learning . 2006 . Springer . 978-0-387-31073-2 . Christopher M. Bishop.
  6. Sotirios P. Chatzis, “Infinite Markov-Switching Maximum Entropy Discrimination Machines,” Proc. 30th International Conference on Machine Learning (ICML). Journal of Machine Learning Research: Workshop and Conference Proceedings, vol. 28, no. 3, pp. 729–737, June 2013.