Sequential Monte Carlo for ABC

Julia Fukuyama

Simulation-Based Inference ยท Lecture 17

Today

We will try to simulate from the ABC posterior more efficiently.

We will do this sequentially, starting from the prior, and building a sequence of distributions that get closer and closer to the posterior.

One fact we will need

In Lecture 2, we discussed how to specify a joint distribution for \(Y_1, Y_2\) by first specifying a distribution for \(Y_1\) and then specifying a distribution for \(Y_2 \mid Y_1\).

We calculated joint probabilities for discrete random variables using \[ P(Y_1=y_1,Y_2=y_2) = P(Y_1=y_1)\,P(Y_2=y_2\mid Y_1=y_1). \]

The same multiplication rule holds for densities.

If we construct a joint distribution as:

  • \(Y_1\) is a random variable whose pdf is \(f\)
  • The pdf of the conditional distribution of \(Y_2 \mid Y_1\) is \(g_{Y_1}\) (a separate pdf for every possible value of \(Y_1\))

Then:

  • The joint density of \((Y_1, Y_2)\) is \(f(y_1) g_{y_1}(y_2)\).

This is how we specify \(\Theta\) and \(X\) in a Bayesian model.

Another fact we will need

Recall that when we talked about conditional distributions for discrete random variables in Lecture 10, we had

\[ \begin{aligned} P(X = x \mid X \in E) &= \frac{P(X = x \text{ and }X \in E) }{ P(X \in E)}\\ &= \begin{cases} \frac{P(X = x)}{P(X \in E)} & x \in E \\ 0 & x \not\in E \end{cases}\\ &= \frac{P(X = x)}{ P(X \in E)} \mathbf 1(x \in E) \\ &\propto P(X = x) \mathbf 1(x \in E) \end{aligned} \]

(first two lines from Lecture 10, last two lines new here).

The analogous statement for a continuous random variable involves the pdf:

  • Suppose \(X\) is a random variable or random vector, and \(E\) describes some set of possible outcomes for \(X\).
  • If \(X\) has pdf \(f\), then the pdf of the conditional distribution \(X \mid X \in E\) is proportional to \(f(x) \mathbf 1(x \in E)\).

Recall: Rejection ABC

Given a prior \(\pi\), probability model \(P_\theta\) that you can simulate from, summary statistics \(s\), and tolerance \(\epsilon\):

  1. Draw \(\Theta\sim\pi\).
  2. Simulate \(X\sim P_\Theta\).
  3. Retain \(\Theta\) if \[ d(s(X),s(x^{obs}))<\epsilon. \]
  4. Repeat.

The retained parameters are draws from the conditional distribution

\[ \Theta\mid d(s(X),s(x^{obs}))<\epsilon. \]

Smaller tolerances usually mean fewer acceptances.

Rejection ABC picture

Left: prior draws of ฮธ against their simulated X; only those inside the band |X โˆ’ 0.8| < 0.1 are accepted. Right: histogram of the accepted ฮธ, with the exact ABC posterior overlaid.

Usually you will be dealing with summary statistics and/or parameter vectors that are more than 1-dimensional, but the same picture holds: you simulate from the prior and retain draws in a small window around the observed summary.

How might we be more efficient?

After \(N\) samples from the prior, you might not have retained very many draws that matched the summary statistics well enough, but you could still have an idea about values of \(\theta\) that are more or less plausible.

The ABC-SMC idea is that we bias our proposal distribution away from the prior and toward regions we think have higher posterior mass.

Then we can use our importance sampling weights to correct for our biased proposal.

Observation: Rejection ABC is already importance sampling

Suppose our proposal distribution for \((\Theta, X)\) is \(\Theta \sim \pi\), \(X \mid \Theta \sim P_\Theta\).

Suppose our target distribution is \((\Theta, X) \mid d(s(X), s(x^{obs})) < \epsilon\).

  • Proposal pdf is \(\pi(\theta) f_\theta(x)\) (fact one from today).
  • Target pdf is proportional to \(\pi(\theta) f_\theta(x) \mathbf 1(d(s(x), s(x^{obs})) < \epsilon)\) (fact one + fact two from today).

