ArticleslgStudy

science

Gibbs sampling

Gibbs sampling is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Gibbs sampling rather than just read about it. In short: 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 distributi…

Gibbs sampling — main illustration
Gibbs sampling — illustration

Key takeaways

  • Gibbs sampling belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Gibbs sampling to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Gibbs sampling from memory before moving on to harder problems.

Reference excerpt

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.

Illustrations

Gibbs sampling: A pictorial description of the Gibbs sampling algorithm [4]
A pictorial description of the Gibbs sampling algorithm [4]
Gibbs sampling: Schematic description of the information equality associated with the Gibbs sampler at the i-th step within a cycle [4]
Schematic description of the information equality associated with the Gibbs sampler at the i-th step within a cycle [4]

Worked examples

Example 1 — a first encounter with Gibbs sampling

Start with the simplest possible case. Write down what Gibbs sampling claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Gibbs sampling before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Gibbs sampling ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Gibbs sampling

In research
Gibbs sampling appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Gibbs sampling in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Gibbs sampling is common in secondary-school and first-year university syllabi. It links to neighbouring topics Markov chain Monte Carlo, so understanding it makes those chapters shorter.
In everyday life
Look for Gibbs sampling outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Gibbs sampling” →

How to study Gibbs sampling in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Gibbs sampling means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Gibbs sampling out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Gibbs sampling in simple terms?

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 sam…

Why does Gibbs sampling matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Gibbs sampling?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Gibbs sampling.

Tags

  • Markov chain Monte Carlo

Keep exploring