Trace

Struct Trace 

Source
pub struct Trace {
    pub choices: BTreeMap<Address, Choice>,
    pub log_prior: f64,
    pub log_likelihood: f64,
    pub log_factors: f64,
}
Expand description

Complete execution trace of a probabilistic model.

A Trace records the complete execution history of a probabilistic model, including all choices made and accumulated log-weights from different sources. This enables replay, scoring, and inference operations.

Example:


// Execute a model and examine the trace
let model = sample(addr!("theta"), Normal::new(0.0, 1.0).unwrap())
    .bind(|theta| observe(addr!("y"), Normal::new(theta, 0.5).unwrap(), 1.2)
        .map(move |_| theta));

let mut rng = StdRng::seed_from_u64(42);
let (result, trace) = runtime::handler::run(
    PriorHandler { rng: &mut rng, trace: Trace::default() },
    model
);

// Examine trace components
println!("Sampled theta: {:.3}", result);
println!("Prior log-weight: {:.3}", trace.log_prior);
println!("Likelihood log-weight: {:.3}", trace.log_likelihood);
println!("Total log-weight: {:.3}", trace.total_log_weight());

// Type-safe value access
let theta_value = trace.get_f64(&addr!("theta")).unwrap();
assert_eq!(theta_value, result);

Fields§

§choices: BTreeMap<Address, Choice>

Map from addresses to the choices made at those sites.

§log_prior: f64

Accumulated log-prior probability from all sampling sites.

§log_likelihood: f64

Accumulated log-likelihood from all observation sites.

§log_factors: f64

Accumulated log-weight from all factor statements.

Implementations§

Source§

impl Trace

Source

pub fn total_log_weight(&self) -> f64

Compute the total unnormalized log-probability of this execution.

The total log-weight combines all three components (prior, likelihood, factors) and represents the unnormalized log-probability of this execution path.

Example:


let trace = Trace {
    log_prior: -1.5,
    log_likelihood: -2.3,
    log_factors: 0.8,
    ..Default::default()
};

assert_eq!(trace.total_log_weight(), -3.0);
Source

pub fn get_f64(&self, addr: &Address) -> Option<f64>

Type-safe accessor for f64 values in the trace.

Source

pub fn get_bool(&self, addr: &Address) -> Option<bool>

Type-safe accessor for bool values in the trace.

Source

pub fn get_u64(&self, addr: &Address) -> Option<u64>

Type-safe accessor for u64 values in the trace.

Source

pub fn get_usize(&self, addr: &Address) -> Option<usize>

Type-safe accessor for usize values in the trace.

Source

pub fn get_i64(&self, addr: &Address) -> Option<i64>

Type-safe accessor for i64 values in the trace.

Source

pub fn get_f64_result(&self, addr: &Address) -> FugueResult<f64>

Type-safe accessor that returns a Result for better error handling.

Source

pub fn get_bool_result(&self, addr: &Address) -> FugueResult<bool>

Type-safe accessor that returns a Result for better error handling.

Source

pub fn get_u64_result(&self, addr: &Address) -> FugueResult<u64>

Type-safe accessor that returns a Result for better error handling.

Source

pub fn get_usize_result(&self, addr: &Address) -> FugueResult<usize>

Type-safe accessor that returns a Result for better error handling.

Source

pub fn get_i64_result(&self, addr: &Address) -> FugueResult<i64>

Type-safe accessor that returns a Result for better error handling.

Source

pub fn insert_choice(&mut self, addr: Address, value: ChoiceValue, logp: f64)

Insert a typed choice into the trace with type safety.

This is a convenience method for manually constructing traces. Note that this method only updates the choices map - it does not modify the log-weight accumulators (log_prior, log_likelihood, log_factors).

Example:


let mut trace = Trace::default();

// Insert different types of choices
trace.insert_choice(addr!("mu"), ChoiceValue::F64(1.5), -0.125);
trace.insert_choice(addr!("success"), ChoiceValue::Bool(true), -0.693);
trace.insert_choice(addr!("count"), ChoiceValue::U64(10), -2.303);

// Retrieve with type safety
assert_eq!(trace.get_f64(&addr!("mu")), Some(1.5));
assert_eq!(trace.get_bool(&addr!("success")), Some(true));
assert_eq!(trace.get_u64(&addr!("count")), Some(10));

println!("Trace has {} choices", trace.choices.len());

Trait Implementations§

Source§

impl Clone for Trace

Source§

fn clone(&self) -> Trace

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Trace

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Trace

Source§

fn default() -> Trace

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Trace

§

impl RefUnwindSafe for Trace

§

impl Send for Trace

§

impl Sync for Trace

§

impl Unpin for Trace

§

impl UnwindSafe for Trace

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V