Skip to content

Particle filter

polars_ts.bayesian.particle_filter

Particle Filter (Sequential Monte Carlo) for nonlinear state estimation.

Implements bootstrap and auxiliary particle filters with systematic resampling for state estimation in nonlinear, non-Gaussian state-space models.

References

  • Doucet et al. (2001), Sequential Monte Carlo Methods in Practice
  • Andrieu et al. (2010), Particle Markov chain Monte Carlo methods
  • Chopin & Papaspiliopoulos (2020), An Introduction to Sequential Monte Carlo

ParticleFilterResult dataclass

Particle filter output.

Attributes

filtered_mean Filtered state mean at each time step, shape (T,) or (T, state_dim). filtered_var Filtered state variance at each time step. particles_history Full particle trajectories, shape (T, n_particles, state_dim). None if store_history=False. weights_history Normalized weights at each step, shape (T, n_particles). None if store_history=False. ess Effective sample size at each step, shape (T,). log_likelihood Estimate of the log marginal likelihood.

ParticleFilter

Bootstrap Particle Filter for nonlinear state-space models.

Parameters

n_particles Number of particles. transition_fn State transition function: (particles, t, rng) -> new_particles. observation_loglik Observation log-likelihood: (particles, y_t) -> log_weights. resample_method Resampling strategy: "systematic" or "multinomial". resample_threshold Resample when ESS drops below resample_threshold * n_particles. seed Random seed. store_history Whether to store full particle/weight histories.

filter(observations, initial_particles=None)

Run the particle filter on a sequence of observations.

Parameters

observations Array of observations, shape (T,). initial_particles Starting particles, shape (n_particles,). If None, initialized from N(y[0], 1).

Returns

ParticleFilterResult Filtering result with means, variances, ESS, and optionally full particle/weight histories.

_systematic_resample(weights, rng)

Systematic resampling. Returns index array.

_multinomial_resample(weights, rng)

Multinomial resampling. Returns index array.

local_level_transition(sigma_level=1.0)

Create transition function for local level model.

local_level_loglik(sigma_obs=1.0)

Create observation log-likelihood for local level model.

ar1_transition(phi=0.9, sigma=1.0, mu=0.0)

Create transition function for AR(1) model.

stochastic_volatility_transition(phi=0.95, sigma_v=0.2, mu=0.0)

Create transition for stochastic volatility model: h_t = mu + phi(h_{t-1}-mu) + sigma_ve_t.

stochastic_volatility_loglik()

Observation log-likelihood for SV model: y_t ~ N(0, exp(h_t)).

particle_filter(df, transition_fn, observation_loglik, n_particles=1000, resample_method='systematic', resample_threshold=0.5, seed=42, id_col='unique_id', target_col='y', time_col='ds')

Particle filter convenience function for DataFrames.

Parameters

df Input DataFrame. transition_fn State transition function. observation_loglik Observation log-likelihood function. n_particles Number of particles. resample_method "systematic" or "multinomial". resample_threshold ESS threshold for resampling. seed Random seed. id_col, target_col, time_col Column names.

Returns

pl.DataFrame DataFrame with filtered_mean, filtered_var, ess columns.