use rand::{Rng, RngExt};
use simsam::{ConditionalFactorSampler, ConditionalFactorization};
struct ToyFactor;
impl ConditionalFactorization for ToyFactor {
fn dim(&self) -> usize {
2
}
fn sample_i<R: Rng + ?Sized>(&self, rng: &mut R, i: usize, prefix: &[f64]) -> f64 {
let u: f64 = rng.random();
match i {
0 => u, 1 => {
let x = prefix[0];
u * x }
_ => unreachable!(),
}
}
}
fn main() {
let sampler = ConditionalFactorSampler::new(ToyFactor);
let n = 50_000;
let mut sx = 0.0;
let mut sy = 0.0;
for _ in 0..n {
let v = sampler.sample().expect("sample");
sx += v[0];
sy += v[1];
}
println!("mean x ~ 0.5: {:.4}", sx / n as f64);
println!("mean y ~ 0.25: {:.4}", sy / n as f64);
}