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
use crate::statistics::distrib::Discrete;
use crate::statistics::combins;
use crate::algebra::linear::vector::Vector;
use crate::algebra::abstr::Real;
/// Multinomial distribution
///
/// Fore more information:
/// <https://en.wikipedia.org/wiki/Multinomial_distribution>
///
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug)]
pub struct Multinomial<T>
{
p: Vector<T>,
}
impl<T> Multinomial<T>
where T: Real
{
/// 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::*;
/// use mathru::algebra::linear::vector::Vector;
/// use mathru::statistics::distrib::Multinomial;
///
/// let distrib: Multinomial<f64> = Multinomial::new(vector![0.3; 0.2; 0.5]);
/// ```
pub fn new(p: Vector<T>) -> Multinomial<T>
{
Multinomial
{
p: p
}
}
}
impl<T> Discrete<T, Vector<u32>, Vector<T>> for Multinomial<T>
where T: Real
{
/// Probability mass function
///
/// # Arguments
///
/// * `x` Random variable x &isin ࡃ
///
/// # Example
///
/// ```
/// use mathru::*;
/// use mathru::statistics::distrib::{Discrete, Multinomial};
/// use mathru::algebra::linear::vector::Vector;
///
/// let p: Vector<f64> = vector![0.3; 0.7];
/// let distrib: Multinomial<f64> = Multinomial::new(p);
/// let x: Vector<u32> = vector![1; 2];
/// let p: f64 = distrib.pmf(x);
/// ```
fn pmf(&self, x: Vector<u32>) -> T
{
debug_assert_eq!(self.p.dim(), x.dim());
let (m, _n) = x.dim();
let mut prod: T = T::one();
let mut n: u32 = 0;
for k in 0..m
{
let p_k: T = self.p[k];
let n_k: u32 = x[k];
n = n + n_k;
prod = prod * p_k.pow(&T::from_u32(n_k).unwrap()) / T::from_u64(combins::factorial(n_k)).unwrap();
}
return prod * T::from_u64(combins::factorial(n)).unwrap();
}
/// Cumulative distribution function
///
/// # Arguments
///
/// * `x` Random variable
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Discrete, Binomial};
///
/// let distrib: Binomial<f64> = Binomial::new(5, 0.3);
/// let x: f64 = 0.4;
/// let p: f64 = distrib.cdf(x);
/// ```
fn cdf(&self, _x: Vector<T>) -> T
{
/*
let x_supremum : u32 = x.floor() as u32;
let mut prob : f64 = 0.0;
for k in 0..x_supremum + 1
{
prob += self.pmf(k);
}
prob
*/
unimplemented!();
}
/// Expected value
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Discrete, Binomial};
///
/// let distrib: Binomial<f64> = Binomial::new(5, 0.3);
/// let mean: f64 = distrib.mean();
/// ```
fn mean(&self) -> T
{
unimplemented!();
//return &(self.n as f64) * &self.p
}
/// Variance
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Discrete, Binomial};
///
/// let distrib: Binomial<f64> = Binomial::new(5, pi0.3);
/// let var: f64 = distrib.variance();
/// ```
fn variance(&self) -> T
{
unimplemented!();
//return self.mean() * (1.0 - self.p)
}
}