Kalman filtering and optimal estimation library
Project description
Bayesian Filters - Kalman filters and other optimal and non-optimal estimation filters in Python
For people new to Kalman filters, they're well explained here https://www.youtube.com/watch?v=IFeCIbljreY (credit for the above image)
Note: This is a personal fork of the original FilterPy library (now renamed to Bayesian Filters). The original project can be found at https://github.com/rlabbe/filterpy
Maintained by George Pearse, Lead MLE at Visia
This library provides Kalman filtering and various related optimal and non-optimal filtering software written in Python. It contains Kalman filters, Extended Kalman filters, Unscented Kalman filters, Kalman smoothers, Least Squares filters, fading memory filters, g-h filters, discrete Bayes, and more.
This is code originally developed in conjunction with the book Kalman and Bayesian Filter in Python.
Design Philosophy
The original author's aim is largely pedagogical - opting for clear code that matches the equations in the relevant texts on a 1-to-1 basis, even when that has a performance cost. There are places where this tradeoff is unclear - for example, writing a small set of equations using linear algebra may be clearer, but NumPy's overhead on small matrices makes it run slower than writing each equation out by hand.
All computations use NumPy and SciPy.
Documentation
Documentation is available at https://georgepearse.github.io/bayesian_filters
Installation
uv pip install bayesian-filters
Basic Usage
Full documentation is at https://georgepearse.github.io/bayesian_filters
Import the filters
import numpy as np
import bayesian_filters as bf
# Or import specific modules
from bayesian_filters.kalman import KalmanFilter
from bayesian_filters.common import Q_discrete_white_noise
Create the filter
my_filter = KalmanFilter(dim_x=2, dim_z=1)
Initialize the filter's matrices
my_filter.x = np.array([[2.],
[0.]]) # initial state (location and velocity)
my_filter.F = np.array([[1.,1.],
[0.,1.]]) # state transition matrix
my_filter.H = np.array([[1.,0.]]) # Measurement function
my_filter.P *= 1000. # covariance matrix
my_filter.R = 5 # state uncertainty
my_filter.Q = Q_discrete_white_noise(dim=2, dt=0.1, var=0.1) # process uncertainty
Run the filter
while True:
my_filter.predict()
my_filter.update(get_some_measurement())
# do something with the output
x = my_filter.x
do_something_amazing(x)
Library Structure
The library is broken up into subdirectories:
gh- g-h filterskalman- Kalman filters (KF, EKF, UKF, etc.)memory- fading memory filtersleastsq- least squares filtersdiscrete_bayes- discrete Bayes filtersstats- statistical functionscommon- common helper functions
Each subdirectory contains Python files relating to that form of filter. The functions and methods contain comprehensive docstrings.
The book Kalman and Bayesian Filters in Python uses this library and is the best place to learn about Kalman filtering and/or this library.
Requirements
This library requires:
- Python 3.9+
- NumPy
- SciPy
- Matplotlib
Testing
All tests are written to work with pytest. Just run:
pytest
The tests include both unit tests and visual verification. Visual plots are often the best way to see how filters are working, as it's easy for a filter to perform within theoretical limits yet be 'off' in some way.
References
Books
The original author uses three main reference texts:
-
Paul Zarchan's 'Fundamentals of Kalman Filtering: A Practical Approach' - Excellent for practical applications rather than theoretical thesis work.
-
Eli Brookner's 'Tracking and Kalman Filtering Made Easy' - An astonishingly good book with its first chapter readable by laypeople. Brookner starts from the g-h filter and shows how all other filters derive from it, including Kalman filters, least squares, and fading memory filters. Also focuses on practical issues like track initialization, noise detection, and tracking multiple objects.
-
Bar-Shalom's 'Estimation with Applications to Tracking and Navigation' - More mathematical than the previous two. Recommended after gaining some background in control theory or optimal estimation. Every sentence is crystal clear with precise language, and abstract mathematical statements are followed with practical explanations.
Online Resources
- Kalman Filter Background - Comprehensive background and theory on Kalman filtering
License
The MIT License (MIT)
Copyright (c) 2015 Roger R. Labbe Jr
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bayesian_filters-1.4.5.tar.gz.
File metadata
- Download URL: bayesian_filters-1.4.5.tar.gz
- Upload date:
- Size: 147.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
93266955ed7ab08fb1180976d39861c5585bebce0cc6e32ed1eebf1c9f19cb0a
|
|
| MD5 |
8a1f0784f210cc28570790d415968d66
|
|
| BLAKE2b-256 |
e420da275a7f24f0cdec09d2e91298a0ec7420c7e17bf7c8d25e34ad2a7073f3
|
File details
Details for the file bayesian_filters-1.4.5-py3-none-any.whl.
File metadata
- Download URL: bayesian_filters-1.4.5-py3-none-any.whl
- Upload date:
- Size: 168.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d9a6e019ad5166a4a12e95220a6dad1a9bd96b1a97ca4b7d363e061a7d8d1fb
|
|
| MD5 |
dcf2397a402abb85a44aa5a46bd62898
|
|
| BLAKE2b-256 |
a6feb02bddcd0dd427cd96843946e51ecd81e4edfc37d3fcbd729dabfbe1bff6
|