]> git.lizzy.rs Git - rust.git/blob - src/librand/distributions/exponential.rs
doc: remove incomplete sentence
[rust.git] / src / librand / distributions / exponential.rs
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.
4 //
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.
10
11 //! The exponential distribution.
12
13 use core::num::Float;
14
15 use {Rng, Rand};
16 use distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample};
17
18 /// A wrapper around an `f64` to generate Exp(1) random numbers.
19 ///
20 /// See `Exp` for the general exponential distribution.Note that this
21  // has to be unwrapped before use as an `f64` (using either
22 /// `*` or `mem::transmute` is safe).
23 ///
24 /// Implemented via the ZIGNOR variant[1] of the Ziggurat method. The
25 /// exact description in the paper was adjusted to use tables for the
26 /// exponential distribution rather than normal.
27 ///
28 /// [1]: Jurgen A. Doornik (2005). [*An Improved Ziggurat Method to
29 /// Generate Normal Random
30 /// Samples*](http://www.doornik.com/research/ziggurat.pdf). Nuffield
31 /// College, Oxford
32 #[deriving(Copy)]
33 pub struct Exp1(pub f64);
34
35 // This could be done via `-rng.gen::<f64>().ln()` but that is slower.
36 impl Rand for Exp1 {
37     #[inline]
38     fn rand<R:Rng>(rng: &mut R) -> Exp1 {
39         #[inline]
40         fn pdf(x: f64) -> f64 {
41             (-x).exp()
42         }
43         #[inline]
44         fn zero_case<R:Rng>(rng: &mut R, _u: f64) -> f64 {
45             ziggurat_tables::ZIG_EXP_R - rng.gen::<f64>().ln()
46         }
47
48         Exp1(ziggurat(rng, false,
49                       &ziggurat_tables::ZIG_EXP_X,
50                       &ziggurat_tables::ZIG_EXP_F,
51                       pdf, zero_case))
52     }
53 }
54
55 /// The exponential distribution `Exp(lambda)`.
56 ///
57 /// This distribution has density function: `f(x) = lambda *
58 /// exp(-lambda * x)` for `x > 0`.
59 ///
60 /// # Example
61 ///
62 /// ```rust
63 /// use std::rand;
64 /// use std::rand::distributions::{Exp, IndependentSample};
65 ///
66 /// let exp = Exp::new(2.0);
67 /// let v = exp.ind_sample(&mut rand::thread_rng());
68 /// println!("{} is from a Exp(2) distribution", v);
69 /// ```
70 #[deriving(Copy)]
71 pub struct Exp {
72     /// `lambda` stored as `1/lambda`, since this is what we scale by.
73     lambda_inverse: f64
74 }
75
76 impl Exp {
77     /// Construct a new `Exp` with the given shape parameter
78     /// `lambda`. Panics if `lambda <= 0`.
79     pub fn new(lambda: f64) -> Exp {
80         assert!(lambda > 0.0, "Exp::new called with `lambda` <= 0");
81         Exp { lambda_inverse: 1.0 / lambda }
82     }
83 }
84
85 impl Sample<f64> for Exp {
86     fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
87 }
88 impl IndependentSample<f64> for Exp {
89     fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
90         let Exp1(n) = rng.gen::<Exp1>();
91         n * self.lambda_inverse
92     }
93 }
94
95 #[cfg(test)]
96 mod test {
97     use std::prelude::v1::*;
98
99     use distributions::{Sample, IndependentSample};
100     use super::Exp;
101
102     #[test]
103     fn test_exp() {
104         let mut exp = Exp::new(10.0);
105         let mut rng = ::test::rng();
106         for _ in range(0u, 1000) {
107             assert!(exp.sample(&mut rng) >= 0.0);
108             assert!(exp.ind_sample(&mut rng) >= 0.0);
109         }
110     }
111     #[test]
112     #[should_fail]
113     fn test_exp_invalid_lambda_zero() {
114         Exp::new(0.0);
115     }
116     #[test]
117     #[should_fail]
118     fn test_exp_invalid_lambda_neg() {
119         Exp::new(-10.0);
120     }
121 }
122
123 #[cfg(test)]
124 mod bench {
125     extern crate test;
126
127     use std::prelude::v1::*;
128
129     use self::test::Bencher;
130     use std::mem::size_of;
131     use super::Exp;
132     use distributions::Sample;
133
134     #[bench]
135     fn rand_exp(b: &mut Bencher) {
136         let mut rng = ::test::weak_rng();
137         let mut exp = Exp::new(2.71828 * 3.14159);
138
139         b.iter(|| {
140             for _ in range(0, ::RAND_BENCH_N) {
141                 exp.sample(&mut rng);
142             }
143         });
144         b.bytes = size_of::<f64>() as u64 * ::RAND_BENCH_N;
145     }
146 }