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
//! Standard normal CDF using ACM Algorithm 209
/// Computes the standard normal CDF using ACM Algorithm 209.
/// Returns P(Z <= x) where Z is a standard normal random variable.
///
/// Calculates (1/sqrt(2*pi)) * integral from -infinity to x of e^(-u^2/2) du
/// by means of polynomial approximations due to A. M. Murray of Aberdeen University.
///
/// See: http://dl.acm.org/citation.cfm?id=367664
///
/// # Arguments
///
/// * `x` - -infinity..+infinity
///
/// # Returns
///
/// Area under the Standard Normal Curve from -infinity to x
pub fn gauss_cdf(x: f64) -> f64 {
let z: f64;
if x.abs() < 1e-9 {
z = 0.0;
} else {
let mut y = x.abs() / 2.0;
if y >= 3.0 {
z = 1.0;
} else if y < 1.0 {
let w = y * y;
z = 0.000124818987f64
.mul_add(w, -0.001075204047)
.mul_add(w, 0.005198775019)
.mul_add(w, -0.019198292004)
.mul_add(w, 0.059054035642)
.mul_add(w, -0.151968751364)
.mul_add(w, 0.319152932694)
.mul_add(w, -0.531923007300)
.mul_add(w, 0.797884560593)
* y
* 2.0;
} else {
y -= 2.0;
z = (-0.000045255659f64)
.mul_add(y, 0.000152529290)
.mul_add(y, -0.000019538132)
.mul_add(y, -0.000676904986)
.mul_add(y, 0.001390604284)
.mul_add(y, -0.000794620820)
.mul_add(y, -0.002034254874)
.mul_add(y, 0.006549791214)
.mul_add(y, -0.010557625006)
.mul_add(y, 0.011630447319)
.mul_add(y, -0.009279453341)
.mul_add(y, 0.005353579108)
.mul_add(y, -0.002141268741)
.mul_add(y, 0.000535310849)
.mul_add(y, 0.999936657524);
}
}
if x > 0.0 {
(z + 1.0) / 2.0
} else {
(1.0 - z) / 2.0
}
}