Importance sampling:

  • Draw \(\Theta, X\) from proposal distribution.
  • Compute unnormalized weight \(w = \frac{\pi(\theta) f_\theta(x) \mathbf 1(d(s(x), s(x^{obs})) < \epsilon)}{\pi(\theta) f_\theta(x)}\).
  • Weight simplifies to \(\mathbf 1(d(s(x), s(x^{obs})) < \epsilon)\), so we weight every draw whose summary statistic is within \(\epsilon\) of the observed one equally and give all the remaining draws weight zero.

What does this tell us?

Importance sampling works well for describing a distribution if the target and proposal distributions are similar.

If the data are informative, the prior and posterior will be far from each other.

ABC-SMC idea: sequentially modify the proposal/target distributions so that you gradually get to the desired posterior.

ABC-SMC: Basic idea

Choose a sequence of tolerances \(\epsilon_1 > \epsilon_2 > \cdots > \epsilon_T\).

At round \(t\), our target posterior is \(\Theta \mid d(s(X), s(x^{obs})) < \epsilon_t\).

Round 1: Run rejection ABC with tolerance \(\epsilon_1\). Give the retained draws equal weight.

Rounds 2 to \(T\):

  1. Construct a proposal distribution for \(\Theta\) based on the previous roundโ€™s accepted draws.
  2. Draw a proposed parameter and simulate a new dataset \(X\).
  3. Retain the proposed parameter if \(d(s(X), s(x^{obs})) < \epsilon_t\).
  4. Give each retained parameter an importance weight correcting for the fact that weโ€™re not proposing from the prior.

Repeat until we get to the final tolerance \(\epsilon_T\).

Each round produces a weighted collection of parameter draws, called particles.

ABC-SMC: The proposal distribution

Suppose the previous round produced particles \(\theta_1,\ldots, \theta_N\) with weights \(w_1,\ldots, w_N\).

For round \(t\), choose a noise distribution that you can simulate from and which has a pdf that you can compute.

Let \(k_{t, \mu}\) denote the pdf of that noise distribution shifted so that it is centered at \(\mu\).

To propose a parameter for this round, we:

  1. Draw an index \(J\) with \(P(J = j) = w_j\).
  2. Draw a new parameter from the density \(k_{t, \theta_J}\) (equivalently, take \(\theta_J\) and perturb it using the noise with distribution \(k_{t, 0}\)).

The resulting proposal density is \[ q_t(\theta)=\sum_{j=1}^N w_j\,k_{t,\theta_j}(\theta). \]

We can compute this because we have chosen \(k\) to have a pdf we can compute.

Why is this a good proposal distribution?

The previous roundโ€™s weighted particles \((\theta_1, w_1), \ldots, (\theta_N, w_N)\) are supposed to approximate the ABC posterior with tolerance \(\epsilon_{t-1}\).

The proposal selects particles according to their weights and then adds noise.

The noise both smooths out the approximating distribution (we can get proposals that are not exactly the same as the particles), and widens it.

If \(\epsilon_t\) is only a bit smaller than \(\epsilon_{t-1}\), we expect that the proposals from this distribution will match the target posterior with \(\epsilon_t\) reasonably well.

ABC-SMC: The weights

At round \(t\), our target is the ABC posterior with tolerance \(\epsilon_t\).

Our proposal distribution is \(\Theta \sim q_t\), \(X \mid \Theta \sim P_\Theta\). Write \(f_\theta(x)\) to denote the density of \(X \mid \Theta\). This is going to cancel out so we wonโ€™t need to ever compute it.

  • Proposal pdf is \(q_t(\theta) f_\theta(x)\) (fact one from today).
  • Target pdf is proportional to \(\pi(\theta) f_\theta(x) \mathbf 1(d(s(x), s(x^{obs})) < \epsilon_t)\) (fact one + fact two from today).

Importance sampling:

  • Draw \(\Theta, X\) from proposal distribution.
  • Compute unnormalized weight \(w = \frac{\pi(\theta) f_\theta(x) \mathbf 1(d(s(x), s(x^{obs})) < \epsilon_t)}{q_t(\theta) f_\theta(x)}\).
  • Weight simplifies to \(\frac{\pi(\theta)}{q_t(\theta)}\mathbf 1(d(s(x), s(x^{obs})) < \epsilon_t)\). Draws that arenโ€™t within \(\epsilon_t\) on summary statistics get weight zero, and other draws get a weight proportional to \(\pi(\theta) / q_t(\theta)\).
  • Normalize the weights so that they sum to 1.

