Tutorials › Stochastic Processes › Gibbs Sampling for Discrete Graphical Models

Markov Chain Monte Carlo · Tutorial 921 of 994

Gibbs Sampling for Discrete Graphical Models

Learn how the Ising interaction determines each spin’s conditional distribution and how to verify a single-site Gibbs update exactly.

Advanced 10 min read

What You'll Learn

  • Write the finite Ising target distribution using spin interactions and external fields.
  • Derive a spin’s conditional odds from its neighbors.
  • Compute exact update probabilities for small graphs.
  • Verify conditional probabilities by enumerating joint configuration weights.
  • Check that a fixed-site Gibbs update preserves the target distribution.

From Ising interactions to a local update

In a discrete graphical model, the joint distribution can involve many variables, but the conditional distribution of one variable may depend only on its immediate neighbors. This locality is what makes single-site Gibbs sampling practical: at each step, one spin is redrawn from a two-point conditional distribution rather than from the full joint distribution. The update probability is not guessed from the graph; it follows by holding all other spins fixed and comparing the two possible joint weights.

Let \(G=(V,E)\) be a finite undirected graph, with spin \(x_i\in\{-1,+1\}\) at each vertex \(i\). For each edge \(\{i,j\}\in E\), let \(K_{ij}=K_{ji}\) be a real interaction parameter, and let \(b_i\) be a real external-field parameter at vertex \(i\). The target distribution on configurations \(x=(x_i)_{i\in V}\) is

$$ \pi(x)=\frac{1}{Z}\exp\!\left(\sum_{\{i,j\}\in E}K_{ij}x_ix_j+\sum_{i\in V}b_ix_i\right), \qquad Z=\sum_{x\in\{-1,+1\}^{V}} \exp\!\left(\sum_{\{i,j\}\in E}K_{ij}x_ix_j+\sum_{i\in V}b_ix_i\right). $$

Here \(Z\) is the normalizing constant. It is a finite sum of positive terms, so it is finite and strictly positive. In the common parametrization \(K_{ij}=\beta J_{ij}\) and \(b_i=\beta h_i\), \(\beta\) is inverse temperature, \(J_{ij}\) is the edge coupling, and \(h_i\) is the field. Positive \(K_{ij}\) favors matching spins; negative \(K_{ij}\) favors opposite spins.

Definition: A single-site Gibbs update at vertex \(i\) replaces \(x_i\) by a draw from \(\pi(x_i\mid x_{-i})\), where \(x_{-i}\) denotes all spins except \(x_i\), and leaves every other spin unchanged. A Gibbs chain is a discrete-time process \((X_n)_{n\ge0}\) on the finite state space \(S=\{-1,+1\}^{V}\), whose transition at a step is such an update. When conditioning on the chain history, use the filtration \(\mathcal{F}_n=\sigma(X_0,\ldots,X_n)\).

Because every configuration has positive target probability, the conditional distribution given any fixed \(x_{-i}\) is well-defined. Only terms in the exponent involving \(x_i\) matter: terms involving no \(x_i\) are the same for both possible values and cancel in the conditional normalization.

Theorem (single-site Ising conditional): For a vertex \(i\), define its local field at configuration \(x\) by \(H_i(x)=b_i+\sum_{j:\{i,j\}\in E}K_{ij}x_j\). Then \(\pi(x_i=+1\mid x_{-i})=\frac{\exp(H_i(x))}{\exp(H_i(x))+\exp(-H_i(x))}=\frac{1}{1+\exp(-2H_i(x))}\), and \(\pi(x_i=-1\mid x_{-i})=\frac{1}{1+\exp(2H_i(x))}\).

Proof. Fix the values \(x_{-i}\). In the joint exponent, the terms that depend on \(x_i\) are \(x_i b_i+\sum_{j:\{i,j\}\in E}K_{ij}x_ix_j=x_iH_i(x)\). All remaining terms have a common value, say \(C\), for the two choices of \(x_i\). Thus the unnormalized weights for \(x_i=+1\) and \(x_i=-1\) are respectively \(\exp(C+H_i(x))\) and \(\exp(C-H_i(x))\). Dividing the first by their sum cancels \(\exp(C)\) and gives \(\exp(H_i)/(\exp(H_i)+\exp(-H_i))\). Multiplying numerator and denominator by \(\exp(H_i)\) gives \(1/(1+\exp(-2H_i))\). The probability for \(-1\) is the other normalized weight, equivalently \(1/(1+\exp(2H_i))\). The two probabilities are positive and sum to one, including when \(H_i=0\). \(\square\)

