ndtrif.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* ndtrif.c
  2. *
  3. * Inverse of Normal distribution function
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, y, ndtrif();
  10. *
  11. * x = ndtrif( y );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns the argument, x, for which the area under the
  18. * Gaussian probability density function (integrated from
  19. * minus infinity to x) is equal to y.
  20. *
  21. *
  22. * For small arguments 0 < y < exp(-2), the program computes
  23. * z = sqrt( -2.0 * log(y) ); then the approximation is
  24. * x = z - log(z)/z - (1/z) P(1/z) / Q(1/z).
  25. * There are two rational functions P/Q, one for 0 < y < exp(-32)
  26. * and the other for y up to exp(-2). For larger arguments,
  27. * w = y - 0.5, and x/sqrt(2pi) = w + w**3 R(w**2)/S(w**2)).
  28. *
  29. *
  30. * ACCURACY:
  31. *
  32. * Relative error:
  33. * arithmetic domain # trials peak rms
  34. * IEEE 1e-38, 1 30000 3.6e-7 5.0e-8
  35. *
  36. *
  37. * ERROR MESSAGES:
  38. *
  39. * message condition value returned
  40. * ndtrif domain x <= 0 -MAXNUM
  41. * ndtrif domain x >= 1 MAXNUM
  42. *
  43. */
  44. /*
  45. Cephes Math Library Release 2.2: July, 1992
  46. Copyright 1984, 1987, 1989, 1992 by Stephen L. Moshier
  47. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  48. */
  49. #include <math.h>
  50. extern float MAXNUMF;
  51. /* sqrt(2pi) */
  52. static float s2pi = 2.50662827463100050242;
  53. /* approximation for 0 <= |y - 0.5| <= 3/8 */
  54. static float P0[5] = {
  55. -5.99633501014107895267E1,
  56. 9.80010754185999661536E1,
  57. -5.66762857469070293439E1,
  58. 1.39312609387279679503E1,
  59. -1.23916583867381258016E0,
  60. };
  61. static float Q0[8] = {
  62. /* 1.00000000000000000000E0,*/
  63. 1.95448858338141759834E0,
  64. 4.67627912898881538453E0,
  65. 8.63602421390890590575E1,
  66. -2.25462687854119370527E2,
  67. 2.00260212380060660359E2,
  68. -8.20372256168333339912E1,
  69. 1.59056225126211695515E1,
  70. -1.18331621121330003142E0,
  71. };
  72. /* Approximation for interval z = sqrt(-2 log y ) between 2 and 8
  73. * i.e., y between exp(-2) = .135 and exp(-32) = 1.27e-14.
  74. */
  75. static float P1[9] = {
  76. 4.05544892305962419923E0,
  77. 3.15251094599893866154E1,
  78. 5.71628192246421288162E1,
  79. 4.40805073893200834700E1,
  80. 1.46849561928858024014E1,
  81. 2.18663306850790267539E0,
  82. -1.40256079171354495875E-1,
  83. -3.50424626827848203418E-2,
  84. -8.57456785154685413611E-4,
  85. };
  86. static float Q1[8] = {
  87. /* 1.00000000000000000000E0,*/
  88. 1.57799883256466749731E1,
  89. 4.53907635128879210584E1,
  90. 4.13172038254672030440E1,
  91. 1.50425385692907503408E1,
  92. 2.50464946208309415979E0,
  93. -1.42182922854787788574E-1,
  94. -3.80806407691578277194E-2,
  95. -9.33259480895457427372E-4,
  96. };
  97. /* Approximation for interval z = sqrt(-2 log y ) between 8 and 64
  98. * i.e., y between exp(-32) = 1.27e-14 and exp(-2048) = 3.67e-890.
  99. */
  100. static float P2[9] = {
  101. 3.23774891776946035970E0,
  102. 6.91522889068984211695E0,
  103. 3.93881025292474443415E0,
  104. 1.33303460815807542389E0,
  105. 2.01485389549179081538E-1,
  106. 1.23716634817820021358E-2,
  107. 3.01581553508235416007E-4,
  108. 2.65806974686737550832E-6,
  109. 6.23974539184983293730E-9,
  110. };
  111. static float Q2[8] = {
  112. /* 1.00000000000000000000E0,*/
  113. 6.02427039364742014255E0,
  114. 3.67983563856160859403E0,
  115. 1.37702099489081330271E0,
  116. 2.16236993594496635890E-1,
  117. 1.34204006088543189037E-2,
  118. 3.28014464682127739104E-4,
  119. 2.89247864745380683936E-6,
  120. 6.79019408009981274425E-9,
  121. };
  122. #ifdef ANSIC
  123. float polevlf(float, float *, int);
  124. float p1evlf(float, float *, int);
  125. float logf(float), sqrtf(float);
  126. #else
  127. float polevlf(), p1evlf(), logf(), sqrtf();
  128. #endif
  129. float ndtrif(float yy0)
  130. {
  131. float y0, x, y, z, y2, x0, x1;
  132. int code;
  133. y0 = yy0;
  134. if( y0 <= 0.0 )
  135. {
  136. mtherr( "ndtrif", DOMAIN );
  137. return( -MAXNUMF );
  138. }
  139. if( y0 >= 1.0 )
  140. {
  141. mtherr( "ndtrif", DOMAIN );
  142. return( MAXNUMF );
  143. }
  144. code = 1;
  145. y = y0;
  146. if( y > (1.0 - 0.13533528323661269189) ) /* 0.135... = exp(-2) */
  147. {
  148. y = 1.0 - y;
  149. code = 0;
  150. }
  151. if( y > 0.13533528323661269189 )
  152. {
  153. y = y - 0.5;
  154. y2 = y * y;
  155. x = y + y * (y2 * polevlf( y2, P0, 4)/p1evlf( y2, Q0, 8 ));
  156. x = x * s2pi;
  157. return(x);
  158. }
  159. x = sqrtf( -2.0 * logf(y) );
  160. x0 = x - logf(x)/x;
  161. z = 1.0/x;
  162. if( x < 8.0 ) /* y > exp(-32) = 1.2664165549e-14 */
  163. x1 = z * polevlf( z, P1, 8 )/p1evlf( z, Q1, 8 );
  164. else
  165. x1 = z * polevlf( z, P2, 8 )/p1evlf( z, Q2, 8 );
  166. x = x0 - x1;
  167. if( code != 0 )
  168. x = -x;
  169. return( x );
  170. }