State estimation in a linear Gaussian state space model


Minimal working example of state estimation in a linear Gaussian state space model using Kalman filtering and a fully-adapted particle filter. The code estimates the bias and mean squared error (compared with the Kalman estimate) while varying the number of particles in the particle filter.




The Kalman filter is a standard implementation without an input. The particle filter is fully adapted (i.e. takes the current observation into account when proposing new particles and computing the weights).


Returns a plot with the generated observations y and the difference in the state estimates obtained by the Kalman filter (the optimal solution) and the particle filter (with 20 particles). Furthermore, the function returns plots of the estimated bias and mean squared error of the state estimate obtained using the particle filter (while varying the number of particles) and the Kalman estimates.

The function returns a list with the elements:

  • y: The observations generated from the model.

  • x: The states generated from the model.

  • xhatKF: The estimate of the state from the Kalman filter.

  • xhatPF: The estimate of the state from the particle filter with 20 particles.


See Section 4.2 in the reference for more details.


Johan Dahlin <>


Dahlin, J. & Schoen, T. B. "Getting started with particle Metropolis-Hastings for inference in nonlinear dynamical models." pre-print, arXiv:1511.01707, 2015.

Questions? Problems? Suggestions? or email at

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.