|
8 | 8 | // option. This file may not be copied, modified, or distributed |
9 | 9 | // except according to those terms. |
10 | 10 |
|
11 | | -//! Sampling from random distributions. |
| 11 | +//! Generating random samples from [probability distributions]. |
12 | 12 | //! |
13 | | -//! This module is the home of the [`Distribution`] trait, and several |
14 | | -//! implementations. It is the workhorse behind some of the convenient methods |
15 | | -//! of the [`Rng`] trait, like [`gen`] and [`gen_range`]. A distribution takes |
16 | | -//! one or more values of an RNG to produce a value with the type and |
17 | | -//! distribution you need. |
18 | | -//! |
19 | | -//! Any type implementing [`Distribution`] is stateless (i.e. immutable), but it |
20 | | -//! may have internal parameters set at construction time. [`Uniform`] for |
21 | | -//! example has configurable bounds for the range, while [`Standard`] is just a |
22 | | -//! unit struct. |
23 | | -//! |
24 | | -//! Is is possible to sample from a distribution through both the |
25 | | -//! [`Distribution`] and [`Rng`] traits, via `distr.sample(&mut rng)` and |
26 | | -//! `rng.sample(distr)`. They also both offer the [`sample_iter`] method, which |
27 | | -//! produces an iterator that samples from the distribution. |
| 13 | +//! This module is the home of the [`Distribution`] trait and several of its |
| 14 | +//! implementations, which is the workhorse behind some of the convenient |
| 15 | +//! functionality of the [`Rng`] trait, including [`gen`], [`gen_range`] and |
| 16 | +//! of course [`sample`]. |
| 17 | +//! |
| 18 | +//! Abstractly, a probability distribution describes the probability of |
| 19 | +//! occurance of each value in its sample space. |
| 20 | +//! More concretely, an implementation of `Distribution<T>` for type `X` is an |
| 21 | +//! algorithm for choosing values from the sample space (a subset of `T`) |
| 22 | +//! according to the distribution `X` represents, using an external source of |
| 23 | +//! randomness (an RNG supplied to the `sample` function). |
| 24 | +//! A type `X` may implement `Distribution<T>` for multiple types `T`. |
| 25 | +//! Any type implementing [`Distribution`] is stateless (i.e. immutable), |
| 26 | +//! but it may have internal parameters set at construction time (for example, |
| 27 | +//! [`Uniform`] allows specification of its sample space as a range within `T`). |
28 | 28 | //! |
29 | 29 | //! |
30 | 30 | //! # The `Standard` distribution |
31 | 31 | //! |
32 | | -//! The [`Standard`] distribution is important to call out. This is the |
33 | | -//! distribution used by [`Rng::gen()`]. [`Standard`] will generate a value in |
34 | | -//! a way that is the most appropriate for some type. See the documentation of |
35 | | -//! [`Standard`] for more details, like for which primitive types Rand contains |
36 | | -//! an implementation, and what is deemed 'most appropriate' for some type. |
| 32 | +//! The [`Standard`] distribution is important to mention. This is the |
| 33 | +//! distribution used by [`Rng::gen()`] and represents the "default" way to |
| 34 | +//! produce a random value for many different types, including most primitive |
| 35 | +//! types, tuples, arrays, and a few derived types. See the documentation of |
| 36 | +//! [`Standard`] for more details. |
37 | 37 | //! |
38 | | -//! Implementing [`Standard`] for a user type makes it available for use with |
39 | | -//! [`Rng::gen()`], and by extension for the simple [`random()`] function. |
| 38 | +//! It is possible to implement `Distribution<T>` for [`Standard`] for user |
| 39 | +//! types `T`; doing so makes it possible to generate type `T` with |
| 40 | +//! [`Rng::gen()`], and by extension also with the [`random()`] function. |
40 | 41 | //! |
41 | 42 | //! |
42 | 43 | //! # Distribution to sample from a `Uniform` range |
43 | 44 | //! |
44 | | -//! The [`Uniform`] distribution is the basis to sample values from a range |
45 | | -//! using [`Rng::gen_range`]. It is possible to implement [`Uniform`] for custom |
46 | | -//! types, so that they also work with [`gen_range`]. More documentation on that |
47 | | -//! in the [`uniform` module]. |
| 45 | +//! The [`Uniform`] distribution is more flexible than [`Standard`], but also |
| 46 | +//! more specialised: it supports fewer target types, but allows the sample |
| 47 | +//! space to be specified as an arbitrary range within its target type `T`. |
| 48 | +//! Both [`Standard`] and [`Uniform`] are in some sense uniform distributions. |
48 | 49 | //! |
49 | | -//! Sometimes you may want to sample from [`Uniform`] directly. For one it can |
50 | | -//! provide better performance when you need to sample multiple values from the |
51 | | -//! same range, because the set-up cost then needs to be done only once. Also it |
52 | | -//! supports setting up an inclusive range with [`new_inclusive`], and offers a |
53 | | -//! `From` implementation for range syntax. |
| 50 | +//! Values may be sampled from this distribution using [`Rng::gen_range`] or |
| 51 | +//! by creating a distribution object with [`Uniform::new`], |
| 52 | +//! [`Uniform::new_inclusive`] or `From<Range>`; |
| 53 | +//! when the range limits are not known at compile time it is typically faster |
| 54 | +//! to reuse an existing distribution object than to call [`Rng::gen_range`]. |
| 55 | +//! |
| 56 | +//! User types `T` may also implement `Distribution<T>` for [`Uniform`], |
| 57 | +//! although this is less straightforward than for [`Standard`] (see the |
| 58 | +//! documentation in the [`uniform` module]. Doing so enables generation of |
| 59 | +//! values of type `T` with [`Rng::gen_range`]. |
54 | 60 | //! |
55 | 61 | //! |
56 | 62 | //! # Other distributions |
57 | 63 | //! |
58 | 64 | //! There are surprisingly many ways to uniformly generate random floats. A |
59 | 65 | //! range between 0 and 1 is standard, but the exact bounds (open vs closed) |
60 | | -//! and accuracy differ. In addition to the `Standard` distribution Rand offers |
| 66 | +//! and accuracy differ. In addition to the [`Standard`] distribution Rand offers |
61 | 67 | //! [`Open01`] and [`OpenClosed01`]. See [Floating point implementation] for |
62 | 68 | //! more details. |
63 | 69 | //! |
64 | | -//! [`Alphanumeric`] is a simple distribution to generate `Char`s, which can |
65 | | -//! often be more useful than sampling from the full set of possible Unicode |
66 | | -//! characters, which [`Standard`] does. |
| 70 | +//! [`Alphanumeric`] is a simple distribution to sample random letters and |
| 71 | +//! numbers of the `char` type; in contrast [`Standard`] may sample any valid |
| 72 | +//! `char`. |
67 | 73 | //! |
68 | 74 | //! |
69 | | -//! # Probability distributions |
| 75 | +//! # Non-uniform probability distributions |
70 | 76 | //! |
71 | | -//! Rand currently provides the following probability distributions (but needs |
72 | | -//! expansion): |
| 77 | +//! Rand currently provides the following probability distributions: |
73 | 78 | //! |
74 | 79 | //! - Related to real-valued quantities that grow linearly |
75 | | -//! (e.g. errors, offsets) |
| 80 | +//! (e.g. errors, offsets): |
76 | 81 | //! - [`Normal`] distribution, and [`StandardNormal`] as a primitive |
77 | | -//! - Related to Bernoulli trials (yes/no events, with a given probability) |
| 82 | +//! - Related to Bernoulli trials (yes/no events, with a given probability): |
78 | 83 | //! - [`Binomial`] distribution |
79 | 84 | //! - Related to positive real-valued quantities that grow exponentially |
80 | | -//! (e.g. prices, incomes, populations) |
| 85 | +//! (e.g. prices, incomes, populations): |
81 | 86 | //! - [`LogNormal`] distribution |
82 | | -//! - Related to events in a Poisson process (events that occur independently |
| 87 | +//! - Related to rate of occurrance of indenpendant events: |
83 | 88 | //! with a given rate) |
84 | 89 | //! - [`Poisson`] distribution |
85 | 90 | //! - [`Exp`]onential distribution, and [`Exp1`] as a primitive |
| 91 | +//! - Gamma and derived distributions: |
86 | 92 | //! - [`Gamma`] distribution |
87 | | -//! - Related to normally distributed quantities operated with sum of squares |
88 | | -//! (for hypothesis testing) |
89 | 93 | //! - [`ChiSquared`] distribution |
90 | 94 | //! - [`StudentT`] distribution |
91 | 95 | //! - [`FisherF`] distribution |
|
123 | 127 | //! ``` |
124 | 128 | //! |
125 | 129 | //! |
| 130 | +//! [probability distributions]: https://en.wikipedia.org/wiki/Probability_distribution |
126 | 131 | //! [`Distribution`]: trait.Distribution.html |
127 | 132 | //! [`gen_range`]: ../trait.Rng.html#method.gen_range |
128 | 133 | //! [`gen`]: ../trait.Rng.html#method.gen |
| 134 | +//! [`sample`]: ../trait.Rng.html#method.sample |
129 | 135 | //! [`new_inclusive`]: struct.Uniform.html#method.new_inclusive |
130 | 136 | //! [`random()`]: ../fn.random.html |
131 | 137 | //! [`Rng::gen_bool`]: ../trait.Rng.html#method.gen_bool |
@@ -278,6 +284,11 @@ mod impls { |
278 | 284 |
|
279 | 285 | /// Types (distributions) that can be used to create a random instance of `T`. |
280 | 286 | /// |
| 287 | +/// It is possible to sample from a distribution through both the |
| 288 | +/// [`Distribution`] and [`Rng`] traits, via `distr.sample(&mut rng)` and |
| 289 | +/// `rng.sample(distr)`. They also both offer the [`sample_iter`] method, which |
| 290 | +/// produces an iterator that samples from the distribution. |
| 291 | +/// |
281 | 292 | /// All implementations are expected to be immutable; this has the significant |
282 | 293 | /// advantage of not needing to consider thread safety, and for most |
283 | 294 | /// distributions efficient state-less sampling algorithms are available. |
|
0 commit comments