Skip to content

Commit c4844d6

Browse files
committed
Documentation: revise for distributions module
1 parent 84c5327 commit c4844d6

File tree

1 file changed

+56
-45
lines changed

1 file changed

+56
-45
lines changed

src/distributions/mod.rs

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,84 +8,88 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! Sampling from random distributions.
11+
//! Generating random samples from [probability distributions].
1212
//!
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`).
2828
//!
2929
//!
3030
//! # The `Standard` distribution
3131
//!
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.
3737
//!
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.
4041
//!
4142
//!
4243
//! # Distribution to sample from a `Uniform` range
4344
//!
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.
4849
//!
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`].
5460
//!
5561
//!
5662
//! # Other distributions
5763
//!
5864
//! There are surprisingly many ways to uniformly generate random floats. A
5965
//! 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
6167
//! [`Open01`] and [`OpenClosed01`]. See [Floating point implementation] for
6268
//! more details.
6369
//!
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`.
6773
//!
6874
//!
69-
//! # Probability distributions
75+
//! # Non-uniform probability distributions
7076
//!
71-
//! Rand currently provides the following probability distributions (but needs
72-
//! expansion):
77+
//! Rand currently provides the following probability distributions:
7378
//!
7479
//! - Related to real-valued quantities that grow linearly
75-
//! (e.g. errors, offsets)
80+
//! (e.g. errors, offsets):
7681
//! - [`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):
7883
//! - [`Binomial`] distribution
7984
//! - Related to positive real-valued quantities that grow exponentially
80-
//! (e.g. prices, incomes, populations)
85+
//! (e.g. prices, incomes, populations):
8186
//! - [`LogNormal`] distribution
82-
//! - Related to events in a Poisson process (events that occur independently
87+
//! - Related to rate of occurrance of indenpendant events:
8388
//! with a given rate)
8489
//! - [`Poisson`] distribution
8590
//! - [`Exp`]onential distribution, and [`Exp1`] as a primitive
91+
//! - Gamma and derived distributions:
8692
//! - [`Gamma`] distribution
87-
//! - Related to normally distributed quantities operated with sum of squares
88-
//! (for hypothesis testing)
8993
//! - [`ChiSquared`] distribution
9094
//! - [`StudentT`] distribution
9195
//! - [`FisherF`] distribution
@@ -123,9 +127,11 @@
123127
//! ```
124128
//!
125129
//!
130+
//! [probability distributions]: https://en.wikipedia.org/wiki/Probability_distribution
126131
//! [`Distribution`]: trait.Distribution.html
127132
//! [`gen_range`]: ../trait.Rng.html#method.gen_range
128133
//! [`gen`]: ../trait.Rng.html#method.gen
134+
//! [`sample`]: ../trait.Rng.html#method.sample
129135
//! [`new_inclusive`]: struct.Uniform.html#method.new_inclusive
130136
//! [`random()`]: ../fn.random.html
131137
//! [`Rng::gen_bool`]: ../trait.Rng.html#method.gen_bool
@@ -278,6 +284,11 @@ mod impls {
278284

279285
/// Types (distributions) that can be used to create a random instance of `T`.
280286
///
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+
///
281292
/// All implementations are expected to be immutable; this has the significant
282293
/// advantage of not needing to consider thread safety, and for most
283294
/// distributions efficient state-less sampling algorithms are available.

0 commit comments

Comments
 (0)