bdtrf.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* bdtrf.c
  2. *
  3. * Binomial distribution
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * int k, n;
  10. * float p, y, bdtrf();
  11. *
  12. * y = bdtrf( k, n, p );
  13. *
  14. *
  15. *
  16. * DESCRIPTION:
  17. *
  18. * Returns the sum of the terms 0 through k of the Binomial
  19. * probability density:
  20. *
  21. * k
  22. * -- ( n ) j n-j
  23. * > ( ) p (1-p)
  24. * -- ( j )
  25. * j=0
  26. *
  27. * The terms are not summed directly; instead the incomplete
  28. * beta integral is employed, according to the formula
  29. *
  30. * y = bdtr( k, n, p ) = incbet( n-k, k+1, 1-p ).
  31. *
  32. * The arguments must be positive, with p ranging from 0 to 1.
  33. *
  34. *
  35. *
  36. * ACCURACY:
  37. *
  38. * Relative error (p varies from 0 to 1):
  39. * arithmetic domain # trials peak rms
  40. * IEEE 0,100 2000 6.9e-5 1.1e-5
  41. *
  42. * ERROR MESSAGES:
  43. *
  44. * message condition value returned
  45. * bdtrf domain k < 0 0.0
  46. * n < k
  47. * x < 0, x > 1
  48. *
  49. */
  50. /* bdtrcf()
  51. *
  52. * Complemented binomial distribution
  53. *
  54. *
  55. *
  56. * SYNOPSIS:
  57. *
  58. * int k, n;
  59. * float p, y, bdtrcf();
  60. *
  61. * y = bdtrcf( k, n, p );
  62. *
  63. *
  64. *
  65. * DESCRIPTION:
  66. *
  67. * Returns the sum of the terms k+1 through n of the Binomial
  68. * probability density:
  69. *
  70. * n
  71. * -- ( n ) j n-j
  72. * > ( ) p (1-p)
  73. * -- ( j )
  74. * j=k+1
  75. *
  76. * The terms are not summed directly; instead the incomplete
  77. * beta integral is employed, according to the formula
  78. *
  79. * y = bdtrc( k, n, p ) = incbet( k+1, n-k, p ).
  80. *
  81. * The arguments must be positive, with p ranging from 0 to 1.
  82. *
  83. *
  84. *
  85. * ACCURACY:
  86. *
  87. * Relative error (p varies from 0 to 1):
  88. * arithmetic domain # trials peak rms
  89. * IEEE 0,100 2000 6.0e-5 1.2e-5
  90. *
  91. * ERROR MESSAGES:
  92. *
  93. * message condition value returned
  94. * bdtrcf domain x<0, x>1, n<k 0.0
  95. */
  96. /* bdtrif()
  97. *
  98. * Inverse binomial distribution
  99. *
  100. *
  101. *
  102. * SYNOPSIS:
  103. *
  104. * int k, n;
  105. * float p, y, bdtrif();
  106. *
  107. * p = bdtrf( k, n, y );
  108. *
  109. *
  110. *
  111. * DESCRIPTION:
  112. *
  113. * Finds the event probability p such that the sum of the
  114. * terms 0 through k of the Binomial probability density
  115. * is equal to the given cumulative probability y.
  116. *
  117. * This is accomplished using the inverse beta integral
  118. * function and the relation
  119. *
  120. * 1 - p = incbi( n-k, k+1, y ).
  121. *
  122. *
  123. *
  124. *
  125. * ACCURACY:
  126. *
  127. * Relative error (p varies from 0 to 1):
  128. * arithmetic domain # trials peak rms
  129. * IEEE 0,100 2000 3.5e-5 3.3e-6
  130. *
  131. * ERROR MESSAGES:
  132. *
  133. * message condition value returned
  134. * bdtrif domain k < 0, n <= k 0.0
  135. * x < 0, x > 1
  136. *
  137. */
  138. /* bdtr() */
  139. /*
  140. Cephes Math Library Release 2.2: July, 1992
  141. Copyright 1984, 1987, 1992 by Stephen L. Moshier
  142. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  143. */
  144. #include <math.h>
  145. #ifdef ANSIC
  146. float incbetf(float, float, float), powf(float, float);
  147. float incbif( float, float, float );
  148. #else
  149. float incbetf(), powf(), incbif();
  150. #endif
  151. float bdtrcf( int k, int n, float pp )
  152. {
  153. float p, dk, dn;
  154. p = pp;
  155. if( (p < 0.0) || (p > 1.0) )
  156. goto domerr;
  157. if( k < 0 )
  158. return( 1.0 );
  159. if( n < k )
  160. {
  161. domerr:
  162. mtherr( "bdtrcf", DOMAIN );
  163. return( 0.0 );
  164. }
  165. if( k == n )
  166. return( 0.0 );
  167. dn = n - k;
  168. if( k == 0 )
  169. {
  170. dk = 1.0 - powf( 1.0-p, dn );
  171. }
  172. else
  173. {
  174. dk = k + 1;
  175. dk = incbetf( dk, dn, p );
  176. }
  177. return( dk );
  178. }
  179. float bdtrf( int k, int n, float pp )
  180. {
  181. float p, dk, dn;
  182. p = pp;
  183. if( (p < 0.0) || (p > 1.0) )
  184. goto domerr;
  185. if( (k < 0) || (n < k) )
  186. {
  187. domerr:
  188. mtherr( "bdtrf", DOMAIN );
  189. return( 0.0 );
  190. }
  191. if( k == n )
  192. return( 1.0 );
  193. dn = n - k;
  194. if( k == 0 )
  195. {
  196. dk = powf( 1.0-p, dn );
  197. }
  198. else
  199. {
  200. dk = k + 1;
  201. dk = incbetf( dn, dk, 1.0 - p );
  202. }
  203. return( dk );
  204. }
  205. float bdtrif( int k, int n, float yy )
  206. {
  207. float y, dk, dn, p;
  208. y = yy;
  209. if( (y < 0.0) || (y > 1.0) )
  210. goto domerr;
  211. if( (k < 0) || (n <= k) )
  212. {
  213. domerr:
  214. mtherr( "bdtrif", DOMAIN );
  215. return( 0.0 );
  216. }
  217. dn = n - k;
  218. if( k == 0 )
  219. {
  220. p = 1.0 - powf( y, 1.0/dn );
  221. }
  222. else
  223. {
  224. dk = k + 1;
  225. p = 1.0 - incbif( dn, dk, y );
  226. }
  227. return( p );
  228. }