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
// Binomial distribution
use crate::{
algebra::abstr::Real,
statistics::{combins, distrib::Discrete},
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::clone::Clone;
/// Binomial distribution
///
/// Fore more information:
/// <https://en.wikipedia.org/wiki/Binomial_distribution>
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug)]
pub struct Binomial<T> {
p: T,
n: u32,
}
impl<T> Binomial<T> {
/// Create a probability distribution with
///
/// # Arguments
///
/// * `p` Probability that random variable, p ∈ [0, 1]
/// * `n` number of trials, n ∈ ℕ
///
/// # Panics
///
/// if p < 0 || p > 1.0
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::Binomial;
///
/// let distrib: Binomial<f64> = Binomial::new(5, 0.3);
/// ```
pub fn new(n: u32, p: T) -> Binomial<T> {
Binomial { p, n }
}
}
impl<T> Discrete<T, u32, T> for Binomial<T>
where
T: Real,
{
/// Probability mass function
///
/// # Arguments
///
/// * `x` Random variable x &isin ࡃ
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Binomial, Discrete};
///
/// let distrib: Binomial<f64> = Binomial::new(5, 0.3);
/// let x: u32 = 0;
/// let p: f64 = distrib.pmf(x);
/// ```
fn pmf(&self, x: u32) -> T {
if x > self.n {
return T::zero();
}
let f: T = T::from_u32(combins::binom(self.n, x));
let diff: i32 = (self.n as i32) - (x as i32);
let pdf: T =
f * (self.p.pow(T::from_u32(x))) * ((T::one() - self.p).pow(T::from_i32(diff)));
pdf
}
/// Cumulative distribution function
///
/// # Arguments
///
/// * `x` Random variable
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Binomial, Discrete};
///
/// let distrib: Binomial<f64> = Binomial::new(5, 0.3);
/// let x: f64 = 0.4;
/// let p: f64 = distrib.cdf(x);
/// ```
fn cdf(&self, x: T) -> T {
let x_supremum: u32 = x.floor().to_u32();
let mut prob: T = T::zero();
for k in 0..x_supremum + 1 {
prob += self.pmf(k);
}
prob
}
/// Expected value
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Binomial, Discrete};
///
/// let distrib: Binomial<f64> = Binomial::new(5, 0.3);
/// let mean: f64 = distrib.mean();
/// ```
fn mean(&self) -> T {
T::from_u32(self.n) * self.p
}
/// Variance
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Binomial, Discrete};
///
/// let distrib: Binomial<f64> = Binomial::new(5, 0.3);
/// let var: f64 = distrib.variance();
/// ```
fn variance(&self) -> T {
self.mean() * (T::one() - self.p)
}
}