Checkerboard Gibbs sampling Created 2026-10-07 Updated 2026-11-05
For a Markov random field whose positive probability density function factors into single-vertex and pairwise edge factors on a bipartite graph, variables in one color class are conditionally independent given the other color. A checkerboard Gibbs sampler therefore draws every black variable from its full conditional distribution in parallel, then every white variable conditional on the newly drawn blacks. Each color update is a blocked Gibbs sampler preserving the joint target, and their composition preserves it too.
Bipartiteness of a displayed pixel-neighbour graph is insufficient if the actual likelihood introduces factors coupling several same-color variables. For example, an observation depending jointly on two black neighbours can destroy their conditional independence. The conditional factorization of the posterior, rather than the picture alone, justifies the parallel updates.
Write for the true DNA methylation status and for its observation. The observation likelihood function is a binary symmetric channel, so
For the loop suggested in the practical, set and add when , or otherwise. The following independent implementation combines these terms without multiplying many small numbers:
import numpy as np

def methylation_score(pattern, observed):
    """Conditional-product score used by the practical's natural loop filling."""
    pattern=np.asarray(pattern,dtype=int);observed=np.asarray(observed,dtype=int)
    if pattern[0]!=1 or pattern[-1]!=1:return -np.inf
    neighbour_sum=pattern[:-2]+pattern[2:]
    p=np.array([.1,.5,.9])[neighbour_sum]
    prior_terms=np.where(pattern[1:-1]==1,p,1-p)
    observation_terms=np.where(pattern==observed,.8,.2)
    return float(np.log(prior_terms).sum()+np.log(observation_terms).sum())

log_likelihood = methylation_score
print(f"{log_likelihood(obs, obs):.2f}")
The conditional-product log score at the observed pattern is . The endpoint observations are included once each, while the endpoint statuses are fixed rather than assigned an additional prior probability.
There is a modeling qualification: multiplying the specified neighbour conditional probabilities gives a pseudolikelihood, not generally the joint probability distribution having those conditional probabilities. Thus the function above defines the practical's conditional-product target; the pure observation log-likelihood at would instead be .
If the neighbour probabilities are intended literally as full conditionals of a Markov random field, a coherent alternative has edge weights and for , with both endpoints fixed. Given two equal neighbours, the matching centre has conditional odds ; mixed neighbours give equal weights. Consequently its unnormalized posterior probability has log score
For this alternative, before the constant associated with conditioning the endpoints. It can be implemented as follows, but its numerical results must not be mixed with the conditional-product results in the remaining parts:
def joint_methylation_score(pattern,observed):
    """Coherent nearest-neighbour model with the specified full conditionals."""
    pattern=np.asarray(pattern,dtype=int);observed=np.asarray(observed,dtype=int)
    if pattern[0]!=1 or pattern[-1]!=1:return -np.inf
    edges=np.where(pattern[1:]==pattern[:-1],.75,.25)
    emissions=np.where(pattern==observed,.8,.2)
    return float(np.log(edges).sum()+np.log(emissions).sum())
Writing , the edge term rewards neighboring computers having the same infection state, as in a ferromagnetic Ising model, while expresses a mild prior preference for the rarer uninfected state. Thus the prior encodes local transmission over the network without assuming independent infections.
The observation likelihood is
Consequently the posterior distribution is
This remains a binary pairwise Markov random field on a tree. To find its maximum a posteriori estimate, run max-product belief propagation: send a two-entry message in each direction along every edge, then backtrack from the maximizing root state. Each message examines four state pairs, and the maximum degree is bounded, so the total cost is
Pseudolikelihood 2026-10-20
A pseudolikelihood multiplies conditional probabilities rather than the full joint likelihood function. It can be useful for fitting a Markov random field, but using the product itself as a probability density defines a different model unless an additional equivalence is established.