pmath 0.1.0

A general-purpose mathematics crate for Rust
Documentation
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//! Probability distributions.

use num_traits::{ConstOne, PrimInt, ToPrimitive};
use rand::Rng;
use rand::distr::Distribution as RandDistribution;
use rand::distr::uniform::SampleUniform;
use std::borrow::Borrow;
use std::collections::HashMap;
use std::hash::Hash;

#[cfg_attr(doc, katexit::katexit)]
/// A trait representing a generic probability distribution.
///
/// A random variable $X$ is said to follow a distribution $\\mathcal{D}$
/// (denoted as $X \\sim \\mathcal{D}$) if the probability of $X$
/// taking a value $x$ is given by the distribution functions.
pub trait Distribution<T>: RandDistribution<T> {
    /// Cumulative distribution function (CDF).
    ///
    /// The CDF of a random variable $X$ is defined as:
    /// $$
    ///     F(x) = P(X \\leq x)
    /// $$
    /// # Arguments
    /// * `x` - The value at which to evaluate the CDF.
    /// # Returns
    /// * The value of the CDF at `x`.
    /// # Panics
    /// * If `x` cannot be converted to [f64].
    fn cdf<U: ToPrimitive>(&self, x: U) -> f64;

    /// Mean of the distribution.
    ///
    /// Also known as the expected value or expectation,
    /// the mean is a measure of the central tendency of the distribution.
    ///
    /// The mean of a discrete random variable $X$ is defined as:
    /// $$
    ///     \mu = E\[X\] = \\sum_{i} x_i \\cdot P(X = x_i)
    /// $$
    /// The mean of a continuous random variable $X$ is defined as:
    /// $$
    ///    \mu = E\[X\] = \\int_{-\\infty}^{\\infty} x \\cdot f(x) dx
    /// $$
    /// # Returns
    /// * An [Option] with the mean of the distribution if it is defined.
    fn mean(&self) -> Option<f64>;

    /// Variance of the distribution.
    ///
    /// The variance is a measure of the spread of the distribution,
    /// which quantifies how much the values of the random variable differ from the mean.
    /// The variance of a random variable $X$ is defined as:
    /// $$
    ///     \\operatorname{Var}\[X\] = \\sigma^2 = E\[(X - \\mu)^2\] = E\[X^2\] - \\mu^2 = E\[X^2\] - (E\[X\])^2
    /// $$
    /// # Returns
    /// * An [Option] with the variance of the distribution if it is defined.
    fn variance(&self) -> Option<f64>;

    /// Standard deviation of the distribution.
    ///
    /// The standard deviation is the square root of the variance,
    /// which provides a measure of the spread of the distribution in the same units as the random variable.
    /// The standard deviation of a random variable $X$ is defined as:
    /// $$
    ///     \\sigma = \\sqrt{\\operatorname{Var}\[X\]} = \\sqrt{E\[(X - \\mu)^2\]} = \\sqrt{E\[X^2\] - (E\[X\])^2}
    /// $$
    /// # Returns
    /// * An [Option] with the standard deviation of the distribution if it is defined.
    fn stddev(&self) -> Option<f64> {
        self.variance().map(|v| v.sqrt())
    }
}

#[cfg_attr(doc, katexit::katexit)]
/// A trait representing a discrete probability distribution.
pub trait DiscreteDistribution<T>: Distribution<T> {
    /// Probability mass function (PMF).
    ///
    /// The PMF of a discrete random variable $X$ is defined as:
    /// $$
    ///     f(x) = P(X = x)
    /// $$
    /// The important property of a PMF is:
    /// $$
    ///     \\sum_{x} f(x) = 1
    /// $$
    /// # Arguments
    /// * `x` - The value at which to evaluate the PMF.
    /// # Returns
    /// * The value of the PMF at `x`.
    fn pmf(&self, x: T) -> f64;
}

#[cfg_attr(doc, katexit::katexit)]
/// A trait representing a continuous probability distribution.
pub trait ContinuousDistribution<T>: Distribution<T> {
    /// Probability density function (PDF).
    ///
    /// The PDF of a continuous random variable $X$ tells the probability
    /// of $X$ taking a value in an infinitesimally small interval around $x$.
    /// The important property of a PDF is:
    /// $$
    ///     \\int_{-\\infty}^{\\infty} f(x) dx = 1
    /// $$
    /// # Arguments
    /// * `x` - The value at which to evaluate the PDF.
    /// # Returns
    /// * The value of the PDF at `x`.
    /// # Panics
    /// * If `x` cannot be converted to [f64].
    fn pdf<U: ToPrimitive>(&self, x: U) -> f64;
}

