KKKF - Kernel Koopman Kalman Filter
KKKF is a Python library that implements kernel Extended Dynamic Mode Decomposition (EDMD) of Koopman operators and provides a non-linear variant of the Kalman Filter. This library is particularly useful for state estimation in dynamical systems with non-linear behavior.
Installation
You can install KKKF using pip:
pip install KKKF
Features
- Kernel-based Extended Dynamic Mode Decomposition (EDMD)
- Non-linear Kalman Filter implementation
- Support for general dynamical systems
- Integration with various kernel functions (e.g., Matérn kernel)
- Robust state estimation with noise handling
Dependencies
- NumPy
- SciPy
- scikit-learn (for kernel functions)
- Matplotlib (for visualization)
Quick Start
Here's a complete example of using KKKF to estimate and visualize states in a SIR (Susceptible-Infected-Recovered) model:
# Dependencies
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from sklearn.gaussian_process.kernels import Matern
from KKKF.DynamicalSystems import DynamicalSystem
from KKKF.kEDMD import KoopmanOperator
from KKKF.applyKKKF import apply_koopman_kalman_filter
# Define system parameters
beta, gamma = 0.12, 0.04
# Define system dynamics
def f(x):
return x + np.array([-beta*x[0]*x[1], beta*x[0]*x[1] - gamma*x[1], gamma*x[1]])
# Define system observations
def g(x):
return np.array([x[1]])
# Setup system dimensions and kernel
N = 300
nx, ny = 3, 1
k = Matern(length_scale=N**(-1/nx), nu=0.5)
# Setup distributions
X_dist = stats.dirichlet(alpha=np.ones(nx))
dyn_dist = stats.multivariate_normal(mean=np.zeros(nx), cov=1e-5*np.eye(3))
obs_dist = stats.multivariate_normal(mean=np.zeros(ny), cov=1e-3*np.eye(1))
# Create dynamical system
dyn = DynamicalSystem(nx, ny, f, g, X_dist, dyn_dist, obs_dist)
# Generate synthetic data
iters = 100
x0 = np.array([0.9, 0.1, 0.0])
x = np.zeros((iters, nx))
y = np.zeros((iters, ny))
x[0] = x0
y[0] = g(x[0]) + obs_dist.rvs()
for i in range(1, iters):
x[i] = f(x[i-1]) + dyn.dist_dyn.rvs()
y[i] = g(x[i]) + obs_dist.rvs()
# Initialize and apply Koopman Kalman Filter
# Prior for the initial condition
x0_prior = np.array([0.8, 0.15, 0.05])
d0 = stats.multivariate_normal(mean=x0_prior, cov=0.1*np.eye(3))
# Koopman operator
Koop = KoopmanOperator(k, dyn)
# Solution
sol = apply_koopman_kalman_filter(Koop, y, d0, N, noise_samples=100)
# Visualization with confidence intervals
conf = np.zeros((iters, nx))
for i in range(iters):
conf[i, :] = np.sqrt(np.diag(sol.Px_plus[i,:,:]))
# 95% confidence interval
err1 = sol.x_plus - 1.96*conf
err2 = sol.x_plus + 1.96*conf
# Plot elements
labels = ["S (True)", "I (True)", "R (True)"]
colors = ["blue", "red", "green"]
plt.plot(sol.x_plus, label=["S (KKF)", "I (KKF)", "R (KKF)"])
for i in range(nx):
plt.fill_between(np.arange(iters), err1[:,i], err2[:,i], alpha=0.6)
plt.scatter(np.arange(iters), x[:,i], label=labels[i], color=colors[i], s=1.4)
plt.xlabel("Days")
plt.ylabel("Propotion of population")
plt.title("KKKF Estimation")
plt.legend()
plt.show()
API Reference
DynamicalSystem
DynamicalSystem(nx, ny, f, g, X_dist, dyn_dist, obs_dist)
Creates a dynamical system with:
nx: State dimensionny: Observation dimensionf: State transition functiong: Observation functionX_dist: State distributiondyn_dist: Dynamic noise distributionobs_dist: Observation noise distribution
KoopmanOperator
KoopmanOperator(kernel, dynamical_system)
Initializes a Koopman operator with:
kernel: Kernel function (e.g., Matérn kernel)dynamical_system: Instance of DynamicalSystem
apply_koopman_kalman_filter
apply_koopman_kalman_filter(koopman, observations, initial_distribution, N, noise_samples=100)
Applies the Koopman-based Kalman filter with:
koopman: KoopmanOperator instanceobservations: Observation datainitial_distribution: Initial state distributionN: Number of samplesnoise_samples: Number of noise samples for uncertainty estimation
Returns a solution object containing:
x_plus: State estimatesPx_plus: Covariance matrices- Additional filter statistics
Visualization
The library supports visualization of results with confidence intervals. The example above demonstrates how to:
- Plot state estimates
- Add confidence intervals (shaded regions)
- Compare with real data (if available)
- Customize plot appearance
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Citation
If you use this library in your research, please cite:
@software{kkkf,
title = {KKKF: Kernel Koopman Kalman Filter},
year = {2024},
author = {Diego Olguín-Wende},
url = {https://github.com/diegoolguinw/KKKF}
}
Metadata
Release files for KKKF 0.11
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 |
|---|---|---|---|---|
| KKKF-0.11.tar.gz | 10.6 kB | Without provenance | Details for KKKF-0.11.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| KKKF-0.11-py3-none-any.whl | Python 3 | none | any | Details for KKKF-0.11-py3-none-any.whl |
Total release size: 23.3 kB
Release files / KKKF-0.11.tar.gz
| Download URL | KKKF-0.11.tar.gz |
|---|---|
| Size | 10.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d7c08085966ccd3214525730997f2a75cf3a0cb4ba8b784210547cca67854568
|
|
BLAKE2b-256 checksum How to use checksums |
6eb15cb22e2527f50741eedea4875bf24808edadc8d2fcd8d2552edcf30751a7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.3
|
Release files / KKKF-0.11-py3-none-any.whl
| Download URL | KKKF-0.11-py3-none-any.whl |
|---|---|
| Size | 12.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d83f0997f9a2f62f2f33c997f5fff177e1a2c2a5ec780785dee5b18b75b34614
|
|
BLAKE2b-256 checksum How to use checksums |
a4eae3e3d2389ab81dbe3cc8e52f51c3ecb443461b1e88ad54c620a7d4a1196e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.3
|
Security for KKKF 0.11
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.