Implement some more transformation functions using code from Jason
[pspp-builds.git] / lib / gsl-extras / poisson.c
1 /* cdf/poisson.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  * Computes the cumulative distribution function for a Poisson
21  * random variable. For a Poisson random variable X with parameter
22  * lambda,
23  *
24  *          Pr( X <= k ) = Pr( Y >= p )
25  *
26  * where Y is a gamma random variable with parameters k+1 and 1.
27  *
28  * Reference: 
29  * 
30  * W. Feller, "An Introduction to Probability and Its
31  * Applications," volume 1. Wiley, 1968. Exercise 46, page 173,
32  * chapter 6.
33  */
34 #include <config.h>
35 #include <math.h>
36 #include <gsl/gsl_math.h>
37 #include <gsl/gsl_errno.h>
38 #include <gsl/gsl_cdf.h>
39 #include "gsl-extras.h"
40 #include "gsl-extras.h"
41
42 /*
43  * Pr (X <= k) for a Poisson random variable X.
44  */
45 double
46 gslextras_cdf_poisson_P (const long k, const double lambda)
47 {
48   double P;
49   double a;
50   
51   if ( lambda <= 0.0 )
52     {
53       GSLEXTRAS_CDF_ERROR ("lambda <= 0", GSL_EDOM);
54     }
55   if ( k < 0 )
56     {
57       P = 0.0;
58     }
59   else
60     {
61       a = (double) k+1;
62       P = gsl_cdf_gamma_Q ( lambda, a, 1.0);
63     }
64   return P;
65 }
66
67 /*
68  * Pr ( X > k ) for a Possion random variable X.
69  */
70 double
71 gslextras_cdf_poisson_Q (const long k, const double lambda)
72 {
73   double P;
74   double a;
75   
76   if ( lambda <= 0.0 )
77     {
78       GSLEXTRAS_CDF_ERROR ("lambda <= 0", GSL_EDOM);
79     }
80   if ( k < 0 )
81     {
82       P = 1.0;
83     }
84   else
85     {
86       a = (double) k+1;
87       P = gsl_cdf_gamma_P ( lambda, a, 1.0);
88     }
89   return P;
90 }
91