Skip to main content

VariationalParam

Enum VariationalParam 

Source
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-deviation
  • LogNormal - Log-normal approximation for positive variables
  • Beta - 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.

Fields

§mu: f64

Mean parameter.

§log_sigma: f64

Log of standard deviation (for positivity).

§

LogNormal

Log-normal variational distribution for positive variables.

Fields

§mu: f64

Mean of underlying normal.

§log_sigma: f64

Log of standard deviation of underlying normal.

§

Beta

Beta variational distribution for variables in [0,1].

Fields

§log_alpha: f64

Log of first shape parameter (for positivity).

§log_beta: f64

Log of second shape parameter (for positivity).

Implementations§

Source§

impl VariationalParam

Source

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.

Source

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.

Source

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.

Source

pub fn log_prob(&self, x: f64) -> f64

Compute log-probability of a value under this variational distribution.

This is used for computing entropy terms in the ELBO and for evaluating the quality of the variational approximation.

§Arguments
  • x - Value to evaluate
§Returns

Log-probability density at the given value.

Trait Implementations§

Source§

impl Clone for VariationalParam

Source§

fn clone(&self) -> VariationalParam

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for VariationalParam

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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