Probabilistic inference, Monte Carlo, and posterior computation · Probabilistic computation and inference foundations · INF015 · Algorithm
Sequential Monte Carlo (SMC) and particle filtering
Approximates a sequence of probability distributions using particles, importance weights, and resampling. Particle filtering applies SMC to latent states observed over time;
In CodeSplash
A CodeSplash method runs this entry in Python and R. Set it up from the method's page.
- Particle filter (state-space model with PMMH) Verified in Python and R
Do it in CodeSplash Run it on your own data, in Python or R.
- Availability
- Runs in CodeSplash
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- Verified in Python and R through ts.particle_filter. The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Approximates a sequence of probability distributions using particles, importance weights, and resampling. Particle filtering applies SMC to latent states observed over time; general SMC can instead traverse tempered distributions or growing data targets.
Question / hypotheses
How can a weighted approximation be updated as new observations arrive without solving the entire latent-state posterior from scratch?
Architecture / computational structure
Propagate particles, compute incremental weights, normalize, estimate quantities, and resample when needed. In the bootstrap filter after equal-weight initialization/resampling, using the state transition as proposal leaves only the observation likelihood in incremental weights. Maintain likelihood-normalizer bookkeeping separately from normalized weights.
Assumptions
Proposal support must cover the incremental target, transition/observation densities or appropriate weights must be evaluable, and resampling must follow a valid scheme. Particle count must be adequate for dimension and observation informativeness. Filtering and whole-trajectory smoothing have different degeneracy problems.
Formula / computational definition
Parameters and notation
Model transition and emission parameters are distinct from particles and weights. Algorithm choices include particle count, proposal, resampling threshold/scheme, rejuvenation moves, and precision. The displayed formula is sequential importance weighting without resampling. After ordinary weighted ancestor resampling, ancestor-selection probabilities cancel the old weights; do not multiply them in again.
Fitting, fine-tuning, and adaptation
Update particles online for each new observation under a fixed model. Learning static parameters additionally requires an appropriate estimator, particle MCMC, or another justified procedure; treating a fixed parameter as a nearly noiseless state can degenerate. Changing a neural emission model requires a coherent restart or reweighting strategy.
How to use and calibrate
Monitor weight ESS, repeated-run likelihood estimates, unique ancestors, and filtering error on simulated data. Plot particle clouds and credible bands through time. Increase particles and improve proposals when observations collapse weights; resampling alone cannot create missing modes.
Example application
Track an unobserved dynamical state from noisy sensor readings with nonlinear transitions or non-Gaussian observations, where a basic Kalman filter is inappropriate.
Interpretation / pitfalls
Watch out
ESS computed from weights can recover after resampling while particle diversity remains poor. Products of mean incremental weights estimate likelihood under specified filters; averaging log likelihood estimates is a different operation. Path degeneracy makes long-history smoothing harder than current-state filtering.
Python and R code examples
Scope: One bootstrap-filter observation update and systematic resampling from supplied propagated particles; no multi-time state-space fit.
Implementation route: Native NumPy/base R calculate the component. R pomp provides pfilter for specified process/measurement models. Python implementations can use a model-specific loop; production use needs careful ancestry and likelihood bookkeeping.
Dependencies and validation: Python — executed successfully on the supplied synthetic inputs; imports identify NumPy/SciPy dependencies. R — base R, reviewed but not executed because Rscript is unavailable. Random-number streams differ between languages; matching seed integers do not imply matching draws.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — One bootstrap-filter observation update and systematic resampling from supplied propagated particles; no multi-time state-space fit. · needs numpy
import numpy as np
states=np.array([-.5,0.,.4,.8,1.2]); noise=np.array([.1,-.1,.2,0.,-.1]); x=.9*states+noise
obs=1.; sd=.5; likelihood=np.exp(-.5*((obs-x)/sd)**2)/(sd*np.sqrt(2*np.pi))
w=likelihood/likelihood.sum(); ess=1/np.sum(w*w); estimate=w@x
positions=(np.arange(len(x))+.2)/len(x); ancestors=np.searchsorted(np.cumsum(w),positions)
print('mean',estimate,'ESS',ess,'incremental likelihood',likelihood.mean())
print('resampled states',x[ancestors])
Component demo, not a trained model — One bootstrap-filter observation update and systematic resampling from supplied propagated particles; no multi-time state-space fit.
states <- c(-.5,0,.4,.8,1.2); noise <- c(.1,-.1,.2,0,-.1); x <- .9*states+noise
obs <- 1; sd.obs <- .5; likelihood <- dnorm(obs,x,sd.obs)
w <- likelihood/sum(likelihood); ess <- 1/sum(w*w); estimate <- sum(w*x)
positions <- ((0:(length(x)-1))+.2)/length(x); ancestors <- findInterval(positions,cumsum(w))+1L
print(c(mean=estimate,ESS=ess,incremental.likelihood=mean(likelihood)))
print(x[ancestors])
Sources
- Primary/official source 1stats.ox.ac.uk
- Primary/official source 2kingaa.github.io
From the CodeSplash reference, entry INF015.