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
//! Poisson distribution
use crate::{
algebra::abstr::Real,
special,
special::gamma::Gamma,
statistics::{combins, distrib::Discrete},
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::clone::Clone;
/// Poisson distribution
///
/// Fore more information:
/// <https://en.wikipedia.org/wiki/Poisson_distribution>
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug)]
pub struct Poisson<T> {
gamma: T,
}
impl<T> Poisson<T>
where
T: Real,
{
/// Creates a probability distribution
///
/// # Arguments
///
/// * `gamma` gamma > 0.0
///
/// # Panics
///
/// if gamma <= 0.0
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::Poisson;
///
/// let distrib: Poisson<f64> = Poisson::new(&0.2);
/// ```
pub fn new(gamma: &T) -> Poisson<T> {
if *gamma <= T::zero() {
panic!();
}
Poisson { gamma: *gamma }
}
}
impl<T> Discrete<T, u32, u32> for Poisson<T>
where
T: Real + Gamma,
{
/// Probability mass function
///
/// # Arguments
///
/// * `x` Random variable x ∈ ℕ
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Discrete, Poisson};
///
/// let distrib: Poisson<f64> = Poisson::new(&0.2);
/// let x: u32 = 5;
/// let p: f64 = distrib.pmf(x);
/// ```
fn pmf(&self, x: u32) -> T {
let k_fact: T = T::from_u64(combins::factorial(x));
self.gamma.pow(T::from_u32(x)) * (-self.gamma).exp() / k_fact
}
/// Cumulative distribution function of the Bernoulli distribution
///
/// # Arguments
///
/// * `x` Random variable x ∈ ℕ
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Discrete, Poisson};
///
/// let distrib: Poisson<f64> = Poisson::new(&0.2);
/// let x: u32 = 4;
/// let p: f64 = distrib.cdf(x);
/// ```
fn cdf(&self, x: u32) -> T {
special::gamma::gamma_ur(T::from_u32(x + 1), self.gamma)
}
/// Expected value
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Discrete, Poisson};
///
/// let distrib: Poisson<f64> = Poisson::new(&0.2);
/// let mean: f64 = distrib.mean();
/// ```
fn mean(&self) -> T {
self.gamma
}
/// Variance
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Discrete, Poisson};
///
/// let distrib: Poisson<f64> = Poisson::new(&0.2);
/// let var: f64 = distrib.variance();
/// ```
fn variance(&self) -> T {
self.gamma
}
}