Skip to the lesson
FixTheRobot Explore Robotics / Probability for Robotics / M9 Particle Filters and Monte Carlo Localization / M9-T1

Probability for Robotics · Theory · M9

M9-T1 — Monte Carlo Belief and Importance Sampling

When you cannot sample from the belief you want, sample from one you can, and correct with weights.

  • Lesson 1 of 4
  • Intermediate
  • Module 9: Particle Filters and Monte Carlo Localization
  • Theory
  • About 13 minutes to read

A particle filter represents a belief by a cloud of samples. The step that makes this work for a posterior known only up to a constant, prior times likelihood, is importance sampling: draw from an easy distribution, and give each sample a weight that corrects for the difference. This lesson derives the method, shows the 1/√N error of Monte Carlo, and introduces the effective sample size, the number that tells how many samples are really doing the work.

Learning objectives

After this lesson, you should be able to:

  • Approximate an expectation by a Monte Carlo average and state how its error scales with the number of samples.
  • Derive importance sampling and self-normalised weights for a target known up to a constant.
  • Compute the effective sample size and explain why a poor proposal wastes samples.
  • Choose a proposal and recognise the choice made by the bootstrap particle filter.

The intuition

The Bayes filter of module 6 multiplies a prior by a likelihood and normalises. On a grid that is easy in one dimension and impossible in six. A Gaussian filter does it in closed form but only for Gaussians. A particle filter takes a third route: represent the belief by samples and do the multiplication with weights.

Suppose a robot's prior belief is N(0, 2²) m and one reading, z = 3 m with 0.5 m of noise, arrives. The posterior is the prior times the likelihood, narrow and centred near 2.8 m. We cannot sample from it directly (in general we can only evaluate prior times likelihood at a point). But we can sample from the prior, and for each sample ask how well it explains the reading. A sample at 0.1 m explains it very badly; one at 2.9 m explains it very well. Give each sample a weight equal to its likelihood, and the weighted cloud is the posterior.

Importance sampling: samples from the prior, weighted to represent the posterior; and the effective sample size of four proposalsLeft: a wide bell curve for the prior centred at 0, a narrow tall bell curve for the posterior centred at 2.82, and 100 circles at the sample positions on the baseline, mostly spread between -4 and 4, with large circles only near 2 to 4 where the posterior is. Right: bars of the effective sample size out of 100 for four proposals: prior 12.4, wide 11.8, narrow 2.2 and a good proposal near the posterior 93.7.-6-4-2024600.2120.4250.6370.85position x (m)density orange: prior (the proposal)blue: posterior (the target)circle area ∝ importance weight 012340255075100effective sample size (of 100)prior12.4wide11.8narrow2.2good93.7
Left: 100 samples from the prior (orange curve), drawn with an area proportional to their weight; they represent the posterior (blue curve). Right: the effective sample size of four proposals, out of 100.

The left panel shows the result with 100 samples. Most of the prior samples are wasted: they lie where the posterior has no mass, and their weights are almost zero. A dozen or so samples near 2 to 4 m carry the answer. The effective sample size counts these: 12.4 of 100 for the prior proposal. The right panel shows how the choice of the proposal changes it: a proposal that is a good guess of the posterior (a Gaussian at 2.8 m, 0.6 m wide) has 93.7, a proposal that is too narrow and in the wrong place (0.5 m around the origin) has 2.2: the whole answer rests on two samples.

Despite the waste, the estimate is good: the posterior mean from 100 prior samples is within 0.11 m of the true 2.82 m, and with 10 000 the error is 1 cm. The error falls as 1/√N, for any dimension: that is why Monte Carlo is the method of choice where grids fail.

Importance sampling turns the problem of sampling from a distribution you can only evaluate into the problem of sampling from one you can draw from, at the price of weights. The closer the proposal is to the target, the less is wasted.

Definitions