#[cfg_attr(doc, katexit::katexit)]
/// A continuous uniform distribution.
///
/// The continuous uniform distribution $\\mathcal{U}(a, b)$
/// is a distribution where all values in the interval $\[a, b\]$
/// are equally likely.
/// # Example
/// ```
/// use pmath::probability::distributions::{ContinuousUniform, Distribution, ContinuousDistribution};
///
/// let dist = ContinuousUniform::new(0.0, 1.0);
/// assert!((dist.cdf(0.5) - 0.5).abs() < 1e-10);
/// assert!((dist.pdf(0.5) - 1.0).abs() < 1e-10);
/// assert!((dist.mean().unwrap() - 0.5).abs() < 1e-10);
/// assert!((dist.variance().unwrap() - 1.0 / 12.0).abs() < 1e-10);
/// assert!((dist.stddev().unwrap() - (1.0 / 12.0f64).sqrt()).abs() < 1e-10);
/// ```
pub struct ContinuousUniform {
    a: f64,
    b: f64,
}
impl ContinuousUniform {
    /// Create a new continuous uniform distribution.
    /// # Arguments
    /// * `a` - The minimum value of the distribution.
    /// * `b` - The maximum value of the distribution.
    /// # Returns
    /// * A new [ContinuousUniform] distribution.
    /// # Panics
    /// * If `a` cannot be converted to [f64].
    /// * If `b` cannot be converted to [f64].
    /// * If `a` is greater than `b`.
    pub fn new<T, U>(a: T, b: U) -> Self
    where
        T: ToPrimitive,
        U: ToPrimitive,
    {
        let a = a.to_f64().expect("a cannot be converted to f64");
        let b = b.to_f64().expect("b cannot be converted to f64");
        if a > b {
            panic!("a must be less than or equal to b.");
        }
        Self { a, b }
    }

    /// Get the minimum value of the distribution.
    /// # Returns
    /// * The minimum value of the distribution.
    pub fn a(&self) -> f64 {
        self.a
    }

    /// Get the maximum value of the distribution.
    /// # Returns
    /// * The maximum value of the distribution.
    pub fn b(&self) -> f64 {
        self.b
    }
}
impl Distribution<f64> for ContinuousUniform {
    fn cdf<U: ToPrimitive>(&self, x: U) -> f64 {
        let x_f64 = x.to_f64().expect("x cannot be converted to f64");
        if x_f64 < self.a {
            0.0
        } else if x_f64 > self.b {
            1.0
        } else {
            (x_f64 - self.a) / (self.b - self.a)
        }
    }

    fn mean(&self) -> Option<f64> {
        Some((self.a + self.b) / 2.0)
    }

    fn variance(&self) -> Option<f64> {
        Some((self.b - self.a).powi(2) / 12.0)
    }
}
impl RandDistribution<f64> for ContinuousUniform {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
        rng.random_range(self.a..=self.b)
    }
}
impl ContinuousDistribution<f64> for ContinuousUniform {
    fn pdf<U: ToPrimitive>(&self, x: U) -> f64 {
        let x_f64 = x.to_f64().expect("x cannot be converted to f64");
        if x_f64 < self.a || x_f64 > self.b {
            0.0
        } else {
            1.0 / (self.b - self.a)
        }
    }
}

