incbi.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* incbi()
  2. *
  3. * Inverse of imcomplete beta integral
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double a, b, x, y, incbi();
  10. *
  11. * x = incbi( a, b, y );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Given y, the function finds x such that
  18. *
  19. * incbet( a, b, x ) = y .
  20. *
  21. * The routine performs interval halving or Newton iterations to find the
  22. * root of incbet(a,b,x) - y = 0.
  23. *
  24. *
  25. * ACCURACY:
  26. *
  27. * Relative error:
  28. * x a,b
  29. * arithmetic domain domain # trials peak rms
  30. * IEEE 0,1 .5,10000 50000 5.8e-12 1.3e-13
  31. * IEEE 0,1 .25,100 100000 1.8e-13 3.9e-15
  32. * IEEE 0,1 0,5 50000 1.1e-12 5.5e-15
  33. * VAX 0,1 .5,100 25000 3.5e-14 1.1e-15
  34. * With a and b constrained to half-integer or integer values:
  35. * IEEE 0,1 .5,10000 50000 5.8e-12 1.1e-13
  36. * IEEE 0,1 .5,100 100000 1.7e-14 7.9e-16
  37. * With a = .5, b constrained to half-integer or integer values:
  38. * IEEE 0,1 .5,10000 10000 8.3e-11 1.0e-11
  39. */
  40. /*
  41. Cephes Math Library Release 2.8: June, 2000
  42. Copyright 1984, 1996, 2000 by Stephen L. Moshier
  43. */
  44. #include <math.h>
  45. extern double MACHEP, MAXNUM, MAXLOG, MINLOG;
  46. #ifdef ANSIPROT
  47. extern double ndtri ( double );
  48. extern double exp ( double );
  49. extern double fabs ( double );
  50. extern double log ( double );
  51. extern double sqrt ( double );
  52. extern double lgam ( double );
  53. extern double incbet ( double, double, double );
  54. #else
  55. double ndtri(), exp(), fabs(), log(), sqrt(), lgam(), incbet();
  56. #endif
  57. double incbi( aa, bb, yy0 )
  58. double aa, bb, yy0;
  59. {
  60. double a, b, y0, d, y, x, x0, x1, lgm, yp, di, dithresh, yl, yh, xt;
  61. int i, rflg, dir, nflg;
  62. i = 0;
  63. if( yy0 <= 0 )
  64. return(0.0);
  65. if( yy0 >= 1.0 )
  66. return(1.0);
  67. x0 = 0.0;
  68. yl = 0.0;
  69. x1 = 1.0;
  70. yh = 1.0;
  71. nflg = 0;
  72. if( aa <= 1.0 || bb <= 1.0 )
  73. {
  74. dithresh = 1.0e-6;
  75. rflg = 0;
  76. a = aa;
  77. b = bb;
  78. y0 = yy0;
  79. x = a/(a+b);
  80. y = incbet( a, b, x );
  81. goto ihalve;
  82. }
  83. else
  84. {
  85. dithresh = 1.0e-4;
  86. }
  87. /* approximation to inverse function */
  88. yp = -ndtri(yy0);
  89. if( yy0 > 0.5 )
  90. {
  91. rflg = 1;
  92. a = bb;
  93. b = aa;
  94. y0 = 1.0 - yy0;
  95. yp = -yp;
  96. }
  97. else
  98. {
  99. rflg = 0;
  100. a = aa;
  101. b = bb;
  102. y0 = yy0;
  103. }
  104. lgm = (yp * yp - 3.0)/6.0;
  105. x = 2.0/( 1.0/(2.0*a-1.0) + 1.0/(2.0*b-1.0) );
  106. d = yp * sqrt( x + lgm ) / x
  107. - ( 1.0/(2.0*b-1.0) - 1.0/(2.0*a-1.0) )
  108. * (lgm + 5.0/6.0 - 2.0/(3.0*x));
  109. d = 2.0 * d;
  110. if( d < MINLOG )
  111. {
  112. x = 1.0;
  113. goto under;
  114. }
  115. x = a/( a + b * exp(d) );
  116. y = incbet( a, b, x );
  117. yp = (y - y0)/y0;
  118. if( fabs(yp) < 0.2 )
  119. goto newt;
  120. /* Resort to interval halving if not close enough. */
  121. ihalve:
  122. dir = 0;
  123. di = 0.5;
  124. for( i=0; i<100; i++ )
  125. {
  126. if( i != 0 )
  127. {
  128. x = x0 + di * (x1 - x0);
  129. if( x == 1.0 )
  130. x = 1.0 - MACHEP;
  131. if( x == 0.0 )
  132. {
  133. di = 0.5;
  134. x = x0 + di * (x1 - x0);
  135. if( x == 0.0 )
  136. goto under;
  137. }
  138. y = incbet( a, b, x );
  139. yp = (x1 - x0)/(x1 + x0);
  140. if( fabs(yp) < dithresh )
  141. goto newt;
  142. yp = (y-y0)/y0;
  143. if( fabs(yp) < dithresh )
  144. goto newt;
  145. }
  146. if( y < y0 )
  147. {
  148. x0 = x;
  149. yl = y;
  150. if( dir < 0 )
  151. {
  152. dir = 0;
  153. di = 0.5;
  154. }
  155. else if( dir > 3 )
  156. di = 1.0 - (1.0 - di) * (1.0 - di);
  157. else if( dir > 1 )
  158. di = 0.5 * di + 0.5;
  159. else
  160. di = (y0 - y)/(yh - yl);
  161. dir += 1;
  162. if( x0 > 0.75 )
  163. {
  164. if( rflg == 1 )
  165. {
  166. rflg = 0;
  167. a = aa;
  168. b = bb;
  169. y0 = yy0;
  170. }
  171. else
  172. {
  173. rflg = 1;
  174. a = bb;
  175. b = aa;
  176. y0 = 1.0 - yy0;
  177. }
  178. x = 1.0 - x;
  179. y = incbet( a, b, x );
  180. x0 = 0.0;
  181. yl = 0.0;
  182. x1 = 1.0;
  183. yh = 1.0;
  184. goto ihalve;
  185. }
  186. }
  187. else
  188. {
  189. x1 = x;
  190. if( rflg == 1 && x1 < MACHEP )
  191. {
  192. x = 0.0;
  193. goto done;
  194. }
  195. yh = y;
  196. if( dir > 0 )
  197. {
  198. dir = 0;
  199. di = 0.5;
  200. }
  201. else if( dir < -3 )
  202. di = di * di;
  203. else if( dir < -1 )
  204. di = 0.5 * di;
  205. else
  206. di = (y - y0)/(yh - yl);
  207. dir -= 1;
  208. }
  209. }
  210. mtherr( "incbi", PLOSS );
  211. if( x0 >= 1.0 )
  212. {
  213. x = 1.0 - MACHEP;
  214. goto done;
  215. }
  216. if( x <= 0.0 )
  217. {
  218. under:
  219. mtherr( "incbi", UNDERFLOW );
  220. x = 0.0;
  221. goto done;
  222. }
  223. newt:
  224. if( nflg )
  225. goto done;
  226. nflg = 1;
  227. lgm = lgam(a+b) - lgam(a) - lgam(b);
  228. for( i=0; i<8; i++ )
  229. {
  230. /* Compute the function at this point. */
  231. if( i != 0 )
  232. y = incbet(a,b,x);
  233. if( y < yl )
  234. {
  235. x = x0;
  236. y = yl;
  237. }
  238. else if( y > yh )
  239. {
  240. x = x1;
  241. y = yh;
  242. }
  243. else if( y < y0 )
  244. {
  245. x0 = x;
  246. yl = y;
  247. }
  248. else
  249. {
  250. x1 = x;
  251. yh = y;
  252. }
  253. if( x == 1.0 || x == 0.0 )
  254. break;
  255. /* Compute the derivative of the function at this point. */
  256. d = (a - 1.0) * log(x) + (b - 1.0) * log(1.0-x) + lgm;
  257. if( d < MINLOG )
  258. goto done;
  259. if( d > MAXLOG )
  260. break;
  261. d = exp(d);
  262. /* Compute the step to the next approximation of x. */
  263. d = (y - y0)/d;
  264. xt = x - d;
  265. if( xt <= x0 )
  266. {
  267. y = (x - x0) / (x1 - x0);
  268. xt = x0 + 0.5 * y * (x - x0);
  269. if( xt <= 0.0 )
  270. break;
  271. }
  272. if( xt >= x1 )
  273. {
  274. y = (x1 - x) / (x1 - x0);
  275. xt = x1 - 0.5 * y * (x1 - x);
  276. if( xt >= 1.0 )
  277. break;
  278. }
  279. x = xt;
  280. if( fabs(d/x) < 128.0 * MACHEP )
  281. goto done;
  282. }
  283. /* Did not converge. */
  284. dithresh = 256.0 * MACHEP;
  285. goto ihalve;
  286. done:
  287. if( rflg )
  288. {
  289. if( x <= MACHEP )
  290. x = 1.0 - MACHEP;
  291. else
  292. x = 1.0 - x;
  293. }
  294. return( x );
  295. }