3 * Inverse of imcomplete beta integral
9 * double a, b, x, y, incbi();
11 * x = incbi( a, b, y );
17 * Given y, the function finds x such that
19 * incbet( a, b, x ) = y .
21 * The routine performs interval halving or Newton iterations to find the
22 * root of incbet(a,b,x) - y = 0.
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
43 Cephes Math Library Release 2.8: June, 2000
44 Copyright 1984, 1996, 2000 by Stephen L. Moshier
49 extern double MACHEP, MAXNUM, MAXLOG, MINLOG;
51 extern double ndtri ( double );
52 extern double exp ( double );
53 extern double fabs ( double );
54 extern double log ( double );
55 extern double sqrt ( double );
56 extern double lgam ( double );
57 extern double incbet ( double, double, double );
59 double ndtri(), exp(), fabs(), log(), sqrt(), lgam(), incbet();
62 double incbi( aa, bb, yy0 )
65 double a, b, y0, d, y, x, x0, x1, lgm, yp, di, dithresh, yl, yh, xt;
66 int i, rflg, dir, nflg;
80 if( aa <= 1.0 || bb <= 1.0 )
88 y = incbet( a, b, x );
95 /* approximation to inverse function */
115 lgm = (yp * yp - 3.0)/6.0;
116 x = 2.0/( 1.0/(2.0*a-1.0) + 1.0/(2.0*b-1.0) );
117 d = yp * sqrt( x + lgm ) / x
118 - ( 1.0/(2.0*b-1.0) - 1.0/(2.0*a-1.0) )
119 * (lgm + 5.0/6.0 - 2.0/(3.0*x));
126 x = a/( a + b * exp(d) );
127 y = incbet( a, b, x );
132 /* Resort to interval halving if not close enough. */
137 for( i=0; i<100; i++ )
141 x = x0 + di * (x1 - x0);
147 x = x0 + di * (x1 - x0);
151 y = incbet( a, b, x );
152 yp = (x1 - x0)/(x1 + x0);
153 if( fabs(yp) < dithresh )
156 if( fabs(yp) < dithresh )
169 di = 1.0 - (1.0 - di) * (1.0 - di);
173 di = (y0 - y)/(yh - yl);
192 y = incbet( a, b, x );
203 if( rflg == 1 && x1 < MACHEP )
219 di = (y - y0)/(yh - yl);
223 mtherr( "incbi", PLOSS );
232 mtherr( "incbi", UNDERFLOW );
242 lgm = lgam(a+b) - lgam(a) - lgam(b);
246 /* Compute the function at this point. */
269 if( x == 1.0 || x == 0.0 )
271 /* Compute the derivative of the function at this point. */
272 d = (a - 1.0) * log(x) + (b - 1.0) * log(1.0-x) + lgm;
278 /* Compute the step to the next approximation of x. */
283 y = (x - x0) / (x1 - x0);
284 xt = x0 + 0.5 * y * (x - x0);
290 y = (x1 - x) / (x1 - x0);
291 xt = x1 - 0.5 * y * (x1 - x);
296 if( fabs(d/x) < 128.0 * MACHEP )
299 /* Did not converge. */
300 dithresh = 256.0 * MACHEP;