pub struct Poisson { /* private fields */ }
Expand description
Possion distribution over x in {0, 1, … }.
§Example
use rv::prelude::*;
// Create Poisson(λ=5.3)
let pois = Poisson::new(5.3).unwrap();
// CDF at 5
assert!((pois.cdf(&5_u16) - 0.56347339228807169).abs() < 1E-12);
// Draw 100 samples
let mut rng = rand::thread_rng();
let xs: Vec<u32> = pois.sample(100, &mut rng);
assert_eq!(xs.len(), 100)
The Poisson can have two modes. The modes are distinct only if the rate is an integer.
{
let pois = Poisson::new(2.0).unwrap();
let modes: (u32, u32) = pois.mode().unwrap();
assert_eq!(modes, (1, 2))
}
{
let pois = Poisson::new(2.1).unwrap();
let modes: (u32, u32) = pois.mode().unwrap();
assert_eq!(modes, (2, 2))
}
If we know that the rate is not an integer, or we only care about one of the modes, we can call mode for an unsigned type, which will return the leftmost (lowest) mode.
let pois = Poisson::new(2.1).unwrap();
let mode: u32 = pois.mode().unwrap();
assert_eq!(mode, 2)
Implementations§
source§impl Poisson
impl Poisson
sourcepub fn new(rate: f64) -> Result<Self, PoissonError>
pub fn new(rate: f64) -> Result<Self, PoissonError>
Create a new Poisson distribution with given rate
sourcepub fn new_unchecked(rate: f64) -> Self
pub fn new_unchecked(rate: f64) -> Self
Creates a new Poisson without checking whether the parameter is valid.
sourcepub fn rate(&self) -> f64
pub fn rate(&self) -> f64
Get the rate parameter
§Example
let pois = Poisson::new(2.0).unwrap();
assert_eq!(pois.rate(), 2.0);
sourcepub fn set_rate(&mut self, rate: f64) -> Result<(), PoissonError>
pub fn set_rate(&mut self, rate: f64) -> Result<(), PoissonError>
Set the rate parameter
§Example
use rv::dist::Poisson;
let mut pois = Poisson::new(1.0).unwrap();
assert_eq!(pois.rate(), 1.0);
pois.set_rate(1.1).unwrap();
assert_eq!(pois.rate(), 1.1);
Will error for invalid values
assert!(pois.set_rate(1.1).is_ok());
assert!(pois.set_rate(0.0).is_err());
assert!(pois.set_rate(-1.0).is_err());
assert!(pois.set_rate(std::f64::INFINITY).is_err());
assert!(pois.set_rate(std::f64::NEG_INFINITY).is_err());
assert!(pois.set_rate(std::f64::NAN).is_err());
sourcepub fn set_rate_unchecked(&mut self, rate: f64)
pub fn set_rate_unchecked(&mut self, rate: f64)
Set the rate parameter without input validation
Trait Implementations§
source§impl ConjugatePrior<u16, Poisson> for Gamma
impl ConjugatePrior<u16, Poisson> for Gamma
source§fn posterior(&self, x: &DataOrSuffStat<'_, u16, Poisson>) -> Self
fn posterior(&self, x: &DataOrSuffStat<'_, u16, Poisson>) -> 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<'_, u16, Poisson>
) -> f64
fn ln_m_with_cache( &self, cache: &Self::LnMCache, x: &DataOrSuffStat<'_, u16, Poisson> ) -> f64
Log marginal likelihood with supplied cache.
source§fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, u16, Poisson>) -> Self::LnPpCache
fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, u16, Poisson>) -> 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: &u16) -> f64
fn ln_pp_with_cache(&self, cache: &Self::LnPpCache, y: &u16) -> 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 ConjugatePrior<u32, Poisson> for Gamma
impl ConjugatePrior<u32, Poisson> for Gamma
source§fn posterior(&self, x: &DataOrSuffStat<'_, u32, Poisson>) -> Self
fn posterior(&self, x: &DataOrSuffStat<'_, u32, Poisson>) -> 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<'_, u32, Poisson>
) -> f64
fn ln_m_with_cache( &self, cache: &Self::LnMCache, x: &DataOrSuffStat<'_, u32, Poisson> ) -> f64
Log marginal likelihood with supplied cache.
source§fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, u32, Poisson>) -> Self::LnPpCache
fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, u32, Poisson>) -> 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: &u32) -> f64
fn ln_pp_with_cache(&self, cache: &Self::LnPpCache, y: &u32) -> 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 ConjugatePrior<u8, Poisson> for Gamma
impl ConjugatePrior<u8, Poisson> for Gamma
source§fn posterior(&self, x: &DataOrSuffStat<'_, u8, Poisson>) -> Self
fn posterior(&self, x: &DataOrSuffStat<'_, u8, Poisson>) -> 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<'_, u8, Poisson>
) -> f64
fn ln_m_with_cache( &self, cache: &Self::LnMCache, x: &DataOrSuffStat<'_, u8, Poisson> ) -> f64
Log marginal likelihood with supplied cache.
source§fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, u8, Poisson>) -> Self::LnPpCache
fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, u8, Poisson>) -> 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: &u8) -> f64
fn ln_pp_with_cache(&self, cache: &Self::LnPpCache, y: &u8) -> 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<Poisson> for Gamma
impl ContinuousDistr<Poisson> for Gamma
source§impl<'de> Deserialize<'de> for Poisson
impl<'de> Deserialize<'de> for Poisson
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 DiscreteDistr<u16> for Poisson
impl DiscreteDistr<u16> for Poisson
source§impl DiscreteDistr<u32> for Poisson
impl DiscreteDistr<u32> for Poisson
source§impl DiscreteDistr<u8> for Poisson
impl DiscreteDistr<u8> for Poisson
source§impl DiscreteDistr<usize> for Poisson
impl DiscreteDistr<usize> for Poisson
source§impl HasSuffStat<u16> for Poisson
impl HasSuffStat<u16> for Poisson
source§impl HasSuffStat<u32> for Poisson
impl HasSuffStat<u32> for Poisson
source§impl HasSuffStat<u8> for Poisson
impl HasSuffStat<u8> for Poisson
source§impl HasSuffStat<usize> for Poisson
impl HasSuffStat<usize> for Poisson
source§impl KlDivergence for Poisson
impl KlDivergence for Poisson
source§impl PartialEq for Poisson
impl PartialEq for Poisson
source§impl Rv<u16> for Poisson
impl Rv<u16> for Poisson
source§impl Rv<u32> for Poisson
impl Rv<u32> for Poisson
source§impl Rv<usize> for Poisson
impl Rv<usize> for Poisson
Auto Trait Implementations§
impl !Freeze for Poisson
impl RefUnwindSafe for Poisson
impl Send for Poisson
impl Sync for Poisson
impl Unpin for Poisson
impl UnwindSafe for Poisson
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.