incbetf.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* incbetf.c
  2. *
  3. * Incomplete beta integral
  4. *
  5. *
  6. * SYNOPSIS:
  7. *
  8. * float a, b, x, y, incbetf();
  9. *
  10. * y = incbetf( 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. * If a < 1, the function calls itself recursively after a
  35. * transformation to increase a to a+1.
  36. *
  37. * ACCURACY:
  38. *
  39. * Tested at random points (a,b,x) with a and b in the indicated
  40. * interval and x between 0 and 1.
  41. *
  42. * arithmetic domain # trials peak rms
  43. * Relative error:
  44. * IEEE 0,30 10000 3.7e-5 5.1e-6
  45. * IEEE 0,100 10000 1.7e-4 2.5e-5
  46. * The useful domain for relative error is limited by underflow
  47. * of the single precision exponential function.
  48. * Absolute error:
  49. * IEEE 0,30 100000 2.2e-5 9.6e-7
  50. * IEEE 0,100 10000 6.5e-5 3.7e-6
  51. *
  52. * Larger errors may occur for extreme ratios of a and b.
  53. *
  54. * ERROR MESSAGES:
  55. * message condition value returned
  56. * incbetf domain x<0, x>1 0.0
  57. */
  58. /*
  59. Cephes Math Library, Release 2.2: July, 1992
  60. Copyright 1984, 1987, 1992 by Stephen L. Moshier
  61. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  62. */
  63. #include <math.h>
  64. #ifdef ANSIC
  65. float lgamf(float), expf(float), logf(float);
  66. static float incbdf(float, float, float);
  67. static float incbcff(float, float, float);
  68. float incbpsf(float, float, float);
  69. #else
  70. float lgamf(), expf(), logf();
  71. float incbpsf();
  72. static float incbcff(), incbdf();
  73. #endif
  74. #define fabsf(x) ( (x) < 0 ? -(x) : (x) )
  75. /* BIG = 1/MACHEPF */
  76. #define BIG 16777216.
  77. extern float MACHEPF, MAXLOGF;
  78. #define MINLOGF (-MAXLOGF)
  79. float incbetf( float aaa, float bbb, float xxx )
  80. {
  81. float aa, bb, xx, ans, a, b, t, x, onemx;
  82. int flag;
  83. aa = aaa;
  84. bb = bbb;
  85. xx = xxx;
  86. if( (xx <= 0.0) || ( xx >= 1.0) )
  87. {
  88. if( xx == 0.0 )
  89. return(0.0);
  90. if( xx == 1.0 )
  91. return( 1.0 );
  92. mtherr( "incbetf", DOMAIN );
  93. return( 0.0 );
  94. }
  95. onemx = 1.0 - xx;
  96. /* transformation for small aa */
  97. if( aa <= 1.0 )
  98. {
  99. ans = incbetf( aa+1.0, bb, xx );
  100. t = aa*logf(xx) + bb*logf( 1.0-xx )
  101. + lgamf(aa+bb) - lgamf(aa+1.0) - lgamf(bb);
  102. if( t > MINLOGF )
  103. ans += expf(t);
  104. return( ans );
  105. }
  106. /* see if x is greater than the mean */
  107. if( xx > (aa/(aa+bb)) )
  108. {
  109. flag = 1;
  110. a = bb;
  111. b = aa;
  112. t = xx;
  113. x = onemx;
  114. }
  115. else
  116. {
  117. flag = 0;
  118. a = aa;
  119. b = bb;
  120. t = onemx;
  121. x = xx;
  122. }
  123. /* transformation for small aa */
  124. /*
  125. if( a <= 1.0 )
  126. {
  127. ans = a*logf(x) + b*logf( onemx )
  128. + lgamf(a+b) - lgamf(a+1.0) - lgamf(b);
  129. t = incbetf( a+1.0, b, x );
  130. if( ans > MINLOGF )
  131. t += expf(ans);
  132. goto bdone;
  133. }
  134. */
  135. /* Choose expansion for optimal convergence */
  136. if( b > 10.0 )
  137. {
  138. if( fabsf(b*x/a) < 0.3 )
  139. {
  140. t = incbpsf( a, b, x );
  141. goto bdone;
  142. }
  143. }
  144. ans = x * (a+b-2.0)/(a-1.0);
  145. if( ans < 1.0 )
  146. {
  147. ans = incbcff( a, b, x );
  148. t = b * logf( t );
  149. }
  150. else
  151. {
  152. ans = incbdf( a, b, x );
  153. t = (b-1.0) * logf(t);
  154. }
  155. t += a*logf(x) + lgamf(a+b) - lgamf(a) - lgamf(b);
  156. t += logf( ans/a );
  157. if( t < MINLOGF )
  158. {
  159. t = 0.0;
  160. if( flag == 0 )
  161. {
  162. mtherr( "incbetf", UNDERFLOW );
  163. }
  164. }
  165. else
  166. {
  167. t = expf(t);
  168. }
  169. bdone:
  170. if( flag )
  171. t = 1.0 - t;
  172. return( t );
  173. }
  174. /* Continued fraction expansion #1
  175. * for incomplete beta integral
  176. */
  177. static float incbcff( float aa, float bb, float xx )
  178. {
  179. float a, b, x, xk, pk, pkm1, pkm2, qk, qkm1, qkm2;
  180. float k1, k2, k3, k4, k5, k6, k7, k8;
  181. float r, t, ans;
  182. static float big = BIG;
  183. int n;
  184. a = aa;
  185. b = bb;
  186. x = xx;
  187. k1 = a;
  188. k2 = a + b;
  189. k3 = a;
  190. k4 = a + 1.0;
  191. k5 = 1.0;
  192. k6 = b - 1.0;
  193. k7 = k4;
  194. k8 = a + 2.0;
  195. pkm2 = 0.0;
  196. qkm2 = 1.0;
  197. pkm1 = 1.0;
  198. qkm1 = 1.0;
  199. ans = 1.0;
  200. r = 0.0;
  201. n = 0;
  202. do
  203. {
  204. xk = -( x * k1 * k2 )/( k3 * k4 );
  205. pk = pkm1 + pkm2 * xk;
  206. qk = qkm1 + qkm2 * xk;
  207. pkm2 = pkm1;
  208. pkm1 = pk;
  209. qkm2 = qkm1;
  210. qkm1 = qk;
  211. xk = ( x * k5 * k6 )/( k7 * k8 );
  212. pk = pkm1 + pkm2 * xk;
  213. qk = qkm1 + qkm2 * xk;
  214. pkm2 = pkm1;
  215. pkm1 = pk;
  216. qkm2 = qkm1;
  217. qkm1 = qk;
  218. if( qk != 0 )
  219. r = pk/qk;
  220. if( r != 0 )
  221. {
  222. t = fabsf( (ans - r)/r );
  223. ans = r;
  224. }
  225. else
  226. t = 1.0;
  227. if( t < MACHEPF )
  228. goto cdone;
  229. k1 += 1.0;
  230. k2 += 1.0;
  231. k3 += 2.0;
  232. k4 += 2.0;
  233. k5 += 1.0;
  234. k6 -= 1.0;
  235. k7 += 2.0;
  236. k8 += 2.0;
  237. if( (fabsf(qk) + fabsf(pk)) > big )
  238. {
  239. pkm2 *= MACHEPF;
  240. pkm1 *= MACHEPF;
  241. qkm2 *= MACHEPF;
  242. qkm1 *= MACHEPF;
  243. }
  244. if( (fabsf(qk) < MACHEPF) || (fabsf(pk) < MACHEPF) )
  245. {
  246. pkm2 *= big;
  247. pkm1 *= big;
  248. qkm2 *= big;
  249. qkm1 *= big;
  250. }
  251. }
  252. while( ++n < 100 );
  253. cdone:
  254. return(ans);
  255. }
  256. /* Continued fraction expansion #2
  257. * for incomplete beta integral
  258. */
  259. static float incbdf( float aa, float bb, float xx )
  260. {
  261. float a, b, x, xk, pk, pkm1, pkm2, qk, qkm1, qkm2;
  262. float k1, k2, k3, k4, k5, k6, k7, k8;
  263. float r, t, ans, z;
  264. static float big = BIG;
  265. int n;
  266. a = aa;
  267. b = bb;
  268. x = xx;
  269. k1 = a;
  270. k2 = b - 1.0;
  271. k3 = a;
  272. k4 = a + 1.0;
  273. k5 = 1.0;
  274. k6 = a + b;
  275. k7 = a + 1.0;;
  276. k8 = a + 2.0;
  277. pkm2 = 0.0;
  278. qkm2 = 1.0;
  279. pkm1 = 1.0;
  280. qkm1 = 1.0;
  281. z = x / (1.0-x);
  282. ans = 1.0;
  283. r = 0.0;
  284. n = 0;
  285. do
  286. {
  287. xk = -( z * k1 * k2 )/( k3 * k4 );
  288. pk = pkm1 + pkm2 * xk;
  289. qk = qkm1 + qkm2 * xk;
  290. pkm2 = pkm1;
  291. pkm1 = pk;
  292. qkm2 = qkm1;
  293. qkm1 = qk;
  294. xk = ( z * k5 * k6 )/( k7 * k8 );
  295. pk = pkm1 + pkm2 * xk;
  296. qk = qkm1 + qkm2 * xk;
  297. pkm2 = pkm1;
  298. pkm1 = pk;
  299. qkm2 = qkm1;
  300. qkm1 = qk;
  301. if( qk != 0 )
  302. r = pk/qk;
  303. if( r != 0 )
  304. {
  305. t = fabsf( (ans - r)/r );
  306. ans = r;
  307. }
  308. else
  309. t = 1.0;
  310. if( t < MACHEPF )
  311. goto cdone;
  312. k1 += 1.0;
  313. k2 -= 1.0;
  314. k3 += 2.0;
  315. k4 += 2.0;
  316. k5 += 1.0;
  317. k6 += 1.0;
  318. k7 += 2.0;
  319. k8 += 2.0;
  320. if( (fabsf(qk) + fabsf(pk)) > big )
  321. {
  322. pkm2 *= MACHEPF;
  323. pkm1 *= MACHEPF;
  324. qkm2 *= MACHEPF;
  325. qkm1 *= MACHEPF;
  326. }
  327. if( (fabsf(qk) < MACHEPF) || (fabsf(pk) < MACHEPF) )
  328. {
  329. pkm2 *= big;
  330. pkm1 *= big;
  331. qkm2 *= big;
  332. qkm1 *= big;
  333. }
  334. }
  335. while( ++n < 100 );
  336. cdone:
  337. return(ans);
  338. }
  339. /* power series */
  340. float incbpsf( float aa, float bb, float xx )
  341. {
  342. float a, b, x, t, u, y, s;
  343. a = aa;
  344. b = bb;
  345. x = xx;
  346. y = a * logf(x) + (b-1.0)*logf(1.0-x) - logf(a);
  347. y -= lgamf(a) + lgamf(b);
  348. y += lgamf(a+b);
  349. t = x / (1.0 - x);
  350. s = 0.0;
  351. u = 1.0;
  352. do
  353. {
  354. b -= 1.0;
  355. if( b == 0.0 )
  356. break;
  357. a += 1.0;
  358. u *= t*b/a;
  359. s += u;
  360. }
  361. while( fabsf(u) > MACHEPF );
  362. if( y < MINLOGF )
  363. {
  364. mtherr( "incbetf", UNDERFLOW );
  365. s = 0.0;
  366. }
  367. else
  368. s = expf(y) * (1.0 + s);
  369. /*printf( "incbpsf: %.4e\n", s );*/
  370. return(s);
  371. }