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