Struct rv::ConjugateModel

source ·
pub struct ConjugateModel<X, Fx, Pr>
where Fx: Rv<X> + HasSuffStat<X>, Pr: ConjugatePrior<X, Fx>,
{ /* private fields */ }
Expand description

A wrapper for a complete conjugate model

Paramters

X: The type of the data/observations to be modeled Fx: The type of the likelihood, f(x|θ) Pr: The type of the prior on the parameters of Fx, π(θ)

Implementations§

source§

impl<X, Fx, Pr> ConjugateModel<X, Fx, Pr>
where Fx: Rv<X> + HasSuffStat<X>, Pr: ConjugatePrior<X, Fx>,

source

pub fn new(fx: &Fx, pr: Arc<Pr>) -> Self

Create a new conjugate model

Arguments

fx:

Example
use std::sync::Arc;
use rv::prelude::*;
use rv::ConjugateModel;

let pr = Arc::new(Beta::jeffreys());
let fx = Bernoulli::uniform();
let model = ConjugateModel::<bool, Bernoulli, Beta>::new(&fx, pr);
source

pub fn ln_m(&self) -> f64

Log marginal likelihood, f(obs)

source

pub fn ln_pp(&self, y: &X) -> f64

Log posterior predictive, f(y|obs)

source

pub fn posterior(&self) -> Pr::Posterior

Return the posterior distribution

Example
use std::sync::Arc;
use rv::prelude::*;
use rv::ConjugateModel;

let flips: Vec<bool> = vec![true, false, true, false, false, false];

let pr = Arc::new(Beta::new(1.0, 1.0).unwrap());
let fx = Bernoulli::uniform();
let mut model = ConjugateModel::<bool, Bernoulli, Beta>::new(&fx, pr);

model.observe_many(&flips);

let post = model.posterior();

assert_eq!(post, Beta::new(3.0, 5.0).unwrap());

Trait Implementations§

source§

impl<X: Clone, Fx, Pr> Clone for ConjugateModel<X, Fx, Pr>
where Fx: Rv<X> + HasSuffStat<X> + Clone, Pr: ConjugatePrior<X, Fx> + Clone, Fx::Stat: Clone,

source§

fn clone(&self) -> ConjugateModel<X, Fx, Pr>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<X: Debug, Fx, Pr> Debug for ConjugateModel<X, Fx, Pr>
where Fx: Rv<X> + HasSuffStat<X> + Debug, Pr: ConjugatePrior<X, Fx> + Debug, Fx::Stat: Debug,

source§

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

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

impl<X: PartialEq, Fx, Pr> PartialEq for ConjugateModel<X, Fx, Pr>
where Fx: Rv<X> + HasSuffStat<X> + PartialEq, Pr: ConjugatePrior<X, Fx> + PartialEq, Fx::Stat: PartialEq,

source§

fn eq(&self, other: &ConjugateModel<X, Fx, Pr>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<X: PartialOrd, Fx, Pr> PartialOrd for ConjugateModel<X, Fx, Pr>
where Fx: Rv<X> + HasSuffStat<X> + PartialOrd, Pr: ConjugatePrior<X, Fx> + PartialOrd, Fx::Stat: PartialOrd,

source§

fn partial_cmp(&self, other: &ConjugateModel<X, Fx, Pr>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<X, Fx, Pr> Rv<X> for ConjugateModel<X, Fx, Pr>
where Fx: Rv<X> + HasSuffStat<X>, Pr: ConjugatePrior<X, Fx>,

source§

fn ln_f(&self, x: &X) -> f64

Probability function Read more
source§

fn draw<R: Rng>(&self, rng: &mut R) -> X

Single draw from the Rv Read more
source§

fn sample<R: Rng>(&self, n: usize, rng: &mut R) -> Vec<X>

Multiple draws of the Rv Read more
source§

fn f(&self, x: &X) -> f64

Probability function Read more
source§

fn sample_stream<'r, R: Rng>( &'r self, rng: &'r mut R ) -> Box<dyn Iterator<Item = X> + 'r>

Create a never-ending iterator of samples Read more
source§

impl<X, Fx, Pr> SuffStat<X> for ConjugateModel<X, Fx, Pr>
where Fx: Rv<X> + HasSuffStat<X>, Pr: ConjugatePrior<X, Fx>,

source§

fn n(&self) -> usize

Returns the number of observations
source§

fn observe(&mut self, x: &X)

Assimilate the datum x into the statistic
source§

fn forget(&mut self, x: &X)

Remove the datum x from the statistic
source§

fn observe_many(&mut self, xs: &[X])

Assimilate several observations
source§

fn forget_many(&mut self, xs: &[X])

Forget several observations
source§

impl<X, Fx, Pr> StructuralPartialEq for ConjugateModel<X, Fx, Pr>
where Fx: Rv<X> + HasSuffStat<X>, Pr: ConjugatePrior<X, Fx>,

Auto Trait Implementations§

§

impl<X, Fx, Pr> RefUnwindSafe for ConjugateModel<X, Fx, Pr>

§

impl<X, Fx, Pr> Send for ConjugateModel<X, Fx, Pr>
where Pr: Send + Sync, X: Send, <Fx as HasSuffStat<X>>::Stat: Send,

§

impl<X, Fx, Pr> Sync for ConjugateModel<X, Fx, Pr>
where Pr: Send + Sync, X: Sync, <Fx as HasSuffStat<X>>::Stat: Sync,

§

impl<X, Fx, Pr> Unpin for ConjugateModel<X, Fx, Pr>
where X: Unpin, <Fx as HasSuffStat<X>>::Stat: Unpin,

§

impl<X, Fx, Pr> UnwindSafe for ConjugateModel<X, Fx, Pr>
where Pr: RefUnwindSafe, X: UnwindSafe, <Fx as HasSuffStat<X>>::Stat: UnwindSafe,

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> Same for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T> ToOwned for T
where T: Clone,

§

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

§

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

§

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,

source§

impl<T> SendAlias for T

§

impl<T> SendSyncUnwindSafe for T
where T: Send + Sync + UnwindSafe + ?Sized,

source§

impl<T> SyncAlias for T