nbdtrl.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* nbdtrl.c
  2. *
  3. * Negative binomial distribution
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * int k, n;
  10. * long double p, y, nbdtrl();
  11. *
  12. * y = nbdtrl( k, n, p );
  13. *
  14. *
  15. *
  16. * DESCRIPTION:
  17. *
  18. * Returns the sum of the terms 0 through k of the negative
  19. * binomial distribution:
  20. *
  21. * k
  22. * -- ( n+j-1 ) n j
  23. * > ( ) p (1-p)
  24. * -- ( j )
  25. * j=0
  26. *
  27. * In a sequence of Bernoulli trials, this is the probability
  28. * that k or fewer failures precede the nth success.
  29. *
  30. * The terms are not computed individually; instead the incomplete
  31. * beta integral is employed, according to the formula
  32. *
  33. * y = nbdtr( k, n, p ) = incbet( n, k+1, p ).
  34. *
  35. * The arguments must be positive, with p ranging from 0 to 1.
  36. *
  37. *
  38. *
  39. * ACCURACY:
  40. *
  41. * Tested at random points (k,n,p) with k and n between 1 and 10,000
  42. * and p between 0 and 1.
  43. *
  44. * arithmetic domain # trials peak rms
  45. * Absolute error:
  46. * IEEE 0,10000 10000 9.8e-15 2.1e-16
  47. *
  48. */
  49. /* nbdtrcl.c
  50. *
  51. * Complemented negative binomial distribution
  52. *
  53. *
  54. *
  55. * SYNOPSIS:
  56. *
  57. * int k, n;
  58. * long double p, y, nbdtrcl();
  59. *
  60. * y = nbdtrcl( k, n, p );
  61. *
  62. *
  63. *
  64. * DESCRIPTION:
  65. *
  66. * Returns the sum of the terms k+1 to infinity of the negative
  67. * binomial distribution:
  68. *
  69. * inf
  70. * -- ( n+j-1 ) n j
  71. * > ( ) p (1-p)
  72. * -- ( j )
  73. * j=k+1
  74. *
  75. * The terms are not computed individually; instead the incomplete
  76. * beta integral is employed, according to the formula
  77. *
  78. * y = nbdtrc( k, n, p ) = incbet( k+1, n, 1-p ).
  79. *
  80. * The arguments must be positive, with p ranging from 0 to 1.
  81. *
  82. *
  83. *
  84. * ACCURACY:
  85. *
  86. * See incbetl.c.
  87. *
  88. */
  89. /* nbdtril
  90. *
  91. * Functional inverse of negative binomial distribution
  92. *
  93. *
  94. *
  95. * SYNOPSIS:
  96. *
  97. * int k, n;
  98. * long double p, y, nbdtril();
  99. *
  100. * p = nbdtril( k, n, y );
  101. *
  102. *
  103. *
  104. * DESCRIPTION:
  105. *
  106. * Finds the argument p such that nbdtr(k,n,p) is equal to y.
  107. *
  108. * ACCURACY:
  109. *
  110. * Tested at random points (a,b,y), with y between 0 and 1.
  111. *
  112. * a,b Relative error:
  113. * arithmetic domain # trials peak rms
  114. * IEEE 0,100
  115. * See also incbil.c.
  116. */
  117. /*
  118. Cephes Math Library Release 2.3: January,1995
  119. Copyright 1984, 1995 by Stephen L. Moshier
  120. */
  121. #include <math.h>
  122. #ifdef ANSIPROT
  123. extern long double incbetl ( long double, long double, long double );
  124. extern long double powl ( long double, long double );
  125. extern long double incbil ( long double, long double, long double );
  126. #else
  127. long double incbetl(), powl(), incbil();
  128. #endif
  129. long double nbdtrcl( k, n, p )
  130. int k, n;
  131. long double p;
  132. {
  133. long double dk, dn;
  134. if( (p < 0.0L) || (p > 1.0L) )
  135. goto domerr;
  136. if( k < 0 )
  137. {
  138. domerr:
  139. mtherr( "nbdtrl", DOMAIN );
  140. return( 0.0L );
  141. }
  142. dn = n;
  143. if( k == 0 )
  144. return( 1.0L - powl( p, dn ) );
  145. dk = k+1;
  146. return( incbetl( dk, dn, 1.0L - p ) );
  147. }
  148. long double nbdtrl( k, n, p )
  149. int k, n;
  150. long double p;
  151. {
  152. long double dk, dn;
  153. if( (p < 0.0L) || (p > 1.0L) )
  154. goto domerr;
  155. if( k < 0 )
  156. {
  157. domerr:
  158. mtherr( "nbdtrl", DOMAIN );
  159. return( 0.0L );
  160. }
  161. dn = n;
  162. if( k == 0 )
  163. return( powl( p, dn ) );
  164. dk = k+1;
  165. return( incbetl( dn, dk, p ) );
  166. }
  167. long double nbdtril( k, n, p )
  168. int k, n;
  169. long double p;
  170. {
  171. long double dk, dn, w;
  172. if( (p < 0.0L) || (p > 1.0L) )
  173. goto domerr;
  174. if( k < 0 )
  175. {
  176. domerr:
  177. mtherr( "nbdtrl", DOMAIN );
  178. return( 0.0L );
  179. }
  180. dk = k+1;
  181. dn = n;
  182. w = incbil( dn, dk, p );
  183. return( w );
  184. }