pub struct ParticleFilter {
pub num_particles: usize,
pub particles: Vec<Particle>,
pub state_variables: Vec<String>,
pub ess_threshold: f64,
pub track_history: bool,
}Expand description
Particle filter (Sequential Monte Carlo) for temporal inference.
Particle filtering is used for inference in dynamic systems where the state evolves over time.
§Example
use tensorlogic_quantrs_hooks::{ParticleFilter, HiddenMarkovModel, Assignment};
use std::collections::HashMap;
// Create HMM with 2 states, 3 observations, 10 time steps
let hmm = HiddenMarkovModel::new(2, 3, 10);
// Create particle filter
let mut pf = ParticleFilter::new(100, vec!["state".to_string()]);
// Initialize particles
let cardinalities: HashMap<String, usize> = [("state".to_string(), 2)].into_iter().collect();
pf.initialize(&cardinalities);Fields§
§num_particles: usizeNumber of particles
particles: Vec<Particle>Current particles
state_variables: Vec<String>State variable names
ess_threshold: f64Effective sample size threshold for resampling
track_history: boolWhether to track history
Implementations§
Source§impl ParticleFilter
impl ParticleFilter
Sourcepub fn new(num_particles: usize, state_variables: Vec<String>) -> Self
pub fn new(num_particles: usize, state_variables: Vec<String>) -> Self
Create a new particle filter.
Sourcepub fn with_ess_threshold(self, threshold: f64) -> Self
pub fn with_ess_threshold(self, threshold: f64) -> Self
Set the ESS threshold for resampling (as fraction of num_particles).
Sourcepub fn with_history(self, track: bool) -> Self
pub fn with_history(self, track: bool) -> Self
Enable history tracking.
Sourcepub fn initialize(&mut self, cardinalities: &HashMap<String, usize>)
pub fn initialize(&mut self, cardinalities: &HashMap<String, usize>)
Initialize particles uniformly.
Sourcepub fn initialize_from_prior(
&mut self,
prior: &[f64],
cardinalities: &HashMap<String, usize>,
)
pub fn initialize_from_prior( &mut self, prior: &[f64], cardinalities: &HashMap<String, usize>, )
Initialize particles from a prior distribution.
Sourcepub fn predict(
&mut self,
transition: &dyn Fn(&Assignment, u64) -> Assignment,
cardinalities: &HashMap<String, usize>,
)
pub fn predict( &mut self, transition: &dyn Fn(&Assignment, u64) -> Assignment, cardinalities: &HashMap<String, usize>, )
Predict step: propagate particles through transition model.
The transition function takes a state and a random seed, returning the next state.
Sourcepub fn update<F>(&mut self, observation: &Assignment, likelihood: F)
pub fn update<F>(&mut self, observation: &Assignment, likelihood: F)
Update step: weight particles based on observation likelihood.
Sourcepub fn effective_sample_size(&self) -> f64
pub fn effective_sample_size(&self) -> f64
Compute effective sample size.
Sourcepub fn estimate_marginal(&self, var_name: &str, cardinality: usize) -> Vec<f64>
pub fn estimate_marginal(&self, var_name: &str, cardinality: usize) -> Vec<f64>
Estimate marginal distribution from particles.
Sourcepub fn estimate_expectation<F>(&self, func: F) -> f64
pub fn estimate_expectation<F>(&self, func: F) -> f64
Estimate expected value of a function over the particle distribution.
Sourcepub fn map_estimate(&self) -> Option<&Assignment>
pub fn map_estimate(&self) -> Option<&Assignment>
Get the MAP (most likely) state.
Sourcepub fn run_sequence(
&mut self,
observations: &[Assignment],
transition: &dyn Fn(&Assignment, u64) -> Assignment,
likelihood: &dyn Fn(&Assignment, &Assignment) -> f64,
cardinalities: &HashMap<String, usize>,
) -> Vec<Vec<f64>>
pub fn run_sequence( &mut self, observations: &[Assignment], transition: &dyn Fn(&Assignment, u64) -> Assignment, likelihood: &dyn Fn(&Assignment, &Assignment) -> f64, cardinalities: &HashMap<String, usize>, ) -> Vec<Vec<f64>>
Run particle filter on a sequence of observations.
The transition function takes a state and a random seed. The likelihood function computes P(observation | state).
Auto Trait Implementations§
impl Freeze for ParticleFilter
impl RefUnwindSafe for ParticleFilter
impl Send for ParticleFilter
impl Sync for ParticleFilter
impl Unpin for ParticleFilter
impl UnwindSafe for ParticleFilter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.