#[cfg_attr(doc, katexit::katexit)]
/// A discrete uniform distribution.
///
/// A discrete uniform distribution with parameters $a$ and $b$ is a distribution
/// $$
///     \\begin{pmatrix}
///         a & a + 1 & a + 2 & \\ldots & b \\\\
///         \\frac{1}{b - a + 1} & \\frac{1}{b - a + 1} & \\frac{1}{b - a + 1} & \\ldots & \\frac{1}{b - a + 1}
///     \\end{pmatrix}
/// $$
/// # Example
/// ```
/// use pmath::probability::distributions::{DiscreteUniform, Distribution, DiscreteDistribution};
///
/// let dist = DiscreteUniform::new(1, 3);
/// assert!((dist.cdf(2) - 2.0 / 3.0).abs() < 1e-10);
/// assert!((dist.pmf(2) - 1.0 / 3.0).abs() < 1e-10);
/// assert!((dist.mean().unwrap() - 2.0).abs() < 1e-10);
/// assert!((dist.variance().unwrap() - 2.0 / 3.0).abs() < 1e-10);
/// assert!((dist.stddev().unwrap() - (2.0 / 3.0f64).sqrt()).abs() < 1e-10);
/// ```
pub struct DiscreteUniform<T> {
    a: T,
    b: T,
}
impl<T> DiscreteUniform<T>
where
    T: PrimInt,
{
    /// Create a new discrete uniform distribution.
    /// # Arguments
    /// * `a` - The minimum value of the distribution.
    /// * `b` - The maximum value of the distribution.
    /// # Returns
    /// * A new [DiscreteUniform] distribution.
    /// # Panics
    /// * If `a` is greater than `b`.
    pub fn new(a: T, b: T) -> Self {
        if a > b {
            panic!("a must be less than or equal to b.");
        }
        Self { a, b }
    }

    /// Get the minimum value of the distribution.
    /// # Returns
    /// * The minimum value of the distribution.
    pub fn a(&self) -> T {
        self.a
    }

    /// Get the maximum value of the distribution.
    /// # Returns
    /// * The maximum value of the distribution.
    pub fn b(&self) -> T {
        self.b
    }
}
impl<T> Distribution<T> for DiscreteUniform<T>
where
    T: SampleUniform + ConstOne + PrimInt,
{
    fn cdf<U: ToPrimitive>(&self, x: U) -> f64 {
        let x = T::from(x.to_f64().expect("x cannot be converted to f64").floor())
            .expect("x cannot be converted to T");
        if x < self.a {
            0.0
        } else if x >= self.b {
            1.0
        } else {
            (x - self.a + T::ONE).to_f64().unwrap() * self.pmf(self.a)
        }
    }

    fn mean(&self) -> Option<f64> {
        Some((self.a.to_f64().unwrap() + self.b.to_f64().unwrap()) / 2.0)
    }

    fn variance(&self) -> Option<f64> {
        let prob = self.pmf(self.a);
        let mean = self.mean().unwrap();
        let mut result = 0.0;
        let mut v = self.a;
        loop {
            result += v.to_f64().unwrap().powi(2) * prob;
            v = v + T::ONE;
            if v > self.b {
                break;
            }
        }
        Some(result - mean.powi(2))
    }
}
impl<T> RandDistribution<T> for DiscreteUniform<T>
where
    T: SampleUniform + PrimInt,
{
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> T {
        rng.random_range(self.a..=self.b)
    }
}
impl<T> DiscreteDistribution<T> for DiscreteUniform<T>
where
    T: SampleUniform + PrimInt + ConstOne,
{
    fn pmf(&self, x: T) -> f64 {
        if x < self.a || x > self.b {
            0.0
        } else {
            1.0 / (self.b.to_f64().unwrap() - self.a.to_f64().unwrap() + 1.0)
        }
    }
}

