G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG01TCF

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

G01TCF returns a number of deviates associated with the given probabilities of the ${\chi }^{2}$-distribution with real degrees of freedom.

## 2  Specification

 SUBROUTINE G01TCF ( LTAIL, TAIL, LP, P, LDF, DF, X, IVALID, IFAIL)
 INTEGER LTAIL, LP, LDF, IVALID(*), IFAIL REAL (KIND=nag_wp) P(LP), DF(LDF), X(*) CHARACTER(1) TAIL(LTAIL)

## 3  Description

The deviate, ${x}_{{p}_{i}}$, associated with the lower tail probability ${p}_{i}$ of the ${\chi }^{2}$-distribution with ${\nu }_{i}$ degrees of freedom is defined as the solution to
 $P Xi ≤ xpi :νi = pi = 1 2 νi/2 Γ νi/2 ∫ 0 xpi e -Xi/2 Xi vi / 2 - 1 dXi , 0 ≤ xpi < ∞ ; ​ νi > 0 .$
The required ${x}_{{p}_{i}}$ is found by using the relationship between a ${\chi }^{2}$-distribution and a gamma distribution, i.e., a ${\chi }^{2}$-distribution with ${\nu }_{i}$ degrees of freedom is equal to a gamma distribution with scale parameter $2$ and shape parameter ${\nu }_{i}/2$.
For very large values of ${\nu }_{i}$, greater than ${10}^{5}$, Wilson and Hilferty's Normal approximation to the ${\chi }^{2}$ is used; see Kendall and Stuart (1969).
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
Hastings N A J and Peacock J B (1975) Statistical Distributions Butterworth
Kendall M G and Stuart A (1969) The Advanced Theory of Statistics (Volume 1) (3rd Edition) Griffin

## 5  Parameters

1:     $\mathrm{LTAIL}$ – INTEGERInput
On entry: the length of the array TAIL.
Constraint: ${\mathbf{LTAIL}}>0$.
2:     $\mathrm{TAIL}\left({\mathbf{LTAIL}}\right)$ – 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{LDF}}\right)$:
${\mathbf{TAIL}}\left(j\right)=\text{'L'}$
The lower tail probability, i.e., ${p}_{i}=P\left({X}_{i}\le {x}_{{p}_{i}}:{\nu }_{i}\right)$.
${\mathbf{TAIL}}\left(j\right)=\text{'U'}$
The upper tail probability, i.e., ${p}_{i}=P\left({X}_{i}\ge {x}_{{p}_{i}}:{\nu }_{i}\right)$.
Constraint: ${\mathbf{TAIL}}\left(\mathit{j}\right)=\text{'L'}$ or $\text{'U'}$, for $\mathit{j}=1,2,\dots ,{\mathbf{LTAIL}}$.
3:     $\mathrm{LP}$ – INTEGERInput
On entry: the length of the array P.
Constraint: ${\mathbf{LP}}>0$.
4:     $\mathrm{P}\left({\mathbf{LP}}\right)$ – REAL (KIND=nag_wp) arrayInput
On entry: ${p}_{i}$, the probability of the required ${\chi }^{2}$-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:     $\mathrm{LDF}$ – INTEGERInput
On entry: the length of the array DF.
Constraint: ${\mathbf{LDF}}>0$.
6:     $\mathrm{DF}\left({\mathbf{LDF}}\right)$ – REAL (KIND=nag_wp) arrayInput
On entry: ${\nu }_{i}$, the degrees of freedom of the ${\chi }^{2}$-distribution with ${\nu }_{i}={\mathbf{DF}}\left(j\right)$, .
Constraint: ${\mathbf{DF}}\left(\mathit{j}\right)>0.0$, for $\mathit{j}=1,2,\dots ,{\mathbf{LDF}}$.
7:     $\mathrm{X}\left(*\right)$ – REAL (KIND=nag_wp) arrayOutput
Note: the dimension of the array X must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{LTAIL}},{\mathbf{LP}},{\mathbf{LDF}}\right)$.
On exit: ${x}_{{p}_{i}}$, the deviates for the ${\chi }^{2}$-distribution.
8:     $\mathrm{IVALID}\left(*\right)$ – 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{LDF}}\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 ${x}_{{p}_{i}}$.
${\mathbf{IVALID}}\left(i\right)=2$
 On entry, invalid value for ${p}_{i}$.
${\mathbf{IVALID}}\left(i\right)=3$
 On entry, ${\nu }_{i}\le 0.0$.
${\mathbf{IVALID}}\left(i\right)=4$
${p}_{i}$ is too close to $0.0$ or $1.0$ for the result to be calculated.
${\mathbf{IVALID}}\left(i\right)=5$
The solution has failed to converge. The result should be a reasonable approximation.
9:     $\mathrm{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 TAIL, P or DF was invalid, or the solution failed to converge.
${\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{LDF}}>0$.
${\mathbf{IFAIL}}=-99$
See Section 3.8 in the Essential Introduction for further information.
${\mathbf{IFAIL}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
${\mathbf{IFAIL}}=-999$
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

## 7  Accuracy

The results should be accurate to five significant digits for most parameter values. Some accuracy is lost for ${p}_{i}$ close to $0.0$ or $1.0$.

## 8  Parallelism and Performance

Not applicable.

For higher accuracy the relationship described in Section 3 may be used and a direct call to G01TFF made.

## 10  Example

This example reads lower tail probabilities for several ${\chi }^{2}$-distributions, and calculates and prints the corresponding deviates.

### 10.1  Program Text

Program Text (g01tcfe.f90)

### 10.2  Program Data

Program Data (g01tcfe.d)

### 10.3  Program Results

Program Results (g01tcfe.r)