gsl_cdf__gauss.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* cdf/gauss.c
  2. *
  3. * Copyright (C) 2002, 2004 Jason H. Stover.
  4. *
  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.
  9. *
  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.
  14. *
  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.
  18. */
  19. /*
  20. * Computes the cumulative distribution function for the Gaussian
  21. * distribution using a rational function approximation. The
  22. * computation is for the standard Normal distribution, i.e., mean 0
  23. * and standard deviation 1. If you want to compute Pr(X < t) for a
  24. * Gaussian random variable X with non-zero mean m and standard
  25. * deviation sd not equal to 1, find gsl_cdf_ugaussian ((t-m)/sd).
  26. * This approximation is accurate to at least double precision. The
  27. * accuracy was verified with a pari-gp script. The largest error
  28. * found was about 1.4E-20. The coefficients were derived by Cody.
  29. *
  30. * References:
  31. *
  32. * W.J. Cody. "Rational Chebyshev Approximations for the Error
  33. * Function," Mathematics of Computation, v23 n107 1969, 631-637.
  34. *
  35. * W. Fraser, J.F Hart. "On the Computation of Rational Approximations
  36. * to Continuous Functions," Communications of the ACM, v5 1962.
  37. *
  38. * W.J. Kennedy Jr., J.E. Gentle. "Statistical Computing." Marcel Dekker. 1980.
  39. *
  40. *
  41. */
  42. #include "gsl__config.h"
  43. #include <math.h>
  44. #include "gsl_math.h"
  45. #include "gsl_cdf.h"
  46. #ifndef M_1_SQRT2PI
  47. #define M_1_SQRT2PI (M_2_SQRTPI * M_SQRT1_2 / 2.0)
  48. #endif
  49. #define SQRT32 (4.0 * M_SQRT2)
  50. /*
  51. * IEEE double precision dependent constants.
  52. *
  53. * GAUSS_EPSILON: Smallest positive value such that
  54. * gsl_cdf_gaussian(x) > 0.5.
  55. * GAUSS_XUPPER: Largest value x such that gsl_cdf_gaussian(x) < 1.0.
  56. * GAUSS_XLOWER: Smallest value x such that gsl_cdf_gaussian(x) > 0.0.
  57. */
  58. #define GAUSS_EPSILON (GSL_DBL_EPSILON / 2)
  59. #define GAUSS_XUPPER (8.572)
  60. #define GAUSS_XLOWER (-37.519)
  61. #define GAUSS_SCALE (16.0)
  62. static double
  63. get_del (double x, double rational)
  64. {
  65. double xsq = 0.0;
  66. double del = 0.0;
  67. double result = 0.0;
  68. xsq = floor (x * GAUSS_SCALE) / GAUSS_SCALE;
  69. del = (x - xsq) * (x + xsq);
  70. del *= 0.5;
  71. result = exp (-0.5 * xsq * xsq) * exp (-1.0 * del) * rational;
  72. return result;
  73. }
  74. /*
  75. * Normal cdf for fabs(x) < 0.66291
  76. */
  77. static double
  78. gauss_small (const double x)
  79. {
  80. unsigned int i;
  81. double result = 0.0;
  82. double xsq;
  83. double xnum;
  84. double xden;
  85. const double a[5] = {
  86. 2.2352520354606839287,
  87. 161.02823106855587881,
  88. 1067.6894854603709582,
  89. 18154.981253343561249,
  90. 0.065682337918207449113
  91. };
  92. const double b[4] = {
  93. 47.20258190468824187,
  94. 976.09855173777669322,
  95. 10260.932208618978205,
  96. 45507.789335026729956
  97. };
  98. xsq = x * x;
  99. xnum = a[4] * xsq;
  100. xden = xsq;
  101. for (i = 0; i < 3; i++)
  102. {
  103. xnum = (xnum + a[i]) * xsq;
  104. xden = (xden + b[i]) * xsq;
  105. }
  106. result = x * (xnum + a[3]) / (xden + b[3]);
  107. return result;
  108. }
  109. /*
  110. * Normal cdf for 0.66291 < fabs(x) < sqrt(32).
  111. */
  112. static double
  113. gauss_medium (const double x)
  114. {
  115. unsigned int i;
  116. double temp = 0.0;
  117. double result = 0.0;
  118. double xnum;
  119. double xden;
  120. double absx;
  121. const double c[9] = {
  122. 0.39894151208813466764,
  123. 8.8831497943883759412,
  124. 93.506656132177855979,
  125. 597.27027639480026226,
  126. 2494.5375852903726711,
  127. 6848.1904505362823326,
  128. 11602.651437647350124,
  129. 9842.7148383839780218,
  130. 1.0765576773720192317e-8
  131. };
  132. const double d[8] = {
  133. 22.266688044328115691,
  134. 235.38790178262499861,
  135. 1519.377599407554805,
  136. 6485.558298266760755,
  137. 18615.571640885098091,
  138. 34900.952721145977266,
  139. 38912.003286093271411,
  140. 19685.429676859990727
  141. };
  142. absx = fabs (x);
  143. xnum = c[8] * absx;
  144. xden = absx;
  145. for (i = 0; i < 7; i++)
  146. {
  147. xnum = (xnum + c[i]) * absx;
  148. xden = (xden + d[i]) * absx;
  149. }
  150. temp = (xnum + c[7]) / (xden + d[7]);
  151. result = get_del (x, temp);
  152. return result;
  153. }
  154. /*
  155. * Normal cdf for
  156. * {sqrt(32) < x < GAUSS_XUPPER} union { GAUSS_XLOWER < x < -sqrt(32) }.
  157. */
  158. static double
  159. gauss_large (const double x)
  160. {
  161. int i;
  162. double result;
  163. double xsq;
  164. double temp;
  165. double xnum;
  166. double xden;
  167. double absx;
  168. const double p[6] = {
  169. 0.21589853405795699,
  170. 0.1274011611602473639,
  171. 0.022235277870649807,
  172. 0.001421619193227893466,
  173. 2.9112874951168792e-5,
  174. 0.02307344176494017303
  175. };
  176. const double q[5] = {
  177. 1.28426009614491121,
  178. 0.468238212480865118,
  179. 0.0659881378689285515,
  180. 0.00378239633202758244,
  181. 7.29751555083966205e-5
  182. };
  183. absx = fabs (x);
  184. xsq = 1.0 / (x * x);
  185. xnum = p[5] * xsq;
  186. xden = xsq;
  187. for (i = 0; i < 4; i++)
  188. {
  189. xnum = (xnum + p[i]) * xsq;
  190. xden = (xden + q[i]) * xsq;
  191. }
  192. temp = xsq * (xnum + p[4]) / (xden + q[4]);
  193. temp = (M_1_SQRT2PI - temp) / absx;
  194. result = get_del (x, temp);
  195. return result;
  196. }
  197. double
  198. gsl_cdf_ugaussian_P (const double x)
  199. {
  200. double result;
  201. double absx = fabs (x);
  202. if (absx < GAUSS_EPSILON)
  203. {
  204. result = 0.5;
  205. return result;
  206. }
  207. else if (absx < 0.66291)
  208. {
  209. result = 0.5 + gauss_small (x);
  210. return result;
  211. }
  212. else if (absx < SQRT32)
  213. {
  214. result = gauss_medium (x);
  215. if (x > 0.0)
  216. {
  217. result = 1.0 - result;
  218. }
  219. return result;
  220. }
  221. else if (x > GAUSS_XUPPER)
  222. {
  223. result = 1.0;
  224. return result;
  225. }
  226. else if (x < GAUSS_XLOWER)
  227. {
  228. result = 0.0;
  229. return result;
  230. }
  231. else
  232. {
  233. result = gauss_large (x);
  234. if (x > 0.0)
  235. {
  236. result = 1.0 - result;
  237. }
  238. }
  239. return result;
  240. }
  241. double
  242. gsl_cdf_ugaussian_Q (const double x)
  243. {
  244. double result;
  245. double absx = fabs (x);
  246. if (absx < GAUSS_EPSILON)
  247. {
  248. result = 0.5;
  249. return result;
  250. }
  251. else if (absx < 0.66291)
  252. {
  253. result = gauss_small (x);
  254. if (x < 0.0)
  255. {
  256. result = fabs (result) + 0.5;
  257. }
  258. else
  259. {
  260. result = 0.5 - result;
  261. }
  262. return result;
  263. }
  264. else if (absx < SQRT32)
  265. {
  266. result = gauss_medium (x);
  267. if (x < 0.0)
  268. {
  269. result = 1.0 - result;
  270. }
  271. return result;
  272. }
  273. else if (x > -(GAUSS_XLOWER))
  274. {
  275. result = 0.0;
  276. return result;
  277. }
  278. else if (x < -(GAUSS_XUPPER))
  279. {
  280. result = 1.0;
  281. return result;
  282. }
  283. else
  284. {
  285. result = gauss_large (x);
  286. if (x < 0.0)
  287. {
  288. result = 1.0 - result;
  289. }
  290. }
  291. return result;
  292. }
  293. double
  294. gsl_cdf_gaussian_P (const double x, const double sigma)
  295. {
  296. return gsl_cdf_ugaussian_P (x / sigma);
  297. }
  298. double
  299. gsl_cdf_gaussian_Q (const double x, const double sigma)
  300. {
  301. return gsl_cdf_ugaussian_Q (x / sigma);
  302. }