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
Configuration Methods
Set the system matrices before running the filter. All matrices are provided as flat row-major lists.predict()
Propagate the state forward one time step using the transition model.update()
Incorporate a new observation and correct the state estimate.State Access
UnscentedKF
The Unscented Kalman Filter handles nonlinear state transitions and observation models using sigma-point propagation. No Jacobian computation is needed.Constructor
The UKF supports the same
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.
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.hz.kalman_spread
Computes a Kalman-filtered spread between two markets and injects spread statistics for stat-arb strategies.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.