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

# NAG Library Routine DocumentG01TFF

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

G01TFF returns a number of deviates associated with given probabilities of the gamma distribution.

## 2  Specification

 SUBROUTINE G01TFF ( LTAIL, TAIL, LP, P, LA, A, LB, B, TOL, G, IVALID, IFAIL)
 INTEGER LTAIL, LP, LA, LB, IVALID(*), IFAIL REAL (KIND=nag_wp) P(LP), A(LA), B(LB), TOL, G(*) CHARACTER(1) TAIL(LTAIL)

## 3  Description

The deviate, ${g}_{{p}_{i}}$, associated with the lower tail probability, ${p}_{i}$, of the gamma distribution with shape parameter ${\alpha }_{i}$ and scale parameter ${\beta }_{i}$, is defined as the solution to
 $P Gi ≤ gpi :αi,βi = pi = 1 βi αi Γ αi ∫ 0 gpi ei - Gi / βi Gi αi-1 dGi , 0 ≤ gpi < ∞ ; ​ αi , βi > 0 .$
The method used is described by Best and Roberts (1975) making use of the relationship between the gamma distribution and the ${\chi }^{2}$-distribution.
Let ${y}_{i}=2\frac{{g}_{{p}_{i}}}{{\beta }_{i}}$. The required ${y}_{i}$ is found from the Taylor series expansion
 $yi=y0+∑rCry0 r! Eiϕy0 r,$
where ${y}_{0}$ is a starting approximation
• ${C}_{1}\left({u}_{i}\right)=1$,
• ${C}_{r+1}\left({u}_{i}\right)=\left(r\Psi +\frac{d}{d{u}_{i}}\right){C}_{r}\left({u}_{i}\right)$,
• ${\Psi }_{i}=\frac{1}{2}-\frac{{\alpha }_{i}-1}{{u}_{i}}$,
• ${E}_{i}={p}_{i}-\underset{0}{\overset{{y}_{0}}{\int }}{\varphi }_{i}\left({u}_{i}\right)d{u}_{i}$,
• ${\varphi }_{i}\left({u}_{i}\right)=\frac{1}{{2}^{{\alpha }_{i}}\Gamma \left({\alpha }_{i}\right)}{{e}_{i}}^{-{u}_{i}/2}{{u}_{i}}^{{\alpha }_{i}-1}$.
For most values of ${p}_{i}$ and ${\alpha }_{i}$ the starting value
 $y01=2αi zi⁢19αi +1-19αi 3$
is used, where ${z}_{i}$ is the deviate associated with a lower tail probability of ${p}_{i}$ for the standard Normal distribution.
For ${p}_{i}$ close to zero,
 $y02= piαi2αiΓ αi 1/αi$
is used.
For large ${p}_{i}$ values, when ${y}_{01}>4.4{\alpha }_{i}+6.0$,
 $y03=-2ln1-pi-αi-1ln12y01+lnΓ αi$
is found to be a better starting value than ${y}_{01}$.
For small ${\alpha }_{i}$ $\left({\alpha }_{i}\le 0.16\right)$, ${p}_{i}$ is expressed in terms of an approximation to the exponential integral and ${y}_{04}$ is found by Newton–Raphson iterations.
Seven terms of the Taylor series are used to refine the starting approximation, repeating the process if necessary until the required accuracy is obtained.
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

Best D J and Roberts D E (1975) Algorithm AS 91. The percentage points of the ${\chi }^{2}$ distribution Appl. Statist. 24 385–388

## 5  Parameters

