def _log_phi(z):
<EOL>if z * z < <NUM_LIT>:<EOL><INDENT>coef = -z / SQRT2PI<EOL>val = functools.reduce(lambda acc, c: coef * (c + acc), CS, <NUM_LIT:0>)<EOL>res = -<NUM_LIT:2> * val - log(<NUM_LIT:2>)<EOL>dres = exp(-(z * z) / <NUM_LIT:2> - res) / SQRT2PI<EOL><DEDENT>elif z < -<NUM_LIT>:<EOL><INDENT>num = functools.reduce(<EOL>lambda acc, r: -z * acc / SQRT2 + r, RS, <NUM_LIT>)<EOL>den = functools.reduce(lambda acc, q: -z * acc / SQRT2 + q, QS, <NUM_LIT:1.0>)<EOL>res = log(num / (<NUM_LIT:2> * den)) - (z * z) / <NUM_LIT:2><EOL>dres = abs(den / num) * sqrt(<NUM_LIT> / pi)<EOL><DEDENT>else:<EOL><INDENT>res = log(normal_cdf(z))<EOL>dres = exp(-(z * z) / <NUM_LIT:2> - res) / SQRT2PI<EOL><DEDENT>return res, dres<EOL>
Stable computation of the log of the Normal CDF and its derivative.