Trait rv::traits::ConjugatePrior

source ·
pub trait ConjugatePrior<X, Fx>: Sampleable<Fx>
where Fx: HasDensity<X> + HasSuffStat<X>,
{ type Posterior: Sampleable<Fx>; type MCache; type PpCache; // Required methods fn posterior(&self, x: &DataOrSuffStat<'_, X, Fx>) -> Self::Posterior; fn ln_m_cache(&self) -> Self::MCache; fn ln_m_with_cache( &self, cache: &Self::MCache, x: &DataOrSuffStat<'_, X, Fx>, ) -> f64; fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, X, Fx>) -> Self::PpCache; fn ln_pp_with_cache(&self, cache: &Self::PpCache, y: &X) -> f64; // Provided methods fn posterior_from_suffstat(&self, stat: &Fx::Stat) -> Self::Posterior { ... } fn ln_m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64 { ... } fn ln_pp(&self, y: &X, x: &DataOrSuffStat<'_, X, Fx>) -> f64 { ... } fn m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64 { ... } fn pp_with_cache(&self, cache: &Self::PpCache, y: &X) -> f64 { ... } fn pp(&self, y: &X, x: &DataOrSuffStat<'_, X, Fx>) -> f64 { ... } }
Expand description

A prior on Fx that induces a posterior that is the same form as the prior

§Example

Conjugate analysis of coin flips using Bernoulli with a Beta prior on the success probability.

use rv::traits::ConjugatePrior;
use rv::dist::{Bernoulli, Beta};

let flips = vec![true, false, false];
let prior = Beta::jeffreys();

// If we observe more false than true, the posterior predictive
// probability of true decreases.
let pp_no_obs = prior.pp(&true, &(&vec![]).into());
let pp_obs = prior.pp(&true, &(&flips).into());

assert!(pp_obs < pp_no_obs);

Use a cache to speed up repeated computations.

use rv::traits::*;
use rv::traits::SuffStat;
use rv::dist::{Categorical, SymmetricDirichlet};
use rv::data::{CategoricalSuffStat, DataOrSuffStat};
use std::time::Instant;

let ncats = 10;
let symdir = SymmetricDirichlet::jeffreys(ncats).unwrap();
let mut suffstat = CategoricalSuffStat::new(10);
let mut rng = rand::thread_rng();

Categorical::new(&vec![1.0, 1.0, 5.0, 1.0, 2.0, 1.0, 1.0, 2.0, 1.0, 1.0])
    .unwrap()
    .sample_stream(&mut rng)
    .take(1000)
    .for_each(|x: u8| suffstat.observe(&x));


let stat = DataOrSuffStat::SuffStat(&suffstat);

// Get predictions from predictive distribution using the cache
let t_cache = {
    let t_start = Instant::now();
    let cache = symdir.ln_pp_cache(&stat);
    // Argmax
    let k_max = (0..ncats).fold((0, f64::NEG_INFINITY), |(ix, f), y| {
            let f_r = symdir.ln_pp_with_cache(&cache, &y);
            if f_r > f {
                (y, f_r)
            } else {
                (ix, f)
            }

        });

    assert_eq!(k_max.0, 2);
    t_start.elapsed()
};

// Get predictions from predictive distribution w/o cache
let t_no_cache = {
    let t_start = Instant::now();
    // Argmax
    let k_max = (0..ncats).fold((0, f64::NEG_INFINITY), |(ix, f), y| {
            let f_r = symdir.ln_pp(&y, &stat);
            if f_r > f {
                (y, f_r)
            } else {
                (ix, f)
            }

        });

    assert_eq!(k_max.0, 2);
    t_start.elapsed()
};

// Using cache improves runtime
assert!(t_no_cache.as_nanos() > t_cache.as_nanos());

Required Associated Types§

source

type Posterior: Sampleable<Fx>

Type of the posterior distribution

source

type MCache

Type of the cache for the marginal likelihood

source

type PpCache

Type of the cache for the posterior predictive

Required Methods§

source

fn posterior(&self, x: &DataOrSuffStat<'_, X, Fx>) -> Self::Posterior

source

fn ln_m_cache(&self) -> Self::MCache

Compute the cache for the log marginal likelihood.

source

fn ln_m_with_cache( &self, cache: &Self::MCache, x: &DataOrSuffStat<'_, X, Fx>, ) -> f64

Log marginal likelihood with supplied cache.

source

fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, X, Fx>) -> Self::PpCache

Compute the cache for the Log posterior predictive of y given x.

The cache should encompass all information about x.

source

fn ln_pp_with_cache(&self, cache: &Self::PpCache, y: &X) -> f64

Log posterior predictive of y given x with supplied ln(norm)

Provided Methods§

source

fn posterior_from_suffstat(&self, stat: &Fx::Stat) -> Self::Posterior

Computes the posterior distribution from the data

source

fn ln_m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64

The log marginal likelihood

source

fn ln_pp(&self, y: &X, x: &DataOrSuffStat<'_, X, Fx>) -> f64

Log posterior predictive of y given x

source

fn m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64

Marginal likelihood of x

source

fn pp_with_cache(&self, cache: &Self::PpCache, y: &X) -> f64

source

fn pp(&self, y: &X, x: &DataOrSuffStat<'_, X, Fx>) -> f64

Posterior Predictive distribution

Object Safety§

This trait is not object safe.

Implementors§

source§

impl ConjugatePrior<f64, Gaussian> for NormalGamma

source§

impl ConjugatePrior<f64, Gaussian> for NormalInvChiSquared

source§

impl ConjugatePrior<f64, Gaussian> for NormalInvGamma

source§

impl ConjugatePrior<u8, Poisson> for Gamma

§

type Posterior = Gamma

§

type MCache = f64

§

type PpCache = (f64, f64, f64)

source§

impl ConjugatePrior<u16, Poisson> for Gamma

§

type Posterior = Gamma

§

type MCache = f64

§

type PpCache = (f64, f64, f64)

source§

impl ConjugatePrior<u32, Poisson> for Gamma

§

type Posterior = Gamma

§

type MCache = f64

§

type PpCache = (f64, f64, f64)

source§

impl ConjugatePrior<usize, StickBreakingDiscrete> for StickBreaking

Implementation of the ConjugatePrior trait for the StickBreaking struct.

source§

impl ConjugatePrior<Matrix<f64, Dyn, Const<1>, VecStorage<f64, Dyn, Const<1>>>, MvGaussian> for NormalInvWishart

source§

impl<X: Booleable> ConjugatePrior<X, Bernoulli> for Beta

§

type Posterior = Beta

§

type MCache = f64

§

type PpCache = (f64, f64)

source§

impl<X: Booleable> ConjugatePrior<X, Bernoulli> for UnitPowerLaw

§

type Posterior = Beta

§

type MCache = f64

§

type PpCache = (f64, f64)

source§

impl<X: CategoricalDatum> ConjugatePrior<X, Categorical> for Dirichlet

source§

impl<X: CategoricalDatum> ConjugatePrior<X, Categorical> for SymmetricDirichlet