pub struct Poisson<F>(/* private fields */)
where
F: Float + FloatConst,
StandardUniform: Distribution<F>;
Expand description
The Poisson distribution Poisson(λ)
.
The Poisson distribution is a discrete probability distribution with
rate parameter λ
(lambda
). It models the number of events occurring in a fixed
interval of time or space.
This distribution has density function:
f(k) = λ^k * exp(-λ) / k!
for k >= 0
.
§Plot
The following plot shows the Poisson distribution with various values of λ
.
Note how the expected number of events increases with λ
.
§Example
use rand_distr::{Poisson, Distribution};
let poi = Poisson::new(2.0).unwrap();
let v: f64 = poi.sample(&mut rand::rng());
println!("{} is from a Poisson(2) distribution", v);
§Integer vs FP return type
This implementation uses floating-point (FP) logic internally.
Due to the parameter limit λ < Self::MAX_LAMBDA
, it
statistically impossible to sample a value larger u64::MAX
. As such, it
is reasonable to cast generated samples to u64
using as
:
distr.sample(&mut rng) as u64
(and memory safe since Rust 1.45).
Similarly, when λ < 4.2e9
it can be safely assumed that samples are less
than u32::MAX
.
Implementations§
Source§impl<F> Poisson<F>
impl<F> Poisson<F>
Sourcepub const MAX_LAMBDA: f64 = 1.844E+19f64
pub const MAX_LAMBDA: f64 = 1.844E+19f64
The maximum supported value of lambda
This value was selected such that
MAX_LAMBDA + 1e6 * sqrt(MAX_LAMBDA) < 2^64 - 1
,
thus ensuring that the probability of sampling a value larger than
u64::MAX
is less than 1e-1000.
Applying this limit also solves #1312.