mod.rs 13.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

S
Steve Klabnik 已提交
11 12 13 14 15 16 17 18
//! Sampling from random distributions.
//!
//! This is a generalization of `Rand` to allow parameters to control the
//! exact properties of the generated values, e.g. the mean and standard
//! deviation of a normal distribution. The `Sample` trait is the most
//! general, and allows for generating values that change some state
//! internally. The `IndependentSample` trait is for generating values
//! that do not need to record state.
19

A
Alex Crichton 已提交
20
use core::prelude::*;
21
use core::num::{Float, Int};
22
use core::marker::PhantomData;
A
Alex Crichton 已提交
23

H
Huon Wilson 已提交
24
use {Rng, Rand};
25

26
pub use self::range::Range;
27
pub use self::gamma::{Gamma, ChiSquared, FisherF, StudentT};
28
pub use self::normal::{Normal, LogNormal};
29
pub use self::exponential::Exp;
30 31

pub mod range;
32
pub mod gamma;
33 34
pub mod normal;
pub mod exponential;
35

36
/// Types that can be used to create a random instance of `Support`.
37 38 39 40 41 42
pub trait Sample<Support> {
    /// Generate a random value of `Support`, using `rng` as the
    /// source of randomness.
    fn sample<R: Rng>(&mut self, rng: &mut R) -> Support;
}

43 44
/// `Sample`s that do not require keeping track of state.
///
H
Huon Wilson 已提交
45
/// Since no state is recorded, each sample is (statistically)
46 47
/// independent of all others, assuming the `Rng` used has this
/// property.
48
// FIXME maybe having this separate is overkill (the only reason is to
49 50 51 52 53 54 55
// take &self rather than &mut self)? or maybe this should be the
// trait called `Sample` and the other should be `DependentSample`.
pub trait IndependentSample<Support>: Sample<Support> {
    /// Generate a random value.
    fn ind_sample<R: Rng>(&self, &mut R) -> Support;
}

56 57
/// A wrapper for generating types that implement `Rand` via the
/// `Sample` & `IndependentSample` traits.
58 59 60 61 62 63 64
pub struct RandSample<Sup> { _marker: PhantomData<Sup> }

impl<Sup> RandSample<Sup> {
    pub fn new() -> RandSample<Sup> {
        RandSample { _marker: PhantomData }
    }
}
65 66 67 68 69 70 71 72 73 74 75

impl<Sup: Rand> Sample<Sup> for RandSample<Sup> {
    fn sample<R: Rng>(&mut self, rng: &mut R) -> Sup { self.ind_sample(rng) }
}

impl<Sup: Rand> IndependentSample<Sup> for RandSample<Sup> {
    fn ind_sample<R: Rng>(&self, rng: &mut R) -> Sup {
        rng.gen()
    }
}

76 77 78
/// A value with a particular weight for use with `WeightedChoice`.
pub struct Weighted<T> {
    /// The numerical weight of this item
79
    pub weight: uint,
80
    /// The actual item which is being weighted
81
    pub item: T,
82 83 84 85 86 87 88 89
}

/// A distribution that selects from a finite collection of weighted items.
///
/// Each item has an associated weight that influences how likely it
/// is to be chosen: higher weight is more likely.
///
/// The `Clone` restriction is a limitation of the `Sample` and
H
Huon Wilson 已提交
90
/// `IndependentSample` traits. Note that `&T` is (cheaply) `Clone` for
91 92 93
/// all `T`, as is `uint`, so one can store references or indices into
/// another vector.
///
S
Steve Klabnik 已提交
94
/// # Examples
95 96
///
/// ```rust
A
Alex Crichton 已提交
97 98
/// use std::rand;
/// use std::rand::distributions::{Weighted, WeightedChoice, IndependentSample};
99
///
A
Alex Crichton 已提交
100 101 102 103
/// let mut items = vec!(Weighted { weight: 2, item: 'a' },
///                      Weighted { weight: 4, item: 'b' },
///                      Weighted { weight: 1, item: 'c' });
/// let wc = WeightedChoice::new(items.as_mut_slice());
S
Simonas Kazlauskas 已提交
104
/// let mut rng = rand::thread_rng();
A
Alfie John 已提交
105
/// for _ in 0..16 {
106 107
///      // on average prints 'a' 4 times, 'b' 8 and 'c' twice.
///      println!("{}", wc.ind_sample(&mut rng));
108 109
/// }
/// ```
110
pub struct WeightedChoice<'a, T:'a> {
A
Alex Crichton 已提交
111
    items: &'a mut [Weighted<T>],
112
    weight_range: Range<uint>
113 114
}

