gsl_cdf__geometric.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* cdf/geometric.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. /* Pr (X <= k), i.e., the probability of n or fewer failures until the
  26. first success. */
  27. double
  28. gsl_cdf_geometric_P (const unsigned int k, const double p)
  29. {
  30. double P, a, q;
  31. if (p > 1.0 || p < 0.0)
  32. {
  33. CDF_ERROR ("p < 0 or p > 1", GSL_EDOM);
  34. }
  35. if (k < 1)
  36. {
  37. return 0.0;
  38. }
  39. q = 1.0 - p;
  40. a = (double) k;
  41. if (p < 0.5)
  42. {
  43. P = -expm1 (a * log1p (-p));
  44. }
  45. else
  46. {
  47. P = 1.0 - pow (q, a);
  48. }
  49. return P;
  50. }
  51. double
  52. gsl_cdf_geometric_Q (const unsigned int k, const double p)
  53. {
  54. double Q, a, q;
  55. if (p > 1.0 || p < 0.0)
  56. {
  57. CDF_ERROR ("p < 0 or p > 1", GSL_EDOM);
  58. }
  59. if (k < 1)
  60. {
  61. Q = 1.0;
  62. }
  63. q = 1.0 - p;
  64. a = (double) k;
  65. if (p < 0.5)
  66. {
  67. Q = exp (a * log1p (-p));
  68. }
  69. else
  70. {
  71. Q = pow (q, a);
  72. }
  73. return Q;
  74. }