pub struct Poisson { /* private fields */ }
Expand description
Poisson 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(f64::INFINITY).is_err());
assert!(pois.set_rate(f64::NEG_INFINITY).is_err());
assert!(pois.set_rate(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
fn posterior(&self, x: &DataOrSuffStat<'_, u16, Poisson>) -> 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<'_, u16, Poisson>,
) -> f64
fn ln_m_with_cache( &self, cache: &Self::MCache, x: &DataOrSuffStat<'_, u16, Poisson>, ) -> f64
Log marginal likelihood with supplied cache.
source§fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, u16, Poisson>) -> Self::PpCache
fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, u16, Poisson>) -> 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: &u16) -> f64
fn ln_pp_with_cache(&self, cache: &Self::PpCache, y: &u16) -> 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 ConjugatePrior<u32, Poisson> for Gamma
impl ConjugatePrior<u32, Poisson> for Gamma
fn posterior(&self, x: &DataOrSuffStat<'_, u32, Poisson>) -> 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<'_, u32, Poisson>,
) -> f64
fn ln_m_with_cache( &self, cache: &Self::MCache, x: &DataOrSuffStat<'_, u32, Poisson>, ) -> f64
Log marginal likelihood with supplied cache.
source§fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, u32, Poisson>) -> Self::PpCache
fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, u32, Poisson>) -> 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: &u32) -> f64
fn ln_pp_with_cache(&self, cache: &Self::PpCache, y: &u32) -> 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 ConjugatePrior<u8, Poisson> for Gamma
impl ConjugatePrior<u8, Poisson> for Gamma
fn posterior(&self, x: &DataOrSuffStat<'_, u8, Poisson>) -> 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<'_, u8, Poisson>,
) -> f64
fn ln_m_with_cache( &self, cache: &Self::MCache, x: &DataOrSuffStat<'_, u8, Poisson>, ) -> f64
Log marginal likelihood with supplied cache.
source§fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, u8, Poisson>) -> Self::PpCache
fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, u8, Poisson>) -> 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: &u8) -> f64
fn ln_pp_with_cache(&self, cache: &Self::PpCache, y: &u8) -> 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<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 HasDensity<Poisson> for Gamma
impl HasDensity<Poisson> for Gamma
source§impl HasDensity<u16> for Poisson
impl HasDensity<u16> for Poisson
source§impl HasDensity<u32> for Poisson
impl HasDensity<u32> for Poisson
source§impl HasDensity<u8> for Poisson
impl HasDensity<u8> for Poisson
source§impl HasDensity<usize> for Poisson
impl HasDensity<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 Parameterized for Poisson
impl Parameterized for Poisson
type Parameters = f64
fn emit_params(&self) -> Self::Parameters
fn from_params(rate: Self::Parameters) -> Self
source§impl PartialEq for Poisson
impl PartialEq for Poisson
source§impl Sampleable<Poisson> for Gamma
impl Sampleable<Poisson> for Gamma
source§impl Sampleable<u16> for Poisson
impl Sampleable<u16> for Poisson
source§impl Sampleable<u32> for Poisson
impl Sampleable<u32> for Poisson
source§impl Sampleable<u8> for Poisson
impl Sampleable<u8> for Poisson
source§impl Sampleable<usize> for Poisson
impl Sampleable<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<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.