incbi.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 "mconf.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) double aa, bb, yy0;
  58. {
  59. double a, b, y0, d, y, x, x0, x1, lgm, yp, di, dithresh, yl, yh, xt;
  60. int i, rflg, dir, nflg;
  61. i = 0;
  62. if (yy0 <= 0)
  63. return (0.0);
  64. if (yy0 >= 1.0)
  65. return (1.0);
  66. x0 = 0.0;
  67. yl = 0.0;
  68. x1 = 1.0;
  69. yh = 1.0;
  70. nflg = 0;
  71. if (aa <= 1.0 || bb <= 1.0) {
  72. dithresh = 1.0e-6;
  73. rflg = 0;
  74. a = aa;
  75. b = bb;
  76. y0 = yy0;
  77. x = a / (a + b);
  78. y = incbet(a, b, x);
  79. goto ihalve;
  80. } else {
  81. dithresh = 1.0e-4;
  82. }
  83. /* approximation to inverse function */
  84. yp = -ndtri(yy0);
  85. if (yy0 > 0.5) {
  86. rflg = 1;
  87. a = bb;
  88. b = aa;
  89. y0 = 1.0 - yy0;
  90. yp = -yp;
  91. } else {
  92. rflg = 0;
  93. a = aa;
  94. b = bb;
  95. y0 = yy0;
  96. }
  97. lgm = (yp * yp - 3.0) / 6.0;
  98. x = 2.0 / (1.0 / (2.0 * a - 1.0) + 1.0 / (2.0 * b - 1.0));
  99. d = yp * sqrt(x + lgm) / x - (1.0 / (2.0 * b - 1.0) - 1.0 / (2.0 * a - 1.0)) *
  100. (lgm + 5.0 / 6.0 - 2.0 / (3.0 * x));
  101. d = 2.0 * d;
  102. if (d < MINLOG) {
  103. x = 1.0;
  104. goto under;
  105. }
  106. x = a / (a + b * exp(d));
  107. y = incbet(a, b, x);
  108. yp = (y - y0) / y0;
  109. if (fabs(yp) < 0.2)
  110. goto newt;
  111. /* Resort to interval halving if not close enough. */
  112. ihalve:
  113. dir = 0;
  114. di = 0.5;
  115. for (i = 0; i < 100; i++) {
  116. if (i != 0) {
  117. x = x0 + di * (x1 - x0);
  118. if (x == 1.0)
  119. x = 1.0 - MACHEP;
  120. if (x == 0.0) {
  121. di = 0.5;
  122. x = x0 + di * (x1 - x0);
  123. if (x == 0.0)
  124. goto under;
  125. }
  126. y = incbet(a, b, x);
  127. yp = (x1 - x0) / (x1 + x0);
  128. if (fabs(yp) < dithresh)
  129. goto newt;
  130. yp = (y - y0) / y0;
  131. if (fabs(yp) < dithresh)
  132. goto newt;
  133. }
  134. if (y < y0) {
  135. x0 = x;
  136. yl = y;
  137. if (dir < 0) {
  138. dir = 0;
  139. di = 0.5;
  140. } else if (dir > 3)
  141. di = 1.0 - (1.0 - di) * (1.0 - di);
  142. else if (dir > 1)
  143. di = 0.5 * di + 0.5;
  144. else
  145. di = (y0 - y) / (yh - yl);
  146. dir += 1;
  147. if (x0 > 0.75) {
  148. if (rflg == 1) {
  149. rflg = 0;
  150. a = aa;
  151. b = bb;
  152. y0 = yy0;
  153. } else {
  154. rflg = 1;
  155. a = bb;
  156. b = aa;
  157. y0 = 1.0 - yy0;
  158. }
  159. x = 1.0 - x;
  160. y = incbet(a, b, x);
  161. x0 = 0.0;
  162. yl = 0.0;
  163. x1 = 1.0;
  164. yh = 1.0;
  165. goto ihalve;
  166. }
  167. } else {
  168. x1 = x;
  169. if (rflg == 1 && x1 < MACHEP) {
  170. x = 0.0;
  171. goto done;
  172. }
  173. yh = y;
  174. if (dir > 0) {
  175. dir = 0;
  176. di = 0.5;
  177. } else if (dir < -3)
  178. di = di * di;
  179. else if (dir < -1)
  180. di = 0.5 * di;
  181. else
  182. di = (y - y0) / (yh - yl);
  183. dir -= 1;
  184. }
  185. }
  186. mtherr("incbi", PLOSS);
  187. if (x0 >= 1.0) {
  188. x = 1.0 - MACHEP;
  189. goto done;
  190. }
  191. if (x <= 0.0) {
  192. under:
  193. mtherr("incbi", UNDERFLOW);
  194. x = 0.0;
  195. goto done;
  196. }
  197. newt:
  198. if (nflg)
  199. goto done;
  200. nflg = 1;
  201. lgm = lgam(a + b) - lgam(a) - lgam(b);
  202. for (i = 0; i < 8; i++) {
  203. /* Compute the function at this point. */
  204. if (i != 0)
  205. y = incbet(a, b, x);
  206. if (y < yl) {
  207. x = x0;
  208. y = yl;
  209. } else if (y > yh) {
  210. x = x1;
  211. y = yh;
  212. } else if (y < y0) {
  213. x0 = x;
  214. yl = y;
  215. } else {
  216. x1 = x;
  217. yh = y;
  218. }
  219. if (x == 1.0 || x == 0.0)
  220. break;
  221. /* Compute the derivative of the function at this point. */
  222. d = (a - 1.0) * log(x) + (b - 1.0) * log(1.0 - x) + lgm;
  223. if (d < MINLOG)
  224. goto done;
  225. if (d > MAXLOG)
  226. break;
  227. d = exp(d);
  228. /* Compute the step to the next approximation of x. */
  229. d = (y - y0) / d;
  230. xt = x - d;
  231. if (xt <= x0) {
  232. y = (x - x0) / (x1 - x0);
  233. xt = x0 + 0.5 * y * (x - x0);
  234. if (xt <= 0.0)
  235. break;
  236. }
  237. if (xt >= x1) {
  238. y = (x1 - x) / (x1 - x0);
  239. xt = x1 - 0.5 * y * (x1 - x);
  240. if (xt >= 1.0)
  241. break;
  242. }
  243. x = xt;
  244. if (fabs(d / x) < 128.0 * MACHEP)
  245. goto done;
  246. }
  247. /* Did not converge. */
  248. dithresh = 256.0 * MACHEP;
  249. goto ihalve;
  250. done:
  251. if (rflg) {
  252. if (x <= MACHEP)
  253. x = 1.0 - MACHEP;
  254. else
  255. x = 1.0 - x;
  256. }
  257. return (x);
  258. }