The formula uses the sum of the neighboring spins weighted by their interactions, not the number of neighbors alone. If \(i\) has no neighbors, the sum is empty and equals zero, so the conditional depends only on \(b_i\). With zero field and zero neighbor contribution, each spin value has probability one-half.

Exact calculations on small graphs

Worked Example: Updating the Center of a Three-Vertex Path

Take the path \(1-2-3\), with \(K_{12}=K_{23}=\log 2\) and \(b_1=b_2=b_3=0\). The target is proportional to \(2^{x_1x_2+x_2x_3}\). Every configuration has positive weight, so conditioning on any pair of fixed spins is valid. Find the probability that an update at vertex \(2\) sets its spin to \(+1\).

For fixed endpoint spins, the local field at the center is \(H_2=(\log 2)(x_1+x_3)\). If both endpoints are \(+1\), then \(H_2=2\log 2\), so the odds of \(+1\) against \(-1\) are \(\exp(2H_2)=\exp(4\log 2)=16\). The conditional probability is therefore \(16/(16+1)=16/17\). If the endpoints are opposite, then \(H_2=0\) and the probability is \(1/2\). If both endpoints are \(-1\), then \(H_2=-2\log 2\), the odds are \(1/16\), and the probability is \(1/(16+1)=1/17\).

Check: For each of the three endpoint cases, the probability of \(-1\) is the complement: respectively \(1/17\), \(1/2\), and \(16/17\). Each pair of conditional probabilities sums to one.

Worked Example: Enumerating the Whole Path Target

For the same path and parameters, enumerate all eight configurations to verify the center conditional when \(x_1=x_3=+1\). The exponent is \((\log 2)(x_1x_2+x_2x_3)\), so a configuration with edge-product sum \(r\) has unnormalized weight \(2^r\).

Configuration \((x_1,x_2,x_3)\)Edge-product sumUnnormalized weight
\((+1,+1,+1)\)24
\((+1,+1,-1)\)01
\((+1,-1,+1)\)-2\(\tfrac14\)
\((+1,-1,-1)\)01
\((-1,+1,+1)\)01
\((-1,+1,-1)\)-2\(\tfrac14\)
\((-1,-1,+1)\)01
\((-1,-1,-1)\)24

Adding the displayed weights gives \(Z=4+1+\tfrac14+1+1+\tfrac14+1+4=\tfrac{25}{2}\). Given \(x_1=x_3=+1\), the only compatible configurations are \((+1,+1,+1)\), of weight \(4\), and \((+1,-1,+1)\), of weight \(\tfrac14\). Thus the conditional probability of a center spin \(+1\), computed directly from the joint target, is \(4/(4+\tfrac14)=16/17\), agreeing with the local-field formula.

Check: The conditional probability of center spin \(-1\) is \((\tfrac14)/(4+\tfrac14)=1/17\), and \(16/17+1/17=1\). The enumeration also checks the normalizer: dividing all eight positive weights by \(25/2\) gives probabilities summing to one.

Enumeration is a useful verification tool for small graphs, but it scales poorly: a graph with \(|V|\) vertices has \(2^{|V|}\) configurations. The conditional formula instead requires only the neighboring spins and the incident edge parameters. The following example also checks how a single-site update relates to the full target, without needing to compute its normalizing constant.

Worked Example: A Field on a Three-Vertex Cycle

Take a triangle with all three edge parameters equal to \(\log 2\), with \(b_1=\log 2\) and \(b_2=b_3=0\). Consider updating vertex \(1\) while \(x_2=+1\) and \(x_3=-1\). The local field is \(H_1=\log 2+(\log 2)(+1)+(\log 2)(-1)=\log 2\). Hence the probability of setting \(x_1=+1\) is \(1/(1+\exp(-2\log 2))=1/(1+\tfrac14)=4/5\); the probability of setting it to \(-1\) is \(1/5\).

