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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use crate::special::Gamma;
/// A module containing functions to work with the Chi-squared distribution.
pub struct Chi2;
impl Chi2 {
/// Calculates the Probability Density Function (PDF) of the Chi-squared distribution.
///
/// The Chi-squared distribution describes the distribution of the sum of the squares of
/// independent standard normal random variables.
///
/// # Parameters
///
/// - `x`: The value at which to evaluate the PDF.
/// - `df`: The degrees of freedom parameter.
///
/// # Returns
///
/// The calculated PDF.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::distr::Chi2;
///
/// let x = 6.0;
/// let df = 5.0;
///
/// let pdf = Chi2::pdf(x, df);
///
/// println!("PDF at x = {}: {}", x, pdf);
/// ```
/// <hr/>
pub fn pdf(x: f64, df: f64) -> f64 {
if x >= 0_f64 {
let p1 = x.powf((df - 2_f64) / 2_f64);
let p2 = std::f64::consts::E.powf(-x / 2_f64);
let p3 = 2_f64.powf(df / 2_f64);
let p4 = Gamma::lanczos(df / 2_f64);
(p1 * p2) / (p3 * p4)
} else {
0_f64
}
}
/// Calculates the Cumulative Density Function (CDF) of the Chi-squared distribution.
///
/// # Parameters
///
/// - `bound`: The upper bound of integration for the CDF.
/// - `df`: The degrees of freedom parameter.
///
/// # Returns
///
/// The calculated CDF.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::distr::Chi2;
///
/// let bound = 2.0;
/// let df = 4.0;
///
/// let cdf = Chi2::cdf(bound, df);
///
/// println!("CDF at bound = {}: {}", bound, cdf);
/// ```
/// <hr/>
pub fn cdf(bound: f64, df: f64) -> f64 {
Gamma::reggamma(df / 2_f64, bound / 2_f64)
}
/// Calculates a 2 Tailed Cumulative Density Function (CDF) of the Chi-squared distribution.
///
/// # Parameters
///
/// - `lower`: The lower bound of integration for the CDF.
/// - `upper`: The upper bound of integration for the CDF.
/// - `df`: The degrees of freedom parameter.
///
/// # Returns
///
/// The calculated 2-tailed CDF.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::distr::Chi2;
///
/// let lower = 2.0;
/// let upper = 5.0;
/// let df = 4.0;
///
/// let cdf = Chi2::tailcdf(lower, upper, df);
///
/// println!("2-tailed CDF between {} and {}: {}", lower, upper, cdf);
/// ```
/// <hr/>
pub fn tailcdf(lower: f64, upper: f64, df: f64) -> f64 {
Self::cdf(upper, df) - Self::cdf(lower, df)
}
/// Calculates the median of the Chi-squared distribution.
///
/// # Parameters
///
/// - `df`: The degrees of freedom parameter.
///
/// # Returns
///
/// The calculated median.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::distr::Chi2;
///
/// let df = 5.0;
///
/// let median = Chi2::median(df);
///
/// println!("Median for df = {}: {}", df, median);
/// ```
/// <hr/>
pub fn median(df: f64) -> f64 {
let p1 = 1_f64 - 2_f64 / (9_f64 * df);
df * p1.powi(3)
}
/// Calculates the mode of the Chi-squared distribution.
///
/// # Parameters
///
/// - `df`: The degrees of freedom parameter.
///
/// # Returns
///
/// The calculated mode.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::distr::Chi2;
///
/// let df = 4.0;
///
/// let mode = Chi2::mode(df);
///
/// println!("Mode for df = {}: {}", df, mode);
/// ```
/// <hr/>
pub fn mode(df: f64) -> f64 {
if df == 0_f64 {
return 0_f64;
}
df - 2_f64
}
/// Calculates the variance of the Chi-squared distribution.
///
/// # Parameters
///
/// - `df`: The degrees of freedom parameter.
///
/// # Returns
///
/// The calculated variance.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::distr::Chi2;
///
/// let df = 4.0;
///
/// let variance = Chi2::variance(df);
///
/// println!("Variance for df = {}: {}", df, variance);
/// ```
/// <hr/>
pub fn variance(df: f64) -> f64 {
2_f64 * df
}
/// Calculates the standard deviation of the Chi-squared distribution.
///
/// # Parameters
///
/// - `df`: The degrees of freedom parameter.
///
/// # Returns
///
/// The calculated standard deviation.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::distr::Chi2;
///
/// let df = 4.0;
///
/// let sd = Chi2::sd(df);
///
/// println!("Standard Deviation for df = {}: {}", df, sd);
/// ```
/// <hr/>
pub fn sd(df: f64) -> f64 {
Chi2::variance(df).sqrt()
}
/// Calculates the skewness of the Chi-squared distribution.
///
/// # Parameters
///
/// - `df`: The degrees of freedom parameter.
///
/// # Returns
///
/// The calculated skewness.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::distr::Chi2;
///
/// let df = 4.0;
///
/// let skewness = Chi2::skewness(df);
///
/// println!("Skewness for df = {}: {}", df, skewness);
/// ```
/// <hr/>
pub fn skewness(df: f64) -> f64 {
(8_f64 / df).sqrt()
}
/// Calculates the kurtosis of the Chi-squared distribution.
///
/// # Parameters
///
/// - `df`: The degrees of freedom parameter.
///
/// # Returns
///
/// The calculated kurtosis.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::distr::Chi2;
///
/// let df = 4.0;
///
/// let kurtosis = Chi2::kurtosis(df);
///
/// println!("Kurtosis for df = {}: {}", df, kurtosis);
/// ```
/// <hr/>
pub fn kurtosis(df: f64) -> f64 {
12_f64 / df
}
}