Struct rv::dist::Beta[][src]

pub struct Beta { /* fields omitted */ }

Beta distribution, Beta(α, β) over x in (0, 1).

Examples

Beta as a conjugate prior for Bernoulli

use rv::prelude::*;

// A prior that encodes our strong belief that coins are fair:
let beta = Beta::new(5.0, 5.0).unwrap();

// The posterior predictive probability that a coin will come up heads given
// no new observations.
let p_prior_heads = beta.pp(&true, &DataOrSuffStat::None); // 0.5
assert!((p_prior_heads - 0.5).abs() < 1E-12);

// Five Bernoulli trials. We flipped a coin five times and it came up head
// four times.
let flips = vec![true, true, false, true, true];

// The posterior predictive probability that a coin will come up heads given
// the five flips we just saw.
let p_pred_heads = beta.pp(&true, &DataOrSuffStat::Data(&flips)); // 9/15
assert!((p_pred_heads - 3.0/5.0).abs() < 1E-12);

Implementations

impl Beta[src]

pub fn new(alpha: f64, beta: f64) -> Result<Self, BetaError>[src]

Create a Beta distribution with even density over (0, 1).

Example

// Uniform
let beta_unif = Beta::new(1.0, 1.0);
assert!(beta_unif.is_ok());

// Jefferey's prior
let beta_jeff  = Beta::new(0.5, 0.5);
assert!(beta_jeff.is_ok());

// Invalid negative parameter
let beta_nope  = Beta::new(-5.0, 1.0);
assert!(beta_nope.is_err());

pub fn new_unchecked(alpha: f64, beta: f64) -> Self[src]

Creates a new Beta without checking whether the parameters are valid.

pub fn uniform() -> Self[src]

Create a Beta distribution with even density over (0, 1).

Example

let beta = Beta::uniform();
assert_eq!(beta, Beta::new(1.0, 1.0).unwrap());

pub fn jeffreys() -> Self[src]

Create a Beta distribution with the Jeffrey’s parameterization, Beta(0.5, 0.5).

Example

let beta = Beta::jeffreys();
assert_eq!(beta, Beta::new(0.5, 0.5).unwrap());

pub fn alpha(&self) -> f64[src]

Get the alpha parameter

Example

let beta = Beta::new(1.0, 5.0).unwrap();
assert_eq!(beta.alpha(), 1.0);

pub fn set_alpha(&mut self, alpha: f64) -> Result<(), BetaError>[src]

Set the alpha parameter

Example

let mut beta = Beta::new(1.0, 5.0).unwrap();

beta.set_alpha(2.0).unwrap();
assert_eq!(beta.alpha(), 2.0);

Will error for invalid values

assert!(beta.set_alpha(0.1).is_ok());
assert!(beta.set_alpha(0.0).is_err());
assert!(beta.set_alpha(-1.0).is_err());
assert!(beta.set_alpha(std::f64::INFINITY).is_err());
assert!(beta.set_alpha(std::f64::NAN).is_err());

pub fn set_alpha_unchecked(&mut self, alpha: f64)[src]

Set alpha without input validation

pub fn beta(&self) -> f64[src]

Get the beta parameter

Example

let beta = Beta::new(1.0, 5.0).unwrap();
assert_eq!(beta.beta(), 5.0);

pub fn set_beta(&mut self, beta: f64) -> Result<(), BetaError>[src]

Set the beta parameter

Example

let mut beta = Beta::new(1.0, 5.0).unwrap();

beta.set_beta(2.0).unwrap();
assert_eq!(beta.beta(), 2.0);

Will error for invalid values

assert!(beta.set_beta(0.1).is_ok());
assert!(beta.set_beta(0.0).is_err());
assert!(beta.set_beta(-1.0).is_err());
assert!(beta.set_beta(std::f64::INFINITY).is_err());
assert!(beta.set_beta(std::f64::NAN).is_err());

pub fn set_beta_unchecked(&mut self, beta: f64)[src]

Set beta without input validation

Trait Implementations

impl Cdf<f32> for Beta[src]

impl Cdf<f64> for Beta[src]

impl Clone for Beta[src]

impl<X: Booleable> ConjugatePrior<X, Bernoulli> for Beta[src]

type Posterior = Self

Type of the posterior distribution

type LnMCache = f64

Type of the ln_m cache

type LnPpCache = (f64, f64)

Type of the ln_pp cache

impl ContinuousDistr<Bernoulli> for Beta[src]

impl ContinuousDistr<f32> for Beta[src]

impl ContinuousDistr<f64> for Beta[src]

impl Debug for Beta[src]

impl Default for Beta[src]

impl Display for Beta[src]

impl Entropy for Beta[src]

impl Kurtosis for Beta[src]

impl Mean<f32> for Beta[src]

impl Mean<f64> for Beta[src]

impl Mode<f32> for Beta[src]

impl Mode<f64> for Beta[src]

impl PartialEq<Beta> for Beta[src]

impl Rv<Bernoulli> for Beta[src]

impl Rv<f32> for Beta[src]

impl Rv<f64> for Beta[src]

impl Skewness for Beta[src]

impl Support<Bernoulli> for Beta[src]

impl Support<f32> for Beta[src]

impl Support<f64> for Beta[src]

impl Variance<f64> for Beta[src]

Auto Trait Implementations

impl RefUnwindSafe for Beta

impl Send for Beta

impl Sync for Beta

impl Unpin for Beta

impl UnwindSafe for Beta

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

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

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

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