| 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 | |
| 20 | #include "gsl__config.h" |
| 21 | #include <math.h> |
| 22 | #include "gsl_math.h" |
| 23 | #include "gsl_errno.h" |
| 24 | #include "gsl_cdf.h" |
| 25 | |
| 26 | #include "gsl_cdf__error.h" |
| 27 | |
| 28 | /* Pr (X <= k), i.e., the probability of n or fewer failures until the |
| 29 | first success. */ |
| 30 | |
| 31 | double |
| 32 | gsl_cdf_geometric_P (const unsigned int k, const double p) |
| 33 | { |
| 34 | double P, a, q; |
| 35 | |
| 36 | if (p > 1.0 || p < 0.0) |
| 37 | { |
| 38 | CDF_ERROR ("p < 0 or p > 1", GSL_EDOM); |
| 39 | } |
| 40 | |
| 41 | if (k < 1) |
| 42 | { |
| 43 | return 0.0; |
| 44 | } |
| 45 | |
| 46 | q = 1.0 - p; |
| 47 | a = (double) k; |
| 48 | |
| 49 | if (p < 0.5) |
| 50 | { |
| 51 | P = -expm1 (a * log1p (-p)); |
| 52 | } |
| 53 | else |
| 54 | { |
| 55 | P = 1.0 - pow (q, a); |
| 56 | } |
| 57 | |
| 58 | return P; |
| 59 | } |
| 60 | |
| 61 | double |
| 62 | gsl_cdf_geometric_Q (const unsigned int k, const double p) |
| 63 | { |
| 64 | double Q, a, q; |
| 65 | |
| 66 | if (p > 1.0 || p < 0.0) |
| 67 | { |
| 68 | CDF_ERROR ("p < 0 or p > 1", GSL_EDOM); |
| 69 | } |
| 70 | |
| 71 | if (k < 1) |
| 72 | { |
| 73 | Q = 1.0; |
| 74 | } |
| 75 | |
| 76 | q = 1.0 - p; |
| 77 | a = (double) k; |
| 78 | |
| 79 | if (p < 0.5) |
| 80 | { |
| 81 | Q = exp (a * log1p (-p)); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | Q = pow (q, a); |
| 86 | } |
| 87 | |
| 88 | return Q; |
| 89 | } |