nbdtrf.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* nbdtrf.c
  2. *
  3. * Negative binomial distribution
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * int k, n;
  10. * float p, y, nbdtrf();
  11. *
  12. * y = nbdtrf( 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. * Relative error:
  42. * arithmetic domain # trials peak rms
  43. * IEEE 0,100 5000 1.5e-4 1.9e-5
  44. *
  45. */
  46. /* nbdtrcf.c
  47. *
  48. * Complemented negative binomial distribution
  49. *
  50. *
  51. *
  52. * SYNOPSIS:
  53. *
  54. * int k, n;
  55. * float p, y, nbdtrcf();
  56. *
  57. * y = nbdtrcf( k, n, p );
  58. *
  59. *
  60. *
  61. * DESCRIPTION:
  62. *
  63. * Returns the sum of the terms k+1 to infinity of the negative
  64. * binomial distribution:
  65. *
  66. * inf
  67. * -- ( n+j-1 ) n j
  68. * > ( ) p (1-p)
  69. * -- ( j )
  70. * j=k+1
  71. *
  72. * The terms are not computed individually; instead the incomplete
  73. * beta integral is employed, according to the formula
  74. *
  75. * y = nbdtrc( k, n, p ) = incbet( k+1, n, 1-p ).
  76. *
  77. * The arguments must be positive, with p ranging from 0 to 1.
  78. *
  79. *
  80. *
  81. * ACCURACY:
  82. *
  83. * Relative error:
  84. * arithmetic domain # trials peak rms
  85. * IEEE 0,100 5000 1.4e-4 2.0e-5
  86. *
  87. */
  88. /*
  89. Cephes Math Library Release 2.2: July, 1992
  90. Copyright 1984, 1987 by Stephen L. Moshier
  91. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  92. */
  93. #include <math.h>
  94. #ifdef ANSIC
  95. float incbetf(float, float, float);
  96. #else
  97. float incbetf();
  98. #endif
  99. float nbdtrcf( int k, int n, float pp )
  100. {
  101. float dk, dn, p;
  102. p = pp;
  103. if( (p < 0.0) || (p > 1.0) )
  104. goto domerr;
  105. if( k < 0 )
  106. {
  107. domerr:
  108. mtherr( "nbdtrf", DOMAIN );
  109. return( 0.0 );
  110. }
  111. dk = k+1;
  112. dn = n;
  113. return( incbetf( dk, dn, 1.0 - p ) );
  114. }
  115. float nbdtrf( int k, int n, float pp )
  116. {
  117. float dk, dn, p;
  118. p = pp;
  119. if( (p < 0.0) || (p > 1.0) )
  120. goto domerr;
  121. if( k < 0 )
  122. {
  123. domerr:
  124. mtherr( "nbdtrf", DOMAIN );
  125. return( 0.0 );
  126. }
  127. dk = k+1;
  128. dn = n;
  129. return( incbetf( dn, dk, p ) );
  130. }