Giter Club home page Giter Club logo

pyfilter's Introduction

About the project

pyfilter is a package designed for joint parameter and state inference in state space models using particle filters and particle filter based inference algorithms. It's borne out of my layman's interest in Sequential Monte Carlo methods, and a continuation of my Master's thesis.

Some features include:

  1. Particle filters in the form of SISR and APF together with different proposal distributions.
  2. Both online and offline inference algorithms such as
    1. SMC2
    2. NESS
    3. SMC2FW
    4. PMMH
    5. Variational inference targeting parameters coupled with particle filters for estimating log-likelihood.
  3. pytorch backend enables GPU accelerated inference - what took hours on a CPU now takes minutes (or even seconds).

Getting started

Follow the below instructions in order to get started with pyfilter.

Prerequisites

Start by installing pytorch.

Installation

To install pyfilter just copy the below command into your favourite terminal

pip install git+https://github.com/tingiskhan/pyfilter

Usage

All examples are located here, but you'll find a short one below in which we define a sine diffusion process which we observe with some noise, and then back out using the APF together with the "optimal proposal" distribution

from stochproc import timeseries as ts
import torch
from pyro.distributions import Normal
import matplotlib.pyplot as plt
from pyfilter.filters.particle import APF, proposals
from math import sqrt


def f(x, gamma, sigma):
    return torch.sin(x.value - gamma), sigma


def build_observation(x, a, s):
    return Normal(loc=a * x.value, scale=s)


def initial_kernel(gamma, sigma):
    return Normal(torch.zeros_like(gamma), torch.ones_like(gamma))


dt = 0.1

gamma = 0.0
sigma = 1.0

inc_dist = Normal(loc=0.0, scale=sqrt(dt))
sine_diffusion = ts.AffineEulerMaruyama(f, (gamma, sigma), inc_dist, initial_kernel=initial_kernel, dt=dt)

a = 1.0
s = 0.1

ssm = ts.StateSpaceModel(sine_diffusion, build_observation, (a, s))

sample_result = ssm.sample_states(250)
x, y = sample_result.get_paths()

fig, ax = plt.subplots()

ax.set_title("Latent")
ax.plot(sample_result.time_indexes, x, label="True", color="gray")
ax.plot(sample_result.time_indexes, y, marker="o", linestyle="None", label="Observed", color="lightblue")

filt = APF(ssm, 250, proposal=proposals.LinearGaussianObservations(0))
result = filt.batch_filter(y)

ax.plot(sample_result.time_indexes, result.filter_means.numpy()[1:], label="Filtered", color="salmon", alpha=0.5)
ax.legend()
Logo

Contributing

Contributions are always welcome! Simply

  1. Fork the project.
  2. Create your feature branch (I try to follow Microsoft's naming).
  3. Push the branch to origin.
  4. Open a pull request.

License

Distributed under the MIT License, see LICENSE for more information.

Contact

Contact details are located under setup.py.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.