1 /* cdf/hypergeometric.c
3 * Copyright (C) 2004 Jason H. Stover.
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.
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.
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.
21 * Computes the cumulative distribution function for a hypergeometric
22 * random variable. A hypergeometric random variable X is the number
23 * of elements of type 1 in a sample of size t, drawn from a population
24 * of size n1 + n2, in which n1 are of type 1 and n2 are of type 2.
26 * This algorithm computes Pr( X <= k ) by summing the terms from
27 * the mass function, Pr( X = k ).
31 * T. Wu. An accurate computation of the hypergeometric distribution
32 * function. ACM Transactions on Mathematical Software. Volume 19, number 1,
34 * This algorithm is not used, since it requires factoring the
35 * numerator and denominator, then cancelling. It is more accurate
36 * than the algorithm used here, but the cancellation requires more
37 * time than the algorithm used here.
39 * W. Feller. An Introduction to Probability Theory and Its Applications,
40 * third edition. 1968. Chapter 2, section 6.
45 #include <gsl/gsl_math.h>
46 #include <gsl/gsl_errno.h>
47 #include <gsl/gsl_cdf.h>
48 #include <gsl/gsl_randist.h>
53 lower_tail (const unsigned int k, const unsigned int n1,
54 const unsigned int n2, const unsigned int t)
60 s = gsl_ran_hypergeometric_pdf (i, n1, n2, t);
66 (i / (n1 - i + 1.0)) * ((n2 + i - t) / (t - i + 1.0));
70 if (relerr < GSL_DBL_EPSILON)
79 upper_tail (const unsigned int k, const unsigned int n1,
80 const unsigned int n2, const unsigned int t)
83 unsigned int i = k + 1;
86 s = gsl_ran_hypergeometric_pdf (i, n1, n2, t);
92 ((n1 - i) / (i + 1.0)) * ((t - i) / (n2 + i + 1.0 - t));
96 if (relerr < GSL_DBL_EPSILON)
111 gsl_cdf_hypergeometric_P (const unsigned int k,
112 const unsigned int n1,
113 const unsigned int n2, const unsigned int t)
119 CDF_ERROR ("t larger than population size", GSL_EDOM);
121 else if (k >= n1 || k >= t)
131 double midpoint = ((double)t * n1) / ((double)n1 + (double)n2);
135 P = 1 - upper_tail (k, n1, n2, t);
139 P = lower_tail (k, n1, n2, t);
150 gsl_cdf_hypergeometric_Q (const unsigned int k,
151 const unsigned int n1,
152 const unsigned int n2, const unsigned int t)
158 CDF_ERROR ("t larger than population size", GSL_EDOM);
160 else if (k >= n1 || k >= t)
170 double midpoint = ((double)t * n1) / ((double)n1 + (double)n2);
174 Q = 1 - lower_tail (k, n1, n2, t);
178 Q = upper_tail (k, n1, n2, t);