In statistics, Gibbs sampling or a Gibbs sampler is a Markov chain Monte Carlo (MCMC) algorithm for sampling from a specified multivariate probability distribution when direct sampling from the joint distribution is difficult, but sampling from the conditional distribution is more practical. This sequence can be used to approximate the joint distribution (e.g., to generate a histogram of the distribution); to approximate the marginal distribution of one of the variables, or some subset of the variables (for example, the unknown parameters or latent variables); or to compute an integral (such as the expected value of one of the variables). Typically, some of the variables correspond to observations whose values are known, and hence do not need to be sampled.
Gibbs sampling is commonly used as a means of statistical inference, especially Bayesian inference. It is a randomized algorithm (i.e. an algorithm that makes use of random numbers), and is an alternative to deterministic algorithms for statistical inference such as the expectation–maximization algorithm (EM).
As with other MCMC algorithms, Gibbs sampling generates a Markov chain of samples, each of which is correlated with nearby samples. As a result, care must be taken if independent samples are desired. Generally, samples from the beginning of the chain (the burn-in period) may not accurately represent the desired distribution and are usually discarded.
Gibbs sampling is named after the physicist Josiah Willard Gibbs, in reference to an analogy between the sampling algorithm and statistical physics. The algorithm was described by brothers Stuart and Donald Geman in 1984, some eight decades after the death of Gibbs,[1] and became popularized in the statistics community for calculating marginal probability distribution, especially the posterior distribution.[2]
In its basic version, Gibbs sampling is a special case of the Metropolis–Hastings algorithm. However, in its extended versions (see below), it can be considered a general framework for sampling from a large set of variables by sampling each variable (or in some cases, each group of variables) in turn, and can incorporate the Metropolis–Hastings algorithm (or methods such as slice sampling) to implement one or more of the sampling steps.
Gibbs sampling is applicable when the joint distribution is not known explicitly or is difficult to sample from directly, but the conditional distribution of each variable is known and is easy (or at least, easier) to sample from. The Gibbs sampling algorithm generates an instance from the distribution of each variable in turn, conditional on the current values of the other variables. It can be shown that the sequence of samples constitutes a Markov chain, and the stationary distribution of that Markov chain is just the sought-after joint distribution.[3]
Gibbs sampling is particularly well-adapted to sampling the posterior distribution of a Bayesian network, since Bayesian networks are typically specified as a collection of conditional distributions.
Gibbs sampling, in its basic incarnation, is a special case of the Metropolis–Hastings algorithm. The point of Gibbs sampling is that given a multivariate distribution it is simpler to sample from a conditional distribution than to marginalize by integrating over a joint distribution. Suppose we want to obtain
\left.k\right.
X=(x1,...,xn)
p(x1,...,xn)
i
X(i)=
(i) | |
\left(x | |
1 |
,...,
(i) | |
x | |
n |
\right)
X(0)
X(i+1)
X(i+1)=
(i+1) | |
\left(x | |
1 |
,
(i+1) | |
x | |
2 |
,...,
(i+1) | |
x | |
n |
\right)
(i+1) | |
x | |
j |
X(i+1)
(i+1) | |
x | |
j-1 |
X(i)
(i) | |
x | |
j+1 |
(i) | |
x | |
n |
(i+1) | |
x | |
j |
(i+1) | |
p\left(x | |
j |
(i+1) | |
|x | |
1 |
(i+1) | |
,...,x | |
j-1 |
(i) | |
,x | |
j+1 |
(i) | |
,...,x | |
n |
\right)
(j+1)
i
(i+1)
k
If such sampling is performed, these important facts hold:
When performing the sampling:
n
n
Furthermore, the conditional distribution of one variable given all others is proportional to the joint distribution:
p(xj\midx1,...,xj-1,xj+1,...,xn)=
p(x1,...,xn) | |
p(x1,...,xj-1,xj+1,...,xn) |
\proptop(x1,...,xn)
"Proportional to" in this case means that the denominator is not a function of
xj
xj
xj
xj
xj
xj
Gibbs sampling is commonly used for statistical inference (e.g. determining the best value of a parameter, such as determining the number of people likely to shop at a particular store on a given day, the candidate a voter will most likely vote for, etc.). The idea is that observed data is incorporated into the sampling process by creating separate variables for each piece of observed data and fixing the variables in question to their observed values, rather than sampling from those variables. The distribution of the remaining variables is then effectively a posterior distribution conditioned on the observed data.
The most likely value of a desired parameter (the mode) could then simply be selected by choosing the sample value that occurs most commonly; this is essentially equivalent to maximum a posteriori estimation of a parameter. (Since the parameters are usually continuous, it is often necessary to "bin" the sampled values into one of a finite number of ranges or "bins" in order to get a meaningful estimate of the mode.) More commonly, however, the expected value (mean or average) of the sampled values is chosen; this is a Bayes estimator that takes advantage of the additional data about the entire distribution that is available from Bayesian sampling, whereas a maximization algorithm such as expectation maximization (EM) is capable of only returning a single point from the distribution. For example, for a unimodal distribution the mean (expected value) is usually similar to the mode (most common value), but if the distribution is skewed in one direction, the mean will be moved in that direction, which effectively accounts for the extra probability mass in that direction. (If a distribution is multimodal, the expected value may not return a meaningful point, and any of the modes is typically a better choice.)
Although some of the variables typically correspond to parameters of interest, others are uninteresting ("nuisance") variables introduced into the model to properly express the relationships among variables. Although the sampled values represent the joint distribution over all variables, the nuisance variables can simply be ignored when computing expected values or modes; this is equivalent to marginalizing over the nuisance variables. When a value for multiple variables is desired, the expected value is simply computed over each variable separately. (When computing the mode, however, all variables must be considered together.)
Supervised learning, unsupervised learning and semi-supervised learning (aka learning with missing values) can all be handled by simply fixing the values of all variables whose values are known, and sampling from the remainder.
For observed data, there will be one variable for each observation—rather than, for example, one variable corresponding to the sample mean or sample variance of a set of observations. In fact, there generally will be no variables at all corresponding to concepts such as "sample mean" or "sample variance". Instead, in such a case there will be variables representing the unknown true mean and true variance, and the determination of sample values for these variables results automatically from the operation of the Gibbs sampler.
Generalized linear models (i.e. variations of linear regression) can sometimes be handled by Gibbs sampling as well. For example, probit regression for determining the probability of a given binary (yes/no) choice, with normally distributed priors placed over the regression coefficients, can be implemented with Gibbs sampling because it is possible to add additional variables and take advantage of conjugacy. However, logistic regression cannot be handled this way. One possibility is to approximate the logistic function with a mixture (typically 7–9) of normal distributions. More commonly, however, Metropolis–Hastings is used instead of Gibbs sampling.
Suppose that a sample
\left.X\right.
\theta\in\Theta
\left.d\right.
g(\theta1,\ldots,\thetad)
\left.d\right.
\left.\thetai\right.
\left.\Theta\right.
1\leqj\leqd
\left.\thetaj\right.
g(\theta1,\ldots,\thetaj-1, ⋅ ,\thetaj+1,\ldots,\thetad)
These steps define a reversible Markov chain with the desired invariant distribution
\left.g\right.
x\simjy
\left.xi=yi\right.
i ≠ j
\left.pxy\right.
x\in\Theta
y\in\Theta
pxy=\begin{cases}
1 | |
d |
g(y) | ||||||
|
&x\simjy\\ 0&otherwise \end{cases}
So
g(x)pxy=
1 | |
d |
g(x)g(y) | ||||||
|
=
1 | |
d |
g(y)g(x) | ||||||
|
=g(y)pyx
since
x\simjy
\left.g\right.
In practice, the index
\left.j\right.
Let
y
f(y|\theta)
\pi(\theta)
\Theta
\pi(\theta|y)=
f(y|\theta) ⋅ \pi(\theta) | |
m(y) |
m(y)=\int\Thetaf(y|\theta) ⋅ \pi(\theta)d\theta
y
To explain the Gibbs sampler, we additionally assume that the parameter space
\Theta
\Theta=
K | |
\prod | |
i=1 |
\Thetai=\Theta1 x … \Thetai x … x \ThetaK, (K>1)
x
\Thetai
Define a set
\Theta-i
\Thetai
i
i=1, … ,K
\pi(\thetai|\theta-i,y)=\pi(\thetai|\theta1, … ,\thetai-1,\thetai+1, … ,\thetaK,y)
The following algorithm details a generic Gibbs sampler:
Initialize:pickarbitrarystartingvalue\theta(1)=
(1) | |
(\theta | |
1 |
(1) | |
,\theta | |
2 |
(1) | |
, … ,\theta | |
i |
(1) | |
,\theta | |
i+1 |
(1) | |
, … ,\theta | |
K |
)
IterateaCycle:
Step1.draw
(s+1) | |
\theta | |
1 |
\sim\pi(\theta1|\theta
(s) | |
2 |
(s) | |
,\theta | |
3 |
(s) | |
, … ,\theta | |
K |
,y)
Step2.draw
(s+1) | |
\theta | |
2 |
\sim\pi(\theta2|\theta
(s+1) | |
1 |
(s) | |
,\theta | |
3 |
(s) | |
, … ,\theta | |
K |
,y)
\vdots
Stepi.draw
(s+1) | |
\theta | |
i |
\sim\pi(\thetai|\theta
(s+1) | |
1 |
(s+1) | |
,\theta | |
2 |
(s+1) | |
, … , \theta | |
i-1 |
(s) | |
,\theta | |
i+1 |
, … ,
(s) | |
\theta | |
K |
,y)
Stepi+1.draw
(s+1) | |
\theta | |
i+1 |
\sim\pi(\thetai+1
(s+1) | |
|\theta | |
1 |
(s+1) | |
,\theta | |
2 |
(s+1) | |
, … , \theta | |
i |
(s) | |
,\theta | |
i+2 |
, … ,
(s) | |
\theta | |
K |
,y)
\vdots
StepK.draw
(s+1) | |
\theta | |
K |
\sim\pi(\thetaK|\theta
(s+1) | |
1 |
(s+1) | |
,\theta | |
2 |
(s+1) | |
, … ,\theta | |
K-1 |
,y)
endIterate
Note that Gibbs sampler is operated by the iterative Monte Carlo scheme within a cycle. The
S
\{\theta(s)
S | |
\} | |
s=1 |
\pi(\theta|y)
Now, for each
i=1, … ,K
I(\thetai;\theta-i)=KL(\pi(\theta|y)||\pi(\thetai|y) ⋅ \pi(\theta-i|y)) =\int\Theta\pi(\theta|y)log(
\pi(\theta|y) | |
\pi(\thetai|y) ⋅ \pi(\theta-i|y) |
)d\theta,
H(\theta-i)=
-\int | |
\Theta-i |
\pi(\theta-i|y)log\pi(\theta-i|y)d\theta-i,
H(\theta-i|\thetai)=-\int\Theta\pi(\theta|y)log\pi(\theta-i|\thetai,y)d\theta,
namely, posterior mutual information, posterior differential entropy, and posterior conditional differential entropy, respectively. We can similarly define information theoretic quantities
I(\theta-i;\thetai)
H(\thetai)
H(\thetai|\theta-i)
i
-i
K
I(\thetai;\theta-i)=H(\theta-i)-H(\theta-i|\thetai)=H(\thetai)-H(\thetai|\theta-i)=I(\theta-i;\thetai), (i=1, … ,K)
The mutual information
I(\thetai;\theta-i)
\thetai
\theta-i
\thetai
\theta-i
I(\thetai;\theta-i)
i
i+1
Numerous variations of the basic Gibbs sampler exist. The goal of these variations is to reduce the autocorrelation between samples sufficiently to overcome any added computational costs.
In hierarchical Bayesian models with categorical variables, such as latent Dirichlet allocation and various other models used in natural language processing, it is quite common to collapse out the Dirichlet distributions that are typically used as prior distributions over the categorical variables. The result of this collapsing introduces dependencies among all the categorical variables dependent on a given Dirichlet prior, and the joint distribution of these variables after collapsing is a Dirichlet-multinomial distribution. The conditional distribution of a given categorical variable in this distribution, conditioned on the others, assumes an extremely simple form that makes Gibbs sampling even easier than if the collapsing had not been done. The rules are as follows:
In general, any conjugate prior can be collapsed out, if its only children have distributions conjugate to it. The relevant math is discussed in the article on compound distributions. If there is only one child node, the result will often assume a known distribution. For example, collapsing an inverse-gamma-distributed variance out of a network with a single Gaussian child will yield a Student's t-distribution. (For that matter, collapsing both the mean and variance of a single Gaussian child will still yield a Student's t-distribution, provided both are conjugate, i.e. Gaussian mean, inverse-gamma variance.)
If there are multiple child nodes, they will all become dependent, as in the Dirichlet-categorical case. The resulting joint distribution will have a closed form that resembles in some ways the compound distribution, although it will have a product of a number of factors, one for each child node, in it.
In addition, and most importantly, the resulting conditional distribution of one of the child nodes given the others (and also given the parents of the collapsed node(s), but not given the children of the child nodes) will have the same density as the posterior predictive distribution of all the remaining child nodes. Furthermore, the posterior predictive distribution has the same density as the basic compound distribution of a single node, although with different parameters. The general formula is given in the article on compound distributions.
For example, given a Bayes network with a set of conditionally independent identically distributed Gaussian-distributed nodes with conjugate prior distributions placed on the mean and variance, the conditional distribution of one node given the others after compounding out both the mean and variance will be a Student's t-distribution. Similarly, the result of compounding out the gamma prior of a number of Poisson-distributed nodes causes the conditional distribution of one node given the others to assume a negative binomial distribution.
In these cases where compounding produces a well-known distribution, efficient sampling procedures often exist, and using them will often (although not necessarily) be more efficient than not collapsing, and instead sampling both prior and child nodes separately. However, in the case where the compound distribution is not well-known, it may not be easy to sample from, since it generally will not belong to the exponential family and typically will not be log-concave (which would make it easy to sample using adaptive rejection sampling, since a closed form always exists).
In the case where the child nodes of the collapsed nodes themselves have children, the conditional distribution of one of these child nodes given all other nodes in the graph will have to take into account the distribution of these second-level children. In particular, the resulting conditional distribution will be proportional to a product of the compound distribution as defined above, and the conditional distributions of all of the child nodes given their parents (but not given their own children). This follows from the fact that the full conditional distribution is proportional to the joint distribution. If the child nodes of the collapsed nodes are continuous, this distribution will generally not be of a known form, and may well be difficult to sample from despite the fact that a closed form can be written, for the same reasons as described above for non-well-known compound distributions. However, in the particular case that the child nodes are discrete, sampling is feasible, regardless of whether the children of these child nodes are continuous or discrete. In fact, the principle involved here is described in fair detail in the article on the Dirichlet-multinomial distribution.
(i) | |
x | |
j |
(i-1) | |
x | |
j |
(i-1) | |
x | |
j |
(i) | |
x | |
j |
It is also possible to extend Gibbs sampling in various ways. For example, in the case of variables whose conditional distribution is not easy to sample from, a single iteration of slice sampling or the Metropolis–Hastings algorithm can be used to sample from the variables in question.It is also possible to incorporate variables that are not random variables, but whose value is deterministically computed from other variables. Generalized linear models, e.g. logistic regression (aka "maximum entropy models"), can be incorporated in this fashion. (BUGS, for example, allows this type of mixing of models.)
There are two ways that Gibbs sampling can fail. The first is when there are islands of high-probability states, with no paths between them. For example, consider a probability distribution over 2-bit vectors, where the vectors (0,0) and (1,1) each have probability, but the other two vectors (0,1) and (1,0) have probability zero. Gibbs sampling will become trapped in one of the two high-probability vectors, and will never reach the other one. More generally, for any distribution over high-dimensional, real-valued vectors, if two particular elements of the vector are perfectly correlated (or perfectly anti-correlated), those two elements will become stuck, and Gibbs sampling will never be able to change them.
The second problem can happen even when all states have nonzero probability and there is only a single island of high-probability states. For example, consider a probability distribution over 100-bit vectors, where the all-zeros vector occurs with probability, and all other vectors are equally probable, and so have a probability of
1 | |
2(2100-1) |
2100
This problem occurs no matter how long the burn-in period is. This is because in the true distribution, the zero vector occurs half the time, and those occurrences are randomly mixed in with the nonzero vectors. Even a small sample will see both zero and nonzero vectors. But Gibbs sampling will alternate between returning only the zero vector for long periods (about
299
299
299