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
paramter.
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(std::f64::INFINITY).is_err());
assert!(b.set_p(std::f64::NEG_INFINITY).is_err());
assert!(b.set_p(std::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
source§fn posterior(&self, x: &DataOrSuffStat<'_, X, Bernoulli>) -> Self
fn posterior(&self, x: &DataOrSuffStat<'_, X, Bernoulli>) -> Self
Computes the posterior distribution from the data
source§fn ln_m_cache(&self) -> Self::LnMCache
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
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
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
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
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
source§impl ContinuousDistr<Bernoulli> for Beta
impl ContinuousDistr<Bernoulli> for Beta
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<X: Booleable> HasSuffStat<X> for Bernoulli
impl<X: Booleable> HasSuffStat<X> for Bernoulli
source§impl KlDivergence for Bernoulli
impl KlDivergence for Bernoulli
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 moreimpl 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<Fx> Rv<Datum> for Fxwhere
Fx: RvDatum,
impl<Fx> Rv<Datum> for Fxwhere
Fx: RvDatum,
source§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.