Kalman Filter & Unscented Kalman Filter¶
This module provides generic Kalman and unscented Kalman filter implementations built on an abstract state-space model.
It is influenced by the implementation of the State-Space model & Kalman filter in the Python particles library.
A state-space model separates a time series into two layers:
- A latent state \(x_t\) that carries the unobserved dynamics.
- An observation \(y_t\) that is a (noisy) function of the current state.
See the kalman filter theory page for more details on the algorithms and their applications.
quantflow.ta.kalman.StateSpaceModel
pydantic-model
¶
Bases: BaseModel, ABC
Generic state-space model
where \(p_x\) is the transition distribution of the latent state \(x_t\) and \(p_y\) is the observation distribution of the observable \(y_t\) given \(x_t\).
get_px0
abstractmethod
¶
get_px
abstractmethod
¶
Distribution \(p_x\left(x_t \mid x_{t-1}\right)\) of \(x_t\) given \(x_{t-1} = x_p\).
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Time index \(t\).
TYPE:
|
xp
|
State at time \(t-1\).
TYPE:
|
Source code in quantflow/ta/kalman.py
get_py
abstractmethod
¶
Distribution of \(y_t\) given \(x_{t-1}=x_p\) and \(x_t=x\).
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Time index \(t\).
TYPE:
|
xp
|
State at time \(t-1\).
TYPE:
|
x
|
State at time \(t\).
TYPE:
|
Source code in quantflow/ta/kalman.py
simulate_given_x
¶
Simulate observations given a state trajectory.
Returns \([y_0, \ldots, y_{T-1}]\) where \(y_t \sim\) get_py\((t, x_{t-1}, x_t)\).
| PARAMETER | DESCRIPTION |
|---|---|
x
|
State trajectory \(x_0, \ldots, x_{T-1}\).
TYPE:
|
Source code in quantflow/ta/kalman.py
simulate
¶
Simulate state and observation trajectories of length \(T\).
Returns a tuple of two lists: the state trajectory \([x_0, \ldots, x_{T-1}]\) and the observation trajectory \([y_0, \ldots, y_{T-1}]\).
| PARAMETER | DESCRIPTION |
|---|---|
T
|
Number of time steps.
TYPE:
|
Source code in quantflow/ta/kalman.py
unscented_filter
¶
Build an [UnscentedKalmanFilter][quantflow.ta.kalman.StateSpaceModel.UnscentedKalmanFilter] for this model and the given observations.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observations \(y_{1:T}\) of shape \((T, n_y)\).
TYPE:
|
Source code in quantflow/ta/kalman.py
quantflow.ta.kalman.LinearGaussianModel
pydantic-model
¶
Bases: StateSpaceModel
State-space model with linear-Gaussian dynamics.
Fields:
-
Q(FloatArray) -
R(FloatArray) -
F(FloatArray) -
H(FloatArray) -
mu0(FloatArray) -
cov0(FloatArray)
H
pydantic-field
¶
Observation matrix \(H\) of shape \((n_y, n_x)\), or \((n_y,)\) when \(n_x = 1\).
get_px0
¶
get_px
¶
Transition distribution of latent state \(x_t\) given \(x_{t-1}=x_p\).
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Time index \(t\).
TYPE:
|
xp
|
State at time \(t-1\).
TYPE:
|
Source code in quantflow/ta/kalman.py
get_py
¶
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Time index \(t\).
TYPE:
|
xp
|
State at time \(t-1\).
TYPE:
|
x
|
State at time \(t\).
TYPE:
|
Source code in quantflow/ta/kalman.py
kalman_filter
¶
Convenience method to create a [KalmanFilter][quantflow.ta.kalman.LinearGaussianModel.KalmanFilter] for this model and the given observations.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observations \(y_{1:T}\) of shape \((T, n_y)\).
TYPE:
|
Source code in quantflow/ta/kalman.py
simulate_given_x
¶
Simulate observations given a state trajectory.
Returns \([y_0, \ldots, y_{T-1}]\) where \(y_t \sim\) get_py\((t, x_{t-1}, x_t)\).
| PARAMETER | DESCRIPTION |
|---|---|
x
|
State trajectory \(x_0, \ldots, x_{T-1}\).
TYPE:
|
Source code in quantflow/ta/kalman.py
simulate
¶
Simulate state and observation trajectories of length \(T\).
Returns a tuple of two lists: the state trajectory \([x_0, \ldots, x_{T-1}]\) and the observation trajectory \([y_0, \ldots, y_{T-1}]\).
| PARAMETER | DESCRIPTION |
|---|---|
T
|
Number of time steps.
TYPE:
|
Source code in quantflow/ta/kalman.py
unscented_filter
¶
Build an [UnscentedKalmanFilter][quantflow.ta.kalman.LinearGaussianModel.UnscentedKalmanFilter] for this model and the given observations.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observations \(y_{1:T}\) of shape \((T, n_y)\).
TYPE:
|
Source code in quantflow/ta/kalman.py
quantflow.ta.kalman.KalmanFilter
pydantic-model
¶
Bases: BaseModel
Kalman filter for a LinearGaussianModel.
Starting from the prior \((\mu_0, \Sigma_0)\), the forward pass alternates a prediction step with an observation update, accumulating the log-likelihood of the observations.
The state estimate of \(x_t\) given observations \(y_{1:s}\) is written \(\hat{x}_{t \mid s}\) with covariance \(P_{t \mid s}\).
The prediction propagates the filtered state through the linear dynamics of the model:
The update corrects the prediction with the observation \(y_t\), where \(S_t\) is the innovation covariance and \(K_t\) the optimal Kalman gain:
Each step adds its contribution to the Gaussian log-likelihood:
When the state is one-dimensional and the observation noise is isotropic (\(R = h^2 I\)), the innovation covariance \(S_t = h^2 I + P_{t \mid t-1}\, c c^\top\) is a rank-1 update to a scaled identity, where \(c = H\) is a column vector.
The Sherman-Morrison identity inverts it in \(O(n_y)\) instead of \(O(n_y^3)\):
The update detects this case automatically (see [isotropic_noise][quantflow.ta.kalman.isotropic_noise]) and applies the fast path; otherwise it falls back to a dense Cholesky solve.
Fields:
-
model(LinearGaussianModel) -
y(FloatArray) -
states(list[MeanAndCov])
states
pydantic-field
¶
List of filtered state estimates. states[t] is the Gaussian approximation of \(p(x_t \mid y_{1:t})\) with mean and covariance.
filter
¶
Run the Kalman forward pass.
Starts from the model prior, then for each observation applies predict followed by update. Populates states with the filtered estimates and returns the total log-likelihood of the observations.
Source code in quantflow/ta/kalman.py
predict
¶
One-step-ahead state prediction.
| PARAMETER | DESCRIPTION |
|---|---|
state
|
Filtered state \((\hat{x}_{t-1}, P_{t-1})\).
TYPE:
|
Source code in quantflow/ta/kalman.py
update
¶
Correct the prediction with the observation \(y_t\).
Returns the filtered state and the contribution of \(y_t\) to the Gaussian log-likelihood.
The log-likelihood contribution of \(y_t\) is
| PARAMETER | DESCRIPTION |
|---|---|
pred
|
Predicted state \((\hat{x}_{t \mid t-1}, P_{t \mid t-1})\).
TYPE:
|
y
|
Observation \(y_t\) of shape \((n_y,)\).
TYPE:
|
Source code in quantflow/ta/kalman.py
quantflow.ta.kalman.UnscentedKalmanFilter
pydantic-model
¶
Bases: BaseModel
Unscented Kalman filter (UKF) for a StateSpaceModel.
The UKF handles non-linear, non-Gaussian dynamics by propagating \(2 n_x + 1\) sigma points through the model transition (get_px) and observation (get_py) distributions, matching the first two moments of each. Only the conditional mean and covariance of those distributions are used, so any model that exposes them is supported.
The predicted moments combine the spread of the propagated means with the average conditional covariance (the law of total variance):
where \(\chi_i\) are the sigma points and \(\mu, \Sigma\) are the mean and covariance of get_px. The observation update forms the innovation covariance \(S_t\) and cross covariance \(C_t\) the same way through get_py, with gain \(K_t = C_t S_t^{-1}\). For a LinearGaussianModel the filter reduces to the exact KalmanFilter.
Fields:
-
model(StateSpaceModel) -
y(FloatArray) -
states(list[MeanAndCov]) -
alpha(float) -
beta(float) -
kappa(float)
states
pydantic-field
¶
List of filtered state estimates. states[t] is the Gaussian approximation of \(p(x_t \mid y_{1:t})\) with mean and covariance.
filter
¶
Run the UKF forward pass.
Populates states with the filtered estimates and returns the total log-likelihood of the observations.