1:     LTAIL – INTEGERInput
On entry: the length of the array TAIL.
Constraint: ${\mathbf{LTAIL}}>0$.
2:     TAIL(LTAIL) – CHARACTER(1) arrayInput
On entry: indicates which tail the supplied probabilities represent. For , for $\mathit{i}=1,2,\dots ,\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{LTAIL}},{\mathbf{LP}},{\mathbf{LA}},{\mathbf{LB}}\right)$:
${\mathbf{TAIL}}\left(j\right)=\text{'L'}$
The lower tail probability, i.e., ${p}_{i}=P\left({G}_{i}\le {g}_{{p}_{i}}:{\alpha }_{i},{\beta }_{i}\right)$.
${\mathbf{TAIL}}\left(j\right)=\text{'U'}$
The upper tail probability, i.e., ${p}_{i}=P\left({G}_{i}\ge {g}_{{p}_{i}}:{\alpha }_{i},{\beta }_{i}\right)$.
Constraint: ${\mathbf{TAIL}}\left(\mathit{j}\right)=\text{'L'}$ or $\text{'U'}$, for $\mathit{j}=1,2,\dots ,{\mathbf{LTAIL}}$.
3:     LP – INTEGERInput
On entry: the length of the array P.
Constraint: ${\mathbf{LP}}>0$.
4:     P(LP) – REAL (KIND=nag_wp) arrayInput
On entry: ${p}_{i}$, the probability of the required gamma distribution as defined by TAIL with ${p}_{i}={\mathbf{P}}\left(j\right)$, .
Constraints:
• if ${\mathbf{TAIL}}\left(k\right)=\text{'L'}$, $0.0\le {\mathbf{P}}\left(\mathit{j}\right)<1.0$;
• otherwise $0.0<{\mathbf{P}}\left(\mathit{j}\right)\le 1.0$.
Where  and .
5:     LA – INTEGERInput
On entry: the length of the array A.
Constraint: ${\mathbf{LA}}>0$.
6:     A(LA) – REAL (KIND=nag_wp) arrayInput
On entry: ${\alpha }_{i}$, the first parameter of the required gamma distribution with ${\alpha }_{i}={\mathbf{A}}\left(j\right)$, .
Constraint: $0.0<{\mathbf{A}}\left(\mathit{j}\right)\le {10}^{6}$, for $\mathit{j}=1,2,\dots ,{\mathbf{LA}}$.
7:     LB – INTEGERInput
On entry: the length of the array B.
Constraint: ${\mathbf{LB}}>0$.
8:     B(LB) – REAL (KIND=nag_wp) arrayInput
On entry: ${\beta }_{i}$, the second parameter of the required gamma distribution 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}}$.
9:     TOL – REAL (KIND=nag_wp)Input
On entry: the relative accuracy required by you in the results. If G01TFF is entered with TOL greater than or equal to $1.0$ or less than  (see X02AJF), then the value of  is used instead.
10:   G($*$) – REAL (KIND=nag_wp) arrayOutput
Note: the dimension of the array G must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{LTAIL}},{\mathbf{LP}},{\mathbf{LA}},{\mathbf{LB}}\right)$.
On exit: ${g}_{{p}_{i}}$, the deviates for the gamma distribution.
11:   IVALID($*$) – INTEGER arrayOutput
Note: the dimension of the array IVALID must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{LTAIL}},{\mathbf{LP}},{\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$
 On entry, invalid value supplied in TAIL when calculating ${g}_{{p}_{i}}$.
${\mathbf{IVALID}}\left(i\right)=2$
 On entry, invalid value for ${p}_{i}$.
${\mathbf{IVALID}}\left(i\right)=3$
 On entry, ${\alpha }_{i}\le 0.0$, or ${\alpha }_{i}>{10}^{6}$, or ${\beta }_{i}\le 0.0$.
${\mathbf{IVALID}}\left(i\right)=4$
${p}_{i}$ is too close to $0.0$ or $1.0$ to enable the result to be calculated.
${\mathbf{IVALID}}\left(i\right)=5$
The solution has failed to converge. The result may be a reasonable approximation.
12:   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, because for this routine the values of the output parameters may be useful even if ${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}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).
Note: G01TFF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
On entry, at least one value of TAIL, P, A, or B was invalid.
Check IVALID for more information.
${\mathbf{IFAIL}}=2$
On entry, $\text{array size}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{LTAIL}}>0$.
${\mathbf{IFAIL}}=3$
On entry, $\text{array size}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{LP}}>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$.
${\mathbf{IFAIL}}=-999$
Dynamic memory allocation failed.

## 7  Accuracy

In most cases the relative accuracy of the results should be as specified by TOL. However, for very small values of ${\alpha }_{i}$ or very small values of ${p}_{i}$ there may be some loss of accuracy.

None.

## 9  Example

This example reads lower tail probabilities for several gamma distributions, and calculates and prints the corresponding deviates until the end of data is reached.

### 9.1  Program Text

Program Text (g01tffe.f90)

### 9.2  Program Data

Program Data (g01tffe.d)

### 9.3  Program Results

Program Results (g01tffe.r)

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