pub struct Bernoulli { /* private fields */ }
Expand description
Bernoulli distribution with success probability p
§Example
let b = Bernoulli::new(0.75).unwrap();
assert::close(b.pmf(&true), 0.75, 1E-12);
The following example panics because 2 is out of outside the Bernoulli support
ⓘ
let b = Bernoulli::new(0.75).unwrap();
assert!(!b.supports(&2_u8));
b.pmf(&2_u8); // panics
Implementations§
source§impl Bernoulli
impl Bernoulli
sourcepub fn new(p: f64) -> Result<Self, BernoulliError>
pub fn new(p: f64) -> Result<Self, BernoulliError>
Create a new Bernoulli distribution.
§Examples
let b = Bernoulli::new(0.5).unwrap();
let coin_flips: Vec<bool> = b.sample(5, &mut rng);
assert_eq!(coin_flips.len(), 5);
Bernoulli::new
will return an Error
type if given an invalid
parameter.
assert!(Bernoulli::new(-1.0).is_err());
assert!(Bernoulli::new(1.1).is_err());
sourcepub fn new_unchecked(p: f64) -> Self
pub fn new_unchecked(p: f64) -> Self
Creates a new Bernoulli without checking whether parameter value is valid.
sourcepub fn uniform() -> Self
pub fn uniform() -> Self
A Bernoulli distribution with a 50% chance of success
§Example
let b = Bernoulli::uniform();
assert_eq!(b.p(), 0.5);
assert_eq!(b.q(), 0.5);
sourcepub fn p(&self) -> f64
pub fn p(&self) -> f64
Get p, the probability of success.
§Example
let b = Bernoulli::new(0.2).unwrap();
assert_eq!(b.p(), 0.2);
sourcepub fn set_p(&mut self, p: f64) -> Result<(), BernoulliError>
pub fn set_p(&mut self, p: f64) -> Result<(), BernoulliError>
Set p, the probability of success.
§Example
let mut b = Bernoulli::new(0.2).unwrap();
b.set_p(0.5).unwrap();
assert_eq!(b.p(), 0.5);
Will error for invalid values
assert!(b.set_p(0.0).is_ok());
assert!(b.set_p(1.0).is_ok());
assert!(b.set_p(-1.0).is_err());
assert!(b.set_p(1.1).is_err());
assert!(b.set_p(f64::INFINITY).is_err());
assert!(b.set_p(std::f64::NEG_INFINITY).is_err());
assert!(b.set_p(f64::NAN).is_err());
sourcepub fn set_p_unchecked(&mut self, p: f64)
pub fn set_p_unchecked(&mut self, p: f64)
Set p 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<X: Booleable> ConjugatePrior<X, Bernoulli> for UnitPowerLaw
impl<X: Booleable> ConjugatePrior<X, Bernoulli> for UnitPowerLaw
fn posterior(&self, x: &DataOrSuffStat<'_, X, Bernoulli>) -> Beta
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<Bernoulli> for UnitPowerLaw
impl ContinuousDistr<Bernoulli> for UnitPowerLaw
source§impl<'de> Deserialize<'de> for Bernoulli
impl<'de> Deserialize<'de> for Bernoulli
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<X: Booleable> DiscreteDistr<X> for Bernoulli
impl<X: Booleable> DiscreteDistr<X> for Bernoulli
source§impl HasDensity<Bernoulli> for Beta
impl HasDensity<Bernoulli> for Beta
source§impl HasDensity<Bernoulli> for UnitPowerLaw
impl HasDensity<Bernoulli> for UnitPowerLaw
source§impl<X: Booleable> HasDensity<X> for Bernoulli
impl<X: Booleable> HasDensity<X> for Bernoulli
source§impl<X: Booleable> HasSuffStat<X> for Bernoulli
impl<X: Booleable> HasSuffStat<X> for Bernoulli
source§impl KlDivergence for Bernoulli
impl KlDivergence for Bernoulli
source§impl Parameterized for Bernoulli
impl Parameterized for Bernoulli
type Parameters = f64
fn emit_params(&self) -> Self::Parameters
fn from_params(params: Self::Parameters) -> Self
source§impl PartialEq for Bernoulli
impl PartialEq for Bernoulli
source§impl PartialOrd for Bernoulli
impl PartialOrd for Bernoulli
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
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 moresource§impl Sampleable<Bernoulli> for Beta
impl Sampleable<Bernoulli> for Beta
source§impl Sampleable<Bernoulli> for UnitPowerLaw
impl Sampleable<Bernoulli> for UnitPowerLaw
source§impl<X: Booleable> Sampleable<X> for Bernoulli
impl<X: Booleable> Sampleable<X> for Bernoulli
source§impl Support<Bernoulli> for UnitPowerLaw
impl Support<Bernoulli> for UnitPowerLaw
impl StructuralPartialEq for Bernoulli
Auto Trait Implementations§
impl Freeze for Bernoulli
impl RefUnwindSafe for Bernoulli
impl Send for Bernoulli
impl Sync for Bernoulli
impl Unpin for Bernoulli
impl UnwindSafe for Bernoulli
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.