Monte Carlo estimate
E[f(X)]≈1N∑if(xi)\mathbb{E}[f(X)] \approx \frac1N\sum_i f(x_i) with xix_i drawn from the distribution.
Target pp, proposal qq
The distribution we want to represent, and the one we sample from. qq must be positive wherever pp is.
Importance weight wiw_i
p(xi)/q(xi)p(x_i)/q(x_i): how much more (or less) the target likes the sample than the proposal did.
Self-normalised weights w~i\tilde w_i
wi/∑jwjw_i/\sum_j w_j: work when pp is known only up to a constant, as a posterior is.
Effective sample size
Neff=1/∑iw~i2N_\mathrm{eff} = 1/\sum_i\tilde w_i^2: NN for equal weights and 1 when one sample has all the weight.
Bootstrap choice
The proposal is the prior (the motion model) and the weight the likelihood: wi=p(z∣xi)w_i = p(z\mid x_i). Simple, and what the standard particle filter does.
Self-normalised importance sampling
Ep[f]=∫f(x)p(x)q(x) q(x) dx;≈;∑i=1Nw~i f(xi),xi∼q,w~i=p(xi)/q(xi)∑jp(xj)/q(xj)\mathbb{E}_p[f] = \int f(x)\frac{p(x)}{q(x)}\,q(x)\,dx ;\approx; \sum_{i=1}^N\tilde w_i\,f(x_i),\qquad x_i\sim q,\quad \tilde w_i=\frac{p(x_i)/q(x_i)}{\sum_j p(x_j)/q(x_j)}
The unknown normalising constant of p cancels in the ratio of weights.
Effective sample size
Neff=1∑iw~i2;≈;N1+Varq(w)/wˉ2N_\mathrm{eff} = \frac{1}{\sum_i\tilde w_i^2};\approx;\frac{N}{1 + \mathrm{Var}_q(w)/\bar w^2}
The second form says the effective number falls with the relative variance of the weights.
Error scaling
rms error;∝;1Neff\mathrm{rms\ error};\propto;\frac{1}{\sqrt{N_\mathrm{eff}}}
Monte Carlo error falls as the inverse square root of the effective number of samples, independent of the dimension.

Derivation: why the weighted samples represent p

Monte Carlo error

  1. μ^=1N∑if(xi),xi independent\hat\mu = \frac1N\sum_i f(x_i),\quad x_i\ \text{independent}

    The average of N independent copies of f(X)f(X).

  2. E[μ^]=μ,Var(μ^)=Var(f)N\mathbb{E}[\hat\mu]=\mu,\qquad \mathrm{Var}(\hat\mu)=\frac{\mathrm{Var}(f)}{N}

    The estimate is unbiased, and the variance of an average of independent terms is the variance of one divided by N (the variance lesson of module 4).

  3. std(μ^)=σfN\mathrm{std}(\hat\mu)=\frac{\sigma_f}{\sqrt N}

    Hence the 1/√N: to halve the error, take four times as many samples. The dimension of x does not appear.

Importance sampling

  1. Ep[f]=∫f(x) p(x) dx=∫f(x) p(x)q(x) q(x) dx=Eq ⁣[f(X) w(X)]\mathbb{E}_p[f]=\int f(x)\,p(x)\,dx=\int f(x)\,\frac{p(x)}{q(x)}\,q(x)\,dx=\mathbb{E}_q\!\left[f(X)\,w(X)\right]

    Multiply and divide by qq (needs q>0q>0 wherever p>0p>0). The expectation under p becomes one under q of the weighted function.

  2. Ep[f]≈1N∑iwif(xi)\mathbb{E}_p[f]\approx\frac1N\sum_i w_i f(x_i)

    The Monte Carlo average of the weighted function over samples from q. Unbiased when p is normalised.

  3. Ep[f]=Eq[w f]Eq[w]≈∑iwif(xi)∑iwi\mathbb{E}_p[f]=\frac{\mathbb{E}_q[w\,f]}{\mathbb{E}_q[w]}\approx\frac{\sum_i w_if(x_i)}{\sum_i w_i}

    If p is known only up to a constant, p=p~/Zp = \tilde p/Z, use p~/q\tilde p/q as the weight: ZZ appears in numerator and denominator and cancels. Slightly biased for finite N, consistent as N grows.

The effective sample size

The variance of the weighted estimate is governed by the variance of the weights. For equal weights w~i=1/N\tilde w_i = 1/N the sum ∑w~i2=1/N\sum\tilde w_i^2 = 1/N and Neff=NN_\mathrm{eff}=N. If one weight is 1 and the others 0, Neff=1N_\mathrm{eff}=1. In between, NeffN_\mathrm{eff} is the number of equally weighted samples that would give the same variance (to a good approximation).

The bootstrap choice

For the posterior p(x∣z)∝p(z∣x) p(x)p(x\mid z)\propto p(z\mid x)\,p(x) with the prior as proposal, w∝p(z∣x)p(x)/p(x)=p(z∣x)w\propto p(z\mid x)p(x)/p(x) = p(z\mid x): the weight is simply the likelihood. No density of the prior is ever needed, only the ability to sample it, which is why the motion model of a robot (easy to sample, hard to write as a density) fits.

The importance weight of a prior sample is its likelihood. The prior samples are where the robot might be; the weights say how well each place explains the reading.

A worked example

