pub enum VariationalParam {
Normal {
mu: f64,
log_sigma: f64,
},
LogNormal {
mu: f64,
log_sigma: f64,
},
Beta {
log_alpha: f64,
log_beta: f64,
},
}Expand description
Variational distribution parameters for a single random variable.
Each random variable in the model gets its own variational distribution that approximates its marginal posterior. Scale parameters are stored in log-space (unconstrained) for numerical stability and to guarantee positivity.
§Variants
Normal- Gaussian approximation with mean and log-standard-deviationLogNormal- Log-normal approximation for positive variablesBeta- Beta approximation for variables constrained to [0,1]
§Examples
use fugue::*;
use rand::rngs::StdRng;
use rand::SeedableRng;
// Create variational parameters
let normal_param = VariationalParam::Normal {
mu: 1.5,
log_sigma: -0.693 // sigma = 0.5
};
let beta_param = VariationalParam::Beta {
log_alpha: 1.099, // alpha = 3.0
log_beta: 0.693, // beta = 2.0
};
// Sample from variational distribution
let mut rng = StdRng::seed_from_u64(42);
let sample = normal_param.sample(&mut rng);
let log_prob = normal_param.log_prob(sample);Variants§
Normal
Normal/Gaussian variational distribution.
LogNormal
Log-normal variational distribution for positive variables.
Fields
Beta
Beta variational distribution for variables in [0,1].
Implementations§
Source§impl VariationalParam
impl VariationalParam
Sourcepub fn for_support(support: Support, init_value: f64) -> Self
pub fn for_support(support: Support, init_value: f64) -> Self
Build a variational factor initialized for a latent with the given Support.
The family is chosen to match the support so that samples are always in the
model latent’s support (avoiding the -inf ELBO of a support-mismatched guide,
finding FG-17). The scale is initialized to a moderate spread derived from
init_value; it will be optimized alongside the location.
Support::Real→Normal { mu: init_value, .. }Support::Positive→LogNormal { mu: ln(init_value), .. }Support::Unit→Betawith mean ≈init_value
Sourcepub fn sample<R: Rng>(&self, rng: &mut R) -> f64
pub fn sample<R: Rng>(&self, rng: &mut R) -> f64
Sample a value from this variational distribution with numerical stability.
Generates a random sample using the current variational parameters. For the Beta family this draws an exact Beta sample (finding FG-60): there is no moment-matched-Gaussian approximation and no clamping.
§Arguments
rng- Random number generator
§Returns
A sample from the variational distribution, or NaN if parameters are invalid.
Sourcepub fn sample_with_aux<R: Rng>(&self, rng: &mut R) -> (f64, f64)
pub fn sample_with_aux<R: Rng>(&self, rng: &mut R) -> (f64, f64)
Sample a value together with auxiliary information for pathwise gradients.
For the location-scale families (VariationalParam::Normal,
VariationalParam::LogNormal) the auxiliary value is the standard-normal base
draw z used to reparameterize the sample (x = μ + σ·z), which supports the
reparameterization trick.
The VariationalParam::Beta family has no location-scale reparameterization.
This method therefore samples the Beta exactly (finding FG-60 — the previous
implementation used a moment-matched Gaussian clamped to [0.001, 0.999], which is
a different, biased distribution) and returns f64::NAN as the auxiliary value to
signal that no reparameterization base exists. Beta variational parameters are
optimized with finite-difference ELBO gradients (see elbo_gradient_fd), not
pathwise gradients.
Trait Implementations§
Source§impl Clone for VariationalParam
impl Clone for VariationalParam
Source§fn clone(&self) -> VariationalParam
fn clone(&self) -> VariationalParam
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more