Struct rv::dist::Beta

source ·
pub struct Beta { /* private fields */ }
Expand description

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§

source§

impl Beta

source

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

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());
source

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

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

source

pub fn uniform() -> Self

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());
source

pub fn jeffreys() -> Self

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());
source

pub fn alpha(&self) -> f64

Get the alpha parameter

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

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

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());
source

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

Set alpha without input validation

source

pub fn beta(&self) -> f64

Get the beta parameter

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

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

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());
source

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

Set beta without input validation

Trait Implementations§

source§

impl Cdf<f32> for Beta

source§

fn cdf(&self, x: &f32) -> f64

The value of the Cumulative Density Function at x Read more
source§

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

Survival function, 1 - CDF(x)
source§

impl Cdf<f64> for Beta

source§

fn cdf(&self, x: &f64) -> f64

The value of the Cumulative Density Function at x Read more
source§

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

Survival function, 1 - CDF(x)
source§

impl Clone for Beta

source§

fn clone(&self) -> Beta

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: Booleable> ConjugatePrior<X, Bernoulli> for Beta

§

type Posterior = Beta

Type of the posterior distribution
§

type LnMCache = f64

Type of the ln_m cache
§

type LnPpCache = (f64, f64)

Type of the ln_pp cache
source§

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

Computes the posterior distribution from the data
source§

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

Compute the cache for the log marginal likelihood.
source§

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

Log marginal likelihood with supplied cache.
source§

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

Compute the cache for the Log posterior predictive of y given x. Read more
source§

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

Log posterior predictive of y given x with supplied ln(norm)
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(&self, y: &X, x: &DataOrSuffStat<'_, X, Fx>) -> f64

Posterior Predictive distribution
source§

impl ContinuousDistr<Bernoulli> for Beta

source§

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

The value of the Probability Density Function (PDF) at x Read more
source§

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

The value of the log Probability Density Function (PDF) at x Read more
source§

impl ContinuousDistr<f32> for Beta

source§

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

The value of the Probability Density Function (PDF) at x Read more
source§

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

The value of the log Probability Density Function (PDF) at x Read more
source§

impl ContinuousDistr<f64> for Beta

source§

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

The value of the Probability Density Function (PDF) at x Read more
source§

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

The value of the log Probability Density Function (PDF) at x Read more
source§

impl Debug for Beta

source§

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

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

impl Default for Beta

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for Beta

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Beta

source§

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

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

impl Entropy for Beta

source§

fn entropy(&self) -> f64

The entropy, H(X)
source§

impl From<&Beta> for String

source§

fn from(beta: &Beta) -> String

Converts to this type from the input type.
source§

impl From<&UnitPowerLaw> for Beta

source§

fn from(powlaw: &UnitPowerLaw) -> Beta

Converts to this type from the input type.
source§

impl HasSuffStat<f32> for Beta

§

type Stat = BetaSuffStat

source§

fn empty_suffstat(&self) -> Self::Stat

source§

fn ln_f_stat(&self, stat: &Self::Stat) -> f64

Return the log likelihood for the data represented by the sufficient statistic.
source§

impl HasSuffStat<f64> for Beta

§

type Stat = BetaSuffStat

source§

fn empty_suffstat(&self) -> Self::Stat

source§

fn ln_f_stat(&self, stat: &Self::Stat) -> f64

Return the log likelihood for the data represented by the sufficient statistic.
source§

impl Kurtosis for Beta

source§

impl Mean<f32> for Beta

source§

fn mean(&self) -> Option<f32>

Returns None if the mean is undefined
source§

impl Mean<f64> for Beta

source§

fn mean(&self) -> Option<f64>

Returns None if the mean is undefined
source§

impl Mode<f32> for Beta

source§

fn mode(&self) -> Option<f32>

Returns None if the mode is undefined or is not a single value
source§

impl Mode<f64> for Beta

source§

fn mode(&self) -> Option<f64>

Returns None if the mode is undefined or is not a single value
source§

impl PartialEq for Beta

source§

fn eq(&self, other: &Beta) -> 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 Rv<Bernoulli> for Beta

source§

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

Probability function Read more
source§

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

Single draw from the Rv Read more
source§

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

Probability function Read more
source§

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

Multiple draws of the Rv 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 Rv<f32> for Beta

source§

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

Probability function Read more
source§

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

Single draw from the Rv Read more
source§

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

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 Rv<f64> for Beta

source§

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

Probability function Read more
source§

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

Single draw from the Rv Read more
source§

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

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 Serialize for Beta

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Skewness for Beta

source§

impl Support<Bernoulli> for Beta

source§

fn supports(&self, x: &Bernoulli) -> bool

Returns true if x is in the support of the Rv Read more
source§

impl Support<f32> for Beta

source§

fn supports(&self, x: &f32) -> bool

Returns true if x is in the support of the Rv Read more
source§

impl Support<f64> for Beta

source§

fn supports(&self, x: &f64) -> bool

Returns true if x is in the support of the Rv Read more
source§

impl Variance<f64> for Beta

source§

fn variance(&self) -> Option<f64>

Returns None if the variance is undefined

Auto Trait Implementations§

§

impl !Freeze for Beta

§

impl RefUnwindSafe for Beta

§

impl Send for Beta

§

impl Sync for Beta

§

impl Unpin for Beta

§

impl UnwindSafe for Beta

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<Fx> Rv<Datum> for Fx
where Fx: RvDatum,

source§

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

Probability function Read more
source§

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

Single draw from the Rv Read more
source§

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

Multiple draws of the Rv Read more
source§

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

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

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

Probability function Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

source§

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

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

fn is_in_subset(&self) -> bool

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

fn to_subset_unchecked(&self) -> SS

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

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.
source§

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

source§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

source§

impl<T> DeserializeOwnedAlias for T

source§

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

source§

impl<T> SendAlias for T

source§

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

source§

impl<T> SerializeAlias for T
where T: Serialize,

source§

impl<T> SyncAlias for T