Struct Gamma

Source
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.

Gamma distribution

§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.


  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 

Implementations§

Source§

impl<F> Gamma<F>

Source

pub fn new(shape: F, scale: F) -> Result<Gamma<F>, Error>

Construct an object representing the Gamma(shape, scale) distribution.

Trait Implementations§

Source§

impl<F> Clone for Gamma<F>

Source§

fn clone(&self) -> Gamma<F>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F> Debug for Gamma<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F> Distribution<F> for Gamma<F>

Source§

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> F

Generate a random value of T, using rng as the source of randomness.
Source§

fn sample_iter<R>(self, rng: R) -> Iter<Self, R, T>
where R: Rng, Self: Sized,

Create an iterator that generates random values of T, using rng as the source of randomness. Read more
Source§

fn map<F, S>(self, func: F) -> Map<Self, F, T, S>
where F: Fn(T) -> S, Self: Sized,

Map sampled values to type S Read more
Source§

impl<F> PartialEq for Gamma<F>

Source§

fn eq(&self, other: &Gamma<F>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<F> Copy for Gamma<F>

Source§

impl<F> StructuralPartialEq for Gamma<F>

Auto Trait Implementations§

§

impl<F> Freeze for Gamma<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for Gamma<F>
where F: RefUnwindSafe,

§

impl<F> Send for Gamma<F>
where F: Send,

§

impl<F> Sync for Gamma<F>
where F: Sync,

§

impl<F> Unpin for Gamma<F>
where F: Unpin,

§

impl<F> UnwindSafe for Gamma<F>
where F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.