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::from(&vec![])); // 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
impl Beta
sourcepub fn new(alpha: f64, beta: f64) -> Result<Self, BetaError>
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());
sourcepub fn new_unchecked(alpha: f64, beta: f64) -> Self
pub fn new_unchecked(alpha: f64, beta: f64) -> Self
Creates a new Beta without checking whether the parameters are valid.
sourcepub fn uniform() -> Self
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());
sourcepub fn jeffreys() -> Self
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());
sourcepub fn alpha(&self) -> f64
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);
sourcepub fn set_alpha(&mut self, alpha: f64) -> Result<(), BetaError>
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(f64::INFINITY).is_err());
assert!(beta.set_alpha(f64::NAN).is_err());
sourcepub fn set_alpha_unchecked(&mut self, alpha: f64)
pub fn set_alpha_unchecked(&mut self, alpha: f64)
Set alpha without input validation
sourcepub fn beta(&self) -> f64
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);
sourcepub fn set_beta(&mut self, beta: f64) -> Result<(), BetaError>
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(f64::INFINITY).is_err());
assert!(beta.set_beta(f64::NAN).is_err());
sourcepub fn set_beta_unchecked(&mut self, beta: f64)
pub fn set_beta_unchecked(&mut self, beta: f64)
Set beta without input validation
Trait Implementations§
source§impl<X: Booleable> ConjugatePrior<X, Bernoulli> for Beta
impl<X: Booleable> ConjugatePrior<X, Bernoulli> for Beta
fn posterior(&self, x: &DataOrSuffStat<'_, X, Bernoulli>) -> Self
source§fn ln_m_cache(&self) -> Self::MCache
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, Bernoulli>,
) -> f64
fn ln_m_with_cache( &self, cache: &Self::MCache, x: &DataOrSuffStat<'_, X, Bernoulli>, ) -> f64
Log marginal likelihood with supplied cache.
source§fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, X, Bernoulli>) -> Self::PpCache
fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, X, Bernoulli>) -> Self::PpCache
Compute the cache for the Log posterior predictive of y given x. Read more
source§fn ln_pp_with_cache(&self, cache: &Self::PpCache, y: &X) -> f64
fn ln_pp_with_cache(&self, cache: &Self::PpCache, y: &X) -> f64
Log posterior predictive of y given x with supplied ln(norm)
source§fn posterior_from_suffstat(&self, stat: &Fx::Stat) -> Self::Posterior
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
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
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
fn m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64
Marginal likelihood of x
fn pp_with_cache(&self, cache: &Self::PpCache, y: &X) -> f64
source§impl ContinuousDistr<Bernoulli> for Beta
impl ContinuousDistr<Bernoulli> for Beta
source§impl ContinuousDistr<f32> for Beta
impl ContinuousDistr<f32> for Beta
source§impl ContinuousDistr<f64> for Beta
impl ContinuousDistr<f64> for Beta
source§impl<'de> Deserialize<'de> for Beta
impl<'de> Deserialize<'de> for Beta
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
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 From<&UnitPowerLaw> for Beta
impl From<&UnitPowerLaw> for Beta
source§fn from(powlaw: &UnitPowerLaw) -> Beta
fn from(powlaw: &UnitPowerLaw) -> Beta
Converts to this type from the input type.
source§impl HasDensity<Bernoulli> for Beta
impl HasDensity<Bernoulli> for Beta
source§impl HasDensity<f32> for Beta
impl HasDensity<f32> for Beta
source§impl HasDensity<f64> for Beta
impl HasDensity<f64> for Beta
source§impl HasSuffStat<f32> for Beta
impl HasSuffStat<f32> for Beta
source§impl HasSuffStat<f64> for Beta
impl HasSuffStat<f64> for Beta
source§impl Parameterized for Beta
impl Parameterized for Beta
type Parameters = BetaParameters
fn emit_params(&self) -> Self::Parameters
fn from_params(params: Self::Parameters) -> Self
source§impl PartialEq for Beta
impl PartialEq for Beta
source§impl Sampleable<Bernoulli> for Beta
impl Sampleable<Bernoulli> for Beta
source§impl Sampleable<f32> for Beta
impl Sampleable<f32> for Beta
source§impl Sampleable<f64> for Beta
impl Sampleable<f64> for Beta
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read moresource§fn is_in_subset(&self) -> bool
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.