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 //! The normal and derived distributions.
15 use {Rng, Rand, Open01};
16 use distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample};
18 /// A wrapper around an `f64` to generate N(0, 1) random numbers
19 /// (a.k.a. a standard normal, or Gaussian).
21 /// See `Normal` for the general normal distribution. That this has to
22 /// be unwrapped before use as an `f64` (using either `*` or
23 /// `mem::transmute` is safe).
25 /// Implemented via the ZIGNOR variant[1] of the Ziggurat method.
27 /// [1]: Jurgen A. Doornik (2005). [*An Improved Ziggurat Method to
28 /// Generate Normal Random
29 /// Samples*](http://www.doornik.com/research/ziggurat.pdf). Nuffield
31 #[derive(Copy, Clone)]
32 pub struct StandardNormal(pub f64);
34 impl Rand for StandardNormal {
35 fn rand<R: Rng>(rng: &mut R) -> StandardNormal {
37 fn pdf(x: f64) -> f64 {
41 fn zero_case<R: Rng>(rng: &mut R, u: f64) -> f64 {
42 // compute a random number in the tail by hand
44 // strange initial conditions, because the loop is not
45 // do-while, so the condition should be true on the first
46 // run, they get overwritten anyway (0 < 1, so these are
51 while -2.0 * y < x * x {
52 let Open01(x_) = rng.gen::<Open01<f64>>();
53 let Open01(y_) = rng.gen::<Open01<f64>>();
55 x = x_.ln() / ziggurat_tables::ZIG_NORM_R;
60 x - ziggurat_tables::ZIG_NORM_R
62 ziggurat_tables::ZIG_NORM_R - x
66 StandardNormal(ziggurat(rng,
67 true, // this is symmetric
68 &ziggurat_tables::ZIG_NORM_X,
69 &ziggurat_tables::ZIG_NORM_F,
75 /// The normal distribution `N(mean, std_dev**2)`.
77 /// This uses the ZIGNOR variant of the Ziggurat method, see
78 /// `StandardNormal` for more details.
79 #[derive(Copy, Clone)]
86 /// Construct a new `Normal` distribution with the given mean and
87 /// standard deviation.
91 /// Panics if `std_dev < 0`.
92 pub fn new(mean: f64, std_dev: f64) -> Normal {
93 assert!(std_dev >= 0.0, "Normal::new called with `std_dev` < 0");
100 impl Sample<f64> for Normal {
101 fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
105 impl IndependentSample<f64> for Normal {
106 fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
107 let StandardNormal(n) = rng.gen::<StandardNormal>();
108 self.mean + self.std_dev * n
113 /// The log-normal distribution `ln N(mean, std_dev**2)`.
115 /// If `X` is log-normal distributed, then `ln(X)` is `N(mean,
116 /// std_dev**2)` distributed.
117 #[derive(Copy, Clone)]
118 pub struct LogNormal {
123 /// Construct a new `LogNormal` distribution with the given mean
124 /// and standard deviation.
128 /// Panics if `std_dev < 0`.
129 pub fn new(mean: f64, std_dev: f64) -> LogNormal {
130 assert!(std_dev >= 0.0, "LogNormal::new called with `std_dev` < 0");
131 LogNormal { norm: Normal::new(mean, std_dev) }
134 impl Sample<f64> for LogNormal {
135 fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
139 impl IndependentSample<f64> for LogNormal {
140 fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
141 self.norm.ind_sample(rng).exp()
147 use distributions::{Sample, IndependentSample};
148 use super::{Normal, LogNormal};
152 let mut norm = Normal::new(10.0, 10.0);
153 let mut rng = ::test::rng();
155 norm.sample(&mut rng);
156 norm.ind_sample(&mut rng);
161 fn test_normal_invalid_sd() {
162 Normal::new(10.0, -1.0);
167 fn test_log_normal() {
168 let mut lnorm = LogNormal::new(10.0, 10.0);
169 let mut rng = ::test::rng();
171 lnorm.sample(&mut rng);
172 lnorm.ind_sample(&mut rng);
177 fn test_log_normal_invalid_sd() {
178 LogNormal::new(10.0, -1.0);
185 use self::test::Bencher;
186 use std::mem::size_of;
187 use distributions::Sample;
191 fn rand_normal(b: &mut Bencher) {
192 let mut rng = ::test::weak_rng();
193 let mut normal = Normal::new(-2.71828, 3.14159);
196 for _ in 0..::RAND_BENCH_N {
197 normal.sample(&mut rng);
200 b.bytes = size_of::<f64>() as u64 * ::RAND_BENCH_N;