The posterior of the figure: prior N(0, 2²), one reading z = 3 with 0.5 m of noise. Exactly, the posterior has mean 2.8235 and standard deviation 0.485 (fusion of Gaussians). We estimate its mean by importance sampling with four proposals, 500 repetitions of N = 100 samples each, and then study the error against N.

  1. Proposals. The prior N(0, 2) (the bootstrap choice) gives an rms error of 0.114 m and an effective sample size of 12.4. The wider N(0, 5) is no better: 0.109 and 11.8 (it also wastes samples far out). The narrow N(0, 0.5) is badly wrong: 1.63 m and 2.2, since almost no sample reaches the posterior. The good proposal N(2.8, 0.6) gives 0.043 m and 93.7.
  2. Number of samples. With the prior as proposal, N = 10, 100, 1 000 and 10 000 give rms errors of 0.67, 0.109, 0.031 and 0.011 m. Multiplying by √N gives 2.1, 1.09, 0.97 and 1.07: the error falls as 1/√N, as the derivation says (the first value is high because with ten samples and an effective size of 1.8 the self-normalised estimate is poor).
  3. Effective size. The effective sample size is about 12% of N at every N (12.3 of 100, 121 of 1 000, 1 211 of 10 000): the waste is a fixed fraction, set by how much narrower the posterior is than the proposal.
import math
from robotprob import Rng, gaussian_pdf, normalize_weights, effective_sample_size, mean

# target: the posterior of a robot position with prior N(0, 2^2) and one reading z = 3 from a sensor with noise 0.5
prior_sd, z, noise = 2.0, 3.0, 0.5
unnorm = lambda x: gaussian_pdf(x, 0.0, prior_sd) * gaussian_pdf(z, x, noise)      # prior times likelihood: known up to a constant
prec = 1 / prior_sd**2 + 1 / noise**2
exact_mean, exact_sd = (z / noise**2) / prec, math.sqrt(1 / prec)
print('exact posterior: mean', round(exact_mean, 4), ' std', round(exact_sd, 4))

def estimate(rng, n, q_mean, q_sd):
    xs = [rng.gauss(q_mean, q_sd) for _ in range(n)]
    w = [unnorm(x) / gaussian_pdf(x, q_mean, q_sd) for x in xs]       # importance weights p / q
    w = normalize_weights(w)
    return sum(wi * xi for wi, xi in zip(w, xs)), effective_sample_size(w)

print('proposal q            N     rms error of the mean   mean ESS')
for name, qm, qs in [('prior N(0, 2)', 0.0, 2.0), ('wide N(0, 5)', 0.0, 5.0), ('narrow N(0, 0.5)', 0.0, 0.5), ('good N(2.8, 0.6)', 2.8, 0.6)]:
    rng = Rng(3)
    for n in (100,):
        res = [estimate(rng, n, qm, qs) for _ in range(500)]
        rms = math.sqrt(mean([(m - exact_mean) ** 2 for m, e in res]))
        print(name.ljust(20), str(n).rjust(5), str(round(rms, 4)).rjust(16), str(round(mean([e for m, e in res]), 1)).rjust(14))

print('error against the number of samples (proposal = the prior):')
rng = Rng(4)
for n in (10, 100, 1000, 10000):
    reps = 200 if n < 10000 else 50
    res = [estimate(rng, n, 0.0, prior_sd) for _ in range(reps)]
    rms = math.sqrt(mean([(m - exact_mean) ** 2 for m, e in res]))
    print(str(n).rjust(7), ' rms error', str(round(rms, 4)).rjust(7), ' rms * sqrt(N)', str(round(rms * math.sqrt(n), 2)).rjust(6), ' mean ESS', round(mean([e for m, e in res]), 1))
exact posterior: mean 2.8235  std 0.4851
proposal q            N     rms error of the mean   mean ESS
prior N(0, 2)          100           0.1142           12.4
wide N(0, 5)           100           0.1092           11.8
narrow N(0, 0.5)       100           1.6279            2.2
good N(2.8, 0.6)       100           0.0429           93.7
error against the number of samples (proposal = the prior):
     10  rms error   0.674  rms * sqrt(N)   2.13  mean ESS 1.8
    100  rms error  0.1094  rms * sqrt(N)   1.09  mean ESS 12.3
   1000  rms error  0.0306  rms * sqrt(N)   0.97  mean ESS 121.4
  10000  rms error  0.0107  rms * sqrt(N)   1.07  mean ESS 1210.7
normalize_weights and effective_sample_size are the two helpers; the weights are target over proposal, with the target known only up to a constant (prior times likelihood).

