pub struct CompiledProgram { /* private fields */ }Expand description
A checked program bound to its data, ready to build models.
build constructs a fresh single-use Model,
so || compiled.build() is the model_fn every fugue inference driver
takes. A CompiledProgram is Send + Sync; clones share their warning
buffer.
use fugue::program::{Data, Program, Registry, Value};
use fugue::*;
use rand::{rngs::StdRng, SeedableRng};
let program = Program::parse(
r#"let p <- sample(addr!("p"), Beta(2.0, 2.0));
for i in 0..flips.len() {
observe(addr!("flip", i), Bernoulli(p), flips[i]);
}
pure(p > 0.5)"#,
)
.unwrap();
let data = Data::from_json(r#"{"flips": [true, false, true, true]}"#).unwrap();
let compiled = program.compile(&Registry::new(), &data).unwrap();
let mut rng = StdRng::seed_from_u64(7);
let (favours_heads, trace) = runtime::handler::run(
PriorHandler { rng: &mut rng, trace: Trace::default() },
compiled.build(),
);
assert!(matches!(favours_heads, Value::Bool(_)));
// Addresses are exactly what `addr!` builds in Rust.
assert!(trace.get_f64(&addr!("p")).is_some());
assert!(trace.log_likelihood.is_finite());
// Any inference driver takes it.
let draws = adaptive_mcmc_chain(&mut rng, || compiled.build(), 200, 100);
assert_eq!(draws.len(), 200);Implementations§
Source§impl CompiledProgram
impl CompiledProgram
Sourcepub fn build(&self) -> Model<Value>
pub fn build(&self) -> Model<Value>
Build a fresh single-use model returning the program’s value.
Sourcepub fn build_f64(&self) -> Model<f64>
pub fn build_f64(&self) -> Model<f64>
Build a model returning the program’s value as an f64: a number
converts, a bool reads as 1.0/0.0, an array as NaN — the
playground interpreter’s Model<f64> contract. For other result
types, map build with Value’s
accessors:
use fugue::program::{Data, Program, Registry};
use fugue::*;
let compiled = Program::parse(r#"let k <- sample(addr!("k"), Poisson(3.0)); pure(k)"#)
.unwrap()
.compile(&Registry::new(), &Data::new())
.unwrap();
let as_f64: Model<f64> = compiled.build_f64();
let as_u64: Model<u64> = compiled.build().map(|v| v.as_u64().unwrap_or(0));Sourcepub fn take_warnings(&self) -> Vec<String>
pub fn take_warnings(&self) -> Vec<String>
Drain the runtime warnings accumulated by this program’s models so
far (each a soft error that was mapped to weight -inf). At most 64
are kept between drains.
use fugue::program::{Data, Program, Registry};
use fugue::*;
use rand::{rngs::StdRng, SeedableRng};
let compiled = Program::parse(r#"let s <- sample(addr!("s"), Normal(0.0, -1.0)); pure(s)"#)
.unwrap()
.compile(&Registry::new(), &Data::new())
.unwrap();
let (_, trace) = runtime::handler::run(
PriorHandler { rng: &mut StdRng::seed_from_u64(1), trace: Trace::default() },
compiled.build(),
);
assert_eq!(trace.total_log_weight(), f64::NEG_INFINITY);
let warnings = compiled.take_warnings();
assert!(warnings[0].starts_with("sample `s`"));
assert!(compiled.take_warnings().is_empty());Trait Implementations§
Source§impl Clone for CompiledProgram
impl Clone for CompiledProgram
Auto Trait Implementations§
impl !RefUnwindSafe for CompiledProgram
impl !UnwindSafe for CompiledProgram
impl Freeze for CompiledProgram
impl Send for CompiledProgram
impl Sync for CompiledProgram
impl Unpin for CompiledProgram
impl UnsafeUnpin for CompiledProgram
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
Mutably borrows from an owned value. Read more