gsl_cdf__binomial.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* cdf/binomial.c
  2. *
  3. * Copyright (C) 2004 Jason H. Stover.
  4. * Copyright (C) 2004 Giulio Bottazzi
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include "gsl__config.h"
  21. #include <math.h>
  22. #include "gsl_math.h"
  23. #include "gsl_cdf.h"
  24. #include "gsl_sf_gamma.h"
  25. #include "gsl_cdf__error.h"
  26. /* Computes the cumulative distribution function for a binomial
  27. random variable. For a binomial random variable X with n trials
  28. and success probability p,
  29. Pr( X <= k ) = Pr( Y >= p )
  30. where Y is a beta random variable with parameters k+1 and n-k.
  31. The binomial distribution has the form,
  32. prob(k) = n!/(k!(n-k)!) * p^k (1-p)^(n-k) for k = 0, 1, ..., n
  33. The cumulated distributions can be expressed in terms of normalized
  34. incomplete beta functions (see Abramowitz & Stegun eq. 26.5.26 and
  35. eq. 6.6.3).
  36. Reference:
  37. W. Feller, "An Introduction to Probability and Its
  38. Applications," volume 1. Wiley, 1968. Exercise 45, page 173,
  39. chapter 6.
  40. */
  41. #include "gsl__config.h"
  42. #include <math.h>
  43. #include "gsl_math.h"
  44. #include "gsl_errno.h"
  45. #include "gsl_cdf.h"
  46. double
  47. gsl_cdf_binomial_P (const unsigned int k, const double p, const unsigned int n)
  48. {
  49. double P;
  50. double a;
  51. double b;
  52. if (p > 1.0 || p < 0.0)
  53. {
  54. CDF_ERROR ("p < 0 or p > 1", GSL_EDOM);
  55. }
  56. if (k >= n)
  57. {
  58. P = 1.0;
  59. }
  60. else
  61. {
  62. a = (double) k + 1.0;
  63. b = (double) n - k;
  64. P = gsl_cdf_beta_Q (p, a, b);
  65. }
  66. return P;
  67. }
  68. double
  69. gsl_cdf_binomial_Q (const unsigned int k, const double p, const unsigned int n)
  70. {
  71. double Q;
  72. double a;
  73. double b;
  74. if (p > 1.0 || p < 0.0)
  75. {
  76. CDF_ERROR ("p < 0 or p > 1", GSL_EDOM);
  77. }
  78. if (k >= n)
  79. {
  80. Q = 0.0;
  81. }
  82. else
  83. {
  84. a = (double) k + 1.0;
  85. b = (double) n - k;
  86. Q = gsl_cdf_beta_P (p, a, b);
  87. }
  88. return Q;
  89. }