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.
Installation
pip install minkf
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()
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()
Missing observations
If any element of the observation vector is np.nan, the observation is considered missing. Below is the above simple example with some missing observations in the middle.
y_missing = y.copy()
y_missing[40:60] = np.nan
res_missing = kf.run_filter(y_missing, x0, Cest0, M, K, Q, R, likelihood=True)
res_smo_missing = kf.run_smoother(y_missing, x0, Cest0, M, K, Q, R)
plt.figure()
plt.plot(y_missing, 'bo', ms=5)
plt.plot(res_missing['x'], 'k-')
plt.plot(res_smo_missing['x'], 'r-')
plt.grid(True)
plt.show()
samps_missing = kf.sample(res_missing, M, Q, nsamples=10)
plt.figure()
plt.plot(np.array(samps_missing).T, 'r-', alpha=0.2)
plt.plot(y_missing, 'bo', ms=2)
plt.grid(True)
plt.show()
Metadata
Release files for minkf 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| minkf-0.0.5.tar.gz | 5.8 kB | Without provenance | Details for minkf-0.0.5.tar.gz |
Release files / minkf-0.0.5.tar.gz
| Download URL | minkf-0.0.5.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c9376e9d41a4b12893a13da2a13ce07fc3cbeca8f7cb772df22b05e292ca826e
|
|
BLAKE2b-256 checksum How to use checksums |
b49b179ff427d3a4ef675d4ceba40883dfa763820dc4fd8672e2dc4ea30f7f89
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for minkf 0.0.5
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.