67c19ce38b2df422ac601c4b882c582a9139a30a
[pspp-builds.git] / lib / gsl-extras / negbinom.c
1 /* cdf/negbinom.c
2  *
3  * Copyright (C) 2004 Free Software Foundation, Inc.
4  * Written by Jason H. Stover.
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 2 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20
21
22 #include <config.h>
23 #include <math.h>
24 #include <gsl/gsl_math.h>
25 #include <gsl/gsl_errno.h>
26 #include <gsl/gsl_cdf.h>
27 #include "gsl-extras.h"
28
29 /*
30  * Pr(X <= n) for a negative binomial random variable X, i.e.,
31  * the probability of n or fewer failuers before success k.
32  */
33 double
34 gslextras_cdf_negative_binomial_P(const long n, const long k, const double p)
35 {
36   double P;
37   double a;
38   double b;
39
40   if(p > 1.0 || p < 0.0)
41     {
42       GSLEXTRAS_CDF_ERROR("p < 0 or p > 1",GSL_EDOM);
43     }
44   if ( k < 0 )
45     {
46       GSLEXTRAS_CDF_ERROR ("k < 0",GSL_EDOM);
47     }
48   if ( n < 0 )
49     {
50       P = 0.0;
51     }
52   else
53     {
54       a = (double) k;
55       b = (double) n+1;
56       P = gsl_cdf_beta_P(p, a, b);
57     }
58
59   return P;
60 }
61 /*
62  * Pr ( X > n ).
63  */
64 double
65 gslextras_cdf_negative_binomial_Q(const long n, const long k, const double p)
66 {
67   double P;
68   double a;
69   double b;
70
71   if(p > 1.0 || p < 0.0)
72     {
73       GSLEXTRAS_CDF_ERROR("p < 0 or p > 1",GSL_EDOM);
74     }
75   if ( k < 0 )
76     {
77       GSLEXTRAS_CDF_ERROR ("k < 0",GSL_EDOM);
78     }
79   if ( n < 0 )
80     {
81       P = 1.0;
82     }
83   else
84     {
85       a = (double) k;
86       b = (double) n+1;
87       P = gsl_cdf_beta_Q(p, a, b);
88     }
89
90   return P;
91 }
92