Skip to main content

Single-Site Ancestral Metropolis-Hastings

Ancestral Metropolis-Hastings is one of the most fundamental Bayesian inference methods. In ancestral Metropolis-Hastings, values are sampled from the model's priors, and samples are accepted or rejected based on the sample's Metropolis acceptance probability. As such, ancestral Metropolis-Hastings is a very general inference method, making no strong assumptions about the structure of the model. However, this generality may lead it to be rather inefficient for many models.

Bean Machine provides a single-site variant of ancestral Metropolis-Hastings, in which values of random variables are sampled and updated one variable at a time (hence the name "single-site").


Imagine we are using Single-Site Ancestral Metropolis-Hastings to choose a new value for a random variable XX. Let's assume that XX is currently assigned a value xx. Below are the steps to this algorithm:

  1. First, we need to propose a new value for XX. We do this by sampling from XX's prior distribution, and using that value as the new proposed value for XX. Let's call that sampled value x′x^\prime.
  2. Next, we need to identify other random variables that XX may have a direct influence upon. This set of random variables is referred to as the Markov blanket of XX. The Markov blanket of a random variable consists of the random variable's parents (those that it depends upon), children (those that depend upon it), and the other parents of the random variable's children. We only need to consider the Markov blanket of random variable XX when assessing appropriateness, because only the likelihoods of these distributions are directly affected by a change in the value of XX. All other random variables in the model are conditionally independent of XX given the random variables in XX's Markov blanket.
  3. Now, we need to assess whether this sample is appropriate. We will examine the likelihood of x′x^\prime, conditional on the other variables in its Markov blanket. We can do this computationally by computing the (log) likelihoods of those other random variables when X=x′X = x^\prime.
  4. Finally, we compare the (log) likelihoods when X=xX = x and X=x′X = x^\prime. We use the Metropolis acceptance probability to accept x′x^\prime that tend to have relatively higher (log) likelihoods. The exact acceptance probability can be read about in the linked article, or in the algorithm details below.


This is the standard ancestral Metropolis-Hastings algorithm:

Input:¬†evidence¬†E¬†and¬†queries¬†R\textbf{Input: }\text{evidence }\mathcal{E} \text{ and queries } \mathcal{R}\\ Given:¬†a¬†family¬†of¬†proposal¬†distributions¬†Q\textbf{Given: }\text{a family of proposal distributions }\mathcal{Q}\\ Create:¬†initial¬†world¬†Ōɬ†initialized¬†with¬†E¬†and¬†extended¬†to¬†include¬†R\textbf{Create: } \text{initial world }\sigma\text{ initialized with }\mathcal{E}\text{ and extended to include }\mathcal{R}\\ repeat\textbf{repeat}\\ Let¬†V¬†represent¬†random¬†variables¬†in¬†Ōɬ†excluding¬†E\qquad\text{Let }V\text{ represent random variables in }\sigma\text{ excluding }\mathcal{E}\\ for¬†X¬†in¬†Vdo\qquad\textbf{for }X\textbf{ in }V\textbf{do}\\ Sample¬†x‚Ä≤‚ąľQX(.‚ą£ŌÉ)\qquad\qquad\text{Sample }x'\sim\mathcal{Q}_X(. \mid \sigma)\\ Clone¬†Ōɬ†to¬†ŌÉ‚Ä≤¬†and¬†set¬†ŌÉX‚Ä≤=x‚Ä≤\qquad\qquad\text{Clone }\sigma\text{ to }\sigma'\text{ and set }\sigma'_X=x'\\ Recompute¬†ŌÉY‚Ä≤¬†for¬†Y‚ąą¬†children¬†of¬†X¬†in¬†ŌÉ‚Ä≤\qquad\qquad\text{Recompute }\sigma'_{Y}\text{ for }Y\in\text{ children of } X\text{ in }\sigma'\\ őĪ=min‚Ā°[1,p(ŌÉ‚Ä≤)QX(ŌÉX‚ą£ŌÉ‚Ä≤)p(ŌÉ)QX(ŌÉX‚Ä≤‚ą£ŌÉ)]\qquad\qquad\alpha=\min\left[1, \frac{p(\sigma')\mathcal{Q}_X(\sigma_X\mid\sigma')}{p(\sigma)\mathcal{Q}_X(\sigma'_X\mid\sigma)}\right]\\ u‚ąľUniform(0,¬†1)\qquad\qquad u\sim \text{Uniform(0, 1)}\\ if¬†u<őĪ¬†then\qquad\qquad\textbf{if }u<\alpha\textbf{ then}\\ ŌÉ=ŌÉ‚Ä≤\qquad\qquad\qquad\sigma=\sigma'\\ end¬†if\qquad\qquad\textbf{end if}\\ end¬†for\qquad\textbf{end for}\\ Emit¬†sample¬†ŌÉ\qquad\text{Emit sample }\sigma\\ until¬†Desired¬†number¬†of¬†samples\textbf{until }\text{Desired number of samples}

Or, in pseudo-code:

For each inference iteration:
For each unobserved random variable X:
Perform a Metropolis Hastings (MH) update, which involves:
1. Propose a new value x′ for X using proposal Q
2. Update the world ŌÉ to ŌÉ‚Ä≤
3. Accept / reject the new value x' using Metropolis acceptance probability


The following code snippet illustrates how to use the inference method.

samples = bm.SingleSiteAncestralMetropolisHastings().infer(

The parameters to infer are described below:

queriesA List of @bm.random_variable targets to fit posterior distributions for.
observationsThe Dict of observations. Each key is a random variable, and its value is the observed value for that random variable.
num_samplesNumber of samples to build up distributions for the values listed in queries.
num_chainsNumber of separate inference runs to use. Multiple chains can be used by diagnostics to verify inference ran correctly.