We can compute \(\pi(\theta) / q_t(\theta)\)!

Example: Two rounds in the normal-location model

Same model as the rejection ABC picture:

\[ \Theta\sim\operatorname{Uniform}(-3,3), \qquad X\mid\Theta=\theta\sim N(\theta,0.35^2), \qquad x^{obs}=0.8, \]

with \(s(x)=x\) and \(d(a,b)=|a-b|\), so the ABC check is \(|X-0.8|<\epsilon\).

To make the proposal easy to see, in round 1 we will keep only three particles.

  • Round 1: rejection ABC with \(\epsilon_1=0.5\), keeping \(N_1=3\) particles.
  • Round 2: tolerance \(\epsilon_2=0.1\), keeping \(N_2=1000\) particles, with noise distribution \(N(0,0.3^2)\), so \(k_{2,\mu}\) is the \(N(\mu,0.3^2)\) density.

In practice we would keep many more particles in every round. Here three are enough to build a usable proposal.

Round 1: rejection ABC with a wide band

R

set.seed(686)
x_obs <- 0.8; N1 <- 3
prior_d <- function(theta)
  dunif(theta, -3, 3)

theta1 <- numeric(0)
while (length(theta1) < N1) {
  th <- runif(1, -3, 3)
  X <- rnorm(1, th, 0.35)
  if (abs(X - x_obs) < 0.5)
    theta1 <- c(theta1, th)
}
w1 <- rep(1 / N1, N1)
theta1  # -0.46  0.27  1.22

Python

import numpy as np
rng = np.random.default_rng(686)
x_obs, N1 = 0.8, 3

def prior_d(theta):
    inside = (theta >= -3) & (theta <= 3)
    return np.where(inside, 1/6, 0.0)

theta1 = []
while len(theta1) < N1:
    th = rng.uniform(-3, 3)
    X = rng.normal(th, 0.35)
    if abs(X - x_obs) < 0.5:
        theta1.append(th)
theta1 = np.array(theta1)
w1 = np.full(N1, 1 / N1)

Prior draws of ฮธ against their simulated X, run until three land inside the wide band |X โˆ’ 0.8| < 0.5. The three accepted particles, ฮธโ‚ = โˆ’0.46, ฮธโ‚‚ = 0.27, and ฮธโ‚ƒ = 1.22, are labeled.

Each particle gets weight 1/3. Three points are a crude approximation to the ABC posterior at \(\epsilon_1=0.5\), but they show where to look.

Round 2: perturb, simulate, check

Select a parent with probability \(w_j=1/3\), add \(N(0,0.3^2)\) noise, and keep the proposal if its simulated \(X\) lands in the narrower band \(|X-0.8|<0.1\).

R

tau <- 0.3; N2 <- 1000
theta2 <- numeric(0)
while (length(theta2) < N2) {
  j <- sample.int(N1, 1,
                  prob = w1)
  th <- rnorm(1, theta1[j], tau)
  if (prior_d(th) == 0) next
  X <- rnorm(1, th, 0.35)
  if (abs(X - x_obs) < 0.1)
    theta2 <- c(theta2, th)
}

Python

tau, N2 = 0.3, 1000
theta2 = []
while len(theta2) < N2:
    j = rng.choice(N1, p=w1)
    th = rng.normal(theta1[j], tau)
    if prior_d(th) == 0:
        continue
    X = rng.normal(th, 0.35)
    if abs(X - x_obs) < 0.1:
        theta2.append(th)
theta2 = np.array(theta2)

A proposal outside \([-3,3]\) has prior density 0 and would get weight 0, so we skip it without simulating.

The three round 1 particles, ฮธโ‚ = โˆ’0.46, ฮธโ‚‚ = 0.27, and ฮธโ‚ƒ = 1.22, on the ฮธ axis below an empty plot of simulated X against ฮธ, with the narrow band around x^obs = 0.8.

Each attempt has three steps. Letโ€™s watch three attempts.

Attempt 1, select: ฮธโ‚‚ is circled.

Select a particle: each has probability 1/3. We get \(\theta_2=0.27\).

Attempt 1, perturb: the N(0.27, 0.3ยฒ) kernel is drawn around ฮธโ‚‚, and an arrow moves from ฮธโ‚‚ to ฮธ* = 0.36.

Perturb it: draw \(\Theta^*\) from \(N(0.27,0.3^2)\). We get \(\theta^*=0.36\).

