Skip to main content

High-Performance Kalman Filtering and Smoothing

Project description

kalmantv: High-Performance Kalman Filtering and Smoothing in Python

Mohan Wu, Martin Lysy


Description

kalmantv provides a simple Python interface to the time-varying Kalman filtering and smoothing algorithms. Various low-level backends are provided in the following modules:

  • kalmantv.cython: This module performs the underlying linear algebra using the BLAS/LAPACK routines provided by NumPy through a Cython interface. To maximize speed, no input checks are provided. All inputs must be float64 NumPy arrays in Fortran order.

  • kalmantv.eigen: This module uses the C++ Eigen library for linear algebra. The interface is also through Cython. Here again we have the same input requirements and lack of checks. Eigen is known to be faster than most BLAS/LAPACK implementations, but it needs to be compiled properly to achieve maximum performance. In particular this involves linking against an installed version of Eigen (not provided) and setting the right compiler flags for SIMD and OpenMP support. Some defaults are provided in setup.py, but tweaks may be required depending on the user's system.

  • kalmantv.numba: This module once again uses BLAS/LAPACK but the interface is through Numba. Here input checks are performed and the inputs can be in either C or Fortran order, and single or double precision (float32 and float64). However, C ordered arrays are first converted to Fortran order, so the latter is preferable for performance considerations.

  • kalmantv.jax: This module uses the jit-compiled auto-diff Python library Jax.

Installation

git clone https://github.com/mlysy/kalmantv
cd kalmantv
pip install .

Unit Testing

The unit tests are done against the pykalman library to ensure the same results. From inside the root folder of the kalmantv source code:

pip install .[tests]
cd tests
python -m unittest discover -v

Documentation

The HTML documentation can be compiled from the kalmantv root folder:

pip install .[docs]
cd docs
make html

This will create the documentation in docs/build.

Usage

The usage of the library can be demonstrated through this simple example. We use the kalmantv functions from kalmantv.jax.

Suppose we start with given mean and variance of the priror, mu_state_past and var_state_past, our goal is to compute mu_state_filt and var_state_filt at the next time step. We define mu_state_pred and var_state_pred for the prediction step in the Kalman filtering algorithm. More details can be found in the documentation.

import numpy as np
from kalmantv.jax.kalmantv import *
n_meas = 2 # Set the size of the measurement
n_state = 4 # Set the size of the state

# Set initial mu and var of the prior
mu_state_past = np.random.rand(n_state) 
var_state_past = np.random.rand(n_state, n_state)
var_state_past = var_state_past.dot(var_state_past.T) #Ensure positive semidefinite
var_state_past = np.asfortranarray(var_state_past) # must use Fortran order

# Parameters to the Kalman Filter
mu_state = np.random.rand(n_state)
wgt_state = np.random.rand(n_state, n_state).T # converts to Fortran order
var_state = np.random.rand(n_state, n_state)
var_state = var_state.dot(var_state.T).T
x_meas = np.random.rand(n_meas)
mu_meas = np.random.rand(n_meas)
wgt_meas = np.random.rand(n_state, n_meas).T
var_meas = np.random.rand(n_meas, n_meas)
var_meas = var_meas.dot(var_meas.T).T

# Run the filtering algorithm
mu_state_pred, var_state_pred, mu_state_filt, var_state_filt = \
    ktv.filter(mu_state_past, var_state_past,
               mu_state, wgt_state, var_state,
               x_meas, mu_meas, wgt_meas, var_meas)

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

kalmantv-0.2.4.tar.gz (38.7 kB view details)

Uploaded Source

Built Distribution

kalmantv-0.2.4-cp37-cp37m-win_amd64.whl (314.2 kB view details)

Uploaded CPython 3.7m Windows x86-64

File details

Details for the file kalmantv-0.2.4.tar.gz.

File metadata

  • Download URL: kalmantv-0.2.4.tar.gz
  • Upload date:
  • Size: 38.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.5.0.1 requests/2.21.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for kalmantv-0.2.4.tar.gz
Algorithm Hash digest
SHA256 75fae1cea01f64797c1fe1d932139951bee55f7d228b3e298d65e964017d55b7
MD5 50cf2f6e7fdc1b50913bbba0e84fe774
BLAKE2b-256 482c7cebcdf80b1f850507753fdbb4e3a19ff85e0d637e49b568ba06d5877383

See more details on using hashes here.

File details

Details for the file kalmantv-0.2.4-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: kalmantv-0.2.4-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 314.2 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.5.0.1 requests/2.21.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for kalmantv-0.2.4-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 cb272bf9bbb3ad5e9eef183a0ca636d8f1d36aab22078e4f670751e34e6319a1
MD5 da7d9f408d2b0af4c0c479c07048464c
BLAKE2b-256 952a68701ac99f92ed66421dfd70ee426a3cd202c1b5d0bf46fc817c6ac447e3

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