Module Reference
Auto-generated documentation from source code docstrings.
Base Estimator
- class tfilterspy.base_estimator.BaseEstimator(name=None)[source]
Bases:
objectBase class for all estimators in TFiltersPy.
Provides sklearn-compatible parameter handling, Dask array conversion, and shared methods for all filter implementations.
- score(X_true)[source]
Negative MSE between filtered states and ground truth.
Returns a negative value so that higher is better (sklearn convention).
State Estimation
State Estimation submodule containing implementations for: - KalmanFilter (linear Gaussian systems) - ExtendedKalmanFilter (nonlinear, Jacobian-based) - UnscentedKalmanFilter (nonlinear, sigma points) - EnsembleKalmanFilter (high-dimensional, Dask-parallel) - ParticleFilter (nonlinear, non-Gaussian)
- class tfilterspy.state_estimation.DaskKalmanFilter(state_transition_matrix, observation_matrix, process_noise_cov, observation_noise_cov, initial_state, initial_covariance, chunk_size=64, estimation_strategy='residual_analysis')[source]
Bases:
KalmanFilterBackward-compatible Kalman Filter wrapper.
Accepts the same constructor signature as the original DaskKalmanFilter and delegates to the new efficient KalmanFilter implementation.
- tfilterspy.state_estimation.DaskNonLinearKalmanFilter
alias of
ExtendedKalmanFilter
- class tfilterspy.state_estimation.DaskParticleFilter(state_transition, observation_model, process_noise_cov, observation_noise_cov, initial_state, num_particles=1000, use_dask=True, estimation_strategy='residual_analysis')[source]
Bases:
ParticleFilterBackward-compatible Particle Filter wrapper.
Accepts the original DaskParticleFilter constructor signature and delegates to the new ParticleFilter implementation.
- class tfilterspy.state_estimation.EnsembleKalmanFilter(f, h, Q, R, x0, P0, n_ensemble=100, use_dask=True)[source]
Bases:
BaseEstimatorEnsemble Kalman Filter for high-dimensional state-space models.
Approximates the Kalman filter using a Monte Carlo ensemble of state realizations. Scales to systems with millions of state variables where storing full covariance matrices is infeasible.
Uses Dask for parallel ensemble member propagation when
use_dask=True.State-space model:
x_{k+1} = f(x_k) + w_k, w_k ~ N(0, Q) z_k = h(x_k) + v_k, v_k ~ N(0, R)
- Parameters:
f (callable) – State transition function
f(x) -> x_next.h (callable) – Observation function
h(x) -> z.Q (ndarray, shape (n_state, n_state)) – Process noise covariance.
R (ndarray, shape (n_obs, n_obs)) – Observation noise covariance.
x0 (ndarray, shape (n_state,)) – Initial state estimate.
P0 (ndarray, shape (n_state, n_state)) – Initial state covariance.
n_ensemble (int, optional) – Number of ensemble members (default 100).
use_dask (bool, optional) – Use Dask for parallel ensemble computation (default True).
Examples
>>> import numpy as np >>> from tfilterspy import EnsembleKalmanFilter >>> def f(x): return x >>> def h(x): return x[:1] >>> enkf = EnsembleKalmanFilter(f, h, ... Q=np.eye(2)*0.1, R=np.eye(1)*0.5, ... x0=np.zeros(2), P0=np.eye(2), n_ensemble=50) >>> enkf.fit(np.random.randn(50, 1)) >>> states = enkf.predict()
- class tfilterspy.state_estimation.ExtendedKalmanFilter(f, h, F_jacobian, H_jacobian, Q, R, x0, P0)[source]
Bases:
BaseEstimatorExtended Kalman Filter for nonlinear state-space models.
Linearizes the dynamics using first-order Taylor expansion (Jacobians) around the current state estimate at each time step.
State-space model:
x_{k+1} = f(x_k) + w_k, w_k ~ N(0, Q) z_k = h(x_k) + v_k, v_k ~ N(0, R)
- Parameters:
f (callable) – State transition function
f(x) -> x_next.h (callable) – Observation function
h(x) -> z.F_jacobian (callable) – Jacobian of f:
F_jacobian(x) -> ndarray (n_state, n_state).H_jacobian (callable) – Jacobian of h:
H_jacobian(x) -> ndarray (n_obs, n_state).Q (ndarray, shape (n_state, n_state)) – Process noise covariance.
R (ndarray, shape (n_obs, n_obs)) – Observation noise covariance.
x0 (ndarray, shape (n_state,)) – Initial state estimate.
P0 (ndarray, shape (n_state, n_state)) – Initial state covariance.
Examples
>>> import numpy as np >>> from tfilterspy import ExtendedKalmanFilter >>> def f(x): return x # identity dynamics >>> def h(x): return x[:1] # observe first state only >>> def Fj(x): return np.eye(2) >>> def Hj(x): return np.array([[1.0, 0.0]]) >>> ekf = ExtendedKalmanFilter(f, h, Fj, Hj, ... Q=np.eye(2)*0.1, R=np.eye(1)*0.5, ... x0=np.zeros(2), P0=np.eye(2)) >>> ekf.fit(np.random.randn(50, 1)) >>> states = ekf.predict()
- class tfilterspy.state_estimation.KalmanFilter(F, H, Q, R, x0, P0, store_covariances=True)[source]
Bases:
BaseEstimatorKalman Filter for linear Gaussian state-space models.
Efficient numpy core with optional Dask support for batch processing. Implements the full sklearn-compatible API: fit / predict / score.
State-space model:
x_{k+1} = F @ x_k + w_k, w_k ~ N(0, Q) z_k = H @ x_k + v_k, v_k ~ N(0, R)
- Parameters:
F (ndarray, shape (n_state, n_state)) – State transition matrix.
H (ndarray, shape (n_obs, n_state)) – Observation matrix mapping state to measurement space.
Q (ndarray, shape (n_state, n_state)) – Process noise covariance.
R (ndarray, shape (n_obs, n_obs)) – Observation noise covariance.
x0 (ndarray, shape (n_state,)) – Initial state estimate.
P0 (ndarray, shape (n_state, n_state)) – Initial state covariance.
store_covariances (bool, optional) – If True (default), stores full covariance history. Set to False to save memory on very long time series (disables smooth()).
Examples
>>> import numpy as np >>> from tfilterspy import KalmanFilter >>> F = np.eye(2); H = np.eye(2) >>> Q = np.eye(2) * 0.1; R = np.eye(2) * 0.5 >>> kf = KalmanFilter(F, H, Q, R, np.zeros(2), np.eye(2)) >>> kf.fit(np.random.randn(100, 2)) >>> states = kf.predict() >>> smoothed, _ = kf.smooth()
- filter_step(z)[source]
Single-step online filtering.
- Parameters:
z (ndarray, shape (n_obs,)) – Single measurement vector.
- Returns:
x (ndarray, shape (n_state,)) – Updated state estimate.
P (ndarray, shape (n_state, n_state)) – Updated covariance.
- fit(X)[source]
Run the Kalman filter forward pass on measurements.
- Parameters:
X (ndarray or dask.array, shape (n_timesteps, n_obs)) – Measurement sequence. 1-D arrays are reshaped to (n, 1).
- Return type:
self
- forecast(n_steps)[source]
Forecast future states beyond the observed data.
- Parameters:
n_steps (int) – Number of steps to forecast.
- Returns:
forecasted_states (ndarray, shape (n_steps, n_state))
forecasted_covs (ndarray, shape (n_steps, n_state, n_state))
- class tfilterspy.state_estimation.ParticleFilter(f, h, Q, R, x0, n_particles=1000, resample_threshold=0.5, use_dask=False)[source]
Bases:
BaseEstimatorSequential Importance Resampling (SIR) Particle Filter.
Represents the posterior distribution using a set of weighted particles. Handles nonlinear, non-Gaussian state-space models where Kalman-based approaches fail.
Supports both callable and matrix-based transition/observation models. When matrices are provided, particle propagation is vectorized for speed.
State-space model:
x_{k+1} = f(x_k) + w_k, w_k ~ N(0, Q) z_k = h(x_k) + v_k, v_k ~ N(0, R)
- Parameters:
f (callable or ndarray) – State transition. If callable:
f(x) -> x_next. If ndarray (n_state, n_state): linear transition matrix.h (callable or ndarray) – Observation model. If callable:
h(x) -> z. If ndarray (n_obs, n_state): linear observation matrix.Q (ndarray, shape (n_state, n_state)) – Process noise covariance.
R (ndarray, shape (n_obs, n_obs)) – Observation noise covariance.
x0 (ndarray, shape (n_state,)) – Initial state estimate.
n_particles (int, optional) – Number of particles (default 1000).
resample_threshold (float, optional) – ESS ratio below which resampling triggers (default 0.5).
use_dask (bool, optional) – Parallelize particle propagation with Dask (default False).
Examples
>>> import numpy as np >>> from tfilterspy import ParticleFilter >>> F = np.eye(2) >>> H = np.eye(2) >>> pf = ParticleFilter(F, H, Q=np.eye(2)*0.1, R=np.eye(2)*0.5, ... x0=np.zeros(2), n_particles=500) >>> pf.fit(np.random.randn(100, 2)) >>> states = pf.predict()
- filter_step(z)[source]
Single-step online particle filter update.
- Parameters:
z (ndarray, shape (n_obs,)) – Single measurement.
- Returns:
state – Weighted mean state estimate.
- Return type:
ndarray, shape (n_state,)
- class tfilterspy.state_estimation.UnscentedKalmanFilter(f, h, Q, R, x0, P0, alpha=0.001, beta=2.0, kappa=0.0)[source]
Bases:
BaseEstimatorUnscented Kalman Filter for nonlinear state-space models.
Uses the unscented transform with sigma points to propagate mean and covariance through nonlinear functions — no Jacobians required.
State-space model:
x_{k+1} = f(x_k) + w_k, w_k ~ N(0, Q) z_k = h(x_k) + v_k, v_k ~ N(0, R)
- Parameters:
f (callable) – State transition function
f(x) -> x_next.h (callable) – Observation function
h(x) -> z.Q (ndarray, shape (n_state, n_state)) – Process noise covariance.
R (ndarray, shape (n_obs, n_obs)) – Observation noise covariance.
x0 (ndarray, shape (n_state,)) – Initial state estimate.
P0 (ndarray, shape (n_state, n_state)) – Initial state covariance.
alpha (float, optional) – Spread of sigma points around the mean (default 1e-3).
beta (float, optional) – Prior distribution parameter; 2 is optimal for Gaussian (default 2.0).
kappa (float, optional) – Secondary scaling parameter (default 0).
Examples
>>> import numpy as np >>> from tfilterspy import UnscentedKalmanFilter >>> def f(x): return x >>> def h(x): return x[:1] >>> ukf = UnscentedKalmanFilter(f, h, ... Q=np.eye(2)*0.1, R=np.eye(1)*0.5, ... x0=np.zeros(2), P0=np.eye(2)) >>> ukf.fit(np.random.randn(50, 1)) >>> states = ukf.predict()
Kalman Filter
- class tfilterspy.state_estimation.linear_filters.DaskKalmanFilter(state_transition_matrix, observation_matrix, process_noise_cov, observation_noise_cov, initial_state, initial_covariance, chunk_size=64, estimation_strategy='residual_analysis')[source]
Bases:
KalmanFilterBackward-compatible Kalman Filter wrapper.
Accepts the same constructor signature as the original DaskKalmanFilter and delegates to the new efficient KalmanFilter implementation.
- class tfilterspy.state_estimation.linear_filters.KalmanFilter(F, H, Q, R, x0, P0, store_covariances=True)[source]
Bases:
BaseEstimatorKalman Filter for linear Gaussian state-space models.
Efficient numpy core with optional Dask support for batch processing. Implements the full sklearn-compatible API: fit / predict / score.
State-space model:
x_{k+1} = F @ x_k + w_k, w_k ~ N(0, Q) z_k = H @ x_k + v_k, v_k ~ N(0, R)
- Parameters:
F (ndarray, shape (n_state, n_state)) – State transition matrix.
H (ndarray, shape (n_obs, n_state)) – Observation matrix mapping state to measurement space.
Q (ndarray, shape (n_state, n_state)) – Process noise covariance.
R (ndarray, shape (n_obs, n_obs)) – Observation noise covariance.
x0 (ndarray, shape (n_state,)) – Initial state estimate.
P0 (ndarray, shape (n_state, n_state)) – Initial state covariance.
store_covariances (bool, optional) – If True (default), stores full covariance history. Set to False to save memory on very long time series (disables smooth()).
Examples
>>> import numpy as np >>> from tfilterspy import KalmanFilter >>> F = np.eye(2); H = np.eye(2) >>> Q = np.eye(2) * 0.1; R = np.eye(2) * 0.5 >>> kf = KalmanFilter(F, H, Q, R, np.zeros(2), np.eye(2)) >>> kf.fit(np.random.randn(100, 2)) >>> states = kf.predict() >>> smoothed, _ = kf.smooth()
- filter_step(z)[source]
Single-step online filtering.
- Parameters:
z (ndarray, shape (n_obs,)) – Single measurement vector.
- Returns:
x (ndarray, shape (n_state,)) – Updated state estimate.
P (ndarray, shape (n_state, n_state)) – Updated covariance.
- fit(X)[source]
Run the Kalman filter forward pass on measurements.
- Parameters:
X (ndarray or dask.array, shape (n_timesteps, n_obs)) – Measurement sequence. 1-D arrays are reshaped to (n, 1).
- Return type:
self
- forecast(n_steps)[source]
Forecast future states beyond the observed data.
- Parameters:
n_steps (int) – Number of steps to forecast.
- Returns:
forecasted_states (ndarray, shape (n_steps, n_state))
forecasted_covs (ndarray, shape (n_steps, n_state, n_state))
Extended Kalman Filter
- class tfilterspy.state_estimation.extended_kalman.ExtendedKalmanFilter(f, h, F_jacobian, H_jacobian, Q, R, x0, P0)[source]
Bases:
BaseEstimatorExtended Kalman Filter for nonlinear state-space models.
Linearizes the dynamics using first-order Taylor expansion (Jacobians) around the current state estimate at each time step.
State-space model:
x_{k+1} = f(x_k) + w_k, w_k ~ N(0, Q) z_k = h(x_k) + v_k, v_k ~ N(0, R)
- Parameters:
f (callable) – State transition function
f(x) -> x_next.h (callable) – Observation function
h(x) -> z.F_jacobian (callable) – Jacobian of f:
F_jacobian(x) -> ndarray (n_state, n_state).H_jacobian (callable) – Jacobian of h:
H_jacobian(x) -> ndarray (n_obs, n_state).Q (ndarray, shape (n_state, n_state)) – Process noise covariance.
R (ndarray, shape (n_obs, n_obs)) – Observation noise covariance.
x0 (ndarray, shape (n_state,)) – Initial state estimate.
P0 (ndarray, shape (n_state, n_state)) – Initial state covariance.
Examples
>>> import numpy as np >>> from tfilterspy import ExtendedKalmanFilter >>> def f(x): return x # identity dynamics >>> def h(x): return x[:1] # observe first state only >>> def Fj(x): return np.eye(2) >>> def Hj(x): return np.array([[1.0, 0.0]]) >>> ekf = ExtendedKalmanFilter(f, h, Fj, Hj, ... Q=np.eye(2)*0.1, R=np.eye(1)*0.5, ... x0=np.zeros(2), P0=np.eye(2)) >>> ekf.fit(np.random.randn(50, 1)) >>> states = ekf.predict()
Unscented Kalman Filter
- class tfilterspy.state_estimation.unscented_kalman.UnscentedKalmanFilter(f, h, Q, R, x0, P0, alpha=0.001, beta=2.0, kappa=0.0)[source]
Bases:
BaseEstimatorUnscented Kalman Filter for nonlinear state-space models.
Uses the unscented transform with sigma points to propagate mean and covariance through nonlinear functions — no Jacobians required.
State-space model:
x_{k+1} = f(x_k) + w_k, w_k ~ N(0, Q) z_k = h(x_k) + v_k, v_k ~ N(0, R)
- Parameters:
f (callable) – State transition function
f(x) -> x_next.h (callable) – Observation function
h(x) -> z.Q (ndarray, shape (n_state, n_state)) – Process noise covariance.
R (ndarray, shape (n_obs, n_obs)) – Observation noise covariance.
x0 (ndarray, shape (n_state,)) – Initial state estimate.
P0 (ndarray, shape (n_state, n_state)) – Initial state covariance.
alpha (float, optional) – Spread of sigma points around the mean (default 1e-3).
beta (float, optional) – Prior distribution parameter; 2 is optimal for Gaussian (default 2.0).
kappa (float, optional) – Secondary scaling parameter (default 0).
Examples
>>> import numpy as np >>> from tfilterspy import UnscentedKalmanFilter >>> def f(x): return x >>> def h(x): return x[:1] >>> ukf = UnscentedKalmanFilter(f, h, ... Q=np.eye(2)*0.1, R=np.eye(1)*0.5, ... x0=np.zeros(2), P0=np.eye(2)) >>> ukf.fit(np.random.randn(50, 1)) >>> states = ukf.predict()
Ensemble Kalman Filter
- class tfilterspy.state_estimation.ensemble_kalman.EnsembleKalmanFilter(f, h, Q, R, x0, P0, n_ensemble=100, use_dask=True)[source]
Bases:
BaseEstimatorEnsemble Kalman Filter for high-dimensional state-space models.
Approximates the Kalman filter using a Monte Carlo ensemble of state realizations. Scales to systems with millions of state variables where storing full covariance matrices is infeasible.
Uses Dask for parallel ensemble member propagation when
use_dask=True.State-space model:
x_{k+1} = f(x_k) + w_k, w_k ~ N(0, Q) z_k = h(x_k) + v_k, v_k ~ N(0, R)
- Parameters:
f (callable) – State transition function
f(x) -> x_next.h (callable) – Observation function
h(x) -> z.Q (ndarray, shape (n_state, n_state)) – Process noise covariance.
R (ndarray, shape (n_obs, n_obs)) – Observation noise covariance.
x0 (ndarray, shape (n_state,)) – Initial state estimate.
P0 (ndarray, shape (n_state, n_state)) – Initial state covariance.
n_ensemble (int, optional) – Number of ensemble members (default 100).
use_dask (bool, optional) – Use Dask for parallel ensemble computation (default True).
Examples
>>> import numpy as np >>> from tfilterspy import EnsembleKalmanFilter >>> def f(x): return x >>> def h(x): return x[:1] >>> enkf = EnsembleKalmanFilter(f, h, ... Q=np.eye(2)*0.1, R=np.eye(1)*0.5, ... x0=np.zeros(2), P0=np.eye(2), n_ensemble=50) >>> enkf.fit(np.random.randn(50, 1)) >>> states = enkf.predict()
Particle Filter
- class tfilterspy.state_estimation.particle_filters.DaskParticleFilter(state_transition, observation_model, process_noise_cov, observation_noise_cov, initial_state, num_particles=1000, use_dask=True, estimation_strategy='residual_analysis')[source]
Bases:
ParticleFilterBackward-compatible Particle Filter wrapper.
Accepts the original DaskParticleFilter constructor signature and delegates to the new ParticleFilter implementation.
- class tfilterspy.state_estimation.particle_filters.ParticleFilter(f, h, Q, R, x0, n_particles=1000, resample_threshold=0.5, use_dask=False)[source]
Bases:
BaseEstimatorSequential Importance Resampling (SIR) Particle Filter.
Represents the posterior distribution using a set of weighted particles. Handles nonlinear, non-Gaussian state-space models where Kalman-based approaches fail.
Supports both callable and matrix-based transition/observation models. When matrices are provided, particle propagation is vectorized for speed.
State-space model:
x_{k+1} = f(x_k) + w_k, w_k ~ N(0, Q) z_k = h(x_k) + v_k, v_k ~ N(0, R)
- Parameters:
f (callable or ndarray) – State transition. If callable:
f(x) -> x_next. If ndarray (n_state, n_state): linear transition matrix.h (callable or ndarray) – Observation model. If callable:
h(x) -> z. If ndarray (n_obs, n_state): linear observation matrix.Q (ndarray, shape (n_state, n_state)) – Process noise covariance.
R (ndarray, shape (n_obs, n_obs)) – Observation noise covariance.
x0 (ndarray, shape (n_state,)) – Initial state estimate.
n_particles (int, optional) – Number of particles (default 1000).
resample_threshold (float, optional) – ESS ratio below which resampling triggers (default 0.5).
use_dask (bool, optional) – Parallelize particle propagation with Dask (default False).
Examples
>>> import numpy as np >>> from tfilterspy import ParticleFilter >>> F = np.eye(2) >>> H = np.eye(2) >>> pf = ParticleFilter(F, H, Q=np.eye(2)*0.1, R=np.eye(2)*0.5, ... x0=np.zeros(2), n_particles=500) >>> pf.fit(np.random.randn(100, 2)) >>> states = pf.predict()
- filter_step(z)[source]
Single-step online particle filter update.
- Parameters:
z (ndarray, shape (n_obs,)) – Single measurement.
- Returns:
state – Weighted mean state estimate.
- Return type:
ndarray, shape (n_state,)
Utilities
- class tfilterspy.utils.optimisation_utils.ParameterEstimator(estimation_strategy: str = 'residual_analysis')[source]
Bases:
BaseEstimatorA class for estimating Kalman Filter parameters, such as process noise covariance (Q) and observation noise covariance (R), using different estimation methods.
Estimation strategies include: - Residual Analysis - Maximum Likelihood Estimation (MLE) - Cross-Validation - Adaptive Filtering
References: - Welch, G., & Bishop, G. (1995). An Introduction to the Kalman Filter. - Haykin, S. (2001). Kalman Filtering and Neural Networks.
- estimate_parameters(measurements: Array)[source]
Estimate the parameters Q and R using the specified estimation strategy.
- estimate_with_adaptive_filtering(measurements: Array) tuple[source]
Estimate Q and R adaptively based on innovations over time.
- estimate_with_cross_validation(measurements: Array, k_folds: int = 5) tuple[source]
Estimate Q and R using k-fold cross-validation.