# NAG FL Interfaceg01blf (prob_​hypergeom)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

g01blf returns the lower tail, upper tail and point probabilities associated with a hypergeometric distribution.

## 2Specification

Fortran Interface
 Subroutine g01blf ( n, l, m, k, plek, pgtk, peqk,
 Integer, Intent (In) :: n, l, m, k Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (Out) :: plek, pgtk, peqk
#include <nag.h>
 void g01blf_ (const Integer *n, const Integer *l, const Integer *m, const Integer *k, double *plek, double *pgtk, double *peqk, Integer *ifail)
The routine may be called by the names g01blf or nagf_stat_prob_hypergeom.

## 3Description

Let $X$ denote a random variable having a hypergeometric distribution with parameters $n$, $l$ and $m$ ($n\ge l\ge 0$, $n\ge m\ge 0$). Then
 $Prob{X=k}= ( m k ) ( n-m l-k ) ( n l ) ,$
where $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(0,l-\left(n-m\right)\right)\le k\le \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(l,m\right)$, $0\le l\le n$ and $0\le m\le n$.
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 $\text{}=\frac{lm}{n}$, and the variance $\text{}=\frac{lm\left(n-l\right)\left(n-m\right)}{{n}^{2}\left(n-1\right)}$.
g01blf computes for given $n$, $l$, $m$ and $k$ the probabilities:
 $plek=Prob{X≤k} pgtk=Prob{X>k} peqk=Prob{X=k} .$
The method is similar to the method for the Poisson distribution described in Knüsel (1986).

## 4References

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

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: the parameter $n$ of the hypergeometric distribution.
Constraint: ${\mathbf{n}}\ge 0$.
2: $\mathbf{l}$Integer Input
On entry: the parameter $l$ of the hypergeometric distribution.
Constraint: $0\le {\mathbf{l}}\le {\mathbf{n}}$.
3: $\mathbf{m}$Integer Input
On entry: the parameter $m$ of the hypergeometric distribution.
Constraint: $0\le {\mathbf{m}}\le {\mathbf{n}}$.
4: $\mathbf{k}$Integer Input
On entry: the integer $k$ which defines the required probabilities.
Constraint: $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(0,{\mathbf{l}}-\left({\mathbf{n}}-{\mathbf{m}}\right)\right)\le {\mathbf{k}}\le \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{l}},{\mathbf{m}}\right)$.
5: $\mathbf{plek}$Real (Kind=nag_wp) Output
On exit: the lower tail probability, $\mathrm{Prob}\left\{X\le k\right\}$.
6: $\mathbf{pgtk}$Real (Kind=nag_wp) Output
On exit: the upper tail probability, $\mathrm{Prob}\left\{X>k\right\}$.
7: $\mathbf{peqk}$Real (Kind=nag_wp) Output
On exit: the point probability, $\mathrm{Prob}\left\{X=k\right\}$.
8: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 0$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{l}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{l}}\ge 0$.
On entry, ${\mathbf{l}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{l}}\le {\mathbf{n}}$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}\ge 0$.
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}\le {\mathbf{n}}$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{k}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{l}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{l}}+{\mathbf{m}}-{\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{k}}\ge {\mathbf{l}}+{\mathbf{m}}-{\mathbf{n}}$.
On entry, ${\mathbf{k}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{k}}\ge 0$.
On entry, ${\mathbf{k}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{l}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{k}}\le {\mathbf{l}}$.
On entry, ${\mathbf{k}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{k}}\le {\mathbf{m}}$.
${\mathbf{ifail}}=5$
On entry, n is too large to be represented exactly as a double precision number.
${\mathbf{ifail}}=6$
On entry, the variance $\text{}=\frac{lm\left(n-l\right)\left(n-m\right)}{{n}^{2}\left(n-1\right)}$ exceeds ${10}^{6}$.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

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).

## 8Parallelism and Performance

g01blf is not threaded in any implementation.

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

## 10Example

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.

### 10.1Program Text

Program Text (g01blfe.f90)

### 10.2Program Data

Program Data (g01blfe.d)

### 10.3Program Results

Program Results (g01blfe.r)