Skip to main content

MeanFieldGuide

Struct MeanFieldGuide 

Source
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

Source

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.

Source

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);
Source

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).

Source

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

Source§

fn clone(&self) -> MeanFieldGuide

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 MeanFieldGuide

Source§

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

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

impl Default for MeanFieldGuide

Source§

fn default() -> Self

Returns the “default value” for a type. 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