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
//! Gamma distribution
use crate::{algebra::abstr::Real, special::gamma, statistics::distrib::Continuous};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::clone::Clone;
/// Gamma distribution
///
/// Fore more information:
/// <https://en.wikipedia.org/wiki/Gamma_distribution>
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug)]
pub struct Gamma<T> {
alpha: T,
beta: T,
}
impl<T> Gamma<T>
where
T: Real,
{
/// Creates a probability distribution
///
/// # Arguments
///
/// * `alpha` > 0.0
/// * `beta` > 0.0
///
/// # Panics
///
/// if alpha <= 0.0 || beta <= 0.0
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::Gamma;
///
/// let distrib: Gamma<f64> = Gamma::new(0.3, 0.2);
/// ```
pub fn new(alpha: T, beta: T) -> Gamma<T> {
if alpha <= T::zero() || beta <= T::zero() {
panic!()
}
Gamma { alpha, beta }
}
}
impl<T> Continuous<T> for Gamma<T>
where
T: Real + gamma::Gamma,
{
/// Probability density function
///
/// # Arguments
///
/// * `x` Random variable x ∈ ℕ | x > 0.0
///
/// # Panics
///
/// if x <= 0.0
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Continuous, Gamma};
///
/// let distrib: Gamma<f64> = Gamma::new(0.3, 0.2);
/// let x: f64 = 5.0;
/// let p: f64 = distrib.pdf(x);
/// ```
fn pdf(&self, x: T) -> T {
if x <= T::zero() {
panic!();
}
self.beta.pow(self.alpha) / gamma::gamma(self.alpha)
* x.pow(self.alpha - T::one())
* (-self.beta * x).exp()
}
/// Cumulative distribution function
///
/// # Arguments
///
/// * `x` Random variable
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Continuous, Gamma};
///
/// let distrib: Gamma<f64> = Gamma::new(0.3, 0.2);
/// let x: f64 = 0.4;
/// let p: f64 = distrib.cdf(x);
/// ```
fn cdf(&self, x: T) -> T {
if x == T::zero() {
return T::zero();
}
gamma::gamma_lr(self.alpha, self.beta * x)
}
/// Quantile function of inverse cdf
fn quantile(&self, _p: T) -> T {
unimplemented!();
}
/// Expected value
fn mean(&self) -> T {
self.alpha / self.beta
}
/// Variance
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Continuous, Gamma};
///
/// let distrib: Gamma<f64> = Gamma::new(0.2, 0.5);
/// let var: f64 = distrib.variance();
/// ```
fn variance(&self) -> T {
self.alpha / self.beta.pow(T::from_f64(2.0))
}
fn skewness(&self) -> T {
T::from_f64(2.0) / self.alpha.sqrt()
}
/// Median is the value separating the higher half from the lower half of a
/// probability distribution.
fn median(&self) -> T {
unimplemented!();
}
fn entropy(&self) -> T {
self.alpha - self.beta.ln()
+ gamma::gamma(self.alpha).ln()
+ (T::one() - self.alpha) * gamma::digamma(self.alpha)
}
}