ndtrf.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* ndtrf.c
  2. *
  3. * Normal distribution function
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, y, ndtrf();
  10. *
  11. * y = ndtrf( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns the area under the Gaussian probability density
  18. * function, integrated from minus infinity to x:
  19. *
  20. * x
  21. * -
  22. * 1 | | 2
  23. * ndtr(x) = --------- | exp( - t /2 ) dt
  24. * sqrt(2pi) | |
  25. * -
  26. * -inf.
  27. *
  28. * = ( 1 + erf(z) ) / 2
  29. * = erfc(z) / 2
  30. *
  31. * where z = x/sqrt(2). Computation is via the functions
  32. * erf and erfc.
  33. *
  34. *
  35. * ACCURACY:
  36. *
  37. * Relative error:
  38. * arithmetic domain # trials peak rms
  39. * IEEE -13,0 50000 1.5e-5 2.6e-6
  40. *
  41. *
  42. * ERROR MESSAGES:
  43. *
  44. * See erfcf().
  45. *
  46. */
  47. /* erff.c
  48. *
  49. * Error function
  50. *
  51. *
  52. *
  53. * SYNOPSIS:
  54. *
  55. * float x, y, erff();
  56. *
  57. * y = erff( x );
  58. *
  59. *
  60. *
  61. * DESCRIPTION:
  62. *
  63. * The integral is
  64. *
  65. * x
  66. * -
  67. * 2 | | 2
  68. * erf(x) = -------- | exp( - t ) dt.
  69. * sqrt(pi) | |
  70. * -
  71. * 0
  72. *
  73. * The magnitude of x is limited to 9.231948545 for DEC
  74. * arithmetic; 1 or -1 is returned outside this range.
  75. *
  76. * For 0 <= |x| < 1, erf(x) = x * P(x**2); otherwise
  77. * erf(x) = 1 - erfc(x).
  78. *
  79. *
  80. *
  81. * ACCURACY:
  82. *
  83. * Relative error:
  84. * arithmetic domain # trials peak rms
  85. * IEEE -9.3,9.3 50000 1.7e-7 2.8e-8
  86. *
  87. */
  88. /* erfcf.c
  89. *
  90. * Complementary error function
  91. *
  92. *
  93. *
  94. * SYNOPSIS:
  95. *
  96. * float x, y, erfcf();
  97. *
  98. * y = erfcf( x );
  99. *
  100. *
  101. *
  102. * DESCRIPTION:
  103. *
  104. *
  105. * 1 - erf(x) =
  106. *
  107. * inf.
  108. * -
  109. * 2 | | 2
  110. * erfc(x) = -------- | exp( - t ) dt
  111. * sqrt(pi) | |
  112. * -
  113. * x
  114. *
  115. *
  116. * For small x, erfc(x) = 1 - erf(x); otherwise polynomial
  117. * approximations 1/x P(1/x**2) are computed.
  118. *
  119. *
  120. *
  121. * ACCURACY:
  122. *
  123. * Relative error:
  124. * arithmetic domain # trials peak rms
  125. * IEEE -9.3,9.3 50000 3.9e-6 7.2e-7
  126. *
  127. *
  128. * ERROR MESSAGES:
  129. *
  130. * message condition value returned
  131. * erfcf underflow x**2 > MAXLOGF 0.0
  132. *
  133. *
  134. */
  135. /*
  136. Cephes Math Library Release 2.2: June, 1992
  137. Copyright 1984, 1987, 1988 by Stephen L. Moshier
  138. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  139. */
  140. #include <math.h>
  141. extern float MAXLOGF, SQRTHF;
  142. /* erfc(x) = exp(-x^2) P(1/x), 1 < x < 2 */
  143. static float P[] = {
  144. 2.326819970068386E-002,
  145. -1.387039388740657E-001,
  146. 3.687424674597105E-001,
  147. -5.824733027278666E-001,
  148. 6.210004621745983E-001,
  149. -4.944515323274145E-001,
  150. 3.404879937665872E-001,
  151. -2.741127028184656E-001,
  152. 5.638259427386472E-001
  153. };
  154. /* erfc(x) = exp(-x^2) 1/x P(1/x^2), 2 < x < 14 */
  155. static float R[] = {
  156. -1.047766399936249E+001,
  157. 1.297719955372516E+001,
  158. -7.495518717768503E+000,
  159. 2.921019019210786E+000,
  160. -1.015265279202700E+000,
  161. 4.218463358204948E-001,
  162. -2.820767439740514E-001,
  163. 5.641895067754075E-001
  164. };
  165. /* erf(x) = x P(x^2), 0 < x < 1 */
  166. static float T[] = {
  167. 7.853861353153693E-005,
  168. -8.010193625184903E-004,
  169. 5.188327685732524E-003,
  170. -2.685381193529856E-002,
  171. 1.128358514861418E-001,
  172. -3.761262582423300E-001,
  173. 1.128379165726710E+000
  174. };
  175. /*#define UTHRESH 37.519379347*/
  176. #define UTHRESH 14.0
  177. #define fabsf(x) ( (x) < 0 ? -(x) : (x) )
  178. #ifdef ANSIC
  179. float polevlf(float, float *, int);
  180. float expf(float), logf(float), erff(float), erfcf(float);
  181. #else
  182. float polevlf(), expf(), logf(), erff(), erfcf();
  183. #endif
  184. float ndtrf(float aa)
  185. {
  186. float x, y, z;
  187. x = aa;
  188. x *= SQRTHF;
  189. z = fabsf(x);
  190. if( z < SQRTHF )
  191. y = 0.5 + 0.5 * erff(x);
  192. else
  193. {
  194. y = 0.5 * erfcf(z);
  195. if( x > 0 )
  196. y = 1.0 - y;
  197. }
  198. return(y);
  199. }
  200. float erfcf(float aa)
  201. {
  202. float a, p,q,x,y,z;
  203. a = aa;
  204. x = fabsf(a);
  205. if( x < 1.0 )
  206. return( 1.0 - erff(a) );
  207. z = -a * a;
  208. if( z < -MAXLOGF )
  209. {
  210. under:
  211. mtherr( "erfcf", UNDERFLOW );
  212. if( a < 0 )
  213. return( 2.0 );
  214. else
  215. return( 0.0 );
  216. }
  217. z = expf(z);
  218. q = 1.0/x;
  219. y = q * q;
  220. if( x < 2.0 )
  221. {
  222. p = polevlf( y, P, 8 );
  223. }
  224. else
  225. {
  226. p = polevlf( y, R, 7 );
  227. }
  228. y = z * q * p;
  229. if( a < 0 )
  230. y = 2.0 - y;
  231. if( y == 0.0 )
  232. goto under;
  233. return(y);
  234. }
  235. float erff(float xx)
  236. {
  237. float x, y, z;
  238. x = xx;
  239. if( fabsf(x) > 1.0 )
  240. return( 1.0 - erfcf(x) );
  241. z = x * x;
  242. y = x * polevlf( z, T, 6 );
  243. return( y );
  244. }