Line | |
---|
1 | function e = normcdfln(x) |
---|
2 | % NORMCDFLN log of normal cumulative density function. |
---|
3 | % More accurate than log(normcdf(x)) when x is small. |
---|
4 | % The following is a quick and dirty approximation to normcdfln: |
---|
5 | % normcdfln(x) =approx -(log(1+exp(0.88-x))/1.5)^2 |
---|
6 | |
---|
7 | % Written by Tom Minka |
---|
8 | % (c) Microsoft Corporation. All rights reserved. |
---|
9 | |
---|
10 | % make e the same shape as x, and inherit any NaNs. |
---|
11 | e = x; |
---|
12 | t = -6.5; |
---|
13 | i = find(x >= t); |
---|
14 | if ~isempty(i) |
---|
15 | e(i) = log(normcdf(x(i))); |
---|
16 | end |
---|
17 | i = find(x < t); |
---|
18 | if ~isempty(i) |
---|
19 | x = x(i); |
---|
20 | z = x.^(-2); |
---|
21 | if 0 |
---|
22 | % log of asymptotic series for cdf |
---|
23 | % subs(x=-x,asympt(sqrt(2*Pi)*gauss_cdf(-x),x)); |
---|
24 | c = [-1 3 -15 105 -945 10395 -135135 2027025 -34459425 654729075]; |
---|
25 | y = 0; |
---|
26 | for i = length(c):-1:1 |
---|
27 | y = z.*(y + c(i)); |
---|
28 | end |
---|
29 | %y = z.*(c(1)+z.*(c(2)+z.*(c(3)+z.*(c(4)+z.*(c(5)+z.*(c(6)+z.*c(7))))))); |
---|
30 | y = log(1+y); |
---|
31 | else |
---|
32 | % asymptotic series for logcdf |
---|
33 | % subs(x=-x,asympt(log(gauss_cdf(-x)),x)); |
---|
34 | c = [-1 5/2 -37/3 353/4 -4081/5 55205/6 -854197/7]; |
---|
35 | y = z.*(c(1)+z.*(c(2)+z.*(c(3)+z.*(c(4)+z.*(c(5)+z.*(c(6)+z.*c(7))))))); |
---|
36 | end |
---|
37 | e(i) = y -0.5*log(2*pi) -0.5*x.^2 - log(-x); |
---|
38 | end |
---|
Note: See
TracBrowser
for help on using the repository browser.