1 | /* incbet.c |
---|
2 | * |
---|
3 | * Incomplete beta integral |
---|
4 | * |
---|
5 | * |
---|
6 | * SYNOPSIS: |
---|
7 | * |
---|
8 | * double a, b, x, y, incbet(); |
---|
9 | * |
---|
10 | * y = incbet( a, b, x ); |
---|
11 | * |
---|
12 | * |
---|
13 | * DESCRIPTION: |
---|
14 | * |
---|
15 | * Returns incomplete beta integral of the arguments, evaluated |
---|
16 | * from zero to x. The function is defined as |
---|
17 | * |
---|
18 | * x |
---|
19 | * - - |
---|
20 | * | (a+b) | | a-1 b-1 |
---|
21 | * ----------- | t (1-t) dt. |
---|
22 | * - - | | |
---|
23 | * | (a) | (b) - |
---|
24 | * 0 |
---|
25 | * |
---|
26 | * The domain of definition is 0 <= x <= 1. In this |
---|
27 | * implementation a and b are restricted to positive values. |
---|
28 | * The integral from x to 1 may be obtained by the symmetry |
---|
29 | * relation |
---|
30 | * |
---|
31 | * 1 - incbet( a, b, x ) = incbet( b, a, 1-x ). |
---|
32 | * |
---|
33 | * The integral is evaluated by a continued fraction expansion |
---|
34 | * or, when b*x is small, by a power series. |
---|
35 | * |
---|
36 | * ACCURACY: |
---|
37 | * |
---|
38 | * Tested at uniformly distributed random points (a,b,x) with a and b |
---|
39 | * in "domain" and x between 0 and 1. |
---|
40 | * Relative error |
---|
41 | * arithmetic domain # trials peak rms |
---|
42 | * IEEE 0,5 10000 6.9e-15 4.5e-16 |
---|
43 | * IEEE 0,85 250000 2.2e-13 1.7e-14 |
---|
44 | * IEEE 0,1000 30000 5.3e-12 6.3e-13 |
---|
45 | * IEEE 0,10000 250000 9.3e-11 7.1e-12 |
---|
46 | * IEEE 0,100000 10000 8.7e-10 4.8e-11 |
---|
47 | * Outputs smaller than the IEEE gradual underflow threshold |
---|
48 | * were excluded from these statistics. |
---|
49 | * |
---|
50 | * ERROR MESSAGES: |
---|
51 | * message condition value returned |
---|
52 | * incbet domain x<0, x>1 0.0 |
---|
53 | * incbet underflow 0.0 |
---|
54 | */ |
---|
55 | |
---|
56 | |
---|
57 | /* |
---|
58 | Cephes Math Library, Release 2.8: June, 2000 |
---|
59 | Copyright 1984, 1995, 2000 by Stephen L. Moshier |
---|
60 | */ |
---|
61 | |
---|
62 | #include "mconf.h" |
---|
63 | |
---|
64 | #ifdef DEC |
---|
65 | #define MAXGAM 34.84425627277176174 |
---|
66 | #else |
---|
67 | #define MAXGAM 171.624376956302725 |
---|
68 | #endif |
---|
69 | |
---|
70 | extern double MACHEP, MINLOG, MAXLOG; |
---|
71 | #ifdef ANSIPROT |
---|
72 | extern double gamma ( double ); |
---|
73 | extern double lgam ( double ); |
---|
74 | extern double exp ( double ); |
---|
75 | extern double log ( double ); |
---|
76 | extern double pow ( double, double ); |
---|
77 | extern double fabs ( double ); |
---|
78 | static double incbcf(double, double, double); |
---|
79 | static double incbd(double, double, double); |
---|
80 | static double pseries(double, double, double); |
---|
81 | #else |
---|
82 | double gamma(), lgam(), exp(), log(), pow(), fabs(); |
---|
83 | static double incbcf(), incbd(), pseries(); |
---|
84 | #endif |
---|
85 | |
---|
86 | static double big = 4.503599627370496e15; |
---|
87 | static double biginv = 2.22044604925031308085e-16; |
---|
88 | |
---|
89 | |
---|
90 | double incbet( aa, bb, xx ) |
---|
91 | double aa, bb, xx; |
---|
92 | { |
---|
93 | double a, b, t, x, xc, w, y; |
---|
94 | int flag; |
---|
95 | |
---|
96 | if( aa <= 0.0 || bb <= 0.0 ) |
---|
97 | goto domerr; |
---|
98 | |
---|
99 | if( (xx <= 0.0) || ( xx >= 1.0) ) |
---|
100 | { |
---|
101 | if( xx == 0.0 ) |
---|
102 | return(0.0); |
---|
103 | if( xx == 1.0 ) |
---|
104 | return( 1.0 ); |
---|
105 | domerr: |
---|
106 | mtherr( "incbet", DOMAIN ); |
---|
107 | return( 0.0 ); |
---|
108 | } |
---|
109 | |
---|
110 | flag = 0; |
---|
111 | if( (bb * xx) <= 1.0 && xx <= 0.95) |
---|
112 | { |
---|
113 | t = pseries(aa, bb, xx); |
---|
114 | goto done; |
---|
115 | } |
---|
116 | |
---|
117 | w = 1.0 - xx; |
---|
118 | |
---|
119 | /* Reverse a and b if x is greater than the mean. */ |
---|
120 | if( xx > (aa/(aa+bb)) ) |
---|
121 | { |
---|
122 | flag = 1; |
---|
123 | a = bb; |
---|
124 | b = aa; |
---|
125 | xc = xx; |
---|
126 | x = w; |
---|
127 | } |
---|
128 | else |
---|
129 | { |
---|
130 | a = aa; |
---|
131 | b = bb; |
---|
132 | xc = w; |
---|
133 | x = xx; |
---|
134 | } |
---|
135 | |
---|
136 | if( flag == 1 && (b * x) <= 1.0 && x <= 0.95) |
---|
137 | { |
---|
138 | t = pseries(a, b, x); |
---|
139 | goto done; |
---|
140 | } |
---|
141 | |
---|
142 | /* Choose expansion for better convergence. */ |
---|
143 | y = x * (a+b-2.0) - (a-1.0); |
---|
144 | if( y < 0.0 ) |
---|
145 | w = incbcf( a, b, x ); |
---|
146 | else |
---|
147 | w = incbd( a, b, x ) / xc; |
---|
148 | |
---|
149 | /* Multiply w by the factor |
---|
150 | a b _ _ _ |
---|
151 | x (1-x) | (a+b) / ( a | (a) | (b) ) . */ |
---|
152 | |
---|
153 | y = a * log(x); |
---|
154 | t = b * log(xc); |
---|
155 | if( (a+b) < MAXGAM && fabs(y) < MAXLOG && fabs(t) < MAXLOG ) |
---|
156 | { |
---|
157 | t = pow(xc,b); |
---|
158 | t *= pow(x,a); |
---|
159 | t /= a; |
---|
160 | t *= w; |
---|
161 | t *= gamma(a+b) / (gamma(a) * gamma(b)); |
---|
162 | goto done; |
---|
163 | } |
---|
164 | /* Resort to logarithms. */ |
---|
165 | y += t + lgam(a+b) - lgam(a) - lgam(b); |
---|
166 | y += log(w/a); |
---|
167 | if( y < MINLOG ) |
---|
168 | t = 0.0; |
---|
169 | else |
---|
170 | t = exp(y); |
---|
171 | |
---|
172 | done: |
---|
173 | |
---|
174 | if( flag == 1 ) |
---|
175 | { |
---|
176 | if( t <= MACHEP ) |
---|
177 | t = 1.0 - MACHEP; |
---|
178 | else |
---|
179 | t = 1.0 - t; |
---|
180 | } |
---|
181 | return( t ); |
---|
182 | } |
---|
183 | |
---|
184 | /* Continued fraction expansion #1 |
---|
185 | * for incomplete beta integral |
---|
186 | */ |
---|
187 | |
---|
188 | static double incbcf( a, b, x ) |
---|
189 | double a, b, x; |
---|
190 | { |
---|
191 | double xk, pk, pkm1, pkm2, qk, qkm1, qkm2; |
---|
192 | double k1, k2, k3, k4, k5, k6, k7, k8; |
---|
193 | double r, t, ans, thresh; |
---|
194 | int n; |
---|
195 | |
---|
196 | k1 = a; |
---|
197 | k2 = a + b; |
---|
198 | k3 = a; |
---|
199 | k4 = a + 1.0; |
---|
200 | k5 = 1.0; |
---|
201 | k6 = b - 1.0; |
---|
202 | k7 = k4; |
---|
203 | k8 = a + 2.0; |
---|
204 | |
---|
205 | pkm2 = 0.0; |
---|
206 | qkm2 = 1.0; |
---|
207 | pkm1 = 1.0; |
---|
208 | qkm1 = 1.0; |
---|
209 | ans = 1.0; |
---|
210 | r = 1.0; |
---|
211 | n = 0; |
---|
212 | thresh = 3.0 * MACHEP; |
---|
213 | do |
---|
214 | { |
---|
215 | |
---|
216 | xk = -( x * k1 * k2 )/( k3 * k4 ); |
---|
217 | pk = pkm1 + pkm2 * xk; |
---|
218 | qk = qkm1 + qkm2 * xk; |
---|
219 | pkm2 = pkm1; |
---|
220 | pkm1 = pk; |
---|
221 | qkm2 = qkm1; |
---|
222 | qkm1 = qk; |
---|
223 | |
---|
224 | xk = ( x * k5 * k6 )/( k7 * k8 ); |
---|
225 | pk = pkm1 + pkm2 * xk; |
---|
226 | qk = qkm1 + qkm2 * xk; |
---|
227 | pkm2 = pkm1; |
---|
228 | pkm1 = pk; |
---|
229 | qkm2 = qkm1; |
---|
230 | qkm1 = qk; |
---|
231 | |
---|
232 | if( qk != 0 ) |
---|
233 | r = pk/qk; |
---|
234 | if( r != 0 ) |
---|
235 | { |
---|
236 | t = fabs( (ans - r)/r ); |
---|
237 | ans = r; |
---|
238 | } |
---|
239 | else |
---|
240 | t = 1.0; |
---|
241 | |
---|
242 | if( t < thresh ) |
---|
243 | goto cdone; |
---|
244 | |
---|
245 | k1 += 1.0; |
---|
246 | k2 += 1.0; |
---|
247 | k3 += 2.0; |
---|
248 | k4 += 2.0; |
---|
249 | k5 += 1.0; |
---|
250 | k6 -= 1.0; |
---|
251 | k7 += 2.0; |
---|
252 | k8 += 2.0; |
---|
253 | |
---|
254 | if( (fabs(qk) + fabs(pk)) > big ) |
---|
255 | { |
---|
256 | pkm2 *= biginv; |
---|
257 | pkm1 *= biginv; |
---|
258 | qkm2 *= biginv; |
---|
259 | qkm1 *= biginv; |
---|
260 | } |
---|
261 | if( (fabs(qk) < biginv) || (fabs(pk) < biginv) ) |
---|
262 | { |
---|
263 | pkm2 *= big; |
---|
264 | pkm1 *= big; |
---|
265 | qkm2 *= big; |
---|
266 | qkm1 *= big; |
---|
267 | } |
---|
268 | } |
---|
269 | while( ++n < 300 ); |
---|
270 | |
---|
271 | cdone: |
---|
272 | return(ans); |
---|
273 | } |
---|
274 | |
---|
275 | |
---|
276 | /* Continued fraction expansion #2 |
---|
277 | * for incomplete beta integral |
---|
278 | */ |
---|
279 | |
---|
280 | static double incbd( a, b, x ) |
---|
281 | double a, b, x; |
---|
282 | { |
---|
283 | double xk, pk, pkm1, pkm2, qk, qkm1, qkm2; |
---|
284 | double k1, k2, k3, k4, k5, k6, k7, k8; |
---|
285 | double r, t, ans, z, thresh; |
---|
286 | int n; |
---|
287 | |
---|
288 | k1 = a; |
---|
289 | k2 = b - 1.0; |
---|
290 | k3 = a; |
---|
291 | k4 = a + 1.0; |
---|
292 | k5 = 1.0; |
---|
293 | k6 = a + b; |
---|
294 | k7 = a + 1.0;; |
---|
295 | k8 = a + 2.0; |
---|
296 | |
---|
297 | pkm2 = 0.0; |
---|
298 | qkm2 = 1.0; |
---|
299 | pkm1 = 1.0; |
---|
300 | qkm1 = 1.0; |
---|
301 | z = x / (1.0-x); |
---|
302 | ans = 1.0; |
---|
303 | r = 1.0; |
---|
304 | n = 0; |
---|
305 | thresh = 3.0 * MACHEP; |
---|
306 | do |
---|
307 | { |
---|
308 | |
---|
309 | xk = -( z * k1 * k2 )/( k3 * k4 ); |
---|
310 | pk = pkm1 + pkm2 * xk; |
---|
311 | qk = qkm1 + qkm2 * xk; |
---|
312 | pkm2 = pkm1; |
---|
313 | pkm1 = pk; |
---|
314 | qkm2 = qkm1; |
---|
315 | qkm1 = qk; |
---|
316 | |
---|
317 | xk = ( z * k5 * k6 )/( k7 * k8 ); |
---|
318 | pk = pkm1 + pkm2 * xk; |
---|
319 | qk = qkm1 + qkm2 * xk; |
---|
320 | pkm2 = pkm1; |
---|
321 | pkm1 = pk; |
---|
322 | qkm2 = qkm1; |
---|
323 | qkm1 = qk; |
---|
324 | |
---|
325 | if( qk != 0 ) |
---|
326 | r = pk/qk; |
---|
327 | if( r != 0 ) |
---|
328 | { |
---|
329 | t = fabs( (ans - r)/r ); |
---|
330 | ans = r; |
---|
331 | } |
---|
332 | else |
---|
333 | t = 1.0; |
---|
334 | |
---|
335 | if( t < thresh ) |
---|
336 | goto cdone; |
---|
337 | |
---|
338 | k1 += 1.0; |
---|
339 | k2 -= 1.0; |
---|
340 | k3 += 2.0; |
---|
341 | k4 += 2.0; |
---|
342 | k5 += 1.0; |
---|
343 | k6 += 1.0; |
---|
344 | k7 += 2.0; |
---|
345 | k8 += 2.0; |
---|
346 | |
---|
347 | if( (fabs(qk) + fabs(pk)) > big ) |
---|
348 | { |
---|
349 | pkm2 *= biginv; |
---|
350 | pkm1 *= biginv; |
---|
351 | qkm2 *= biginv; |
---|
352 | qkm1 *= biginv; |
---|
353 | } |
---|
354 | if( (fabs(qk) < biginv) || (fabs(pk) < biginv) ) |
---|
355 | { |
---|
356 | pkm2 *= big; |
---|
357 | pkm1 *= big; |
---|
358 | qkm2 *= big; |
---|
359 | qkm1 *= big; |
---|
360 | } |
---|
361 | } |
---|
362 | while( ++n < 300 ); |
---|
363 | cdone: |
---|
364 | return(ans); |
---|
365 | } |
---|
366 | |
---|
367 | /* Power series for incomplete beta integral. |
---|
368 | Use when b*x is small and x not too close to 1. */ |
---|
369 | |
---|
370 | static double pseries( a, b, x ) |
---|
371 | double a, b, x; |
---|
372 | { |
---|
373 | double s, t, u, v, n, t1, z, ai; |
---|
374 | |
---|
375 | ai = 1.0 / a; |
---|
376 | u = (1.0 - b) * x; |
---|
377 | v = u / (a + 1.0); |
---|
378 | t1 = v; |
---|
379 | t = u; |
---|
380 | n = 2.0; |
---|
381 | s = 0.0; |
---|
382 | z = MACHEP * ai; |
---|
383 | while( fabs(v) > z ) |
---|
384 | { |
---|
385 | u = (n - b) * x / n; |
---|
386 | t *= u; |
---|
387 | v = t / (a + n); |
---|
388 | s += v; |
---|
389 | n += 1.0; |
---|
390 | } |
---|
391 | s += t1; |
---|
392 | s += ai; |
---|
393 | |
---|
394 | u = a * log(x); |
---|
395 | if( (a+b) < MAXGAM && fabs(u) < MAXLOG ) |
---|
396 | { |
---|
397 | t = gamma(a+b)/(gamma(a)*gamma(b)); |
---|
398 | s = s * t * pow(x,a); |
---|
399 | } |
---|
400 | else |
---|
401 | { |
---|
402 | t = lgam(a+b) - lgam(a) - lgam(b) + u + log(s); |
---|
403 | if( t < MINLOG ) |
---|
404 | s = 0.0; |
---|
405 | else |
---|
406 | s = exp(t); |
---|
407 | } |
---|
408 | return(s); |
---|
409 | } |
---|