G01KKF (PDF version)
G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG01KKF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G01KKF returns a number of values of the probability density function (PDF), or its logarithm, for the gamma distribution.

## 2  Specification

 SUBROUTINE G01KKF ( ILOG, LX, X, LA, A, LB, B, PDF, IVALID, IFAIL)
 INTEGER ILOG, LX, LA, LB, IVALID(*), IFAIL REAL (KIND=nag_wp) X(LX), A(LA), B(LB), PDF(*)

## 3  Description

The gamma distribution with shape parameter ${\alpha }_{i}$ and scale parameter ${\beta }_{i}$ has PDF
 $f xi,αi,βi = 1 βi αi Γαi xi αi-1 e -xi / βi if ​ xi ≥ 0 ; αi , βi > 0 fxi,αi,βi=0 otherwise.$
If $0.01\le {x}_{i},{\alpha }_{i},{\beta }_{i}\le 100$ then an algorithm based directly on the gamma distribution's PDF is used. For values outside this range, the function is calculated via the Poisson distribution's PDF as described in Loader (2000) (see Section 8).
The input arrays to this routine are designed to allow maximum flexibility in the supply of vector parameters by re-using elements of any arrays that are shorter than the total number of evaluations required. See Section 2.6 in the G01 Chapter Introduction for further information.

## 4  References

Loader C (2000) Fast and accurate computation of binomial probabilities (not yet published)

## 5  Parameters

1:     ILOG – INTEGERInput
On entry: the value of ILOG determines whether the logarithmic value is returned in PDF.
${\mathbf{ILOG}}=0$
$f\left({x}_{i},{\alpha }_{i},{\beta }_{i}\right)$, the probability density function is returned.
${\mathbf{ILOG}}=1$
$\mathrm{log}\left(f\left({x}_{i},{\alpha }_{i},{\beta }_{i}\right)\right)$, the logarithm of the probability density function is returned.
Constraint: ${\mathbf{ILOG}}=0$ or $1$.
2:     LX – INTEGERInput
On entry: the length of the array X.
Constraint: ${\mathbf{LX}}>0$.
3:     X(LX) – REAL (KIND=nag_wp) arrayInput
On entry: ${x}_{i}$, the values at which the PDF is to be evaluated with ${x}_{i}={\mathbf{X}}\left(j\right)$, , for $i=1,2,\dots ,\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{LX}},{\mathbf{LA}},{\mathbf{LB}}\right)$.
4:     LA – INTEGERInput
On entry: the length of the array A.
Constraint: ${\mathbf{LA}}>0$.
5:     A(LA) – REAL (KIND=nag_wp) arrayInput
On entry: ${\alpha }_{i}$, the shape parameter with ${\alpha }_{i}={\mathbf{A}}\left(j\right)$, .
Constraint: ${\mathbf{A}}\left(\mathit{j}\right)>0.0$, for $\mathit{j}=1,2,\dots ,{\mathbf{LA}}$.
6:     LB – INTEGERInput
On entry: the length of the array B.
Constraint: ${\mathbf{LB}}>0$.
7:     B(LB) – REAL (KIND=nag_wp) arrayInput
On entry: ${\beta }_{i}$, the scale parameter with ${\beta }_{i}={\mathbf{B}}\left(j\right)$, .
Constraint: ${\mathbf{B}}\left(\mathit{j}\right)>0.0$, for $\mathit{j}=1,2,\dots ,{\mathbf{LB}}$.
8:     PDF($*$) – REAL (KIND=nag_wp) arrayOutput
Note: the dimension of the array PDF must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{LX}},{\mathbf{LA}},{\mathbf{LB}}\right)$.
On exit: $f\left({x}_{i},{\alpha }_{i},{\beta }_{i}\right)$ or $\mathrm{log}\left(f\left({x}_{i},{\alpha }_{i},{\beta }_{i}\right)\right)$.
9:     IVALID($*$) – INTEGER arrayOutput
Note: the dimension of the array IVALID must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{LX}},{\mathbf{LA}},{\mathbf{LB}}\right)$.
On exit: ${\mathbf{IVALID}}\left(i\right)$ indicates any errors with the input arguments, with
${\mathbf{IVALID}}\left(i\right)=0$
No error.
${\mathbf{IVALID}}\left(i\right)=1$
${\alpha }_{i}\le 0.0$.
${\mathbf{IVALID}}\left(i\right)=2$
${\beta }_{i}\le 0.0$.
${\mathbf{IVALID}}\left(i\right)=3$
$\frac{{x}_{i}}{{\beta }_{i}}$ overflows, the value returned should be a reasonable approximation.
10:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{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, at least one value of X, A or B was invalid.
Check IVALID for more information.
${\mathbf{IFAIL}}=2$
On entry, ${\mathbf{ILOG}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ILOG}}=0$ or $1$.
${\mathbf{IFAIL}}=3$
On entry, $\text{array size}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{LX}}>0$.
${\mathbf{IFAIL}}=4$
On entry, $\text{array size}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{LA}}>0$.
${\mathbf{IFAIL}}=5$
On entry, $\text{array size}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{LB}}>0$.

