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: f64Accumulated log-prior probability from all sampling sites.
log_likelihood: f64Accumulated log-likelihood from all observation sites.
log_factors: f64Accumulated log-weight from all factor statements.
Implementations§
Source§impl Trace
impl Trace
Sourcepub fn total_log_weight(&self) -> f64
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);Sourcepub fn get_f64(&self, addr: &Address) -> Option<f64>
pub fn get_f64(&self, addr: &Address) -> Option<f64>
Type-safe accessor for f64 values in the trace.
Sourcepub fn get_bool(&self, addr: &Address) -> Option<bool>
pub fn get_bool(&self, addr: &Address) -> Option<bool>
Type-safe accessor for bool values in the trace.
Sourcepub fn get_u64(&self, addr: &Address) -> Option<u64>
pub fn get_u64(&self, addr: &Address) -> Option<u64>
Type-safe accessor for u64 values in the trace.
Sourcepub fn get_usize(&self, addr: &Address) -> Option<usize>
pub fn get_usize(&self, addr: &Address) -> Option<usize>
Type-safe accessor for usize values in the trace.
Sourcepub fn get_i64(&self, addr: &Address) -> Option<i64>
pub fn get_i64(&self, addr: &Address) -> Option<i64>
Type-safe accessor for i64 values in the trace.
Sourcepub fn get_f64_result(&self, addr: &Address) -> FugueResult<f64>
pub fn get_f64_result(&self, addr: &Address) -> FugueResult<f64>
Type-safe accessor that returns a Result for better error handling.
Sourcepub fn get_bool_result(&self, addr: &Address) -> FugueResult<bool>
pub fn get_bool_result(&self, addr: &Address) -> FugueResult<bool>
Type-safe accessor that returns a Result for better error handling.
Sourcepub fn get_u64_result(&self, addr: &Address) -> FugueResult<u64>
pub fn get_u64_result(&self, addr: &Address) -> FugueResult<u64>
Type-safe accessor that returns a Result for better error handling.
Sourcepub fn get_usize_result(&self, addr: &Address) -> FugueResult<usize>
pub fn get_usize_result(&self, addr: &Address) -> FugueResult<usize>
Type-safe accessor that returns a Result for better error handling.
Sourcepub fn get_i64_result(&self, addr: &Address) -> FugueResult<i64>
pub fn get_i64_result(&self, addr: &Address) -> FugueResult<i64>
Type-safe accessor that returns a Result for better error handling.
Sourcepub fn insert_choice(&mut self, addr: Address, value: ChoiceValue, logp: f64)
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());