likelihood weighting = importance sampling
2026-06-22
\[ \newcommand\given{{\,\vert\,}} \]
Learning Objectives
One run by hand worked for one program. Here is the general evaluator: a single recursive function that threads the inference state state (it carries the rng and the running log_w). Four forms are special; everything else is a primitive call.
In your notebook, complete the if case so the evaluator runs. Hint: if is lazy and threads state like every other form, evaluate the test, then evaluate only the branch it selects.
We want \(\mathbb{E}_{p(X \given y)}[r(X)]\). Suppose we can take samples of \(q(X)\) and evaluate its density.
Reweight:
\[\mathbb{E}_{p(X \given y)}[r(X)] = \mathbb{E}_{q(X)}\!\left[ \frac{p(X \given y)}{q(X)}\, r(X) \right], \qquad q = \text{the proposal}\]
\[\mathbb{E}_{q(X)}\!\left[ \frac{p(X \given y)}{q(X)}\, r(X) \right] = \mathbb{E}_{q(X)}\!\left[ \frac{p(X, y)}{p(y)\,q(X)}\, r(X) \right] = \frac{1}{p(y)}\mathbb{E}_{q(X)}\!\left[ \frac{p(X, y)}{q(X)}\, r(X) \right]\]
\[p(y) = \int p(X, y) \, dX = \mathbb{E}_{q(X)}\!\left[\frac{p(X, y)}{q(X)}\right] = \mathbb{E}_{q(X)}\!\left[ \frac{p(X, y)}{q(X)}\mathbf{1}(X) \right]\]
We get
\[\mathbb{E}_{p(X \given y)}[r(X)] =\frac{\mathbb{E}_{q(X)}[W r(X)]}{\mathbb{E}_{q(X)}[W]}.\]
With samples \(X^{(1)},\ldots,X^{(L)} \sim q\):
\[\mathbb{E}_{p(X \given y)}[r(X)] \approx \frac{\sum_{\ell=1}^L W^{(\ell)} r(X^{(\ell)})}{\sum_{\ell=1}^L W^{(\ell)}}.\]
This is called a self-normalized estimate: the unknown normalizer \(p(y)\) is replaced by the sum of the same unnormalized weights.
Different choices of \(r\) give different posterior queries.
\[W = \frac{p(y, X)}{p(X)} = p(y \given X)\]
The name explained
With the prior as proposal, the raw weight is the likelihood of the data, \(W = p(y \given X)\).
The evaluator accumulates the corresponding log weight, \(\log W = \log p(y \given X)\), at observe sites.
Same weighted runs answer any query \(r\), chosen after sampling: \(r(x)=x\) gives the mean, \(r(x)=\mathbb{I}(x > c)\) a tail probability.
The self-normalized estimate is consistent: as \(L \to \infty\) it converges to the true posterior expectation.
Numerically stable: the raw weights \(e^{\log W}\) may underflow to \(0\) or overflow. Shift the log weights by their max \(m = \max_\ell \log W^{(\ell)}\) before exponentiating; \(m\) is a common factor that cancels in the ratio, so the estimate is unchanged.
Likelihood weighting barely works when the prior sits far from the data. Measure how badly, then fix it.
In your notebook, two steps:
mu that makes the ESS much larger. What prior did you choose, and what ESS did you get?A prior centered near the data makes most runs explain the observation, so the weights even out and the ESS climbs toward \(L\). In likelihood weighting the prior is the proposal, and the ESS is a verdict on how well it matches where the data pull. A prior squeezed onto the data is a different model with a different posterior, so the real goal is to propose near the posterior without changing the model.
Likelihood weighting proposes latent values from the prior:
\[ X^{(\ell)} \sim p(X). \]
Then it scores them by the observations:
\[ w^{(\ell)} = p(Y_{\text{obs}} \mid X^{(\ell)}). \]
This works well when prior samples often explain the data.
It struggles when most prior samples are incompatible with the observations: then almost all normalized weight is carried by a few executions.
Likelihood weighting
Run the program forward many times. It is importance sampling with the prior as proposal, so the weight is the likelihood \(p(Y \given X)\) (only observes contribute). Read off any query with \(\sum w f / \sum w\).
Introduction to Probabilistic Programming