Skip to main content
Pro Feature. Requires a Pro or Ultra subscription. Get started at api.mathematicalcompany.com
What is this? A particle filter is like a Kalman filter that can handle jumps, fat tails, and non-linear dynamics. It tracks hidden state by simulating thousands of possible trajectories (‘particles’) and weighting them by how well they match observations. Use it when markets exhibit sudden jumps that Kalman filters smooth over too aggressively.

Particle Filter

Horizon provides a Sequential Monte Carlo (SMC) particle filter implemented in Rust. Unlike Kalman filters, particle filters handle arbitrary nonlinear dynamics and non-Gaussian noise, making them well-suited for jump-diffusion processes, fat-tailed markets, and multimodal state distributions.

Nonlinear & Non-Gaussian

No linearity or Gaussian assumptions. Handles jumps, fat tails, and multimodal posteriors.

Rust-Native SMC

All particle propagation, resampling, and weight computation runs in Rust.

Adaptive Resampling

Systematic resampling triggered when effective sample size drops below threshold.

Pipeline Integration

Drop hz.particle_tracker() into any pipeline for filtered state estimates in ctx.params.

ParticleFilter

The core particle filter class. Maintains a weighted set of particles representing the posterior distribution over the hidden state.

Constructor

For prediction markets, 500-2000 particles provide a good tradeoff between accuracy and speed. The filter processes one observation in ~10-50 microseconds in Rust depending on particle count.

update()

Process a new observation: propagate particles through the state transition, compute weights from the observation likelihood, and resample if needed.
Returns a PFState object with the filtered estimate.

PFState Type

State Access Methods


Pipeline Integration

hz.particle_tracker

Creates a pipeline function that tracks a filtered price estimate using the particle filter. Injects filtered state into ctx.params.

Injected Parameters


Examples

Offline State Tracking

Use the particle filter directly for research without a pipeline:

Particle Filter vs Kalman Filter

The particle filter excels when the state dynamics are nonlinear or noise is non-Gaussian:

Combining with BOCPD

Use change point detection to reset the particle filter after regime shifts:

Mathematical Background

A particle filter represents the posterior distribution of the hidden state given all observations, using a weighted set of N samples (particles). At each step:
  1. Propagate: Draw each particle from the state transition distribution
  2. Weight: Compute the likelihood of the observation given each particle
  3. Normalize: Scale all weights to sum to 1
  4. Resample: If the effective sample size is below the threshold, resample particles proportional to weights
The weighted mean sum(w_i * x_i) approximates the conditional expectation of the state.
ESS = 1 / sum(w_i^2) measures the diversity of the particle cloud. When all weight concentrates on one particle, ESS equals 1 (degenerate). When weights are uniform, ESS equals N (maximum diversity). Horizon resamples when ESS drops below N/2.
Horizon uses systematic resampling, which generates a single uniform random number and deterministically selects particles at evenly spaced intervals through the CDF. This has lower variance than multinomial resampling and O(N) cost.
Particle filters are stochastic: different seeds produce slightly different results. For reproducible backtests, always set the seed parameter. In live trading, the stochasticity is negligible for 1000+ particles.