Not applicable.

## 8  Further Comments

Due to the lack of a stable link to Loader (2000) paper, we give a brief overview of the method, as applied to the Poisson distribution. The Poisson distribution has a continuous mass function given by,
 $px;λ = λx x! e-λ .$ (1)
The usual way of computing this quantity would be to take the logarithm and calculate,
 $log p x;λ = x log⁡λ - log x! - λ .$
For large $x$ and $\lambda$, $x\mathrm{log}\lambda$ and $\mathrm{log}\left(x!\right)$ are very large, of the same order of magnitude and when calculated have rounding errors. The subtraction of these two terms can therefore result in a number, many orders of magnitude smaller and hence we lose accuracy due to subtraction errors. For example for $x=2×{10}^{6}$ and $\lambda =2×{10}^{6}$, $\mathrm{log}\left(x!\right)\approx 2.7×{10}^{7}$ and $\mathrm{log}\left(p\left(x;\lambda \right)\right)=-8.17326744645834$. But calculated with the method shown later we have $\mathrm{log}\left(p\left(x;\lambda \right)\right)=-8.1732674441334492$. The difference between these two results suggests a loss of about 7 significant figures of precision.
Loader introduces an alternative way of expressing (1) based on the saddle point expansion,
 $log p x;λ = log p x;x - Dx;λ ,$ (2)
where $D\left(x;\lambda \right)$, the deviance for the Poisson distribution is given by,
 $Dx;λ = log p x;x - log p x;λ , = λ D0 x λ ,$ (3)
and
 $D0 ε = ε log⁡ε + 1 - ε .$
For $\epsilon$ close to $1$, ${D}_{0}\left(\epsilon \right)$ can be evaluated through the series expansion
 $λ D0 x λ = x-λ 2 x+λ + 2x ∑ j=1 ∞ v 2j+1 2j+1 , where ​ v = x-λ x+λ ,$
otherwise ${D}_{0}\left(\epsilon \right)$ can be evaluated directly. In addition, Loader suggests evaluating $\mathrm{log}\left(x!\right)$ using the Stirling–De Moivre series,
 $logx! = 12 log⁡ 2πx + x logx -x + δx ,$ (4)
where the error $\delta \left(x\right)$ is given by
 $δx = 112x - 1 360x3 + 1 1260x5 + O x-7 .$
Finally $\mathrm{log}\left(p\left(x;\lambda \right)\right)$ can be evaluated by combining equations (1)(4) to get,
 $p x;λ = 1 2πx e - δx - λ D0 x/λ .$

## 9  Example

This example prints the value of the gamma distribution PDF at six different points ${x}_{i}$ with differing ${\alpha }_{i}$ and ${\beta }_{i}$.

### 9.1  Program Text

Program Text (g01kkfe.f90)

### 9.2  Program Data

Program Data (g01kkfe.d)

### 9.3  Program Results

Program Results (g01kkfe.r)

G01KKF (PDF version)
G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual