Skip to content

Commit 22e1154

Browse files
Add distribution plots to rand_distr documentation (rust-random#1434)
1 parent d8c38e9 commit 22e1154

25 files changed

+535
-136
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
9+
### Added
10+
- Add plots for `rand_distr` distributions to documentation (#1434)
11+
712
## [0.5.0-alpha.1] - 2024-03-18
813
- Target `rand` version `0.9.0-alpha.1`
914

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ keywords = ["random", "rng", "distribution", "probability"]
1414
categories = ["algorithms", "no-std"]
1515
edition = "2021"
1616
rust-version = "1.61"
17-
include = ["src/", "LICENSE-*", "README.md", "CHANGELOG.md", "COPYRIGHT"]
17+
include = ["/src", "LICENSE-*", "README.md", "CHANGELOG.md", "COPYRIGHT"]
1818

1919
[package.metadata.docs.rs]
2020
rustdoc-args = ["--cfg docsrs", "--generate-link-to-definition"]

src/binomial.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
//! The binomial distribution.
10+
//! The binomial distribution `Binomial(n, p)`.
1111
1212
use crate::{Distribution, Uniform};
1313
use core::cmp::Ordering;
@@ -16,11 +16,24 @@ use core::fmt;
1616
use num_traits::Float;
1717
use rand::Rng;
1818

19-
/// The binomial distribution `Binomial(n, p)`.
19+
/// The [binomial distribution](https://en.wikipedia.org/wiki/Binomial_distribution) `Binomial(n, p)`.
20+
///
21+
/// The binomial distribution is a discrete probability distribution
22+
/// which describes the probability of seeing `k` successes in `n`
23+
/// independent trials, each of which has success probability `p`.
24+
///
25+
/// # Density function
2026
///
21-
/// This distribution has density function:
2227
/// `f(k) = n!/(k! (n-k)!) p^k (1-p)^(n-k)` for `k >= 0`.
2328
///
29+
/// # Plot
30+
///
31+
/// The following plot of the binomial distribution illustrates the
32+
/// probability of `k` successes out of `n = 10` trials with `p = 0.2`
33+
/// and `p = 0.6` for `0 <= k <= n`.
34+
///
35+
/// ![Binomial distribution](https://raw.githubusercontent.com/rust-random/charts/main/charts/binomial.svg)
36+
///
2437
/// # Example
2538
///
2639
/// ```

src/cauchy.rs

+28-6
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,37 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
//! The Cauchy distribution.
10+
//! The Cauchy distribution `Cauchy(x₀, γ)`.
1111
1212
use crate::{Distribution, Standard};
1313
use core::fmt;
1414
use num_traits::{Float, FloatConst};
1515
use rand::Rng;
1616

17-
/// The Cauchy distribution `Cauchy(median, scale)`.
17+
/// The [Cauchy distribution](https://en.wikipedia.org/wiki/Cauchy_distribution) `Cauchy(x₀, γ)`.
1818
///
19-
/// This distribution has a density function:
20-
/// `f(x) = 1 / (pi * scale * (1 + ((x - median) / scale)^2))`
19+
/// The Cauchy distribution is a continuous probability distribution with
20+
/// parameters `x₀` (median) and `γ` (scale).
21+
/// It describes the distribution of the ratio of two independent
22+
/// normally distributed random variables with means `x₀` and scales `γ`.
23+
/// In other words, if `X` and `Y` are independent normally distributed
24+
/// random variables with means `x₀` and scales `γ`, respectively, then
25+
/// `X / Y` is `Cauchy(x₀, γ)` distributed.
2126
///
22-
/// Note that at least for `f32`, results are not fully portable due to minor
23-
/// differences in the target system's *tan* implementation, `tanf`.
27+
/// # Density function
28+
///
29+
/// `f(x) = 1 / (π * γ * (1 + ((x - x₀) / γ)²))`
30+
///
31+
/// # Plot
32+
///
33+
/// The plot illustrates the Cauchy distribution with various values of `x₀` and `γ`.
34+
/// Note how the median parameter `x₀` shifts the distribution along the x-axis,
35+
/// and how the scale `γ` changes the density around the median.
36+
///
37+
/// The standard Cauchy distribution is the special case with `x₀ = 0` and `γ = 1`,
38+
/// which corresponds to the ratio of two [`StandardNormal`](crate::StandardNormal) distributions.
39+
///
40+
/// ![Cauchy distribution](https://raw.githubusercontent.com/rust-random/charts/main/charts/cauchy.svg)
2441
///
2542
/// # Example
2643
///
@@ -31,6 +48,11 @@ use rand::Rng;
3148
/// let v = cau.sample(&mut rand::thread_rng());
3249
/// println!("{} is from a Cauchy(2, 5) distribution", v);
3350
/// ```
51+
///
52+
/// # Notes
53+
///
54+
/// Note that at least for `f32`, results are not fully portable due to minor
55+
/// differences in the target system's *tan* implementation, `tanf`.
3456
#[derive(Clone, Copy, Debug, PartialEq)]
3557
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
3658
pub struct Cauchy<F>

src/dirichlet.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
//! The dirichlet distribution.
10+
//! The dirichlet distribution `Dirichlet(α₁, α₂, ..., αₙ)`.
11+
1112
#![cfg(feature = "alloc")]
1213
use crate::{Beta, Distribution, Exp1, Gamma, Open01, StandardNormal};
1314
use core::fmt;
@@ -185,11 +186,23 @@ where
185186
FromBeta(DirichletFromBeta<F, N>),
186187
}
187188

188-
/// The Dirichlet distribution `Dirichlet(alpha)`.
189+
/// The [Dirichlet distribution](https://en.wikipedia.org/wiki/Dirichlet_distribution) `Dirichlet(α₁, α₂, ..., αₖ)`.
189190
///
190191
/// The Dirichlet distribution is a family of continuous multivariate
191-
/// probability distributions parameterized by a vector alpha of positive reals.
192-
/// It is a multivariate generalization of the beta distribution.
192+
/// probability distributions parameterized by a vector of positive
193+
/// real numbers `α₁, α₂, ..., αₖ`, where `k` is the number of dimensions
194+
/// of the distribution. The distribution is supported on the `k-1`-dimensional
195+
/// simplex, which is the set of points `x = [x₁, x₂, ..., xₖ]` such that
196+
/// `0 ≤ xᵢ ≤ 1` and `∑ xᵢ = 1`.
197+
/// It is a multivariate generalization of the [`Beta`](crate::Beta) distribution.
198+
/// The distribution is symmetric when all `αᵢ` are equal.
199+
///
200+
/// # Plot
201+
///
202+
/// The following plot illustrates the 2-dimensional simplices for various
203+
/// 3-dimensional Dirichlet distributions.
204+
///
205+
/// ![Dirichlet distribution](https://raw.githubusercontent.com/rust-random/charts/main/charts/dirichlet.png)
193206
///
194207
/// # Example
195208
///

src/exponential.rs

+44-16
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,47 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
//! The exponential distribution.
10+
//! The exponential distribution `Exp(λ)`.
1111
1212
use crate::utils::ziggurat;
1313
use crate::{ziggurat_tables, Distribution};
1414
use core::fmt;
1515
use num_traits::Float;
1616
use rand::Rng;
1717

18-
/// Samples floating-point numbers according to the exponential distribution,
19-
/// with rate parameter `λ = 1`. This is equivalent to `Exp::new(1.0)` or
20-
/// sampling with `-rng.gen::<f64>().ln()`, but faster.
18+
/// The standard exponential distribution `Exp(1)`.
2119
///
22-
/// See `Exp` for the general exponential distribution.
20+
/// This is equivalent to `Exp::new(1.0)` or sampling with
21+
/// `-rng.gen::<f64>().ln()`, but faster.
2322
///
24-
/// Implemented via the ZIGNOR variant[^1] of the Ziggurat method. The exact
25-
/// description in the paper was adjusted to use tables for the exponential
26-
/// distribution rather than normal.
23+
/// See [`Exp`](crate::Exp) for the general exponential distribution.
2724
///
28-
/// [^1]: Jurgen A. Doornik (2005). [*An Improved Ziggurat Method to
29-
/// Generate Normal Random Samples*](
30-
/// https://www.doornik.com/research/ziggurat.pdf).
31-
/// Nuffield College, Oxford
25+
/// # Plot
26+
///
27+
/// The following plot illustrates the exponential distribution with `λ = 1`.
28+
///
29+
/// ![Exponential distribution](https://raw.githubusercontent.com/rust-random/charts/main/charts/exponential_exp1.svg)
3230
///
3331
/// # Example
32+
///
3433
/// ```
3534
/// use rand::prelude::*;
3635
/// use rand_distr::Exp1;
3736
///
3837
/// let val: f64 = thread_rng().sample(Exp1);
3938
/// println!("{}", val);
4039
/// ```
40+
///
41+
/// # Notes
42+
///
43+
/// Implemented via the ZIGNOR variant[^1] of the Ziggurat method. The exact
44+
/// description in the paper was adjusted to use tables for the exponential
45+
/// distribution rather than normal.
46+
///
47+
/// [^1]: Jurgen A. Doornik (2005). [*An Improved Ziggurat Method to
48+
/// Generate Normal Random Samples*](
49+
/// https://www.doornik.com/research/ziggurat.pdf).
50+
/// Nuffield College, Oxford
4151
#[derive(Clone, Copy, Debug)]
4252
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
4353
pub struct Exp1;
@@ -75,12 +85,30 @@ impl Distribution<f64> for Exp1 {
7585
}
7686
}
7787

