Macro r_gen::sample [−][src]
macro_rules! sample { ($sample_ident:ident $trace_ident:ident $name:expr, $dist:expr) => { ... }; }
Expand description
The macro that is used for sampling from a distribution.
Example
use r_gen::{sample, r_gen}; use r_gen::{simulate, distributions::{Value, Distribution}, trace::{Choicemap, Trace}}; use std::rc::Rc; #[r_gen] fn my_model(():()) { let p = sample!(format!("p"), Distribution::Bernoulli(0.5)); print!("p: {}", p); } simulate(&mut my_model, ());
Takes the form: identifier, Distribution. The identifier will have the value sampled from the distribution stored in
it. It can be used later. p will have type Value
.
Example (Store results in an array)
use r_gen::{sample, r_gen}; use r_gen::{simulate, distributions::{Value, Distribution}, trace::{Choicemap, Trace}}; use std::rc::Rc; #[r_gen] fn flip_my_biased_coins((n, p) : (usize, f64)) { let mut flips = vec![Value::Integer(0); n]; for i in 0..n { flips[i] = sample!(format!("flip_{}", i), Distribution::Bernoulli(p)); } } let (tr, _) = simulate(&mut flip_my_biased_coins, (10, 0.5)); println!("{}", tr.get_trace_string());