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
use crate::special::hypergeometric::Hypergeometric;
use crate::{
algebra::abstr::Real,
special::beta::Beta,
special::gamma::Gamma,
special::{beta, gamma, hypergeometric},
statistics::distrib::Continuous,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::clone::Clone;
/// T distribution
///
/// Fore more information:
/// <https://en.wikipedia.org/wiki/T_distribution>
///
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug)]
pub struct T<K> {
// degrees of freedom
n: K,
}
impl<K> T<K>
where
K: Real,
{
/// Create a probability distribution
///
/// # Arguments
///
/// * `n`: > 0.0
///
/// # Panics
///
/// if n <= 0.0
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Continuous, T};
///
/// let distrib: T<f64> = T::new(1.2);
/// ```
pub fn new(n: K) -> T<K> {
if n < K::zero() {
panic!()
}
T { n }
}
}
impl<K> Continuous<K> for T<K>
where
K: Real + Beta + Hypergeometric + Gamma,
{
/// Probability density function
///
///
/// # Arguments
///
/// * `x` Random variable x &isin ࡃ
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Continuous, T};
///
/// let distrib: T<f64> = T::new(2.0);
/// let x: f64 = 0.5;
/// let p: f64 = distrib.pdf(x);
/// ```
fn pdf(&self, x: K) -> K {
gamma::gamma((self.n + K::one()) / K::from_f64(2.0))
* (K::one() + x * x / self.n).pow(-((self.n + K::one()) / K::from_f64(2.0)))
/ ((self.n * K::pi()).sqrt() * gamma::gamma(self.n / K::from_f64(2.0)))
}
/// Cumulative distribution function
///
/// # Arguments
///
/// * `x` Random variable
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Continuous, T};
///
/// let distrib: T<f64> = T::new(1.3);
/// let x: f64 = 0.4;
/// let p: f64 = distrib.cdf(x);
/// ```
fn cdf(&self, x: K) -> K {
let k: K = (self.n + K::one()) / K::from_f64(2.0);
let f21: K = hypergeometric::f21(
K::from_f64(0.5),
k,
K::from_f64(1.5),
-(x.pow(K::from_f64(2.0))) / self.n,
);
K::from_f64(0.5)
+ x * gamma::gamma(k) * f21
/ ((self.n * K::pi()).sqrt() * gamma::gamma(self.n / K::from_f64(2.0)))
}
/// Quantile function of inverse cdf
fn quantile(&self, _p: K) -> K {
unimplemented!();
}
/// Expected value
///
/// # Panics
///
/// if self.n <= 1.0
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Continuous, T};
///
/// let distrib: T<f64> = T::new(1.2);
/// let mean: f64 = distrib.mean();
/// ```
fn mean(&self) -> K {
if self.n > K::one() {
return K::zero();
}
panic!();
}
/// Variance
///
/// # Example
///
/// ```
/// use mathru::statistics::distrib::{Continuous, T};
///
/// let distrib: T<f64> = T::new(2.2);
/// let var: f64 = distrib.variance();
/// ```
fn variance(&self) -> K {
if self.n > K::from_f64(2.0) {
return self.n / (self.n - K::from_f64(2.0));
}
if self.n > K::one() {
K::from_f64(f64::INFINITY)
} else {
panic!()
}
}
///
/// # Panics
///
/// if self.n <= 3
fn skewness(&self) -> K {
if self.n <= K::from_f64(3.0) {
panic!("Skewness is not defined if degrees of freedom is smaller or equal 3");
}
K::zero()
}
/// Median is the value separating the higher half from the lower half of a
/// probability distribution.
fn median(&self) -> K {
K::zero()
}
fn entropy(&self) -> K {
let a: K = (self.n + K::one()) / K::from_f64(2.0);
let b: K = self.n / K::from_f64(2.0);
(a * (gamma::digamma(a) - gamma::digamma(b)))
+ (self.n.sqrt() * beta::beta(b, K::from_f64(0.5))).ln()
}
}