pub struct ImportanceSampler {
pub num_samples: usize,
pub self_normalize: bool,
}Expand description
Importance sampling for approximate inference.
Importance sampling draws samples from a proposal distribution q(x) and weights them by p(x)/q(x) to estimate expectations under p(x).
§Example
use tensorlogic_quantrs_hooks::{FactorGraph, ImportanceSampler, ProposalDistribution};
let mut graph = FactorGraph::new();
graph.add_variable_with_card("x".to_string(), "Binary".to_string(), 2);
let sampler = ImportanceSampler::new(1000);
let result = sampler.run(&graph, ProposalDistribution::Uniform);Fields§
§num_samples: usizeNumber of samples to draw
self_normalize: boolWhether to use self-normalized importance sampling
Implementations§
Source§impl ImportanceSampler
impl ImportanceSampler
Sourcepub fn new(num_samples: usize) -> Self
pub fn new(num_samples: usize) -> Self
Create a new importance sampler with specified number of samples.
Sourcepub fn with_self_normalize(self, self_normalize: bool) -> Self
pub fn with_self_normalize(self, self_normalize: bool) -> Self
Set whether to use self-normalized importance sampling.
Sourcepub fn run(
&self,
graph: &FactorGraph,
proposal: ProposalDistribution,
) -> Result<HashMap<String, ArrayD<f64>>>
pub fn run( &self, graph: &FactorGraph, proposal: ProposalDistribution, ) -> Result<HashMap<String, ArrayD<f64>>>
Run importance sampling to approximate marginals.
Sourcepub fn draw_weighted_samples(
&self,
graph: &FactorGraph,
proposal: &ProposalDistribution,
) -> Result<Vec<WeightedSample>>
pub fn draw_weighted_samples( &self, graph: &FactorGraph, proposal: &ProposalDistribution, ) -> Result<Vec<WeightedSample>>
Draw weighted samples from the proposal distribution.
Sourcepub fn get_weighted_samples(
&self,
graph: &FactorGraph,
proposal: &ProposalDistribution,
) -> Result<Vec<WeightedSample>>
pub fn get_weighted_samples( &self, graph: &FactorGraph, proposal: &ProposalDistribution, ) -> Result<Vec<WeightedSample>>
Get all weighted samples for analysis.
Sourcepub fn effective_sample_size(samples: &[WeightedSample]) -> f64
pub fn effective_sample_size(samples: &[WeightedSample]) -> f64
Compute the effective sample size (ESS).
ESS measures the efficiency of importance sampling. Higher ESS indicates better proposal distribution.
Sourcepub fn weight_coefficient_of_variation(samples: &[WeightedSample]) -> f64
pub fn weight_coefficient_of_variation(samples: &[WeightedSample]) -> f64
Compute the coefficient of variation of weights.
Sourcepub fn resample(samples: &[WeightedSample]) -> Vec<WeightedSample>
pub fn resample(samples: &[WeightedSample]) -> Vec<WeightedSample>
Resample particles based on their weights (for particle filtering).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ImportanceSampler
impl RefUnwindSafe for ImportanceSampler
impl Send for ImportanceSampler
impl Sync for ImportanceSampler
impl Unpin for ImportanceSampler
impl UnwindSafe for ImportanceSampler
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.