78-
/// The exponential distribution `Exp(lambda)`.
88+
/// The [exponential distribution](https://en.wikipedia.org/wiki/Exponential_distribution) `Exp(λ)`.
89+
///
90+
/// The exponential distribution is a continuous probability distribution
91+
/// with rate parameter `λ` (`lambda`). It describes the time between events
92+
/// in a [`Poisson`](crate::Poisson) process, i.e. a process in which
93+
/// events occur continuously and independently at a constant average rate.
94+
///
95+
/// See [`Exp1`](crate::Exp1) for an optimised implementation for `λ = 1`.
96+
///
97+
/// # Density function
98+
///
99+
/// `f(x) = λ * exp(-λ * x)` for `x > 0`, when `λ > 0`.
100+
///
101+
/// For `λ = 0`, all samples yield infinity (because a Poisson process
102+
/// with rate 0 has no events).
103+
///
104+
/// # Plot
79105
///
80-
/// This distribution has density function: `f(x) = lambda * exp(-lambda * x)`
81-
/// for `x > 0`, when `lambda > 0`. For `lambda = 0`, all samples yield infinity.
106+
/// The following plot illustrates the exponential distribution with
107+
/// various values of `λ`.
108+
/// The `λ` parameter controls the rate of decay as `x` approaches infinity,
109+
/// and the mean of the distribution is `1/λ`.
82110
///
83-
/// Note that [`Exp1`](crate::Exp1) is an optimised implementation for `lambda = 1`.
111+
/// ![Exponential distribution](https://raw.githubusercontent.com/rust-random/charts/main/charts/exponential.svg)
84112
///
85113
/// # Example
86114
///

