-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinverse_cdf.jl
66 lines (56 loc) · 1.56 KB
/
inverse_cdf.jl
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
function invnorm(p)
#Coefficients in rational approximations.
a = zeros(6);
a[1] = -3.969683028665376e+01
a[2] = 2.209460984245205e+02
a[3] = -2.759285104469687e+02
a[4] = 1.383577518672690e+02
a[5] = -3.066479806614716e+01
a[6] = 2.506628277459239e+00
b = zeros(5);
b[1] = -5.447609879822406e+01
b[2] = 1.615858368580409e+02
b[3] = -1.556989798598866e+02
b[4] = 6.680131188771972e+01
b[5] = -1.328068155288572e+01
c = zeros(6);
c[1] = -7.784894002430293e-03
c[2] = -3.223964580411365e-01
c[3] = -2.400758277161838e+00
c[4] = -2.549732539343734e+00
c[5] = 4.374664141464968e+00
c[6] = 2.938163982698783e+00
d = zeros(4);
d[1] = 7.784695709041462e-03
d[2] = 3.224671290700398e-01
d[3] = 2.445134137142996e+00
d[4] = 3.754408661907416e+00
#Define break-points.
p_low = 0.02425;
p_high = 1 - p_low;
#Rational approximation for lower region.
if ((0 < p) && (p < p_low))
q = sqrt(-2*log(p))
x = (((((c[1]*q+c[2])*q+c[3])*q+c[4])*q+c[5])*q+c[6]) /
((((d[1]*q+d[2])*q+d[3])*q+d[4])*q+1)
end
#Rational approximation for central region.
if ((p_low <= p) && (p <= p_high))
q = p - 0.5
r = q*q
x = (((((a[1]*r+a[2])*r+a[3])*r+a[4])*r+a[5])*r+a[6])*q /
(((((b[1]*r+b[2])*r+b[3])*r+b[4])*r+b[5])*r+1)
end
#Rational approximation for upper region.
if ((p_high < p) && (p < 1))
q = sqrt(-2*log(1-p))
x = -(((((c[1]*q+c[2])*q+c[3])*q+c[4])*q+c[5])*q+c[6]) /
((((d[1]*q+d[2])*q+d[3])*q+d[4])*q+1)
end
if p == 0
x = -Inf;
elseif p == 1
x = Inf;
end
return x;
end