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