Verify this by comparing the two joint weights with the other spins fixed. For \((x_1,x_2,x_3)=(+1,+1,-1)\), the three edge products sum to \(-1\), and the field contribution is \(+1\), so the exponent divided by \(\log 2\) is \(0\), giving weight \(1\). For \((-1,+1,-1)\), the edge products again sum to \(-1\), while the field contribution is \(-1\); the exponent divided by \(\log 2\) is \(-2\), giving weight \(1/4\). The conditional probability is \(1/(1+\tfrac14)=4/5\), as before.

Let \(K_1\) be the transition rule that updates only vertex \(1\). From the first configuration, the probability of moving to the second is \(1/5\); from the second, the probability of moving to the first is \(4/5\). Their target probabilities are proportional to \(1\) and \(1/4\), respectively, and the two probability flows agree: \(1\cdot\tfrac15=\tfrac14\cdot\tfrac45=\tfrac15\).

Why the fixed-site update preserves the target

The local conditional calculation does more than produce plausible draws. A Gibbs update at any fixed vertex preserves the joint target distribution. The claim concerns one update kernel; it does not assert that a particular update schedule converges, nor does it address how update order affects sampling.

Theorem (invariance of a fixed-site Gibbs update): Let \(\pi\) be a strictly positive distribution on the finite configuration space \(S=\{-1,+1\}^{V}\). For a fixed vertex \(i\), let \(K_i(x,y)\) be the probability that one update at \(i\), starting at \(x\), produces \(y\). Then \(\sum_{x\in S}\pi(x)K_i(x,y)=\pi(y)\) for every \(y\in S\).

Proof. If \(x\) and \(y\) differ at any vertex other than \(i\), then \(K_i(x,y)=0\), because the update changes no other coordinate. For configurations that agree away from \(i\), \(K_i(x,y)=\pi(y_i\mid x_{-i})\). Fix \(y\). In the sum \(\sum_x\pi(x)K_i(x,y)\), only configurations \(x\) with \(x_{-i}=y_{-i}\) contribute. For each such \(x\), the conditional probability of producing \(y_i\) is \(\pi(y_i\mid y_{-i})\), independent of \(x_i\). Therefore \(\sum_x\pi(x)K_i(x,y)=\pi(y_i\mid y_{-i})\sum_{x:x_{-i}=y_{-i}}\pi(x)\). The sum on the right is the marginal probability \(\pi(X_{-i}=y_{-i})\). By the definition of conditional probability, its product with \(\pi(y_i\mid y_{-i})\) equals \(\pi(y)\). This proves the assertion for every \(y\). \(\square\)

In the Ising examples, strict positivity follows because every exponential weight is positive and the normalizer is finite. The theorem therefore applies without excluding any spin configuration. It also explains why the update must use the conditional from the target itself: replacing it with an unrelated probability need not preserve \(\pi\).

A common calculation error

Pitfall: forgetting that conditional odds compare two weights. In the path example with both endpoint spins \(+1\), the center local field is \(2\log 2\). A common wrong calculation is to set the probability of \(+1\) equal to \(\exp(H_2)=4\). That is not a probability, and it also misses the weight for the alternative spin. The correct weights are \(\exp(H_2)=4\) and \(\exp(-H_2)=1/4\); hence the conditional probability is \(4/(4+\tfrac14)=16/17\). Equivalently, the odds are \(\exp(2H_2)=16\), so the probability is \(16/(1+16)=16/17\). Both forms give the same result.

For practical calculations, first isolate all terms involving the updated spin, then evaluate the local field, and finally normalize the two weights. Keep the distinction between a configuration’s unnormalized joint weight and a conditional probability explicit. For a small graph, direct enumeration provides an independent check; for a larger graph, the same local calculation remains valid even when enumerating the joint target is infeasible.

Check Your Understanding

Use the conditional formula and, when possible, verify by comparing the two joint weights with the other spins fixed.

  1. For a vertex with two neighbors, parameters \(K_{i1}=K_{i2}=\log 2\), field \(b_i=0\), and both neighbors \(+1\), compute the conditional probability of \(x_i=+1\).
  2. In the same model, what is the conditional probability when one neighbor is \(+1\) and the other is \(-1\)?
  3. For a single isolated vertex with field \(b_i=\log 2\), compute both spin probabilities from the target distribution.
  4. Explain why every Ising configuration has positive conditional probability when all parameters are finite.
  5. In the invariance proof, why do configurations that differ from \(y\) at a vertex other than \(i\) contribute zero to \(\sum_x\pi(x)K_i(x,y)\)?