nchopin / particles

Sequential Monte Carlo in python
MIT License
416 stars 76 forks source link
bayesian-inference kalman-filter particle-filter pmcmc quasi-monte-carlo sequential-monte-carlo smc2

logo

particles

Sequential Monte Carlo in python.

Motivation

This package was developed to complement the following book:

An introduction to Sequential Monte Carlo

by Nicolas Chopin and Omiros Papaspiliopoulos.

It now also implements algorithms and methods introduced after the book was published, see below.

Features

Example

Here is how you may define a parametric state-space model:

import particles
import particles.state_space_models as ssm
import particles.distributions as dists

class ToySSM(ssm.StateSpaceModel):
    def PX0(self):  # Distribution of X_0 
        return dists.Normal()  # X_0 ~ N(0, 1)
    def PX(self, t, xp):  # Distribution of X_t given X_{t-1}
        return dists.Normal(loc=xp)  # X_t ~ N( X_{t-1}, 1)
    def PY(self, t, xp, x):  # Distribution of Y_t given X_t (and X_{t-1}) 
        return dists.Normal(loc=x, scale=self.sigma)  # Y_t ~ N(X_t, sigma^2)

You may now choose a particular model within this class, and simulate data from it:

my_model = ToySSM(sigma=0.2)
x, y = my_model.simulate(200)  # sample size is 200

To run a bootstrap particle filter for this model and data y, simply do:

alg = particles.SMC(fk=ssm.Bootstrap(ssm=my_model, data=y), N=200)
alg.run()

That's it! Head to the documentation for more examples, explanations, and installation instructions. (It is strongly recommended to start with the tutorials, to get a general idea on what you can do, and how.)

Who do I talk to?

Nicolas Chopin (nicolas.chopin@ensae.fr) is the main author, contributor, and person to blame if things do not work as expected.

Bug reports, feature requests, questions, rants, etc are welcome, preferably on the github page.