1use crate::error_function::erfc;
10use crate::gamma::ln_gamma;
11use crate::incomplete_beta::incomplete_beta;
12use crate::incomplete_gamma::{incomplete_gamma, incomplete_gamma_upper};
13use std::f64::consts::{PI, SQRT_2};
14
15pub fn normal_pdf(x: f64, mean: f64, std_dev: f64) -> f64 {
19 if !(std_dev > 0.0) {
20 return f64::NAN;
21 }
22 let z = (x - mean) / std_dev;
23 (-0.5 * z * z).exp() / (std_dev * (2.0 * PI).sqrt())
24}
25
26pub fn normal_cdf(x: f64, mean: f64, std_dev: f64) -> f64 {
28 if !(std_dev > 0.0) {
29 return f64::NAN;
30 }
31 0.5 * erfc(-(x - mean) / (std_dev * SQRT_2))
32}
33
34pub fn normal_quantile(p: f64, mean: f64, std_dev: f64) -> f64 {
36 if !(std_dev > 0.0) || !(0.0..=1.0).contains(&p) {
37 return f64::NAN;
38 }
39 if p == 0.0 {
40 return f64::NEG_INFINITY;
41 }
42 if p == 1.0 {
43 return f64::INFINITY;
44 }
45 mean + std_dev * standard_normal_quantile(p)
46}
47
48fn standard_normal_quantile(p: f64) -> f64 {
49 const A: [f64; 6] = [
51 -3.969683028665376e+01, 2.209460984245205e+02, -2.759285104469687e+02,
52 1.383_577_518_672_69e2, -3.066479806614716e+01, 2.506628277459239e+00,
53 ];
54 const B: [f64; 5] = [
55 -5.447609879822406e+01, 1.615858368580409e+02, -1.556989798598866e+02,
56 6.680131188771972e+01, -1.328068155288572e+01,
57 ];
58 const C: [f64; 6] = [
59 -7.784894002430293e-03, -3.223964580411365e-01, -2.400758277161838e+00,
60 -2.549732539343734e+00, 4.374664141464968e+00, 2.938163982698783e+00,
61 ];
62 const D: [f64; 4] = [7.784695709041462e-03, 3.224671290700398e-01, 2.445134137142996e+00, 3.754408661907416e+00];
63 let plow = 0.02425;
64 let mut x = if p < plow {
65 let q = (-2.0 * p.ln()).sqrt();
66 (((((C[0] * q + C[1]) * q + C[2]) * q + C[3]) * q + C[4]) * q + C[5])
67 / ((((D[0] * q + D[1]) * q + D[2]) * q + D[3]) * q + 1.0)
68 } else if p <= 1.0 - plow {
69 let q = p - 0.5;
70 let r = q * q;
71 (((((A[0] * r + A[1]) * r + A[2]) * r + A[3]) * r + A[4]) * r + A[5]) * q
72 / (((((B[0] * r + B[1]) * r + B[2]) * r + B[3]) * r + B[4]) * r + 1.0)
73 } else {
74 let q = (-2.0 * (1.0 - p).ln()).sqrt();
75 -(((((C[0] * q + C[1]) * q + C[2]) * q + C[3]) * q + C[4]) * q + C[5])
76 / ((((D[0] * q + D[1]) * q + D[2]) * q + D[3]) * q + 1.0)
77 };
78 for _ in 0..3 {
79 let e = normal_cdf(x, 0.0, 1.0) - p;
80 let u = e * (2.0 * PI).sqrt() * (0.5 * x * x).exp();
81 x -= u / (1.0 + 0.5 * x * u);
82 }
83 x
84}
85
86pub fn gamma_cdf(x: f64, shape: f64, scale: f64) -> f64 {
90 if !(shape > 0.0) || !(scale > 0.0) {
91 return f64::NAN;
92 }
93 if x <= 0.0 { 0.0 } else { incomplete_gamma(shape, x / scale) }
94}
95
96pub fn chi_square_cdf(x: f64, k: f64) -> f64 {
98 gamma_cdf(x, 0.5 * k, 2.0)
99}
100
101pub fn chi_square_survival(x: f64, k: f64) -> f64 {
103 if !(k > 0.0) {
104 return f64::NAN;
105 }
106 if x <= 0.0 { 1.0 } else { incomplete_gamma_upper(0.5 * k, 0.5 * x) }
107}
108
109pub fn chi_square_quantile(p: f64, k: f64) -> f64 {
111 if !(k > 0.0) || !(0.0..=1.0).contains(&p) {
112 return f64::NAN;
113 }
114 if p == 0.0 {
115 return 0.0;
116 }
117 if p == 1.0 {
118 return f64::INFINITY;
119 }
120 invert_monotone(|x| chi_square_cdf(x, k), p, 0.0, f64::INFINITY, k.max(1.0))
121}
122
123pub fn beta_cdf(x: f64, a: f64, b: f64) -> f64 {
127 if !(a > 0.0) || !(b > 0.0) {
128 return f64::NAN;
129 }
130 incomplete_beta(a, b, x.clamp(0.0, 1.0))
131}
132
133pub fn student_t_cdf(t: f64, nu: f64) -> f64 {
136 if !(nu > 0.0) || t.is_nan() {
137 return f64::NAN;
138 }
139 if t.is_infinite() {
140 return if t > 0.0 { 1.0 } else { 0.0 };
141 }
142 let tail = 0.5 * incomplete_beta(0.5 * nu, 0.5, nu / (nu + t * t));
143 if t > 0.0 { 1.0 - tail } else { tail }
144}
145pub fn student_t_quantile(p: f64, nu: f64) -> f64 {
149 if !(nu > 0.0) || !(0.0..=1.0).contains(&p) {
150 return f64::NAN;
151 }
152 if p == 0.0 {
153 return f64::NEG_INFINITY;
154 }
155 if p == 1.0 {
156 return f64::INFINITY;
157 }
158 if p == 0.5 {
159 return 0.0;
160 }
161 let tail = p.min(1.0 - p);
164 let t = invert_monotone(|t| -student_t_cdf(-t, nu), -tail, 0.0, f64::INFINITY, 1.0);
165 if p < 0.5 { -t } else { t }
166}
167
168pub fn f_cdf(x: f64, d1: f64, d2: f64) -> f64 {
170 if !(d1 > 0.0) || !(d2 > 0.0) {
171 return f64::NAN;
172 }
173 if x <= 0.0 {
174 return 0.0;
175 }
176 if x.is_infinite() {
177 return 1.0;
178 }
179 incomplete_beta(0.5 * d1, 0.5 * d2, d1 * x / (d1 * x + d2))
180}
181
182pub fn poisson_pmf(k: u64, lambda: f64) -> f64 {
186 if !(lambda >= 0.0) {
187 return f64::NAN;
188 }
189 if lambda == 0.0 {
190 return if k == 0 { 1.0 } else { 0.0 };
191 }
192 (k as f64 * lambda.ln() - lambda - ln_gamma(k as f64 + 1.0)).exp()
193}
194
195pub fn poisson_cdf(k: u64, lambda: f64) -> f64 {
197 if !(lambda >= 0.0) {
198 return f64::NAN;
199 }
200 if lambda == 0.0 {
201 return 1.0;
202 }
203 incomplete_gamma_upper(k as f64 + 1.0, lambda)
204}
205
206pub fn binomial_pmf(k: u64, n: u64, p: f64) -> f64 {
208 if !(0.0..=1.0).contains(&p) {
209 return f64::NAN;
210 }
211 if k > n {
212 return 0.0;
213 }
214 if p == 0.0 {
215 return if k == 0 { 1.0 } else { 0.0 };
216 }
217 if p == 1.0 {
218 return if k == n { 1.0 } else { 0.0 };
219 }
220 let (kf, nf) = (k as f64, n as f64);
221 (ln_gamma(nf + 1.0) - ln_gamma(kf + 1.0) - ln_gamma(nf - kf + 1.0) + kf * p.ln() + (nf - kf) * (-p).ln_1p()).exp()
222}
223
224pub fn binomial_cdf(k: u64, n: u64, p: f64) -> f64 {
226 if !(0.0..=1.0).contains(&p) {
227 return f64::NAN;
228 }
229 if k >= n {
230 return 1.0;
231 }
232 incomplete_beta((n - k) as f64, k as f64 + 1.0, 1.0 - p)
233}
234
235fn invert_monotone<F: Fn(f64) -> f64>(f: F, target: f64, lo: f64, hi: f64, start: f64) -> f64 {
239 let mut a = lo;
240 let mut b = start;
241 while f(b) < target && b < hi {
242 a = b;
243 b *= 2.0;
244 if b > 1e300 {
245 return f64::INFINITY;
246 }
247 }
248 for _ in 0..300 {
249 let m = 0.5 * (a + b);
250 if f(m) < target {
251 a = m;
252 } else {
253 b = m;
254 }
255 if (b - a) <= 4.0 * f64::EPSILON * b.abs() {
256 break;
257 }
258 }
259 0.5 * (a + b)
260}
261
262#[cfg(test)]
263mod tests {
264 use super::*;
265
266 fn close(a: f64, b: f64, rel: f64) -> bool {
267 (a - b).abs() <= rel * b.abs()
268 }
269
270 #[test]
271 fn normal() {
272 assert!(close(normal_cdf(1.5, 0.0, 1.0), 0.9331927987311419, 1e-14));
273 assert!(close(normal_cdf(-6.0, 0.0, 1.0), 9.865_876_450_376_98e-10, 1e-12));
274 assert!(close(normal_cdf(0.7, 1.0, 1.0), 0.3820885778110474, 1e-14));
275 assert!(close(normal_pdf(0.0, 0.0, 1.0), 0.3989422804014327, 1e-15));
276 assert!(close(normal_pdf(3.0, 1.0, 2.0), 0.12098536225957168, 1e-14));
277 assert!(close(normal_quantile(0.975, 0.0, 1.0), 1.959963984540054, 1e-14));
278 assert!(close(normal_quantile(1e-10, 0.0, 1.0), -6.361340902404056, 1e-13));
279 assert_eq!(normal_quantile(0.5, 3.0, 2.0), 3.0);
280 assert!(close(normal_quantile(0.975, 10.0, 2.0), 10.0 + 2.0 * 1.959963984540054, 1e-14));
281 assert_eq!(normal_quantile(0.0, 0.0, 1.0), f64::NEG_INFINITY);
282 assert!(normal_cdf(0.0, 0.0, -1.0).is_nan());
283 for &p in &[1e-300, 1e-12, 0.01, 0.3, 0.9, 1.0 - 1e-12] {
284 let x = normal_quantile(p, 0.0, 1.0);
285 assert!(close(normal_cdf(x, 0.0, 1.0), p, 1e-12), "p={p}");
286 }
287 }
288
289 #[test]
290 fn chi_square() {
291 assert!(close(chi_square_cdf(3.84, 1.0), 0.9499564787512949, 1e-13));
292 assert!(close(chi_square_cdf(10.0, 4.0), 0.9595723180054872, 1e-13));
293 assert!(close(chi_square_cdf(0.5, 7.0), 0.0005535186095750345, 1e-12));
294 assert!((chi_square_cdf(3.0, 5.0) + chi_square_survival(3.0, 5.0) - 1.0).abs() < 1e-14);
295 assert!(close(chi_square_quantile(0.95, 3.0), 7.814727903251178, 1e-12));
296 assert_eq!(chi_square_cdf(-1.0, 3.0), 0.0);
297 }
298
299 #[test]
300 fn student_and_f() {
301 assert!(close(student_t_cdf(2.0, 5.0), 0.9490302605850708, 1e-13));
302 assert!(close(student_t_cdf(-1.3, 3.0), 0.142_233_754_363_948_7, 1e-13));
303 assert!(close(student_t_cdf(0.4, 30.0), 0.654_004_741_742_904, 1e-13));
304 assert_eq!(student_t_cdf(0.0, 4.0), 0.5);
305 assert!(close(student_t_quantile(0.975, 5.0), 2.5705818356363148, 1e-12));
306 assert!(close(student_t_quantile(0.025, 5.0), -2.5705818356363148, 1e-12));
307 assert!(close(f_cdf(2.5, 4.0, 9.0), 0.883_285_057_283_431_2, 1e-13));
308 assert!(close(f_cdf(0.7, 10.0, 3.0), 0.291_805_644_806_145_4, 1e-13));
309 }
310
311 #[test]
312 fn gamma_and_beta() {
313 assert!(close(gamma_cdf(4.0, 2.5, 1.5), 0.6234323552910231, 1e-13));
314 assert!(close(beta_cdf(0.4, 2.0, 3.0), 0.5248, 1e-13));
315 assert_eq!(beta_cdf(-1.0, 2.0, 3.0), 0.0);
316 assert_eq!(beta_cdf(2.0, 2.0, 3.0), 1.0);
317 }
318
319 #[test]
320 fn discrete() {
321 assert!(close(poisson_pmf(3, 3.2), 0.22261598332718404, 1e-13));
322 assert!(close(poisson_cdf(3, 3.2), 0.6025197244055571, 1e-12));
323 let sum: f64 = (0..=3).map(|k| poisson_pmf(k, 3.2)).sum();
324 assert!(close(poisson_cdf(3, 3.2), sum, 1e-13));
325 assert!(close(binomial_pmf(3, 10, 0.3), 0.266827932, 1e-13));
326 assert!(close(binomial_cdf(4, 10, 0.3), 0.8497316674, 1e-12));
327 assert_eq!(binomial_cdf(10, 10, 0.3), 1.0);
328 assert_eq!(binomial_pmf(11, 10, 0.3), 0.0);
329 assert!(binomial_pmf(1, 10, 1.5).is_nan());
330 assert_eq!(poisson_pmf(0, 0.0), 1.0);
331 }
332
333 #[test]
334 fn student_t_quantile_keeps_tail_probabilities_exact() {
335 for nu in [1.0, 2.5, 10.0, 100.0] {
337 for p in [1e-10, 1e-5, 0.3] {
338 let t = student_t_quantile(p, nu);
339 assert!(((student_t_cdf(t, nu) - p) / p).abs() < 1e-12, "nu = {nu}, p = {p}");
340 if p > 1e-6 {
342 assert!((student_t_quantile(1.0 - p, nu) + t).abs() <= 1e-8 * t.abs());
343 }
344 }
345 }
346 }
347}