###Edit###
I tested a port of alnorm
to Mathematica, which computes the values to arbitrary precision. To compare the results, here is a plot of the natural log of the ratios of upper tail values $1 - \Phi(z)$ with $z\ge 1$. (A positive relative error means alnorm
is too large.)
The values are always accurate to $4 \times 10^{-11}$ relative to the vanishingly small tail probabilities. You can see where the calculation switches to an asymptotic formula (at $z=16$) and it is evident that this formula becomes extremely accurate as $z$ increases. The plot stops at $z=\sqrt(2\times 708) \approx 37.6$ because here is where double-precision exponentiation begins underflowing.
For example, alnorm[-6.0]
returns $9.865\ 876\ 450\ 315E-10$ while the true value, equal to $\frac{1}{2}\text{erfc}(3\sqrt{2})$, is approximately $9.865\ 876\ 450\ 377E-10$, first differing in the twelfth decimal digit.
NB As part of this edit, I changed UPPER_TAIL_IS_ZERO
from 15.
to 16.
in the code: it makes the result a tiny bit more accurate for $Z$ between $15$ and $16$.
(End of edit.)
#----------------------------------------------------------------------#
# ALNORM.AWK
# Compute values of the cumulative normal probability function.
# From G. Dallal's STAT-SAK (Fortran code).
# Additional precision using asymptotic expression added 7/8/92.
#----------------------------------------------------------------------#
BEGIN {
for (i=-85; i<=85; i++) {
x = i/10
p = alnorm(x, 0)
printf("%3.1f %12.10f\n", x, p)
}
exit
}
function alnorm(z,up, y,aln,w) {
#
# ALGORITHM AS 66 APPL. STATIST. (1973) VOL.22, NO.3:
# Hill, I.D. (1973). Algorithm AS 66. The normal integral.
# Appl. Statist.,22,424-427.
#
# Evaluates the tail area of the standard normal curve from
# z to infinity if up, or from -infinity to z if not up.
#
# LOWER_TAIL_IS_ONE, UPPER_TAIL_IS_ZERO, and EXP_MIN_ARG
# must be set to suit this computer and compiler.
LOWER_TAIL_IS_ONE = 8.5 # I.e., alnorm(8.5,0) = .999999999999+
UPPER_TAIL_IS_ZERO = 1516.0 # Changes to power series expression
FORMULA_BREAK = 1.28 # Changes cont. fraction coefficients
EXP_MIN_ARG = -708 # I.e., exp(-708) is essentially true 0
if (z < 0.0) {
up = !up
z = -z
}
if ((z <= LOWER_TAIL_IS_ONE) || (up && z <= UPPER_TAIL_IS_ZERO)) {
y = 0.5 * z * z
if (z > FORMULA_BREAK) {
if (-y > EXP_MIN_ARG) {
aln = .398942280385 * exp(-y) / \
(z - 3.8052E-8 + 1.00000615302 / \
(z + 3.98064794E-4 + 1.98615381364 / \
(z - 0.151679116635 + 5.29330324926 / \
(z + 4.8385912808 - 15.1508972451 / \
(z + 0.742380924027 + 30.789933034 / \
(z + 3.99019417011))))))
} else {
aln = 0.0
}
} else {
aln = 0.5 - z * (0.398942280444 - 0.399903438504 * y / \
(y + 5.75885480458 - 29.8213557808 / \
(y + 2.62433121679 + 48.6959930692 / \
(y + 5.92885724438))))
}
} else {
if (up) { # 7/8/92
# Uses asymptotic expansion for exp(-z*z/2)/alnorm(z)
# Agrees with continued fraction to 11 s.f. when z >= 15
# and coefficients through 706 are used.
y = -0.5*z*z
if (y > EXP_MIN_ARG) {
w = -0.5/y # 1/z^2
aln = 0.3989422804014327*exp(y)/ \
(z*(1 + w*(1 + w*(-2 + w*(10 + w*(-74 + w*706))))))
# Next coefficients would be -8162, 110410
} else {
aln = 0.0
}
} else {
aln = 0.0
}
}
return up ? aln : 1.0 - aln
}
### end of file ###