pub struct Poisson { /* private fields */ }Expand description
§The Poisson Distribution
§Description
Density, distribution function, quantile function and random generation for the Poisson distribution with parameter lambda.
§Arguments
- lambda: (non-negative) means.
§Details
The Poisson distribution has density
$ p(x) = \lambda^x \frac{exp(-\lambda)}{x!} $
for x = 0, 1, 2, … . The mean and variance are $ E(X) = Var(X) = \lambda $.
Note that $ \lambda = 0 $ is really a limit case (setting 0^0 = 1) resulting in a point mass at 0, see also the example.
If an element of x is not integer, the result of dpois is zero, with a warning. p(x) is computed using Loader’s algorithm, see the reference in dbinom.
The quantile is right continuous: qpois(p, lambda) is the smallest integer x such that $P(X ≤ x) ≥ p$.
Setting lower.tail = FALSE allows to get much more precise results when the default, lower.tail = TRUE would return 1, see the example below.
§Density Plot
let pois = PoissonBuilder::new().build();
let x = <[f64]>::sequence_by(-1.0, 7.0, 0.001);
let y = x
.iter()
.map(|x| pois.density(x).unwrap())
.collect::<Vec<_>>();
let root = SVGBackend::new("density.svg", (1024, 768)).into_drawing_area();
Plot::new()
.with_options(PlotOptions {
x_axis_label: "x".to_string(),
y_axis_label: "density".to_string(),
..Default::default()
})
.with_plottable(Line {
x,
y,
color: BLACK,
..Default::default()
})
.plot(&root)
.unwrap();§Source
dpois uses C code contributed by Catherine Loader (see dbinom).
ppois uses pgamma.
qpois uses the Cornish–Fisher Expansion to include a skewness correction to a normal approximation, followed by a search.
rpois uses
Ahrens, J. H. and Dieter, U. (1982). Computer generation of Poisson deviates from modified normal distributions. ACM Transactions on Mathematical Software, 8, 163–179.
§See Also
Distributions for other standard distributions, including dbinom for the binomial and dnbinom for the negative binomial distribution.
poisson.test.
§Examples
// Should be 1
let x = (0..=7).collect::<Vec<_>>();
let pois = PoissonBuilder::new().with_lambda(1).build();
let r = x
.iter()
.map(|x| -(pois.density(x).unwrap() * gamma(1 + x).unwrap().unwrap()).ln())
.collect::<Vec<_>>();
println!("{r:?}");let pois = PoissonBuilder::new().with_lambda(4).build();
let mut rng = MersenneTwister::new();
rng.set_seed(1);
let mut r = (0..50)
.map(|_| pois.random_sample(&mut rng).unwrap() as usize)
.fold(HashMap::new(), |mut acc, r| {
*acc.entry(r).or_insert(0) += 1;
acc
})
.into_iter()
.collect::<Vec<_>>();
r.sort_by(|(i1, _), (i2, _)| i1.cmp(i2));
for (key, index) in &r {
println!("{key:2}: {index}");
}Using lower tail directly fixes the cancellation (values becoming 0)
let x = (15..=25).collect::<Vec<_>>();
let pois = PoissonBuilder::new().with_lambda(100).build();
let r1 = x
.iter()
.map(|x| 1.0 - pois.probability(x * 10, true).unwrap())
.collect::<Vec<_>>();
println!("{r1:?}");
let r2 = x
.iter()
.map(|x| pois.probability(x * 10, false).unwrap())
.collect::<Vec<_>>();
println!("{r2:?}");let x = <[f64]>::sequence_by(-0.01, 5.0, 0.01);
let pois = PoissonBuilder::new().with_lambda(1).build();
let y1 = x
.iter()
.map(|x| pois.probability(x, true).unwrap())
.collect::<Vec<_>>();
let binom = BinomialBuilder::new()
.with_size(100)
.with_success_probability(0.01)
.build();
let y2 = x
.iter()
.map(|x| binom.probability(x, true).unwrap())
.collect::<Vec<_>>();
let root = SVGBackend::new("prob_plots.svg", (1024, 768)).into_drawing_area();
Plot::new()
.with_options(PlotOptions {
x_axis_label: "x".to_string(),
y_axis_label: "F(x)".to_string(),
plot_bottom: 0.5,
title: "Poisson(1) CDF".to_string(),
..Default::default()
})
.with_plottable(Line {
x: x.clone(),
y: y1,
color: BLACK,
..Default::default()
})
.plot(&root)
.unwrap();
Plot::new()
.with_options(PlotOptions {
x_axis_label: "x".to_string(),
y_axis_label: "F(x)".to_string(),
plot_top: 0.5,
title: "Binomial(100, 0.01) CDF".to_string(),
..Default::default()
})
.with_plottable(Line {
x,
y: y2,
color: BLACK,
..Default::default()
})
.plot(&root)
.unwrap();assert_eq!(
PoissonBuilder::new()
.with_lambda(0)
.build()
.density(0)
.unwrap(),
1.0
);
assert_eq!(
PoissonBuilder::new()
.with_lambda(0)
.build()
.probability(0, true)
.unwrap(),
1.0
);
assert_eq!(
PoissonBuilder::new()
.with_lambda(0)
.build()
.quantile(1, true)
.unwrap(),
0.0
);Trait Implementations§
Source§impl Distribution for Poisson
impl Distribution for Poisson
Source§fn log_density<R: Into<Real64>>(&self, x: R) -> Real64
fn log_density<R: Into<Real64>>(&self, x: R) -> Real64
Source§fn probability<R: Into<Real64>>(&self, q: R, lower_tail: bool) -> Probability64
fn probability<R: Into<Real64>>(&self, q: R, lower_tail: bool) -> Probability64
Source§fn log_probability<R: Into<Real64>>(
&self,
q: R,
lower_tail: bool,
) -> LogProbability64
fn log_probability<R: Into<Real64>>( &self, q: R, lower_tail: bool, ) -> LogProbability64
Source§fn quantile<P: Into<Probability64>>(&self, p: P, lower_tail: bool) -> Real64
fn quantile<P: Into<Probability64>>(&self, p: P, lower_tail: bool) -> Real64
Source§fn log_quantile<LP: Into<LogProbability64>>(
&self,
p: LP,
lower_tail: bool,
) -> Real64
fn log_quantile<LP: Into<LogProbability64>>( &self, p: LP, lower_tail: bool, ) -> Real64
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
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>
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>
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<T> Pointable for T
impl<T> Pointable for T
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>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
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
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.