#[cfg_attr(doc, katexit::katexit)]
/// A discrete finite distribution with custom items and probabilities.
///
/// Given a set of items $a_1, a_2, \\ldots, a_n$ with corresponding
/// probabilities $p(a_1), p(a_2), \\ldots, p(a_n)$,
/// the distribution is represented as:
/// $$
///     \\begin{pmatrix}
///         a\_1 & a\_2 & a\_3 & \\ldots & a\_n \\\\
///         p(a\_1) & p(a\_2) & p(a\_3) & \\ldots & p(a\_n)
///     \\end{pmatrix}
/// $$
/// # Example
/// ```
/// use pmath::probability::distributions::{CustomDiscreteFinite, Distribution, DiscreteDistribution};
///
/// let dist = CustomDiscreteFinite::new([(1, 0.25), (2, 0.5), (3, 0.25)]);
/// assert!((dist.cdf(1) - 0.25).abs() < 1e-10);
/// assert!((dist.cdf(2) - 0.75).abs() < 1e-10);
/// assert!((dist.cdf(3) - 1.0).abs() < 1e-10);
/// assert!((dist.pmf(1) - 0.25).abs() < 1e-10);
/// assert!((dist.pmf(2) - 0.5).abs() < 1e-10);
/// assert!((dist.pmf(3) - 0.25).abs() < 1e-10);
/// assert!((dist.mean().unwrap() - 2.0).abs() < 1e-10);
/// assert!((dist.variance().unwrap() - 0.5).abs() < 1e-10);
/// assert!((dist.stddev().unwrap() - 0.5f64.sqrt()).abs() < 1e-10);
/// ```
pub struct CustomDiscreteFinite<T> {
    items_map: HashMap<T, usize>,  // (value, index in items_vec)
    items_vec: Vec<(T, f64, f64)>, // (value, probability, cumulative_probability before this value)
}
impl<T> CustomDiscreteFinite<T>
where
    T: Hash + Eq + Copy + PartialOrd,
{
    /// Create a new custom discrete finite distribution.
    /// # Arguments
    /// * `items` - An iterable collection of tuples where each tuple is `(value, probability)`.
    /// # Returns
    /// * A new [CustomDiscreteFinite] distribution.
    /// # Panics
    /// * If any probability is negative.
    /// * If the sum of all probabilities is `0`.
    /// * If any probability cannot be converted to [f64].
    /// * If values of type `T` cannot be compared (needed for sorting).
    /// # Notes
    /// * The probabilities are normalized to sum to `1`, so the input probabilities
    ///   don't have to sum to `1`.
    /// * The order of tuples in `items` doesn't matter since they will be sorted by their values.
    pub fn new<U, V, Z>(items: U) -> Self
    where
        U: IntoIterator<Item = V>,
        V: Borrow<(T, Z)>,
        Z: ToPrimitive + Copy,
    {
        let mut items_map: HashMap<T, usize> = HashMap::new();
        let mut items_vec: Vec<(T, f64, f64)> = Vec::new();
        let mut total_weight = 0.0;

        for (val, prob) in items.into_iter().map(|i| *i.borrow()) {
            let prob = prob
                .to_f64()
                .expect("Probability cannot be converted to f64");
            if prob < 0.0 {
                panic!("Probability must be non-negative.");
            }
            total_weight += prob;
            if let Some(&index) = items_map.get(&val) {
                items_vec[index].1 += prob; // Accumulate probability if value already exists
            } else {
                items_map.insert(val, items_vec.len());
                items_vec.push((val, prob, 0.0));
            }
        }
        if total_weight == 0.0 {
            panic!("Total weight cannot be zero.");
        }

        // sort items_vec by value
        items_vec
            .sort_unstable_by(|a, b| a.0.partial_cmp(&b.0).expect("Values cannot be compared"));

        // update items_map with new indices after sorting,
        // normalize probabilities and calculate cumulative probabilities
        let mut total_prob = 0.0;
        for (i, (val, prob, cum_prob)) in items_vec.iter_mut().enumerate() {
            *items_map.get_mut(val).unwrap() = i;
            *prob /= total_weight; // Normalize probability
            *cum_prob = total_prob;
            total_prob += *prob;
        }

        Self {
            items_map,
            items_vec,
        }
    }

    /// Get the items and their probabilities.
    /// # Returns
    /// * An iterator over tuples of the form `(value, probability)`.
    pub fn items(&self) -> impl Iterator<Item = (T, f64)> {
        self.items_vec.iter().map(|(val, prob, _)| (*val, *prob))
    }
}
impl<T> Distribution<T> for CustomDiscreteFinite<T>
where
    T: ToPrimitive + Copy,
{
    fn cdf<U: ToPrimitive>(&self, x: U) -> f64 {
        let x_f64 = x.to_f64().expect("x cannot be converted to f64");
        if x_f64
            < self
                .items_vec
                .first()
                .unwrap()
                .0
                .to_f64()
                .expect("Value cannot be converted to f64")
        {
            0.0
        } else if x_f64
            >= self
                .items_vec
                .last()
                .unwrap()
                .0
                .to_f64()
                .expect("Value cannot be converted to f64")
        {
            1.0
        } else {
            // Find the index of the first item with value greater than x
            let index = self.items_vec.partition_point(|item| {
                item.0.to_f64().expect("Value cannot be converted to f64") <= x_f64
            });
            // index shouldn't be 0 here since that case is covered by the first branch of this if statement
            // add probability to cumulative probability (since cumulative probability stored is cumulative probability before this value)
            self.items_vec[index - 1].1 + self.items_vec[index - 1].2
        }
    }

    fn mean(&self) -> Option<f64> {
        Some(
            self.items_vec
                .iter()
                .map(|(val, prob, _)| val.to_f64().expect("Cannot convert to f64.") * prob)
                .sum(),
        )
    }

    fn variance(&self) -> Option<f64> {
        let ex2 = self
            .items_vec
            .iter()
            .map(|(val, prob, _)| val.to_f64().expect("Cannot convert to f64.").powi(2) * prob)
            .sum::<f64>();
        Some(ex2 - self.mean()?.powi(2))
    }
}
impl<T> RandDistribution<T> for CustomDiscreteFinite<T>
where
    T: ToPrimitive + Copy,
{
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> T {
        let rand_float: f64 = rng.random();
        self.items_vec[self.items_vec.partition_point(|item| item.2 <= rand_float) - 1].0
    }
}
impl<T> DiscreteDistribution<T> for CustomDiscreteFinite<T>
where
    T: ToPrimitive + Copy + Hash + Eq,
{
    fn pmf(&self, x: T) -> f64 {
        match self.items_map.get(&x) {
            Some(val) => self.items_vec[*val].1,
            None => 0.0,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use assert_float_eq::assert_float_absolute_eq;
    use rand::rng;

    mod continuous_uniform {
        use super::*;

        #[test]
        fn main() {
            let dist = ContinuousUniform::new(0.0, 1.0);
            assert_float_absolute_eq!(dist.cdf(0.5), 0.5, 1e-10);
            assert_float_absolute_eq!(dist.pdf(0.5), 1.0, 1e-10);
            assert_float_absolute_eq!(dist.mean().unwrap(), 0.5, 1e-10);
            assert_float_absolute_eq!(dist.variance().unwrap(), 1.0 / 12.0, 1e-10);
            assert_float_absolute_eq!(dist.stddev().unwrap(), (1.0 / 12.0f64).sqrt(), 1e-10);
        }

        #[test]
        fn samples() {
            let dist = ContinuousUniform::new(0.0, 1.0);
            for sample in dist.sample_iter(rng()).take(1_000_000) {
                if !(0.0..=1.0).contains(&sample) {
                    panic!("Sample {sample} not in [0, 1]");
                }
            }
        }
    }

    mod discrete_uniform {
        use super::*;

        #[test]
        fn main() {
            let dist = DiscreteUniform::new(1, 3);
            assert_float_absolute_eq!(dist.cdf(2), 2.0 / 3.0, 1e-10);
            assert_float_absolute_eq!(dist.pmf(2), 1.0 / 3.0, 1e-10);
            assert_float_absolute_eq!(dist.mean().unwrap(), 2.0, 1e-10);
            assert_float_absolute_eq!(dist.variance().unwrap(), 2.0 / 3.0, 1e-10);
            assert_float_absolute_eq!(dist.stddev().unwrap(), (2.0 / 3.0f64).sqrt(), 1e-10);
        }

        #[test]
        fn samples() {
            let dist = DiscreteUniform::new(1, 3);
            for sample in dist.sample_iter(rng()).take(1_000_000) {
                if ![1, 2, 3].contains(&sample) {
                    panic!("Sample {sample} not in [1, 2, 3]");
                }
            }
        }
    }

    mod custom_discrete_finite {
        use super::*;

        #[test]
        fn main() {
            let dist = CustomDiscreteFinite::new([(1, 0.25), (2, 0.5), (3, 0.25)]);
            assert_float_absolute_eq!(dist.cdf(1), 0.25, 1e-10);
            assert_float_absolute_eq!(dist.cdf(2), 0.75, 1e-10);
            assert_float_absolute_eq!(dist.cdf(3), 1.0, 1e-10);
            assert_float_absolute_eq!(dist.pmf(1), 0.25, 1e-10);
            assert_float_absolute_eq!(dist.pmf(2), 0.5, 1e-10);
            assert_float_absolute_eq!(dist.pmf(3), 0.25, 1e-10);
            assert_float_absolute_eq!(dist.mean().unwrap(), 2.0, 1e-10);
            assert_float_absolute_eq!(dist.variance().unwrap(), 0.5, 1e-10);
            assert_float_absolute_eq!(dist.stddev().unwrap(), 0.5f64.sqrt(), 1e-10);
        }

        #[test]
        fn samples() {
            let dist = CustomDiscreteFinite::new([(1, 0.25), (2, 0.5), (3, 0.25)]);
            for sample in dist.sample_iter(rng()).take(1_000_000) {
                if ![1, 2, 3].contains(&sample) {
                    panic!("Sample {sample} not in [1, 2, 3]");
                }
            }
        }
    }
}