1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // ignore-lexer-test FIXME #15679
13 //! The Gamma and derived distributions.
18 use super::normal::StandardNormal;
19 use super::{IndependentSample, Sample, Exp};
21 /// The Gamma distribution `Gamma(shape, scale)` distribution.
23 /// The density function of this distribution is
26 /// f(x) = x^(k - 1) * exp(-x / θ) / (Γ(k) * θ^k)
29 /// where `Γ` is the Gamma function, `k` is the shape and `θ` is the
30 /// scale and both `k` and `θ` are strictly positive.
32 /// The algorithm used is that described by Marsaglia & Tsang 2000[1],
33 /// falling back to directly sampling from an Exponential for `shape
34 /// == 1`, and using the boosting technique described in [1] for
41 /// use std::rand::distributions::{IndependentSample, Gamma};
43 /// let gamma = Gamma::new(2.0, 5.0);
44 /// let v = gamma.ind_sample(&mut rand::task_rng());
45 /// println!("{} is from a Gamma(2, 5) distribution", v);
48 /// [1]: George Marsaglia and Wai Wan Tsang. 2000. "A Simple Method
49 /// for Generating Gamma Variables" *ACM Trans. Math. Softw.* 26, 3
51 /// 363-372. DOI:[10.1145/358407.358414](http://doi.acm.org/10.1145/358407.358414)
57 Large(GammaLargeShape),
59 Small(GammaSmallShape)
62 // These two helpers could be made public, but saving the
63 // match-on-Gamma-enum branch from using them directly (e.g. if one
64 // knows that the shape is always > 1) doesn't appear to be much
67 /// Gamma distribution where the shape parameter is less than 1.
69 /// Note, samples from this require a compulsory floating-point `pow`
70 /// call, which makes it significantly slower than sampling from a
71 /// gamma distribution where the shape parameter is greater than or
74 /// See `Gamma` for sampling from a Gamma distribution with general
76 struct GammaSmallShape {
78 large_shape: GammaLargeShape
81 /// Gamma distribution where the shape parameter is larger than 1.
83 /// See `Gamma` for sampling from a Gamma distribution with general
85 struct GammaLargeShape {
92 /// Construct an object representing the `Gamma(shape, scale)`
95 /// Fails if `shape <= 0` or `scale <= 0`.
96 pub fn new(shape: f64, scale: f64) -> Gamma {
97 assert!(shape > 0.0, "Gamma::new called with shape <= 0");
98 assert!(scale > 0.0, "Gamma::new called with scale <= 0");
100 let repr = match shape {
101 1.0 => One(Exp::new(1.0 / scale)),
102 0.0 ... 1.0 => Small(GammaSmallShape::new_raw(shape, scale)),
103 _ => Large(GammaLargeShape::new_raw(shape, scale))
109 impl GammaSmallShape {
110 fn new_raw(shape: f64, scale: f64) -> GammaSmallShape {
112 inv_shape: 1. / shape,
113 large_shape: GammaLargeShape::new_raw(shape + 1.0, scale)
118 impl GammaLargeShape {
119 fn new_raw(shape: f64, scale: f64) -> GammaLargeShape {
120 let d = shape - 1. / 3.;
123 c: 1. / (9. * d).sqrt(),
129 impl Sample<f64> for Gamma {
130 fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
132 impl Sample<f64> for GammaSmallShape {
133 fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
135 impl Sample<f64> for GammaLargeShape {
136 fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
139 impl IndependentSample<f64> for Gamma {
140 fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
142 Small(ref g) => g.ind_sample(rng),
143 One(ref g) => g.ind_sample(rng),
144 Large(ref g) => g.ind_sample(rng),
148 impl IndependentSample<f64> for GammaSmallShape {
149 fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
150 let Open01(u) = rng.gen::<Open01<f64>>();
152 self.large_shape.ind_sample(rng) * u.powf(self.inv_shape)
155 impl IndependentSample<f64> for GammaLargeShape {
156 fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
158 let StandardNormal(x) = rng.gen::<StandardNormal>();
159 let v_cbrt = 1.0 + self.c * x;
160 if v_cbrt <= 0.0 { // a^3 <= 0 iff a <= 0
164 let v = v_cbrt * v_cbrt * v_cbrt;
165 let Open01(u) = rng.gen::<Open01<f64>>();
168 if u < 1.0 - 0.0331 * x_sqr * x_sqr ||
169 u.ln() < 0.5 * x_sqr + self.d * (1.0 - v + v.ln()) {
170 return self.d * v * self.scale
176 /// The chi-squared distribution `χ²(k)`, where `k` is the degrees of
179 /// For `k > 0` integral, this distribution is the sum of the squares
180 /// of `k` independent standard normal random variables. For other
181 /// `k`, this uses the equivalent characterisation `χ²(k) = Gamma(k/2,
188 /// use std::rand::distributions::{ChiSquared, IndependentSample};
190 /// let chi = ChiSquared::new(11.0);
191 /// let v = chi.ind_sample(&mut rand::task_rng());
192 /// println!("{} is from a χ²(11) distribution", v)
194 pub struct ChiSquared {
195 repr: ChiSquaredRepr,
198 enum ChiSquaredRepr {
199 // k == 1, Gamma(alpha, ..) is particularly slow for alpha < 1,
200 // e.g. when alpha = 1/2 as it would be for this case, so special-
201 // casing and using the definition of N(0,1)^2 is faster.
203 DoFAnythingElse(Gamma),
207 /// Create a new chi-squared distribution with degrees-of-freedom
208 /// `k`. Fails if `k < 0`.
209 pub fn new(k: f64) -> ChiSquared {
210 let repr = if k == 1.0 {
213 assert!(k > 0.0, "ChiSquared::new called with `k` < 0");
214 DoFAnythingElse(Gamma::new(0.5 * k, 2.0))
216 ChiSquared { repr: repr }
219 impl Sample<f64> for ChiSquared {
220 fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
222 impl IndependentSample<f64> for ChiSquared {
223 fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
226 // k == 1 => N(0,1)^2
227 let StandardNormal(norm) = rng.gen::<StandardNormal>();
230 DoFAnythingElse(ref g) => g.ind_sample(rng)
235 /// The Fisher F distribution `F(m, n)`.
237 /// This distribution is equivalent to the ratio of two normalised
238 /// chi-squared distributions, that is, `F(m,n) = (χ²(m)/m) /
245 /// use std::rand::distributions::{FisherF, IndependentSample};
247 /// let f = FisherF::new(2.0, 32.0);
248 /// let v = f.ind_sample(&mut rand::task_rng());
249 /// println!("{} is from an F(2, 32) distribution", v)
254 // denom_dof / numer_dof so that this can just be a straight
255 // multiplication, rather than a division.
260 /// Create a new `FisherF` distribution, with the given
261 /// parameter. Fails if either `m` or `n` are not positive.
262 pub fn new(m: f64, n: f64) -> FisherF {
263 assert!(m > 0.0, "FisherF::new called with `m < 0`");
264 assert!(n > 0.0, "FisherF::new called with `n < 0`");
267 numer: ChiSquared::new(m),
268 denom: ChiSquared::new(n),
273 impl Sample<f64> for FisherF {
274 fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
276 impl IndependentSample<f64> for FisherF {
277 fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
278 self.numer.ind_sample(rng) / self.denom.ind_sample(rng) * self.dof_ratio
282 /// The Student t distribution, `t(nu)`, where `nu` is the degrees of
289 /// use std::rand::distributions::{StudentT, IndependentSample};
291 /// let t = StudentT::new(11.0);
292 /// let v = t.ind_sample(&mut rand::task_rng());
293 /// println!("{} is from a t(11) distribution", v)
295 pub struct StudentT {
301 /// Create a new Student t distribution with `n` degrees of
302 /// freedom. Fails if `n <= 0`.
303 pub fn new(n: f64) -> StudentT {
304 assert!(n > 0.0, "StudentT::new called with `n <= 0`");
306 chi: ChiSquared::new(n),
311 impl Sample<f64> for StudentT {
312 fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
314 impl IndependentSample<f64> for StudentT {
315 fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
316 let StandardNormal(norm) = rng.gen::<StandardNormal>();
317 norm * (self.dof / self.chi.ind_sample(rng)).sqrt()
325 use distributions::{Sample, IndependentSample};
326 use super::{ChiSquared, StudentT, FisherF};
329 fn test_chi_squared_one() {
330 let mut chi = ChiSquared::new(1.0);
331 let mut rng = ::test::rng();
332 for _ in range(0u, 1000) {
333 chi.sample(&mut rng);
334 chi.ind_sample(&mut rng);
338 fn test_chi_squared_small() {
339 let mut chi = ChiSquared::new(0.5);
340 let mut rng = ::test::rng();
341 for _ in range(0u, 1000) {
342 chi.sample(&mut rng);
343 chi.ind_sample(&mut rng);
347 fn test_chi_squared_large() {
348 let mut chi = ChiSquared::new(30.0);
349 let mut rng = ::test::rng();
350 for _ in range(0u, 1000) {
351 chi.sample(&mut rng);
352 chi.ind_sample(&mut rng);
357 fn test_chi_squared_invalid_dof() {
358 ChiSquared::new(-1.0);
363 let mut f = FisherF::new(2.0, 32.0);
364 let mut rng = ::test::rng();
365 for _ in range(0u, 1000) {
367 f.ind_sample(&mut rng);
373 let mut t = StudentT::new(11.0);
374 let mut rng = ::test::rng();
375 for _ in range(0u, 1000) {
377 t.ind_sample(&mut rng);
386 use self::test::Bencher;
387 use std::mem::size_of;
388 use distributions::IndependentSample;
393 fn bench_gamma_large_shape(b: &mut Bencher) {
394 let gamma = Gamma::new(10., 1.0);
395 let mut rng = ::test::weak_rng();
398 for _ in range(0, ::RAND_BENCH_N) {
399 gamma.ind_sample(&mut rng);
402 b.bytes = size_of::<f64>() as u64 * ::RAND_BENCH_N;
406 fn bench_gamma_small_shape(b: &mut Bencher) {
407 let gamma = Gamma::new(0.1, 1.0);
408 let mut rng = ::test::weak_rng();
411 for _ in range(0, ::RAND_BENCH_N) {
412 gamma.ind_sample(&mut rng);
415 b.bytes = size_of::<f64>() as u64 * ::RAND_BENCH_N;