pub struct Gamma<F>{ /* private fields */ }Expand description
The Gamma distribution Gamma(k, θ).
The Gamma distribution is a continuous probability distribution
with shape parameter k > 0 (number of events) and
scale parameter θ > 0 (mean waiting time between events).
It describes the time until k events occur in a Poisson
process with rate 1/θ. It is the generalization of the
Exponential distribution.
§Density function
f(x) = x^(k - 1) * exp(-x / θ) / (Γ(k) * θ^k) for x > 0,
where Γ is the gamma function.
§Plot
The following plot illustrates the Gamma distribution with
various values of k and θ.
Curves with θ = 1 are more saturated, while corresponding
curves with θ = 2 have a lighter color.
§Example
use rand_distr::{Distribution, Gamma};
let gamma = Gamma::new(2.0, 5.0).unwrap();
let v = gamma.sample(&mut rand::rng());
println!("{} is from a Gamma(2, 5) distribution", v);§Notes
When the shape (k) or scale (θ) parameters are close to the upper limits
of the floating point type F, the implementation may overflow and produce
inf. On the other hand, when k is relatively close to zero (like 0.005)
and θ is huge (like 1e200), the implementation is likely be affected by
underflow and may fail to produce tiny floating point values (like 1e-200),
returning 0.0 for them instead. The exact thresholds for this to occur
depend on F.
The algorithm used is that described by Marsaglia & Tsang 20001,
falling back to directly sampling from an Exponential for shape == 1, and using the boosting technique described in that paper for
shape < 1.
George Marsaglia and Wai Wan Tsang. 2000. “A Simple Method for Generating Gamma Variables” ACM Trans. Math. Softw. 26, 3 (September 2000), 363-372. DOI:10.1145/358407.358414 ↩