
Sequential Monte Carlo
Department of Biostatistics and Computational Biology, University of Rochester Medical Center
Posterior distribution of the hidden variable \(z \in \mathcal{Z}\) given we observe \(y \in \mathcal{Y}\):
\[ p(z \mid y) = \frac{p(y \mid z) \, p(z)}{p(y)} \]
Compute the expectation of a function \(h\) with respect to the posterior distribution:
\[ I = \mathbb{E}_{p(z \mid y)}[h(z)] = \int h(z) \, p(z \mid y) \, dz, \]
where \(h\) is a known function that we can evaluate.
Example 1: \(h(z) = z\),
\[ \mathbb{E}_{p(z \mid y)}[z] = \int z \, p(z \mid y) \, dz. \]
Example 2: \(h(z) = 1[z > a]\) for \(z, a \in \mathbb{R}\),
\[ \mathbb{E}_{p(z \mid y)}\big[1[z > a]\big] = P(z > a \mid y) = \int 1[z > a] \, p(z \mid y) \, dz. \]
If \(p(z \mid y)\) is a known distribution such as the Normal distribution, we can use a Monte Carlo approximation to compute the expectation no matter how complex \(h\) is:
\[ \frac{1}{N} \sum_{i=1}^{N} h(z^i) \xrightarrow{\text{a.s.}} \mathbb{E}_{p(z \mid y)}[h(z)] \text{ where } z^i \overset{\text{iid}}{\sim} p(z \mid y), \text{ as } N \to \infty, \]
by the strong Law of Large Numbers (LLN), provided \(\mathbb{E}_{p(z \mid y)}|h(z)| < \infty\).
An important exception is if the likelihood and prior are conjugate distributions. In this case, \(p(z \mid y)\) is known and can be sampled from.
\[ \begin{aligned} I &= \int h(z) \, p(z \mid y) \, dz \\ &= \int h(z) \frac{p(z \mid y)}{q(z)} q(z) \, dz \\ &= \int h(z) \, w(z) \, q(z) \, dz \\ &= \mathbb{E}_{q}[h(z) \, w(z)]. \end{aligned} \]
Therefore, importance sampling is a Monte Carlo algorithm that approximates the expectation with respect to \(q\) with test function \(h'(z) = h(z) \, w(z)\).
Problem: we assumed that \(p(z \mid y)\) can be evaluated. However, this requires computing \(p(y)\),
\[ p(y) = \int p(y \mid z) \, p(z) \, dz, \]
which is intractable in many settings. Therefore, \(p(z \mid y) = p(z, y) / p(y)\) cannot be evaluated.
Then,
\[ \sum_{i=1}^{N} \bar{w}^i \, h(z^i) \xrightarrow{\text{a.s.}} I. \]
Write the estimator as a ratio of two sample averages:
\[ \sum_{i=1}^{N} \bar{w}^i \, h(z^i) = \frac{\sum_{i=1}^{N} w(z^i) \, h(z^i)}{\sum_{j=1}^{N} w(z^j)} = \frac{A_N}{B_N}, \]
where
\[ A_N = \frac{1}{N} \sum_{i=1}^{N} h(z^i) \frac{\gamma(z^i)}{q(z^i)}, \qquad B_N = \frac{1}{N} \sum_{i=1}^{N} \frac{\gamma(z^i)}{q(z^i)}. \]
Each is an ordinary Monte Carlo average under \(q\), so the LLN applies to each separately.
By LLN, the numerator converges to \(\int h(z) \, p(z, y) \, dz\):
\[ A_N \xrightarrow{\text{a.s.}} \mathbb{E}_{q}\left[\frac{h(z) \, \gamma(z)}{q(z)}\right], \]
\[ \begin{aligned} \text{RHS} &= \int h(z) \frac{\gamma(z)}{q(z)} q(z) \, dz \\ &= \int h(z) \, \gamma(z) \, dz \\ &= \int h(z) \, p(z, y) \, dz. \end{aligned} \]
The denominator converges to \(Z = p(y)\):
\[ B_N \xrightarrow{\text{a.s.}} \mathbb{E}_{q}\left[\frac{\gamma(z)}{q(z)}\right], \]
\[ \begin{aligned} \text{RHS} &= \int \frac{\gamma(z)}{q(z)} q(z) \, dz \\ &= \int p(z, y) \, dz \\ &= p(y). \end{aligned} \]
We know \(A_N \to a := \int h(z) \, \gamma(z) \, dz\) and \(B_N \to Z\) separately. Does \(A_N / B_N \to a / Z\)?
Theorem (van der Vaart, 1998, Thm 2.3). Let \(W_1, W_2, \ldots\) and \(W\) be random vectors in \(\mathbb{R}^d\). Let \(g : \mathbb{R}^d \to \mathbb{R}^m\) be continuous at every point of a set \(C\) with \(P(W \in C) = 1\). Then
In words: \(g\) may be discontinuous somewhere, as long as the limit \(W\) lands there with probability zero.
Therefore
\[ \sum_{i=1}^{N} \bar{w}^i \, h(z^i) = g(A_N, B_N) \xrightarrow{\text{a.s.}} g(a, Z) = \frac{\int h(z) \, p(z, y) \, dz}{p(y)} = \int h(z) \, p(z \mid y) \, dz = I. \]
The LLN steps require \(\mathbb{E}_{p(z \mid y)}|h(z)| < \infty\) and \(q(z) > 0\) wherever \(\gamma(z) > 0\).
The denominator \(B_N\) is itself an estimator of \(Z = p(y)\):
\[ \hat{Z} = \frac{1}{N} \sum_{i=1}^{N} w(z^i), \qquad w(z) = \frac{\gamma(z)}{q(z)}. \]
The self-normalized estimate of \(I\) is a ratio, and it is biased for finite \(N\). \(\hat{Z}\) is not a ratio, so it has no such bias.
Adapted from Robert and Casella (2013, Example 3.11), which uses an exponential shifted to start at \(4.5\). Importance sampling can be useful in many settings beyond Bayesian statistics.
Let \(X \sim N(0, 1)\), with density \(\varphi\). Estimate \(P(X > 4.5)\).
Note: we need to choose \(q\) to cover the region of interest (i.e., \(x\) for which \(h(x) \, \varphi(x) > 0\)).

The region \(x > 4.5\) (shaded) has probability \(3.4 \times 10^{-6}\) under \(\varphi\), but 0.64 under \(q\): most proposals from \(q\) land where \(h(x) = 1\).

Estimates of \(P(X > 4.5) = 3.4 \times 10^{-6}\), with fresh samples for each \(N\). Monte Carlo from \(\varphi\) returns exactly \(0\) until \(N\) is large enough to see a single draw above \(4.5\), and is noisy after that: a single hit at \(N = 10^5\) gives \(10^{-5}\), three times the truth. Importance sampling is on target from \(N = 10^3\).
Now, let \(\boldsymbol{z} = (z_1, \ldots, z_T)\) be a \(T\)-dimensional vector.
Goal: compute
\[ I = \int h(z_1, \ldots, z_T) \, p(z_1, \ldots, z_T \mid \boldsymbol{y}) \, dz_1 \cdots dz_T. \]
Idea: propose one dimension at a time from \(z_t \sim q_t(\cdot \mid z_{1:t-1})\) for \(t = 1, \ldots, T\).
Choose a sequence of unnormalized intermediate targets \(\gamma_t(z_{1:t})\), one per dimension, ending at the target of interest: \(\gamma_T = \gamma\).
| Proposal | \(z_t^i \sim q_t(\cdot \mid z_{1:t-1}^i)\) |
| Sample extension | \(z_{1:t}^i = (z_{1:t-1}^i, z_t^i)\) |
| Weight computation | \(w(z_{1:t}^i) = \dfrac{\gamma_t(z_{1:t}^i)}{\nu_t(z_{1:t}^i)}\) |
where \(\nu_{t}(z_{1:t}) = \prod_{s=1}^t q_s(z_s \mid z_{1:s-1})\) is the proposal density of the first \(t\) coordinates, and \(z_{s:t} = (z_s, \ldots, z_t)\) for \(s \le t\).
The superscript \(i = 1, \ldots, N\) indexes the samples, which we call particles. We write \(N\) for the number of particles.
\[ \begin{aligned} z_1 &\sim \mu(z_1) \\ z_t \mid z_{t-1} &\sim f(z_t \mid z_{t-1}) \text{ for } t = 2, \ldots, T \\ y_t \mid z_t &\sim g(y_t \mid z_t) \text{ for } t = 1, \ldots, T. \end{aligned} \]
State-space model: the same graph as in the HMM lecture, but the hidden state \(z_t\) may be continuous. The HMM is the special case with discrete \(z_t \in \{1, \ldots, K\}\). \(\mu\) plays the role of \(\pi\), the initial distribution, \(f\) of the transition matrix \(A\), and \(g\) of the emission distribution \(p(y_t \mid \phi_{z_t})\). The emissions can be discrete or continuous in either case. The SMC literature (e.g., Doucet and Johansen, 2009) often calls a state-space model a (general) hidden Markov model.
The natural intermediate targets use the data seen so far:
\[ \gamma_t(z_{1:t}) = p(z_{1:t}, y_{1:t}) = \mu(z_1) \, g(y_1 \mid z_1) \prod_{s=2}^{t} f(z_s \mid z_{s-1}) \, g(y_s \mid z_s), \qquad \gamma_T = \gamma. \]
\[ \begin{aligned} w(z_{1:t}) &= \frac{\gamma_t(z_{1:t})}{\nu_{t}(z_{1:t})} \\ &= \frac{\gamma_{t-1}(z_{1:t-1})}{\nu_{t-1}(z_{1:t-1})} \frac{f(z_t \mid z_{t-1}) \, g(y_t \mid z_t)}{q_t(z_t \mid z_{1:t-1})} \\ &= w(z_{1:t-1}) \, \alpha(z_{1:t-1}, z_t). \end{aligned} \]
At \(t = 1\): \(w(z_1) = \mu(z_1) \, g(y_1 \mid z_1) / q_1(z_1)\).
Therefore, we should store the weight from the previous iteration and compute only the weight update function \(\alpha(z_{1:t-1}, z_t)\) at the current iteration.
Adapted: \(q_t = p(z_t \mid z_{t-1}, y_t)\). By the Markov property, conditioning on \(z_{1:t-1}\) is the same as conditioning on \(z_{t-1}\).
\[ p(z_t \mid z_{t-1}, y_t) = \frac{g(y_t \mid z_t) \, f(z_t \mid z_{t-1})}{p(y_t \mid z_{t-1})}, \qquad p(y_t \mid z_{t-1}) = \int g(y_t \mid z_t) \, f(z_t \mid z_{t-1}) \, dz_t. \]
Weight update function:
\[ \alpha(z_{1:t-1}, z_t) = \frac{f(z_t \mid z_{t-1}) \, g(y_t \mid z_t)}{p(z_t \mid z_{t-1}, y_t)} = p(y_t \mid z_{t-1}), \]
which does not depend on the newly proposed \(z_t\).
\[ \begin{aligned} z_1 &\sim \mathcal{N}(0, \sigma^2) \\ z_t \mid z_{t-1} &\sim \mathcal{N}(\phi z_{t-1}, \sigma^2), & t &= 2, \ldots, T, \\ y_t \mid z_t &\sim \mathcal{N}(0, \beta^2 \exp(z_t)), & t &= 1, \ldots, T. \end{aligned} \]
In the examples: \(\phi = 1\) (a random walk), \(\sigma = 0.16\), \(\beta = 0.64\), \(T = 100\).

Top: the hidden log-volatility \(z_t\) and the observation variance \(\beta^2 \exp(z_t)\). Bottom: the observations \(y_t\), which spread out where the variance is large.










SIS on the SV model with \(N = 10\) particles and the prior proposal. Each click shows \(t = 1, \ldots, 5\), then \(t = 10, 20, 30, 40, 50\). Top: the observations \(y_{1:t}\) seen so far. Bottom: each coloured line is one particle trajectory \(z_{1:t}^i\); the size of its current point is proportional to the normalized weight \(\bar{w}_t^i\). Red stars: the true hidden states.

Left: one particle’s normalized weight stays near \(1\) (log weight near \(0\)) while the others fall by orders of magnitude. Right: the effective sample size \(\text{ESS}_t = 1 / \sum_i (\bar{w}_t^i)^2\) collapses. It is \(N\) when all weights are equal and \(1\) when one particle has all the weight. Here it towards \(1\) particle.
\(t = 1\), for each \(i = 1, \ldots, N\):
\(t \geq 2\), for each \(i = 1, \ldots, N\) independently:
With the prior proposal \(q_t = f\) (so \(\alpha = g\)) and resampling at every step, this is the bootstrap particle filter (Gordon, Salmond and Smith, 1993).










SMC on the same data with \(N = 10\) particles, the prior proposal, and multinomial resampling at every step. Same frames as for SIS. Blue points: the current particles \(z_t^i\), sized by \(\bar{w}_t^i\). Each segment joins a particle to its parent \(z_{t-1}^{a_{t-1}^i}\). Dark: ancestors of a current particle. Grey: lines that died out. Red: the true hidden states.

Samples and weights can be used to approximate the filtering distribution:
\[ \hat{p}(z_t \mid y_{1:t}) = \sum_{i=1}^{N} \bar{w}_t^i \, \delta_{z_t^i}(z_t) \text{ for } t = 1, \ldots, T, \]
or after resampling:
\[ \hat{p}(z_t \mid y_{1:t}) = \frac{1}{N} \sum_{i=1}^{N} \delta_{z_t^i}(z_t) \text{ for } t = 1, \ldots, T. \]

10,000 particles. At each \(t\), a pointwise 95% credible interval for \(z_t\) from \(\hat{p}(z_t \mid y_{1:t})\). The intervals contain the true \(z_t\) at 88% of the time points.
The generated samples can be used to build a predictive distribution:
\[ p(z_{t+1} \mid y_{1:t}) = \int f(z_{t+1} \mid z_t) \, p(z_t \mid y_{1:t}) \, dz_t. \]
For a fixed value of \(z_{t+1}\), this is an expectation under the filtering distribution with test function \(h(z_t) = f(z_{t+1} \mid z_t)\). Plugging in the particle approximation,
\[ \hat{p}(z_{t+1} \mid y_{1:t}) = \sum_{i=1}^{N} \bar{w}_t^i \, f(z_{t+1} \mid z_t^i), \qquad t = 1, \ldots, T-1, \]
a mixture of \(N\) transition densities. To sample from it: draw \(i \sim \text{Categorical}(\bar{w}_t^{1:N})\), then \(z_{t+1} \sim f(\cdot \mid z_t^i)\).