3 * Copyright (C) 2002, 2004 Jason H. Stover.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21 * Computes the cumulative distribution function for the Gaussian
22 * distribution using a rational function approximation. The
23 * computation is for the standard Normal distribution, i.e., mean 0
24 * and standard deviation 1. If you want to compute Pr(X < t) for a
25 * Gaussian random variable X with non-zero mean m and standard
26 * deviation sd not equal to 1, find gsl_cdf_ugaussian ((t-m)/sd).
27 * This approximation is accurate to at least double precision. The
28 * accuracy was verified with a pari-gp script. The largest error
29 * found was about 1.4E-20. The coefficients were derived by Cody.
33 * W.J. Cody. "Rational Chebyshev Approximations for the Error
34 * Function," Mathematics of Computation, v23 n107 1969, 631-637.
36 * W. Fraser, J.F Hart. "On the Computation of Rational Approximations
37 * to Continuous Functions," Communications of the ACM, v5 1962.
39 * W.J. Kennedy Jr., J.E. Gentle. "Statistical Computing." Marcel Dekker. 1980.
46 #include <gsl/gsl_math.h>
47 #include <gsl/gsl_cdf.h>
50 #define M_1_SQRT2PI (M_2_SQRTPI * M_SQRT1_2 / 2.0)
53 #define SQRT32 (4.0 * M_SQRT2)
56 * IEEE double precision dependent constants.
58 * GAUSS_EPSILON: Smallest positive value such that
59 * gsl_cdf_gaussian(x) > 0.5.
60 * GAUSS_XUPPER: Largest value x such that gsl_cdf_gaussian(x) < 1.0.
61 * GAUSS_XLOWER: Smallest value x such that gsl_cdf_gaussian(x) > 0.0.
64 #define GAUSS_EPSILON (GSL_DBL_EPSILON / 2)
65 #define GAUSS_XUPPER (8.572)
66 #define GAUSS_XLOWER (-37.519)
68 #define GAUSS_SCALE (16.0)
71 get_del (double x, double rational)
77 xsq = floor (x * GAUSS_SCALE) / GAUSS_SCALE;
78 del = (x - xsq) * (x + xsq);
81 result = exp (-0.5 * xsq * xsq) * exp (-1.0 * del) * rational;
87 * Normal cdf for fabs(x) < 0.66291
90 gauss_small (const double x)
99 2.2352520354606839287,
100 161.02823106855587881,
101 1067.6894854603709582,
102 18154.981253343561249,
103 0.065682337918207449113
105 const double b[4] = {
106 47.20258190468824187,
107 976.09855173777669322,
108 10260.932208618978205,
109 45507.789335026729956
116 for (i = 0; i < 3; i++)
118 xnum = (xnum + a[i]) * xsq;
119 xden = (xden + b[i]) * xsq;
122 result = x * (xnum + a[3]) / (xden + b[3]);
128 * Normal cdf for 0.66291 < fabs(x) < sqrt(32).
131 gauss_medium (const double x)
140 const double c[9] = {
141 0.39894151208813466764,
142 8.8831497943883759412,
143 93.506656132177855979,
144 597.27027639480026226,
145 2494.5375852903726711,
146 6848.1904505362823326,
147 11602.651437647350124,
148 9842.7148383839780218,
149 1.0765576773720192317e-8
151 const double d[8] = {
152 22.266688044328115691,
153 235.38790178262499861,
154 1519.377599407554805,
155 6485.558298266760755,
156 18615.571640885098091,
157 34900.952721145977266,
158 38912.003286093271411,
159 19685.429676859990727
167 for (i = 0; i < 7; i++)
169 xnum = (xnum + c[i]) * absx;
170 xden = (xden + d[i]) * absx;
173 temp = (xnum + c[7]) / (xden + d[7]);
175 result = get_del (x, temp);
182 * {sqrt(32) < x < GAUSS_XUPPER} union { GAUSS_XLOWER < x < -sqrt(32) }.
185 gauss_large (const double x)
195 const double p[6] = {
197 0.1274011611602473639,
198 0.022235277870649807,
199 0.001421619193227893466,
200 2.9112874951168792e-5,
201 0.02307344176494017303
203 const double q[5] = {
205 0.468238212480865118,
206 0.0659881378689285515,
207 0.00378239633202758244,
208 7.29751555083966205e-5
216 for (i = 0; i < 4; i++)
218 xnum = (xnum + p[i]) * xsq;
219 xden = (xden + q[i]) * xsq;
222 temp = xsq * (xnum + p[4]) / (xden + q[4]);
223 temp = (M_1_SQRT2PI - temp) / absx;
225 result = get_del (x, temp);
231 gsl_cdf_ugaussian_P (const double x)
234 double absx = fabs (x);
236 if (absx < GAUSS_EPSILON)
241 else if (absx < 0.66291)
243 result = 0.5 + gauss_small (x);
246 else if (absx < SQRT32)
248 result = gauss_medium (x);
252 result = 1.0 - result;
257 else if (x > GAUSS_XUPPER)
262 else if (x < GAUSS_XLOWER)
269 result = gauss_large (x);
273 result = 1.0 - result;
281 gsl_cdf_ugaussian_Q (const double x)
284 double absx = fabs (x);
286 if (absx < GAUSS_EPSILON)
291 else if (absx < 0.66291)
293 result = gauss_small (x);
297 result = fabs (result) + 0.5;
301 result = 0.5 - result;
306 else if (absx < SQRT32)
308 result = gauss_medium (x);
312 result = 1.0 - result;
317 else if (x > -(GAUSS_XLOWER))
322 else if (x < -(GAUSS_XUPPER))
329 result = gauss_large (x);
333 result = 1.0 - result;
342 gsl_cdf_gaussian_P (const double x, const double sigma)
344 return gsl_cdf_ugaussian_P (x / sigma);
348 gsl_cdf_gaussian_Q (const double x, const double sigma)
350 return gsl_cdf_ugaussian_Q (x / sigma);