A
Alex Crichton 已提交
115
impl<'a, T: Clone> WeightedChoice<'a, T> {
116 117
    /// Create a new `WeightedChoice`.
    ///
118
    /// Panics if:
119 120 121
    /// - `v` is empty
    /// - the total weight is 0
    /// - the total weight is larger than a `uint` can contain.
N
Niko Matsakis 已提交
122
    pub fn new(items: &'a mut [Weighted<T>]) -> WeightedChoice<'a, T> {
123 124 125
        // strictly speaking, this is subsumed by the total weight == 0 case
        assert!(!items.is_empty(), "WeightedChoice::new called with no items");

J
Jorge Aparicio 已提交
126
        let mut running_total = 0_usize;
127 128 129 130

        // we convert the list from individual weights to cumulative
        // weights so we can binary search. This *could* drop elements
        // with weight == 0 as an optimisation.
131
        for item in &mut *items {
132
            running_total = match running_total.checked_add(item.weight) {
A
Alex Crichton 已提交
133
                Some(n) => n,
S
Steve Klabnik 已提交
134
                None => panic!("WeightedChoice::new called with a total weight \
A
Alex Crichton 已提交
135 136
                               larger than a uint can contain")
            };
137 138 139 140 141 142 143 144 145 146 147 148 149 150

            item.weight = running_total;
        }
        assert!(running_total != 0, "WeightedChoice::new called with a total weight of 0");

        WeightedChoice {
            items: items,
            // we're likely to be generating numbers in this range
            // relatively often, so might as well cache it
            weight_range: Range::new(0, running_total)
        }
    }
}

A
Alex Crichton 已提交
151
impl<'a, T: Clone> Sample<T> for WeightedChoice<'a, T> {
152 153
    fn sample<R: Rng>(&mut self, rng: &mut R) -> T { self.ind_sample(rng) }
}
154

A
Alex Crichton 已提交
155
impl<'a, T: Clone> IndependentSample<T> for WeightedChoice<'a, T> {
156 157 158 159 160 161 162 163 164
    fn ind_sample<R: Rng>(&self, rng: &mut R) -> T {
        // we want to find the first element that has cumulative
        // weight > sample_weight, which we do by binary since the
        // cumulative weights of self.items are sorted.

        // choose a weight in [0, total_weight)
        let sample_weight = self.weight_range.ind_sample(rng);

        // short circuit when it's the first item
A
Alex Crichton 已提交
165 166
        if sample_weight < self.items[0].weight {
            return self.items[0].item.clone();
167 168 169 170 171 172 173 174 175 176 177 178 179 180
        }

        let mut idx = 0;
        let mut modifier = self.items.len();

        // now we know that every possibility has an element to the
        // left, so we can just search for the last element that has
        // cumulative weight <= sample_weight, then the next one will
        // be "it". (Note that this greatest element will never be the
        // last element of the vector, since sample_weight is chosen
        // in [0, total_weight) and the cumulative weight of the last
        // one is exactly the total weight.)
        while modifier > 1 {
            let i = idx + modifier / 2;
A
Alex Crichton 已提交
181
            if self.items[i].weight <= sample_weight {
182 183 184 185 186 187 188 189 190 191 192 193
                // we're small, so look to the right, but allow this
                // exact element still.
                idx = i;
                // we need the `/ 2` to round up otherwise we'll drop
                // the trailing elements when `modifier` is odd.
                modifier += 1;
            } else {
                // otherwise we're too big, so go left. (i.e. do
                // nothing)
            }
            modifier /= 2;
        }
A
Alex Crichton 已提交
194
        return self.items[idx + 1].item.clone();
195 196 197 198
    }
}

mod ziggurat_tables;
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

/// Sample a random number using the Ziggurat method (specifically the
/// ZIGNOR variant from Doornik 2005). Most of the arguments are
/// directly from the paper:
///
/// * `rng`: source of randomness
/// * `symmetric`: whether this is a symmetric distribution, or one-sided with P(x < 0) = 0.
/// * `X`: the $x_i$ abscissae.
/// * `F`: precomputed values of the PDF at the $x_i$, (i.e. $f(x_i)$)
/// * `F_DIFF`: precomputed values of $f(x_i) - f(x_{i+1})$
/// * `pdf`: the probability density function
/// * `zero_case`: manual sampling from the tail when we chose the
///    bottom box (i.e. i == 0)

// the perf improvement (25-50%) is definitely worth the extra code
// size from force-inlining.
#[inline(always)]
216
fn ziggurat<R: Rng, P, Z>(
217 218
            rng: &mut R,
            symmetric: bool,
219 220
            x_tab: ziggurat_tables::ZigTable,
            f_tab: ziggurat_tables::ZigTable,
221 222 223
            mut pdf: P,
            mut zero_case: Z)
            -> f64 where P: FnMut(f64) -> f64, Z: FnMut(&mut R, f64) -> f64 {
224
    const SCALE: f64 = (1u64 << 53) as f64;
225
    loop {
226 227 228 229 230 231
        // reimplement the f64 generation as an optimisation suggested
        // by the Doornik paper: we have a lot of precision-space
        // (i.e. there are 11 bits of the 64 of a u64 to use after
        // creating a f64), so we might as well reuse some to save
        // generating a whole extra random number. (Seems to be 15%
        // faster.)
H
Huon Wilson 已提交
232 233 234 235 236 237 238
        //
        // This unfortunately misses out on the benefits of direct
        // floating point generation if an RNG like dSMFT is
        // used. (That is, such RNGs create floats directly, highly
        // efficiently and overload next_f32/f64, so by not calling it
        // this may be slower than it would be otherwise.)
        // FIXME: investigate/optimise for the above.
239 240 241 242 243 244 245
        let bits: u64 = rng.gen();
        let i = (bits & 0xff) as uint;
        let f = (bits >> 11) as f64 / SCALE;

        // u is either U(-1, 1) or U(0, 1) depending on if this is a
        // symmetric distribution or not.
        let u = if symmetric {2.0 * f - 1.0} else {f};
246
        let x = u * x_tab[i];
247

248
        let test_x = if symmetric { x.abs() } else {x};
249

250 251
        // algebraically equivalent to |u| < x_tab[i+1]/x_tab[i] (or u < x_tab[i+1]/x_tab[i])
        if test_x < x_tab[i + 1] {
252 253 254 255 256 257
            return x;
        }
        if i == 0 {
            return zero_case(rng, u);
        }
        // algebraically equivalent to f1 + DRanU()*(f0 - f1) < 1
258
        if f_tab[i + 1] + (f_tab[i] - f_tab[i + 1]) * rng.gen() < pdf(x) {
259 260 261 262 263
            return x;
        }
    }
}

264 265
#[cfg(test)]
mod tests {
266
    use std::prelude::v1::*;
A
Alex Crichton 已提交
267 268

    use {Rng, Rand};
H
Huon Wilson 已提交
269
    use super::{RandSample, WeightedChoice, Weighted, Sample, IndependentSample};
270

J
Jorge Aparicio 已提交
271
    #[derive(PartialEq, Debug)]
272 273 274 275 276 277 278
    struct ConstRand(uint);
    impl Rand for ConstRand {
        fn rand<R: Rng>(_: &mut R) -> ConstRand {
            ConstRand(0)
        }
    }

279 280 281 282 283 284 285 286 287 288 289 290
    // 0, 1, 2, 3, ...
    struct CountingRng { i: u32 }
    impl Rng for CountingRng {
        fn next_u32(&mut self) -> u32 {
            self.i += 1;
            self.i - 1
        }
        fn next_u64(&mut self) -> u64 {
            self.next_u32() as u64
        }
    }

291 292
    #[test]
    fn test_rand_sample() {
293
        let mut rand_sample = RandSample::<ConstRand>::new();
294

A
Alex Crichton 已提交
295 296
        assert_eq!(rand_sample.sample(&mut ::test::rng()), ConstRand(0));
        assert_eq!(rand_sample.ind_sample(&mut ::test::rng()), ConstRand(0));
297
    }
298 299 300 301 302 303 304
    #[test]
    fn test_weighted_choice() {
        // this makes assumptions about the internal implementation of
        // WeightedChoice, specifically: it doesn't reorder the items,
        // it doesn't do weird things to the RNG (so 0 maps to 0, 1 to
        // 1, internally; modulo a modulo operation).

305
        macro_rules! t {
306
            ($items:expr, $expected:expr) => {{
A
Alex Crichton 已提交
307
                let mut items = $items;
308
                let wc = WeightedChoice::new(&mut items);
309 310 311 312
                let expected = $expected;

                let mut rng = CountingRng { i: 0 };

313
                for &val in &expected {
314 315 316
                    assert_eq!(wc.ind_sample(&mut rng), val)
                }
            }}
317
        }
318

T
Tobias Bucher 已提交
319
        t!(vec!(Weighted { weight: 1, item: 10}), [10]);
320 321

        // skip some
T
Tobias Bucher 已提交
322 323 324 325
        t!(vec!(Weighted { weight: 0, item: 20},
                Weighted { weight: 2, item: 21},
                Weighted { weight: 0, item: 22},
                Weighted { weight: 1, item: 23}),
326
           [21,21, 23]);
327 328

        // different weights
T
Tobias Bucher 已提交
329 330
        t!(vec!(Weighted { weight: 4, item: 30},
                Weighted { weight: 3, item: 31}),
331
           [30,30,30,30, 31,31,31]);
332 333 334 335

        // check that we're binary searching
        // correctly with some vectors of odd
        // length.
T
Tobias Bucher 已提交
336 337 338 339 340
        t!(vec!(Weighted { weight: 1, item: 40},
                Weighted { weight: 1, item: 41},
                Weighted { weight: 1, item: 42},
                Weighted { weight: 1, item: 43},
                Weighted { weight: 1, item: 44}),
341
           [40, 41, 42, 43, 44]);
T
Tobias Bucher 已提交
342 343 344 345 346 347 348
        t!(vec!(Weighted { weight: 1, item: 50},
                Weighted { weight: 1, item: 51},
                Weighted { weight: 1, item: 52},
                Weighted { weight: 1, item: 53},
                Weighted { weight: 1, item: 54},
                Weighted { weight: 1, item: 55},
                Weighted { weight: 1, item: 56}),
349
           [50, 51, 52, 53, 54, 55, 56]);
350 351
    }

352
    #[test] #[should_panic]
353
    fn test_weighted_choice_no_items() {
N
Nick Cameron 已提交
354
        WeightedChoice::<int>::new(&mut []);
355
    }
356
    #[test] #[should_panic]
357
    fn test_weighted_choice_zero_weight() {
T
Tobias Bucher 已提交
358 359
        WeightedChoice::new(&mut [Weighted { weight: 0, item: 0},
                                  Weighted { weight: 0, item: 1}]);
360
    }
361
    #[test] #[should_panic]
362 363
    fn test_weighted_choice_weight_overflows() {
        let x = (-1) as uint / 2; // x + x + 2 is the overflow
T
Tobias Bucher 已提交
364 365 366 367
        WeightedChoice::new(&mut [Weighted { weight: x, item: 0 },
                                  Weighted { weight: 1, item: 1 },
                                  Weighted { weight: x, item: 2 },
                                  Weighted { weight: 1, item: 3 }]);
368
    }
369
}