In statistics, Gibbs sampling or a Gibbs sampler, also known in statistical mechanics as the heat bath algorithm, 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. Samples from the beginning of the chain (the burn-in period) may not accurately represent the desired distribution and are usually discarded.
Introduction 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, and became popularized in the statistics community for calculating marginal probability distribution, especially the posterior distribution. 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. 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.
Implementation 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 k samples of a n-dimensional random vector 𝐗=(X1,…,Xn). We proceed iteratively:
Begin with some initial value 𝐗(0). Given a sample 𝐗(i)=(x1(i),…,xn(i)), to obtain the next sample 𝐗(i+1)=(x1(i+1),x2(i+1),…,xn(i+1)), we can sample each component xj(i+1) from the distribution of Xj, conditioned on all the components sampled so far: we condition on Xℓ(i+1) for all ℓ≤j−1, and on Xℓ(i) for j+1≤ℓ≤n. In other words, we sample xj(i+1) according to the distribution P(Xj=⋅|X1=x1(i+1),…,Xj−1=xj−1(i+1),Xj+1=xj+1(i),…,Xn=xn(i)).
Properties If such sampling is performed, these important facts hold:
The samples approximate the joint distribution of all variables. The marginal distribution of any subset of variables can be approximated by simply considering the samples for that subset of variables, ignoring the rest. The expected value of any variable can be approximated by averaging over all the samples. When performing the sampling:
The initial values of the variables can be determined randomly or by some other algorithm such as expectation–maximization. It is not actually necessary to determine an initial value for the first variable sampled. It is common to ignore some number of samples at the beginning (the so-called burn-in period), and then consider only every nth sample when averaging values to compute an expectation. For example, the first 1,000 samples might be ignored, and then every 100th sample averaged, throwing away all the rest. The reason for this is that (1) the stationary distribution of the Markov chain is the desired joint distribution over the variables, but it may take a while for that stationary distribution to be reached; (2) successive samples are not independent of each other but form a Markov chain with some amount of correlation. Sometimes, algorithms can be used to determine the amount of autocorrelation between samples and the value of n (the period between samples that are actually used) computed from this, but in practice there is a fair amount of "black magic" involved. The process of simulated annealing is often used to reduce the "random walk" behavior in the early part of the sampling process (i.e. the tendency to move slowly around the sample space, with a high amount of autocorrelation between samples, rather than moving around quickly, as is desired). Other techniques that may reduce autocorrelation are collapsed Gibbs sampling, blocked Gibbs sampling, and ordered overrelaxation; see below.
Relation of conditional distribution and joint distribution Furthermore, the conditional distribution of one variable given all others is proportional to the joint distribution, i.e., for all possible value (xi)1≤i≤n of 𝐗:
… excerpt ends here. Continue reading the full article.

![Gibbs sampling: A pictorial description of the Gibbs sampling algorithm [4]](https://thumb.wikimedia.org/wikipedia/commons/thumb/3/37/Gibbs_sampler_picture.jpg/1280px-Gibbs_sampler_picture.jpg?utm_source=en.wikipedia.org&utm_campaign=parser&utm_content=thumbnail)
![Gibbs sampling: Schematic description of the information equality associated with the Gibbs sampler at the i-th step within a cycle [4]](https://thumb.wikimedia.org/wikipedia/commons/thumb/6/62/Gibbs_sampling_info_eq.jpg/500px-Gibbs_sampling_info_eq.jpg?utm_source=en.wikipedia.org&utm_campaign=parser&utm_content=thumbnail)