incbil.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* incbil()
  2. *
  3. * Inverse of imcomplete beta integral
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * long double a, b, x, y, incbil();
  10. *
  11. * x = incbil( 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 up to 10 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 10000 1.1e-14 1.4e-16
  31. */
  32. /*
  33. Cephes Math Library Release 2.3: March, 1995
  34. Copyright 1984, 1995 by Stephen L. Moshier
  35. */
  36. #include <math.h>
  37. extern long double MACHEPL, MAXNUML, MAXLOGL, MINLOGL;
  38. #ifdef ANSIPROT
  39. extern long double incbetl ( long double, long double, long double );
  40. extern long double expl ( long double );
  41. extern long double fabsl ( long double );
  42. extern long double logl ( long double );
  43. extern long double sqrtl ( long double );
  44. extern long double lgaml ( long double );
  45. extern long double ndtril ( long double );
  46. #else
  47. long double incbetl(), expl(), fabsl(), logl(), sqrtl(), lgaml();
  48. long double ndtril();
  49. #endif
  50. long double incbil( aa, bb, yy0 )
  51. long double aa, bb, yy0;
  52. {
  53. long double a, b, y0, d, y, x, x0, x1, lgm, yp, di, dithresh, yl, yh, xt;
  54. int i, rflg, dir, nflg;
  55. if( yy0 <= 0.0L )
  56. return(0.0L);
  57. if( yy0 >= 1.0L )
  58. return(1.0L);
  59. x0 = 0.0L;
  60. yl = 0.0L;
  61. x1 = 1.0L;
  62. yh = 1.0L;
  63. if( aa <= 1.0L || bb <= 1.0L )
  64. {
  65. dithresh = 1.0e-7L;
  66. rflg = 0;
  67. a = aa;
  68. b = bb;
  69. y0 = yy0;
  70. x = a/(a+b);
  71. y = incbetl( a, b, x );
  72. nflg = 0;
  73. goto ihalve;
  74. }
  75. else
  76. {
  77. nflg = 0;
  78. dithresh = 1.0e-4L;
  79. }
  80. /* approximation to inverse function */
  81. yp = -ndtril( yy0 );
  82. if( yy0 > 0.5L )
  83. {
  84. rflg = 1;
  85. a = bb;
  86. b = aa;
  87. y0 = 1.0L - yy0;
  88. yp = -yp;
  89. }
  90. else
  91. {
  92. rflg = 0;
  93. a = aa;
  94. b = bb;
  95. y0 = yy0;
  96. }
  97. lgm = (yp * yp - 3.0L)/6.0L;
  98. x = 2.0L/( 1.0L/(2.0L * a-1.0L) + 1.0L/(2.0L * b - 1.0L) );
  99. d = yp * sqrtl( x + lgm ) / x
  100. - ( 1.0L/(2.0L * b - 1.0L) - 1.0L/(2.0L * a - 1.0L) )
  101. * (lgm + (5.0L/6.0L) - 2.0L/(3.0L * x));
  102. d = 2.0L * d;
  103. if( d < MINLOGL )
  104. {
  105. x = 1.0L;
  106. goto under;
  107. }
  108. x = a/( a + b * expl(d) );
  109. y = incbetl( a, b, x );
  110. yp = (y - y0)/y0;
  111. if( fabsl(yp) < 0.2 )
  112. goto newt;
  113. /* Resort to interval halving if not close enough. */
  114. ihalve:
  115. dir = 0;
  116. di = 0.5L;
  117. for( i=0; i<400; i++ )
  118. {
  119. if( i != 0 )
  120. {
  121. x = x0 + di * (x1 - x0);
  122. if( x == 1.0L )
  123. x = 1.0L - MACHEPL;
  124. if( x == 0.0L )
  125. {
  126. di = 0.5;
  127. x = x0 + di * (x1 - x0);
  128. if( x == 0.0 )
  129. goto under;
  130. }
  131. y = incbetl( a, b, x );
  132. yp = (x1 - x0)/(x1 + x0);
  133. if( fabsl(yp) < dithresh )
  134. goto newt;
  135. yp = (y-y0)/y0;
  136. if( fabsl(yp) < dithresh )
  137. goto newt;
  138. }
  139. if( y < y0 )
  140. {
  141. x0 = x;
  142. yl = y;
  143. if( dir < 0 )
  144. {
  145. dir = 0;
  146. di = 0.5L;
  147. }
  148. else if( dir > 3 )
  149. di = 1.0L - (1.0L - di) * (1.0L - di);
  150. else if( dir > 1 )
  151. di = 0.5L * di + 0.5L;
  152. else
  153. di = (y0 - y)/(yh - yl);
  154. dir += 1;
  155. if( x0 > 0.95L )
  156. {
  157. if( rflg == 1 )
  158. {
  159. rflg = 0;
  160. a = aa;
  161. b = bb;
  162. y0 = yy0;
  163. }
  164. else
  165. {
  166. rflg = 1;
  167. a = bb;
  168. b = aa;
  169. y0 = 1.0 - yy0;
  170. }
  171. x = 1.0L - x;
  172. y = incbetl( a, b, x );
  173. x0 = 0.0;
  174. yl = 0.0;
  175. x1 = 1.0;
  176. yh = 1.0;
  177. goto ihalve;
  178. }
  179. }
  180. else
  181. {
  182. x1 = x;
  183. if( rflg == 1 && x1 < MACHEPL )
  184. {
  185. x = 0.0L;
  186. goto done;
  187. }
  188. yh = y;
  189. if( dir > 0 )
  190. {
  191. dir = 0;
  192. di = 0.5L;
  193. }
  194. else if( dir < -3 )
  195. di = di * di;
  196. else if( dir < -1 )
  197. di = 0.5L * di;
  198. else
  199. di = (y - y0)/(yh - yl);
  200. dir -= 1;
  201. }
  202. }
  203. mtherr( "incbil", PLOSS );
  204. if( x0 >= 1.0L )
  205. {
  206. x = 1.0L - MACHEPL;
  207. goto done;
  208. }
  209. if( x <= 0.0L )
  210. {
  211. under:
  212. mtherr( "incbil", UNDERFLOW );
  213. x = 0.0L;
  214. goto done;
  215. }
  216. newt:
  217. if( nflg )
  218. goto done;
  219. nflg = 1;
  220. lgm = lgaml(a+b) - lgaml(a) - lgaml(b);
  221. for( i=0; i<15; i++ )
  222. {
  223. /* Compute the function at this point. */
  224. if( i != 0 )
  225. y = incbetl(a,b,x);
  226. if( y < yl )
  227. {
  228. x = x0;
  229. y = yl;
  230. }
  231. else if( y > yh )
  232. {
  233. x = x1;
  234. y = yh;
  235. }
  236. else if( y < y0 )
  237. {
  238. x0 = x;
  239. yl = y;
  240. }
  241. else
  242. {
  243. x1 = x;
  244. yh = y;
  245. }
  246. if( x == 1.0L || x == 0.0L )
  247. break;
  248. /* Compute the derivative of the function at this point. */
  249. d = (a - 1.0L) * logl(x) + (b - 1.0L) * logl(1.0L - x) + lgm;
  250. if( d < MINLOGL )
  251. goto done;
  252. if( d > MAXLOGL )
  253. break;
  254. d = expl(d);
  255. /* Compute the step to the next approximation of x. */
  256. d = (y - y0)/d;
  257. xt = x - d;
  258. if( xt <= x0 )
  259. {
  260. y = (x - x0) / (x1 - x0);
  261. xt = x0 + 0.5L * y * (x - x0);
  262. if( xt <= 0.0L )
  263. break;
  264. }
  265. if( xt >= x1 )
  266. {
  267. y = (x1 - x) / (x1 - x0);
  268. xt = x1 - 0.5L * y * (x1 - x);
  269. if( xt >= 1.0L )
  270. break;
  271. }
  272. x = xt;
  273. if( fabsl(d/x) < (128.0L * MACHEPL) )
  274. goto done;
  275. }
  276. /* Did not converge. */
  277. dithresh = 256.0L * MACHEPL;
  278. goto ihalve;
  279. done:
  280. if( rflg )
  281. {
  282. if( x <= MACHEPL )
  283. x = 1.0L - MACHEPL;
  284. else
  285. x = 1.0L - x;
  286. }
  287. return( x );
  288. }