Skip to main content

A pure Python/NumPy library for Bayesian filtering and smoothing

Project description

BayesFilter

BayesFilter is a Python library for Bayesian filtering and smoothing. This library provides tools for implementing Bayesian filters, Rauch-Tung-Striebel smoothers, and other related methods. The only dependency is NumPy.

Installation

To install BayesFilter, just use pip:

pip install bayesfilter

Usage

Basic Structure

The library consists of several modules, each responsible for different parts of the Bayesian filtering and smoothing process:

  • distributions.py: Defines the distribution classes, including the Gaussian distribution used for the filters.
  • filtering.py: Implements the BayesianFilter class, which runs the filtering process.
  • model.py: Contains the StateTransitionModel class for state transitions.
  • observation.py: Defines the Observation class for observation models.
  • smoothing.py: Implements the RTS (Rauch-Tung-Striebel) smoother.
  • unscented.py: Provides functions for the unscented transform.
  • utilities.py: Contains utility functions used throughout the library.
  • test_filtering_smoothing.py: Contains tests for filtering and smoothing.

Example

Here is a basic example of how to set up and run a Bayesian filter with the provided library:

  1. Setup Functions:
def setup_functions():
    def transition_func(x, delta_t_s):
        return np.array([x[0]])
    
    def transition_jacobian_func(x, delta_t_s):
        return np.array([[1.0]])

    def observation_func(x):
        return np.array([np.sin(x[0]), np.cos(x[0])])
    
    def observation_jacobian_func(x):
        return np.array([[np.cos(x[0])], [-np.sin(x[0])]])
    
    return transition_func, transition_jacobian_func, observation_func, observation_jacobian_func
  1. Setup Filter and Observations:
def setup_filter_and_observations():
    rng = np.random.default_rng(0)
    transition_func, transition_jacobian_func, observation_func, observation_jacobian_func = setup_functions()

    transition_model = StateTransitionModel(
        transition_func, 
        1e-8*np.eye(1),
        transition_jacobian_func
    )
    initial_state = Gaussian(np.array([0.0]), np.eye(1))
    filter = BayesianFilter(transition_model, initial_state)

    true_state = np.array([-0.1])
    noise_std = 0.2
    observations = []
    for theta in np.linspace(0, 2*np.pi, 1000):
        observation = observation_func(true_state) + rng.normal(0, noise_std, 2)
        observations.append(Observation(observation, noise_std*np.eye(2), observation_func, observation_jacobian_func))
    return filter, observations, true_state
  1. Run Filter:
def test_filter_noisy_sin(use_jacobian=True):
    filter, observations, true_state = setup_filter_and_observations()
    filter.run(observations, np.linspace(0, 2*np.pi, 1000), 100.0, use_jacobian=use_jacobian)
    np.testing.assert_allclose(filter.state.mean(), true_state, atol=1e-2)

Tests

The library includes a set of tests to ensure the functionality of the filtering and smoothing algorithms. These can be run as follows:

python test_filtering_smoothing.py

Documentation

distributions.py

Defines the Gaussian distribution class used for state representation and propagation.

filtering.py

Implements the BayesianFilter class, responsible for running the filtering process with predict and update steps.

model.py

Contains the StateTransitionModel class, representing the state transition model.

observation.py

Defines the Observation class, representing the observation model.

smoothing.py

Implements the RTS class for Rauch-Tung-Striebel smoothing.

unscented.py

Provides functions for the unscented transform, including unscented_transform and propagate_gaussian.

utilities.py

Contains utility functions like propagate_covariance.

test_filtering_smoothing.py

Includes tests for filtering and smoothing to validate the implementation.

Author

Hugo Hadfield

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bayesfilter-0.0.5.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

bayesfilter-0.0.5-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file bayesfilter-0.0.5.tar.gz.

File metadata

  • Download URL: bayesfilter-0.0.5.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for bayesfilter-0.0.5.tar.gz
Algorithm Hash digest
SHA256 ca949e27e5931b818fefdbce0b6e742b40396e3d64ce557a196b12ac869f3e12
MD5 cb54a8540a98df1c14fb805a3e9ea972
BLAKE2b-256 e2f73261320f2db2883eb501bd66d0f53ceefe2f786fa9ad73c04559a5028850

See more details on using hashes here.

File details

Details for the file bayesfilter-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: bayesfilter-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for bayesfilter-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6bb4ca2a44f0173e873c9560d2ab1f88f39731526036afb1005d5d4cda96794c
MD5 6a2323a86d7adb87134adb4f43f26edd
BLAKE2b-256 7fe704c079f362bd35cf37670038343d5c5ce41ba6834885e3f2c77d0cbb17ef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page