gsl_cdf__nbinomial.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* cdf/negbinom.c
  2. *
  3. * Copyright (C) 2004 Jason H. Stover.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include <math.h>
  21. #include "gsl_math.h"
  22. #include "gsl_errno.h"
  23. #include "gsl_cdf.h"
  24. #include "gsl_cdf__error.h"
  25. /*
  26. * Pr(X <= k) for a negative binomial random variable X, i.e.,
  27. * the probability of k or fewer failuers before success n.
  28. */
  29. double
  30. gsl_cdf_negative_binomial_P (const unsigned int k, const double p, const double n)
  31. {
  32. double P;
  33. double a;
  34. double b;
  35. if (p > 1.0 || p < 0.0)
  36. {
  37. CDF_ERROR ("p < 0 or p > 1", GSL_EDOM);
  38. }
  39. if (n < 0)
  40. {
  41. CDF_ERROR ("n < 0", GSL_EDOM);
  42. }
  43. a = (double) n;
  44. b = (double) k + 1.0;
  45. P = gsl_cdf_beta_P (p, a, b);
  46. return P;
  47. }
  48. /*
  49. * Pr ( X > k ).
  50. */
  51. double
  52. gsl_cdf_negative_binomial_Q (const unsigned int k, const double p, const double n)
  53. {
  54. double Q;
  55. double a;
  56. double b;
  57. if (p > 1.0 || p < 0.0)
  58. {
  59. CDF_ERROR ("p < 0 or p > 1", GSL_EDOM);
  60. }
  61. if (n < 0)
  62. {
  63. CDF_ERROR ("n < 0", GSL_EDOM);
  64. }
  65. a = (double) n;
  66. b = (double) k + 1.0;
  67. Q = gsl_cdf_beta_Q (p, a, b);
  68. return Q;
  69. }