1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//!
//! # Distributions
//!
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
use ;
use basic; // Using the basic functions
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// # Rayleigh distribution
///
/// ## Definition
/// The [Rayleigh distribution](https://en.wikipedia.org/wiki/Rayleigh_distribution) is a continuous
/// probability distribution for any $x\ge0$. It is defined as:
/// $$
/// f_\sigma(x) = \frac{x}{\sigma^2} \exp\left( -\frac{x^2}{2\sigma^2} \right)
/// $$
///
/// ## Inputs
/// - `sigma`: the scale parameter of the distribution ($\sigma$)
/// - `x`: the value at which to evaluate the function, must be $\ge0$ ($x$).
///
/// Returns the density probability at a given point `x` for the `sigma`
/// scale parameter.
///
/// ## Example
/// ```
/// # use scilib::math::distribution::rayleigh;
/// let res = rayleigh(0.5, 0.9);
/// assert!((res - 0.7124353167010128).abs() < f64::EPSILON);
/// ```
///
/// 
/// # Cumulative function of the Rayleigh distribution
///
/// ## Definition
/// The cumulative function for Rayleigh is defined by:
/// $$
/// F_\sigma(x) = 1 - \exp\left(-\frac{x^2}{2\sigma^2}\right)
/// $$
///
/// ## Inputs
/// - `sigma`: the scale parameter of the distribution ($\sigma$)
/// - `x`: the value at which to evaluate the function, must be $\ge0$ ($x$).
///
/// Returns the cumulative density probability at a given point `x` for the `sigma`
/// scale parameter.
///
/// ## Example
/// ```
/// # use scilib::math::distribution::rayleigh_cumulative;
/// let res = rayleigh_cumulative(0.5, 0.9);
/// assert!((res - 0.8021013009163853).abs() < f64::EPSILON);
///
/// let max = rayleigh_cumulative(1.0, f64::INFINITY);
/// let min = rayleigh_cumulative(1.0, 0.0);
/// assert_eq!(max, 1.0);
/// assert_eq!(min, 0.0);
/// ```
///
/// 
/// # Cauchy distribution
///
/// ## Definition
/// The [Cauchy distribution](https://en.wikipedia.org/wiki/Cauchy_distribution) is a continuous
/// probability distribution. It is defined as:
/// $$
/// f_\gamma(x_0, x) = \frac{\gamma}{\pi} \frac{1}{(x-x_0)^2 + \gamma^2}
/// $$
///
/// ## Inputs
/// - `gamma`: the scale parameter, HWHM ($\gamma$).
/// - `x0`: the location of the peak ($x_0$).
/// - `x`: the point at which to evaluate the function.
///
/// Returns the density probability at a given point `x` for the `gamma`
/// scale parameter centered on `x0`.
///
/// ## Example
/// ```
/// use scilib::math::distribution::cauchy;
/// let res = cauchy(0.5, -2.0, 0.9);
/// assert!((res - 0.018378168948255814).abs() < f64::EPSILON)
/// ```
///
/// 
/// # Cumulative function of the Cauchy distribution
///
/// ## Definition
/// The cumulative function for Cauchy is defined by:
/// $$
/// F_\gamma(x_0, x) = \frac{1}{\pi}\arctan\left( \frac{x-x_0}{\gamma} \right) + \frac{1}{2}
/// $$
///
/// ## Inputs
/// - `gamma`: the scale parameter, HWHM ($\gamma$).
/// - `x0`: the location of the peak ($x_0$).
/// - `x`: the point at which to evaluate the function.
///
/// Returns the cumulative density probability at a given point `x` for the `gamma`
/// scale parameter centered on `x0`.
///
/// ## Example
/// ```
/// use scilib::math::distribution::cauchy_cumulative;
/// let res = cauchy_cumulative(0.5, -2.0, 0.9);
/// assert!((res - 0.9456532942677374).abs() < f64::EPSILON);
///
/// let max = cauchy_cumulative(1.0, 0.0, f64::INFINITY);
/// let min = cauchy_cumulative(1.0, 0.0, f64::NEG_INFINITY);
/// assert_eq!(max, 1.0);
/// assert_eq!(min, 0.0);
/// ```
///
/// 
/// # Laplace distribution
///
/// ## Definition
/// The [Laplace distribution](https://en.wikipedia.org/wiki/Laplace_distribution) is a continuous
/// probability distribution. It is defined as:
/// $$
/// f_b(\mu, x) = \frac{1}{2b}\exp\left( -\frac{\lvert x -\mu \rvert}{b} \right)
/// $$
///
/// ## Inputs
/// - `b`: the diversity of the distribution ($b$).
/// - `mu`: the location of the peak ($\mu$).
/// - `x`: the point at which to evaluate the function.
///
/// Returns the density probability at a given point `x` for the `b`
/// diversity parameter centered on `\mu`.
///
/// ## Example
/// ```
/// use scilib::math::distribution::laplace;
/// let res = laplace(0.5, -2.0, 0.9);
/// assert!((res - 0.0030275547453758153).abs() < f64::EPSILON)
/// ```
///
/// 
/// # Cumulative function of the Laplace distribution
///
/// ## Definition
/// The cumulative function for Laplace is defined by:
/// $$
/// F_b(\mu, x) = \frac{1}{2} + \frac{\mathrm{sgn}(x-\mu)}{2} \left( 1 - \exp\left( -\frac{\lvert x -\mu \rvert}{b} \right) \right)
/// $$
///
/// ## Inputs
/// - `b`: the diversity of the distribution ($b$).
/// - `mu`: the location of the peak ($\mu$).
/// - `x`: the point at which to evaluate the function.
///
/// Returns the cumulative density probability at a given point `x` for the `b`
/// diversity parameter centered on `\mu`.
///
/// ## Example
/// ```
/// use scilib::math::distribution::laplace_cumulative;
/// let res = laplace_cumulative(0.5, -2.0, 0.9);
/// assert!((res - 0.9984862226273121).abs() < f64::EPSILON);
///
/// let max = laplace_cumulative(1.0, 0.0, f64::INFINITY);
/// let min = laplace_cumulative(1.0, 0.0, f64::NEG_INFINITY);
/// assert_eq!(max, 1.0);
/// assert_eq!(min, 0.0);
/// ```
///
/// 
/// # Logistic distribution
///
/// ## Definition
/// The [Logistic distribution](https://en.wikipedia.org/wiki/Logistic_distribution) is a continuous
/// probability distribution. It is defined as:
/// $$
/// f_s(\mu, x) = \frac{1}{4s}\mathrm{sech}^2\left( \frac{x - \mu}{2s} \right)
/// $$
///
/// ## Inputs
/// - `s`: the scale parameter of the distribution ($s$).
/// - `mu`: the location of the peak ($\mu$).
/// - `x`: the point at which to evaluate the function.
///
/// Returns the density probability at a given point `x` for the `s`
/// scale parameter centered on `\mu`.
///
/// ## Example
/// ```
/// use scilib::math::distribution::logistic;
/// let res = logistic(0.5, -2.0, 0.9);
/// assert!((res - 0.006018610975198314).abs() < f64::EPSILON)
/// ```
///
/// 
/// # Cumulative function of the Logistic distribution
///
/// ## Definition
/// The cumulative function for Logistic is defined by:
/// $$
/// F_s(\mu, x) = \frac{1}{2} + \frac{1}{2}\tanh\left( \frac{x - \mu}{2s} \right)
/// $$
///
/// ## Inputs
/// - `s`: the scale parameter of the distribution ($s$).
/// - `mu`: the location of the peak ($\mu$).
/// - `x`: the point at which to evaluate the function.
///
/// Returns the cumulative density probability at a given point `x` for the `s`
/// scale parameter centered on `\mu`.
///
/// ## Example
/// ```
/// use scilib::math::distribution::logistic_cumulative;
/// let res = logistic_cumulative(0.5, -2.0, 0.9);
/// assert!((res - 0.9969815836752915).abs() < f64::EPSILON);
///
/// let max = logistic_cumulative(1.0, 0.0, f64::INFINITY);
/// let min = logistic_cumulative(1.0, 0.0, f64::NEG_INFINITY);
/// assert_eq!(max, 1.0);
/// assert_eq!(min, 0.0);
/// ```
///
/// 
/// # Normal function
///
/// ## Definition
/// The [normal](https://en.wikipedia.org/wiki/Normal_distribution) is a continuous
/// probability distribution. It is defined as:
/// $$
/// f_\sigma(\mu, x) = \frac{1}{\sigma\sqrt{2\pi}}\cdot\exp\left( -\frac{(x - \mu)^2}{2\sigma^2} \right)
/// $$
///
/// ## Inputs
/// - `sigma`: the variance ($\sigma$)
/// - `mu`: the central value ($\mu$)
/// - `x`: the value to evaluate ($x$)
///
/// Returns the density probability at a given point `x` for the `sigma`
/// scale parameter centered on `mu`.
///
/// ## Example
/// ```
/// # use scilib::math::distribution::normal;
/// let res: f64 = normal(0.5, -2.0, 0.9);
/// assert!((res - 3.9546392812489344e-08).abs() < f64::EPSILON);
/// ```
///
/// 
/// # Cumulative function of the Normal distribution
///
/// ## Definition
/// The cumulative function for Normal is defined by:
/// $$
/// F_\sigma(\mu, x) = \frac{1}{2}\left( 1 + \mathrm{erf}\left( \frac{x-\mu}{\sigma\sqrt{2}} \right) \right)
/// $$
///
/// Where $\mathrm{erf}$ is the [error function](https://en.wikipedia.org/wiki/Error_function), implemented in
/// the `math::basic` module of this crate.
///
/// ## Inputs
/// - `sigma`: the variance ($\sigma$)
/// - `mu`: the central value ($\mu$)
/// - `x`: the value to evaluate ($x$)
///
/// Returns the density probability at a given point `x` for the `sigma`
/// scale parameter centered on `mu`.
///
/// ## Example
/// ```
/// # use scilib::math::distribution::normal_cumulative;
/// let res: f64 = normal_cumulative(0.5, -2.0, 0.9);
/// assert!((res - 0.9999999966842541).abs() < 1e-10); // Less precise due to `erf`
/// ```
///
/// 