Function r_gen::generate [−][src]
pub fn generate<F, A, R, S: Sampleable>(
generative_function: &mut F,
arguments: A,
conditions: &Choicemap
) -> (Trace, R) where
F: FnMut(Rc<dyn FnMut(&String, S, &mut Trace) -> Value>, &mut Trace, A) -> R,
Expand description
Run a generative model in the forward direction, fixing certian decisions or observations. As input, it takes a generative model (function with the #r_gen tag), the arguments to that function, and a choicemap of the observed variables. Returns a tuple of the trace generated by running the function and the return value of the function itself.
Example
use r_gen::{sample, r_gen}; use r_gen::{generate, distributions::{Value, Distribution}, trace::{Choicemap, Trace}}; use std::rc::Rc; #[r_gen] fn my_biased_coin_model(():()){ let p = sample!(format!("p"), Distribution::Beta(1.0, 1.0)); //Sample p from a uniform. sample!(format!("num_heads"), Distribution::Binomial(100, p.into())); //Flip 100 coins where P(Heads)=p } let choices = Choicemap::from(vec![("p", Value::Real(0.1))]); //Fix the value p=0.1 let (trace, result) = generate(&mut my_biased_coin_model, (), &choices);