NAG Library Function Document

nag_hypergeom_dist (g01blc)


    1  Purpose
    7  Accuracy


nag_hypergeom_dist (g01blc) returns the lower tail, upper tail and point probabilities associated with a hypergeometric distribution.


#include <nag.h>
#include <nagg01.h>
void  nag_hypergeom_dist (Integer n, Integer l, Integer m, Integer k, double *plek, double *pgtk, double *peqk, NagError *fail)


Let X denote a random variable having a hypergeometric distribution with parameters n, l and m (nl0, nm0). Then
ProbX=k= m k n-m l-k n l ,  
where max0,l-n-m k minl,m , 0ln and 0mn.
The hypergeometric distribution may arise if in a population of size n a number m are marked. From this population a sample of size l is drawn and of these k are observed to be marked.
The mean of the distribution = lm n , and the variance = lmn-ln-m n2n-1 .
nag_hypergeom_dist (g01blc) computes for given n, l, m and k the probabilities:
plek=ProbXk pgtk=ProbX>k peqk=ProbX=k .  
The method is similar to the method for the Poisson distribution described in Knüsel (1986).


Knüsel L (1986) Computation of the chi-square and Poisson distribution SIAM J. Sci. Statist. Comput. 7 1022–1036


1:     n IntegerInput
On entry: the parameter n of the hypergeometric distribution.
Constraint: n0.
2:     l IntegerInput
On entry: the parameter l of the hypergeometric distribution.
Constraint: 0ln.
3:     m IntegerInput
On entry: the parameter m of the hypergeometric distribution.
Constraint: 0mn.
4:     k IntegerInput
On entry: the integer k which defines the required probabilities.
Constraint: max0,l-n-mkminl,m.
5:     plek double *Output
On exit: the lower tail probability, ProbXk.
6:     pgtk double *Output
On exit: the upper tail probability, ProbX>k.
7:     peqk double *Output
On exit: the point probability, ProbX=k.
8:     fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

Error Indicators and Warnings

On entry, k=value and l=value.
Constraint: kl.
On entry, k=value and m=value.
Constraint: km.
On entry, l=value and n=value.
Constraint: ln.
On entry, m=value and n=value.
Constraint: mn.
On entry, k=value, l=value, m=value and l+m-n=value.
Constraint: kl+m-n.
Dynamic memory allocation failed.
See Section in How to Use the NAG Library and its Documentation for further information.
On entry, n is too large to be represented exactly as a double precision number.
On entry, argument value had an illegal value.
On entry, k=value.
Constraint: k0.
On entry, l=value.
Constraint: l0.
On entry, m=value.
Constraint: m0.
On entry, n=value.
Constraint: n0.
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
On entry, the variance = l m n-l n-m n2 n-1  exceeds 106.


Results are correct to a relative accuracy of at least 10-6 on machines with a precision of 9 or more decimal digits, and to a relative accuracy of at least 10-3 on machines of lower precision (provided that the results do not underflow to zero).

Parallelism and Performance

nag_hypergeom_dist (g01blc) is not threaded in any implementation.

Further Comments

The time taken by nag_hypergeom_dist (g01blc) depends on the variance (see Section 3) and on k. For given variance, the time is greatest when klm/n (= the mean), and is then approximately proportional to the square-root of the variance.


This example reads values of n, l, m and k from a data file until end-of-file is reached, and prints the corresponding probabilities.

Program Text

Program Text (g01blce.c)

Program Data

Program Data (g01blce.d)

Program Results

Program Results (g01blce.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017