src/frechet.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,36 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
//! The Fréchet distribution.
9+
//! The Fréchet distribution `Fréchet(μ, σ, α)`.
1010
1111
use crate::{Distribution, OpenClosed01};
1212
use core::fmt;
1313
use num_traits::Float;
1414
use rand::Rng;
1515

16-
/// Samples floating-point numbers according to the Fréchet distribution
16+
/// The [Fréchet distribution](https://en.wikipedia.org/wiki/Fr%C3%A9chet_distribution) `Fréchet(α, μ, σ)`.
1717
///
18-
/// This distribution has density function:
19-
/// `f(x) = [(x - μ) / σ]^(-1 - α) exp[-(x - μ) / σ]^(-α) α / σ`,
20-
/// where `μ` is the location parameter, `σ` the scale parameter, and `α` the shape parameter.
18+
/// The Fréchet distribution is a continuous probability distribution
19+
/// with location parameter `μ` (`mu`), scale parameter `σ` (`sigma`),
20+
/// and shape parameter `α` (`alpha`). It describes the distribution
21+
/// of the maximum (or minimum) of a number of random variables.
22+
/// It is also known as the Type II extreme value distribution.
23+
///
24+
/// # Density function
25+
///
26+
/// `f(x) = [(x - μ) / σ]^(-1 - α) exp[-(x - μ) / σ]^(-α) α / σ`
27+
///
28+
/// # Plot
29+
///
30+
/// The plot shows the Fréchet distribution with various values of `μ`, `σ`, and `α`.
31+
/// Note how the location parameter `μ` shifts the distribution along the x-axis,
32+
/// the scale parameter `σ` stretches or compresses the distribution along the x-axis,
33+
/// and the shape parameter `α` changes the tail behavior.
34+
///
35+
/// ![Fréchet distribution](https://raw.githubusercontent.com/rust-random/charts/main/charts/frechet.svg)
2136
///
2237
/// # Example
38+
///
2339
/// ```
2440
/// use rand::prelude::*;
2541
/// use rand_distr::Frechet;

0 commit comments

Comments
 (0)