Skip to main content

Kalman filter, nothing more

Project description

minkf - minimal Kalman filter in Python

Kalman filter/smoother, nothing more. A minimal implementation with only numpy dependency. No fancy classes, just simple functions.

Estimates the states of the system

Calculates also the likelihood of the data, in case one wants to do some hyperparameter tuning. One can also sample from the posterior distribution of the states.

Example

The example does some plots via matplotlib, which is not listed as a dependency in order to keep them minimal. Thus, install matplotlib on top of minkf to get the plots working.

The example is about fitting an existing batch of data. For on-line Kalman filtering, check the minkf.kf_predict and minkf.kf_update functions in the package.

For more examples as jupyter notebooks, check the demos folder.

Reconstructing random 1d data

The first example just generates some 1d random walk data and reconstructs it with Kalman filter/smoother. The forward and observation models are just identities. The user can either give the model and error covariance matrices as lists, which enable using different values for each time step. If the matrices are given as constant numpy arrays, the same matrices are used for every time step.

import numpy as np
import minkf as kf
import matplotlib.pyplot as plt

y = np.cumsum(np.random.standard_normal(100))

x0 = np.array([0.0])
Cest0 = 1*np.array([[1.0]])

M = np.array([[1.0]])
K = np.array([[1.0]])
Q = 0.1*np.array([[1.0]])
R = 0.1*np.array([[1.0]])

res = kf.run_filter(y, x0, Cest0, M, K, Q, R, likelihood=True)
res_smo = kf.run_smoother(y, x0, Cest0, M, K, Q, R)

plt.figure()
plt.plot(y, 'bo', ms=5)
plt.plot(res['x'], 'k-')
plt.plot(res_smo['x'], 'r-')
plt.grid(True)
plt.show()

simple_demo

Result is a dict that contains the estimated states and the filtering/smoothing covariances. If likelihood=True is chosen in the filter, the result structure also contains the log-likelihood of the data given the model. This can be useful in, e.g., estimating hyperparameters: one could run the Kalman filter repeatedly via an optimizer or MCMC sampler, for instance.

res['loglike']
297.0302824838724

Sampling from the posterior of the states given all the data can be done via the sample function. Sampling needs the Kalman filter results and the dynamics model matrix and model error covariance.

samps = kf.sample(res, M, Q, nsamples=10)

plt.figure()
plt.plot(np.array(samps).T, 'r-', alpha=0.2)
plt.plot(y, 'bo', ms=2)
plt.grid(True)
plt.show()

simple_demo_samps

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

minkf-0.0.1.tar.gz (5.1 kB view details)

Uploaded Source

File details

Details for the file minkf-0.0.1.tar.gz.

File metadata

  • Download URL: minkf-0.0.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/3.10.0 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for minkf-0.0.1.tar.gz
Algorithm Hash digest
SHA256 adfadcc65f754bcec006229d6f512b319cea7e68bcbb8dd00557275dd49d77a2
MD5 2c3bf41097e28a7613954f082f9686ed
BLAKE2b-256 9cc536478c4de1d10722db64687c0ec78468ae9f2e0e24f2091a686c462c0e48

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