In robotics

Particle filters

The localisation filter of the next lessons is this method applied at every step: the belief at time t−1 is a set of weighted samples; each is moved by the motion model (the proposal, so it is the prior), weighted by the likelihood of the new reading, and the set is resampled so that the weights return to equal. The Navigation course shows the same filter at work in its localisation lessons.

Monte Carlo in robotics beyond filtering

  • Estimating the probability of a collision along a path with uncertain obstacles.
  • Expected cost in model-predictive control by sampling noise trajectories.
  • Evaluating the risk of a plan under a belief (the sample average of the cost).
  • Bayesian inference of parameters when the posterior has no closed form.

Choosing and checking the proposal

The prior is the easiest proposal and wastes samples when the likelihood is sharper than the prior (an accurate sensor): the effective size falls. Better proposals mix in the measurement (the “optimal” proposal and its approximations, Module 9 lesson 2) at the price of a more complicated derivation. Always monitor the effective sample size: a value near 1 means the filter has effectively collapsed.

Heavy tails

The proposal should have heavier tails than the target. A proposal that is too narrow (the 0.5 m one) misses the regions where the target has mass, with no warning: the weights of the samples you do have are fine, but the estimate is badly biased. The failure is silent, which is one reason to use broad priors, to inject random samples, and to compare against the sensor.

Common misconceptions

“Samples from the prior are samples from the posterior.”

They are samples from the prior. The weights turn them into a representation of the posterior; without the weights they carry no information from the measurement.

“A million samples is always enough.”

The error depends on the effective sample size. If the posterior is a tiny part of the proposal, a million samples may leave a few effective ones.

“Importance sampling gives exact answers if you normalise.”

The self-normalised estimator has a small bias that vanishes as N grows, and a variance set by the weights. It is consistent, not exact.

“The proposal does not matter as long as it covers the target.”

It matters through the variance of the weights: the good proposal had an effective size of 94 and the narrow one of 2, with the same number of samples.

Key takeaways

  1. A Monte Carlo average of N independent samples is unbiased with an error that falls as 1/√N, whatever the dimension.
  2. Importance sampling draws from a proposal q and weights by p/q. Self-normalised weights work when p is known only up to a constant.
  3. The effective sample size 1/Σw̃² counts the samples doing the work. It falls when the proposal is far from the target.
  4. In the bootstrap choice the proposal is the prior (the motion model) and the weight is the likelihood.
  5. Heavy-tailed, broad proposals are safe; narrow ones in the wrong place fail silently.

References and further reading

Each source is marked free or paid. Free to read does not mean free to reuse: the course resources page lists every source with its licence.

Knowledge check

Three questions. Write an answer, then reveal the explanation. Your answers stay in your browser.

Question 1

Three samples from a proposal have unnormalised weights 0.1, 0.1 and 0.8. Compute the normalised weights and the effective sample size, and say what the weights suggest about the proposal.

Explanation

The sum is 1.0, so the normalised weights are 0.1, 0.1, 0.8. The sum of squares is 0.01 + 0.01 + 0.64 = 0.66, so Neff = 1/0.66 = 1.52 out of 3. One sample carries most of the weight, so the proposal is a poor match to the target: only one of the three samples landed where the target has most of its mass. A better proposal, or many more samples, is needed.

Question 2

You want a Monte Carlo estimate with an error of 0.01 and a pilot run with 400 samples gives an error of 0.05. How many samples are needed, assuming the 1/√N law?

Explanation

The error scales as 1/√N, so to reduce it by a factor of 5 we need 25 times as many samples: 400 × 25 = 10 000. (Halving the error needs four times the samples, so five times less needs 25.) If a better proposal could double the effective sample size, the same accuracy would need half as many samples, which is often a cheaper route than brute force.

Question 3

Why can the bootstrap particle filter use a motion model that can be sampled but whose density cannot be written down, and what does it need from the sensor model?

Explanation

With the prior as proposal the weight is p(z | x), the likelihood, and the density of the prior cancels: only samples from the motion model are needed, not its density. This allows simulation-based motion models (a physics engine, a noisy velocity model with complex noise) to be used directly. What the filter needs from the sensor model is the ability to evaluate the likelihood of a reading for a given state, up to a constant.

What’s next

Now the algorithm: apply importance sampling at every time step, resample to keep the weights usable, and see the particle filter localise a robot.

Next lesson

M9-T2 — The Particle Filter Algorithm

Derive the sequential importance resampling filter from the Bayes filter and implement its three steps.

Nothing below is required to finish this lesson.

Other courses on this site treat neighbouring topics from their own angle: