Pro Feature. Requires a Pro or Ultra subscription. Get started at api.mathematicalcompany.com
Kalman Filters
Horizon provides two Kalman filter implementations in Rust: a standard linearKalmanFilter and an UnscentedKF for nonlinear dynamics. Both support online single-step updates for live trading and batch smoothing for offline analysis.
Linear Kalman Filter
Standard Kalman filter for linear state-space models. Optimal for Gaussian noise.
Unscented Kalman Filter
Sigma-point filter for nonlinear dynamics. No Jacobians required.
Hedge Ratio Tracker
Online hedge ratio estimation via time-varying regression coefficients.
Spread Trading
Kalman-filtered spread for pairs/stat-arb strategies on correlated markets.
KalmanFilter
The linear Kalman filter tracks a hidden state vector through noisy observations. It is optimal (minimum variance) when the system is linear and noise is Gaussian.Constructor
| Parameter | Type | Description |
|---|---|---|
state_dim | int | Dimension of the hidden state vector |
obs_dim | int | Dimension of the observation vector |
Configuration Methods
Set the system matrices before running the filter. All matrices are provided as flat row-major lists.| Method | Parameter | Type | Description |
|---|---|---|---|
set_transition | matrix | list[list[float]] | State transition matrix F (state_dim x state_dim) |
set_observation | matrix | list[list[float]] | Observation matrix H (obs_dim x state_dim) |
set_process_noise | matrix | list[list[float]] | Process noise Q (state_dim x state_dim) |
set_measurement_noise | matrix | list[list[float]] | Measurement noise R (obs_dim x obs_dim) |
predict()
Propagate the state forward one time step using the transition model.update()
Incorporate a new observation and correct the state estimate.| Parameter | Type | Description |
|---|---|---|
observation | list[float] | Observation vector (length obs_dim) |
State Access
| Method | Returns | Description |
|---|---|---|
state() | list[float] | Current state estimate vector |
covariance() | list[list[float]] | State covariance matrix P |
log_likelihood() | float | Cumulative log-likelihood of all processed observations |
UnscentedKF
The Unscented Kalman Filter handles nonlinear state transitions and observation models using sigma-point propagation. No Jacobian computation is needed.Constructor
| Parameter | Type | Default | Description |
|---|---|---|---|
state_dim | int | required | Dimension of the state vector |
obs_dim | int | required | Dimension of the observation vector |
alpha | float | 1e-3 | Controls spread of sigma points around the mean |
beta | float | 2.0 | Incorporates prior distribution knowledge (2.0 is optimal for Gaussian) |
kappa | float | 0.0 | Secondary scaling parameter |
set_process_noise, set_measurement_noise, predict, update, state, covariance, and log_likelihood methods as KalmanFilter. The transition and observation models are specified as nonlinear functions during construction or via configuration.
Pipeline Integration
hz.kalman_tracker
Tracks a filtered price estimate using a constant-velocity Kalman model. Injects the smoothed state intoctx.params.
| Parameter | Type | Default | Description |
|---|---|---|---|
feed | str | None | Feed name to read prices from |
process_noise | float | 0.001 | Process noise variance Q |
measurement_noise | float | 0.01 | Measurement noise variance R |
param_name | str | "kalman" | Key in ctx.params |
hz.kalman_hedge_ratio
Estimates a time-varying hedge ratio between two feeds using a Kalman regression model. The state tracks the intercept and slope (hedge ratio) of the linear relationship.| Parameter | Type | Default | Description |
|---|---|---|---|
feed_x | str | required | Feed name for the independent variable |
feed_y | str | required | Feed name for the dependent variable |
process_noise | float | 1e-4 | Process noise for coefficient drift |
measurement_noise | float | 0.01 | Observation noise variance |
hz.kalman_spread
Computes a Kalman-filtered spread between two markets and injects spread statistics for stat-arb strategies.| Key | Type | Description |
|---|---|---|
ctx.params["kalman_spread"]["spread"] | float | Current filtered spread value |
ctx.params["kalman_spread"]["z_score"] | float | Z-score of the spread relative to its filtered mean and variance |
ctx.params["kalman_spread"]["hedge_ratio"] | float | Current hedge ratio estimate |
Example: Hedge Ratio Spread Trading
Mathematical Background
Kalman Filter
Kalman Filter
The Kalman filter recursively estimates the state of a linear system:
- Predict: x_hat = F * x + B * u, P = F * P * F’ + Q
- Update: K = P * H’ * (H * P * H’ + R)^(-1), x = x + K * (z - H * x), P = (I - K * H) * P
Unscented Transform
Unscented Transform
The UKF propagates 2n+1 sigma points (where n is the state dimension) through the nonlinear function, then recovers the mean and covariance from the transformed points. This avoids computing Jacobians and provides second-order accuracy for Gaussian inputs.
Time-Varying Hedge Ratio
Time-Varying Hedge Ratio
Modeling the hedge ratio as a Kalman state allows it to drift over time, adapting to structural changes in the relationship between two markets. This is superior to rolling OLS because the Kalman filter optimally weights old vs. new information based on the noise model.