Skip to main content

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.

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]
tox

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. Here we use the KalmanTV class from the kalmantv.cython module. The same class is defined in kalmantv.eigen and kalmantv.numba with exactly the same methods and signatures.

Suppose we start with given mean and variance of the state variable 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.cython import KalmanTV
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 state variable
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

# Initialize the KalmanTV class
ktv = KalmanTV(n_meas, n_state)

# Allocate memory for storing the output
mu_state_pred = np.empty(n_state)
var_state_pred = np.empty((n_state, n_state), order='F')
mu_state_filt = np.empty(n_state)
var_state_filt = np.empty((n_state, n_state), order='F')

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

Metadata

Release files for kalmantv 0.2.4.9000

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kalmantv 0.2.4.9000
File Size Uploaded Provenance Actions
kalmantv-0.2.4.9000.tar.gz 419.7 kB Without provenance Details for kalmantv-0.2.4.9000.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kalmantv 0.2.4.9000
File Interpreter ABI Platform Actions
kalmantv-0.2.4.9000-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details for kalmantv-0.2.4.9000-cp38-cp38-win_amd64.whl

Total release size: 725.9 kB

Release files / kalmantv-0.2.4.9000.tar.gz

Download URL kalmantv-0.2.4.9000.tar.gz
Size 419.7 kB
Tags Source
SHA-256 checksum
How to use checksums
933972686fd708460c2858c86ed22225d553b47eb9cca7bed623ba200e569f7e
BLAKE2b-256 checksum
How to use checksums
a0a0866c42782c832c003b2e27c5d99369c65fac2fd03389bc3c085b4fe8ac7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.8.12

Release files / kalmantv-0.2.4.9000-cp38-cp38-win_amd64.whl

Download URL kalmantv-0.2.4.9000-cp38-cp38-win_amd64.whl
Size 306.2 kB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
73e29e4b526160a809154d92aeebd9dda2d39a5cd8e42c9cc7ed01fdc949914b
BLAKE2b-256 checksum
How to use checksums
9f13011675c000783d9fb16aa514cc875a5e97543081519b9b1ff584a046e55d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.8.12

Security for kalmantv 0.2.4.9000

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page