Skip to main content

ParticleFilter

Struct ParticleFilter 

Source
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: usize

Number of particles

§particles: Vec<Particle>

Current particles

§state_variables: Vec<String>

State variable names

§ess_threshold: f64

Effective sample size threshold for resampling

§track_history: bool

Whether to track history

Implementations§

Source§

impl ParticleFilter

Source

pub fn new(num_particles: usize, state_variables: Vec<String>) -> Self

Create a new particle filter.

Source

pub fn with_ess_threshold(self, threshold: f64) -> Self

Set the ESS threshold for resampling (as fraction of num_particles).

Source

pub fn with_history(self, track: bool) -> Self

Enable history tracking.

Source

pub fn initialize(&mut self, cardinalities: &HashMap<String, usize>)

Initialize particles uniformly.

Source

pub fn initialize_from_prior( &mut self, prior: &[f64], cardinalities: &HashMap<String, usize>, )

Initialize particles from a prior distribution.

Source

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.

Source

pub fn update<F>(&mut self, observation: &Assignment, likelihood: F)
where F: Fn(&Assignment, &Assignment) -> f64,

Update step: weight particles based on observation likelihood.

Source

pub fn effective_sample_size(&self) -> f64

Compute effective sample size.

Source

pub fn resample(&mut self)

Resample particles using systematic resampling.

Source

pub fn estimate_marginal(&self, var_name: &str, cardinality: usize) -> Vec<f64>

Estimate marginal distribution from particles.

Source

pub fn estimate_expectation<F>(&self, func: F) -> f64
where F: Fn(&Assignment) -> f64,

Estimate expected value of a function over the particle distribution.

Source

pub fn map_estimate(&self) -> Option<&Assignment>

Get the MAP (most likely) state.

Source

pub fn entropy(&self) -> f64

Get entropy of the particle distribution.

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V