Function r_gen::simulate[][src]

pub fn simulate<F, A, R, S: Sampleable>(
    generative_function: &mut F,
    arguments: A
) -> (Trace, R) where
    F: FnMut(Rc<dyn FnMut(&String, S, &mut Trace) -> Value>, &mut Trace, A) -> R, 
Expand description

Run the given generative model in the forward direction. As input, it takes a generative model (function with the #r_gen tag) and the arguments to that function. 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::{simulate, distributions::{Value, Distribution}, trace::{Choicemap, Trace}}; 
use std::rc::Rc;
#[r_gen]
fn my_biased_coin_model(():()){
    sample!(p ~ Distribution::Beta(1.0, 1.0));                    //Sample p from a uniform. 
    sample!(num_heads ~ Distribution::Binomial(100, p.into()));   //Flip 100 coins where P(Heads)=p
}
println!("GO"); 
let (trace, result) = simulate(&mut my_biased_coin_model, ()); 
println!("Trace String: \n{}", trace.get_trace_string());

Outputs:

Trace String: 
num_heads => 37
p => 0.38724904991570935