Attempt 1, simulate: the N(0.36, 0.35ยฒ) density of X is drawn sideways above ฮธ*, and the simulated X = 0.73 lands inside the band.

Simulate \(X\sim N(0.36,0.35^2)\). We get \(X=0.73\), inside the band: keep \(\theta^*=0.36\).

Attempt 2, select: ฮธโ‚ƒ is circled; attempt 1โ€™s kept point is shown faded.

Select a particle. This time we get \(\theta_3=1.22\).

Attempt 2, perturb: the kernel around ฮธโ‚ƒ, and an arrow left from ฮธโ‚ƒ to ฮธ* = 0.65.

Perturb it. We get \(\theta^*=0.65\), moved toward the center.

Attempt 2, simulate: the density of X above ฮธ* = 0.65, and the simulated X = 0.81 lands inside the band.

Simulate. We get \(X=0.81\): keep \(\theta^*=0.65\).

Attempt 3, select: ฮธโ‚ is circled; the first two kept points are shown faded.

Select a particle. We get \(\theta_1=-0.46\).

Attempt 3, perturb: the kernel around ฮธโ‚, and an arrow left from ฮธโ‚ to ฮธ* = โˆ’0.97.

Perturb it. We get \(\theta^*=-0.97\), further from the center.

Attempt 3, simulate: the density of X above ฮธ* = โˆ’0.97 sits well below the band, and the simulated X = โˆ’1.23 is rejected.

Simulate. We get \(X=-1.23\), far outside the band: reject. From \(\theta^*=-0.97\), landing in the band was very unlikely.

All round 2 attempts as points of simulated X against ฮธ*; those inside the narrow band are kept.

Repeat until enough proposals are kept.

Every proposal is a perturbed copy of one of the three particles.

Comparing rejection ABC with one round of ABC-SMC

Draws of (ฮธ, X) with joint density contours. Left: ฮธ from the Uniform(โˆ’3, 3) prior; the draws fill a long diagonal band crossing the strip [x^obs โˆ’ 0.1, x^obs + 0.1]. Right: ฮธ from the three-particle proposal qโ‚‚; the draws are concentrated in three overlapping lumps along the diagonal, so more of them land in the strip.

Left: orange points are exact draws from \((\Theta, X) \mid d(s(X), s(x^{obs})) < .1\).

Right: orange points need to be reweighted by \(\pi(\theta) / q_t(\theta)\) to represent \((\Theta, X) \mid d(s(X), s(x^{obs})) < .1\).

Conditioning on \(d(s(X), s(x^{obs})) < .1\)

Histograms of ฮธ for the draws that landed in the strip. Left, from the prior: a single smooth peak, the ABC posterior. Middle, from the proposal: two humps, with too few draws near 0.75; the importance weight ฯ€(ฮธ)/qโ‚‚(ฮธ) is overlaid on its own axis, with a bump at 0.75 and rising steeply beyond the outer particles. Right: the same proposal draws weighted by ฯ€(ฮธ)/qโ‚‚(ฮธ), which matches the left panel.

Left: Histogram of samples from \(\Theta \mid d(s(X), s(x^{obs})) < .1\) when \(\Theta \sim \pi\) (rejection ABC).

Middle: Histogram of unweighted samples from \(\Theta \mid d(s(X), s(x^{obs})) < .1\) when \(\Theta\) is drawn from the ABC-SMC proposal distribution.

Right: Histogram of weighted samples from \(\Theta \mid d(s(X), s(x^{obs})) < .1\) when \(\Theta\) is drawn from the ABC-SMC proposal distribution.

Notes

  • We have two choices: the tolerance schedule \(\epsilon_1,\ldots, \epsilon_T\), and the noise distribution.
  • The tradeoff with the tolerance schedule is: weโ€™ll get more acceptances on each round if we put the \(\epsilon_t\)โ€™s close together, but weโ€™ll need more of them to go between the prior and the target posterior.
  • The noise distribution needs to be tuned so that it really mixes between the particles, but you donโ€™t want it to be so large that it proposes unreasonable parameters a large fraction of the time.
  • Since the intermediate rounds are only used for building proposals, you donโ€™t always have to have a huge number of particles.
  • But you worry about missing things (e.g.ย another mode if the posterior is multi-modal) if you donโ€™t have enough particles.