Model

Enum Model 

Source
pub enum Model<A> {
    Pure(A),
    SampleF64 {
        addr: Address,
        dist: Box<dyn Distribution<f64>>,
        k: Box<dyn FnOnce(f64) -> Model<A> + Send + 'static>,
    },
    SampleBool {
        addr: Address,
        dist: Box<dyn Distribution<bool>>,
        k: Box<dyn FnOnce(bool) -> Model<A> + Send + 'static>,
    },
    SampleU64 {
        addr: Address,
        dist: Box<dyn Distribution<u64>>,
        k: Box<dyn FnOnce(u64) -> Model<A> + Send + 'static>,
    },
    SampleUsize {
        addr: Address,
        dist: Box<dyn Distribution<usize>>,
        k: Box<dyn FnOnce(usize) -> Model<A> + Send + 'static>,
    },
    ObserveF64 {
        addr: Address,
        dist: Box<dyn Distribution<f64>>,
        value: f64,
        k: Box<dyn FnOnce(()) -> Model<A> + Send + 'static>,
    },
    ObserveBool {
        addr: Address,
        dist: Box<dyn Distribution<bool>>,
        value: bool,
        k: Box<dyn FnOnce(()) -> Model<A> + Send + 'static>,
    },
    ObserveU64 {
        addr: Address,
        dist: Box<dyn Distribution<u64>>,
        value: u64,
        k: Box<dyn FnOnce(()) -> Model<A> + Send + 'static>,
    },
    ObserveUsize {
        addr: Address,
        dist: Box<dyn Distribution<usize>>,
        value: usize,
        k: Box<dyn FnOnce(()) -> Model<A> + Send + 'static>,
    },
    Factor {
        logw: LogF64,
        k: Box<dyn FnOnce(()) -> Model<A> + Send + 'static>,
    },
}
Expand description

Model<A> represents a probabilistic program that yields a value of type A when executed by a handler. Models are built from four variants: Pure, Sample*, Observe*, and Factor.

Example:

// Deterministic value
let m = pure(42.0);

// Sample from distribution
let s = sample(addr!("x"), Normal::new(0.0, 1.0).unwrap());

// Dependent sampling
let chain = s.bind(|x| sample(addr!("y"), Normal::new(x, 0.5).unwrap()));

Variants§

§

Pure(A)

A deterministic computation yielding a pure value.

§

SampleF64

Sample from an f64 distribution (continuous distributions).

Fields

§addr: Address

Unique identifier for this sampling site.

§dist: Box<dyn Distribution<f64>>

Distribution to sample from.

§k: Box<dyn FnOnce(f64) -> Model<A> + Send + 'static>

Continuation function to apply to the sampled value.

§

SampleBool

Sample from a bool distribution (Bernoulli).

Fields

§addr: Address

Unique identifier for this sampling site.

§dist: Box<dyn Distribution<bool>>

Distribution to sample from.

§k: Box<dyn FnOnce(bool) -> Model<A> + Send + 'static>

Continuation function to apply to the sampled value.

§

SampleU64

Sample from a u64 distribution (Poisson, Binomial).

Fields

§addr: Address

Unique identifier for this sampling site.

§dist: Box<dyn Distribution<u64>>

Distribution to sample from.

§k: Box<dyn FnOnce(u64) -> Model<A> + Send + 'static>

Continuation function to apply to the sampled value.

§

SampleUsize

Sample from a usize distribution (Categorical).

Fields

§addr: Address

Unique identifier for this sampling site.

§dist: Box<dyn Distribution<usize>>

Distribution to sample from.

§k: Box<dyn FnOnce(usize) -> Model<A> + Send + 'static>

Continuation function to apply to the sampled value.

§

ObserveF64

Observe/condition on an f64 value.

Fields

§addr: Address

Unique identifier for this observation site.

§dist: Box<dyn Distribution<f64>>

Distribution that generates the observed value.

§value: f64

The observed value to condition on.

§k: Box<dyn FnOnce(()) -> Model<A> + Send + 'static>

Continuation function (always receives unit).

§

ObserveBool

Observe/condition on a bool value.

Fields

§addr: Address

Unique identifier for this observation site.

§dist: Box<dyn Distribution<bool>>

Distribution that generates the observed value.

§value: bool

The observed value to condition on.

§k: Box<dyn FnOnce(()) -> Model<A> + Send + 'static>

Continuation function (always receives unit).

§

ObserveU64

Observe/condition on a u64 value.

Fields

§addr: Address

Unique identifier for this observation site.

§dist: Box<dyn Distribution<u64>>

Distribution that generates the observed value.

§value: u64

The observed value to condition on.

§k: Box<dyn FnOnce(()) -> Model<A> + Send + 'static>

Continuation function (always receives unit).

§

ObserveUsize

Observe/condition on a usize value.

Fields

§addr: Address

Unique identifier for this observation site.

§dist: Box<dyn Distribution<usize>>

Distribution that generates the observed value.

§value: usize

The observed value to condition on.

§k: Box<dyn FnOnce(()) -> Model<A> + Send + 'static>

Continuation function (always receives unit).

§

Factor

Add a log-weight factor to the model.

Fields

§logw: LogF64

Log-weight to add to the model’s total weight.

§k: Box<dyn FnOnce(()) -> Model<A> + Send + 'static>

Continuation function (always receives unit).

Trait Implementations§

Source§

impl<A: 'static> ModelExt<A> for Model<A>

Source§

fn bind<B>(self, k: impl FnOnce(A) -> Model<B> + Send + 'static) -> Model<B>

Monadic bind operation (>>=). Read more
Source§

fn map<B>(self, f: impl FnOnce(A) -> B + Send + 'static) -> Model<B>

Apply a function, f, to transform the result of this model. This is the functor map operation - it transforms the output of a model without adding any additional probabilistic behavior. Read more
Source§

fn and_then<B>(self, k: impl FnOnce(A) -> Model<B> + Send + 'static) -> Model<B>

Alias for bind - chains dependent probabilistic computations. This method provides a more familiar interface for Rust developers used to Option::and_then and Result::and_then. Read more

Auto Trait Implementations§

§

impl<A> Freeze for Model<A>
where A: Freeze,

§

impl<A> !RefUnwindSafe for Model<A>

§

impl<A> Send for Model<A>
where A: Send,

§

impl<A> !Sync for Model<A>

§

impl<A> Unpin for Model<A>
where A: Unpin,

§

impl<A> !UnwindSafe for Model<A>

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> 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, 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