Skip to main content

An implementation of the Kalman Filter, Kalman Smoother, and EM algorithm in Python

Project description

pykalman

Welcome to pykalman, the dead-simple Kalman Filter, Kalman Smoother, and EM library for Python.

Installation

For a quick installation::

pip install pykalman

Alternatively, you can setup from source:

pip install .

Usage

from pykalman import KalmanFilter
import numpy as np
kf = KalmanFilter(transition_matrices = [[1, 1], [0, 1]], observation_matrices = [[0.1, 0.5], [-0.3, 0.0]])
measurements = np.asarray([[1,0], [0,0], [0,1]])  # 3 observations
kf = kf.em(measurements, n_iter=5)
(filtered_state_means, filtered_state_covariances) = kf.filter(measurements)
(smoothed_state_means, smoothed_state_covariances) = kf.smooth(measurements)

Also included is support for missing measurements:

from numpy import ma
measurements = ma.asarray(measurements)
measurements[1] = ma.masked   # measurement at timestep 1 is unobserved
kf = kf.em(measurements, n_iter=5)
(filtered_state_means, filtered_state_covariances) = kf.filter(measurements)
(smoothed_state_means, smoothed_state_covariances) = kf.smooth(measurements)

And for the non-linear dynamics via the UnscentedKalmanFilter:

from pykalman import UnscentedKalmanFilter
ukf = UnscentedKalmanFilter(lambda x, w: x + np.sin(w), lambda x, v: x + v, transition_covariance=0.1)
(filtered_state_means, filtered_state_covariances) = ukf.filter([0, 1, 2])
(smoothed_state_means, smoothed_state_covariances) = ukf.smooth([0, 1, 2])

And for online state estimation:

 for t in range(1, 3):
    filtered_state_means[t], filtered_state_covariances[t] = \
            kf.filter_update(filtered_state_means[t-1], filtered_state_covariances[t-1], measurements[t])

And for numerically robust "square root" filters

from pykalman.sqrt import CholeskyKalmanFilter, AdditiveUnscentedKalmanFilter
kf = CholeskyKalmanFilter(transition_matrices = [[1, 1], [0, 1]], observation_matrices = [[0.1, 0.5], [-0.3, 0.0]])
ukf = AdditiveUnscentedKalmanFilter(lambda x, w: x + np.sin(w), lambda x, v: x + v, observation_covariance=0.1)

Examples

Examples of all of pykalman's functionality can be found in the scripts in the examples/ folder.

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

pykalman-0.9.7.tar.gz (238.4 kB view details)

Uploaded Source

Built Distribution

pykalman-0.9.7-py2.py3-none-any.whl (251.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pykalman-0.9.7.tar.gz.

File metadata

  • Download URL: pykalman-0.9.7.tar.gz
  • Upload date:
  • Size: 238.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.7

File hashes

Hashes for pykalman-0.9.7.tar.gz
Algorithm Hash digest
SHA256 135779200393c6b370221990f7316740472f541346e9211d11630e2c2d8fb8a0
MD5 3707c30036738b8b79a3de34d2937785
BLAKE2b-256 aa53ed32983187f07487244375771b12e601eb1a0a660abd3e0cd4efb6d12ac7

See more details on using hashes here.

File details

Details for the file pykalman-0.9.7-py2.py3-none-any.whl.

File metadata

  • Download URL: pykalman-0.9.7-py2.py3-none-any.whl
  • Upload date:
  • Size: 251.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.7

File hashes

Hashes for pykalman-0.9.7-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fc80ab0832ded593c9f23a634f7208c4a3775478bc12108d6f8587912212d219
MD5 60267dea083c34b5c629383b01349e00
BLAKE2b-256 94311cf59b266af48a7b2e2891f12f479b333e2726abd6194326c80268587bb8

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