pub struct MeanFieldGuide {
pub params: HashMap<Address, VariationalParam>,
}Expand description
Mean-field variational guide for approximate posterior inference.
A mean-field guide specifies independent variational distributions for each random variable in the model. This factorization assumption simplifies optimization but may underestimate correlations between variables.
The guide maps each address (random variable) to its variational parameters, which are optimized to minimize the KL divergence to the true posterior.
§Fields
params- Map from addresses to their variational parameters
§Examples
use fugue::*;
use std::collections::HashMap;
// Create a guide for a two-parameter model
let mut guide = MeanFieldGuide::new();
guide.params.insert(
addr!("mu"),
VariationalParam::Normal { mu: 0.0, log_sigma: 0.0 }
);
guide.params.insert(
addr!("sigma"),
VariationalParam::Normal { mu: 0.0, log_sigma: -1.0 }
);
// Check if parameters are specified
assert!(guide.params.contains_key(&addr!("mu")));
assert!(guide.params.contains_key(&addr!("sigma")));Fields§
§params: HashMap<Address, VariationalParam>Map from addresses to their variational parameters.
Implementations§
Source§impl MeanFieldGuide
impl MeanFieldGuide
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty mean-field guide.
The guide starts with no variational parameters. Add a factor for each latent in
your model with MeanFieldGuide::add_latent (support-aware) or by inserting into
MeanFieldGuide::params directly.
Sourcepub fn add_latent(&mut self, addr: Address, support: Support, init_value: f64)
pub fn add_latent(&mut self, addr: Address, support: Support, init_value: f64)
Add a support-matched variational factor for a latent (finding FG-17).
The variational family is selected from the declared Support so the factor’s
samples always lie in the model latent’s support: real → Normal, positive →
LogNormal, [0,1] → Beta. init_value seeds the factor’s location.
use fugue::*;
use fugue::inference::vi::{MeanFieldGuide, Support};
let mut guide = MeanFieldGuide::new();
guide.add_latent(addr!("theta"), Support::Unit, 0.3); // Beta factor
guide.add_latent(addr!("rate"), Support::Positive, 2.0); // LogNormal factor
guide.add_latent(addr!("mu"), Support::Real, 0.0); // Normal factor
assert_eq!(guide.params.len(), 3);Sourcepub fn from_trace(trace: &Trace) -> Result<Self, GuideError>
pub fn from_trace(trace: &Trace) -> Result<Self, GuideError>
Initialize a guide from a prior trace, defaulting continuous latents to a Normal factor on the real line.
A Trace records only sampled values, not the support of the distributions
that produced them, so this constructor cannot infer positive/unit support from a
single draw (doing so from the sign of one sample was the FG-18 antipattern). It
therefore builds a real-line Normal factor for every continuous (f64) latent,
with a finite, value-scaled initial standard deviation (init_log_sigma, finding
FG-18). For support-aware factors use MeanFieldGuide::add_latent.
Discrete latents (Bool / U64 / Usize / I64) have no continuous variational
factor and yield a typed GuideError::UnsupportedDiscreteLatent instead of a
silent f64 factor that would later panic during scoring (finding FG-17).
Sourcepub fn sample_trace<R: Rng>(&self, rng: &mut R) -> Trace
pub fn sample_trace<R: Rng>(&self, rng: &mut R) -> Trace
Sample a trace from the guide.
Factors are sampled in a deterministic (address-sorted) order so that, for a fixed
RNG seed, two guides with the same set of addresses consume the RNG identically —
this is what makes the common-random-numbers finite differences in
elbo_gradient_fd valid. All factor families are continuous, so values are
stored as ChoiceValue::F64.
Trait Implementations§
Source§impl Clone for MeanFieldGuide
impl Clone for MeanFieldGuide
Source§fn clone(&self) -> MeanFieldGuide
fn clone(&self) -> MeanFieldGuide
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more