G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG01TEF

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

G01TEF returns a number of deviates associated with given probabilities of the beta distribution.

## 2  Specification

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

## 3  Description

The deviate, ${\beta }_{{p}_{i}}$, associated with the lower tail probability, ${p}_{i}$, of the beta distribution with parameters ${a}_{i}$ and ${b}_{i}$ is defined as the solution to
 $P Bi ≤ βpi :ai,bi = pi = Γ ai + bi Γ ai Γ bi ∫ 0 βpi Bi ai-1 1-Bi bi-1 d Bi , 0 ≤ β pi ≤ 1 ; ​ ai , bi > 0 .$
The algorithm is a modified version of the Newton–Raphson method, following closely that of Cran et al. (1977).
An initial approximation, ${\beta }_{i0}$, to ${\beta }_{{p}_{i}}$ is found (see Cran et al. (1977)), and the Newton–Raphson iteration
 $βk = βk-1 - fi βk-1 fi′ βk-1 ,$
where ${f}_{i}\left({\beta }_{k}\right)=P\left({B}_{i}\le {\beta }_{k}:{a}_{i},{b}_{i}\right)-{p}_{i}$ is used, with modifications to ensure that ${\beta }_{k}$ remains in the range $\left(0,1\right)$.
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

Cran G W, Martin K J and Thomas G E (1977) Algorithm AS 109. Inverse of the incomplete beta function ratio Appl. Statist. 26 111–114
Hastings N A J and Peacock J B (1975) Statistical Distributions Butterworth

## 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({B}_{i}\le {\beta }_{{p}_{i}}:{a}_{i},{b}_{i}\right)$.
${\mathbf{TAIL}}\left(j\right)=\text{'U'}$
The upper tail probability, i.e., ${p}_{i}=P\left({B}_{i}\ge {\beta }_{{p}_{i}}:{a}_{i},{b}_{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 beta distribution as defined by TAIL with ${p}_{i}={\mathbf{P}}\left(j\right)$, .
Constraint: $0.0\le {\mathbf{P}}\left(\mathit{j}\right)\le 1.0$, for $\mathit{j}=1,2,\dots ,{\mathbf{LP}}$.
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: ${a}_{i}$, the first parameter of the required beta distribution with ${a}_{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: ${b}_{i}$, the second parameter of the required beta distribution with ${b}_{i}={\mathbf{B}}\left(j\right)$, .
Constraint: $0.0<{\mathbf{B}}\left(\mathit{j}\right)\le {10}^{6}$, 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 G01TEF is entered with TOL greater than or equal to $1.0$ or less than  (see X02AJF), then the value of  is used instead.
10:   BETA($*$) – REAL (KIND=nag_wp) arrayOutput
Note: the dimension of the array BETA must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{LTAIL}},{\mathbf{LP}},{\mathbf{LA}},{\mathbf{LB}}\right)$.
On exit: ${\beta }_{{p}_{i}}$, the deviates for the beta 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 ${\beta }_{{p}_{i}}$.
${\mathbf{IVALID}}\left(i\right)=2$
 On entry, ${p}_{i}<0.0$, or ${p}_{i}>1.0$.
${\mathbf{IVALID}}\left(i\right)=3$
 On entry, ${a}_{i}\le 0.0$, or ${a}_{i}>{10}^{6}$, or ${b}_{i}\le 0.0$, or ${b}_{i}>{10}^{6}$.
${\mathbf{IVALID}}\left(i\right)=4$
The solution has not converged but the result should be a reasonable approximation to the solution.
${\mathbf{IVALID}}\left(i\right)=5$
Requested accuracy not achieved when calculating the beta probability. The result should be a reasonable approximation to the correct solution.
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: G01TEF 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, 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{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

The required precision, given by TOL, should be achieved in most circumstances.

## 8  Further Comments

The typical timing will be several times that of G01SEF and will be very dependent on the input parameter values. See G01SEF for further comments on timings.

## 9  Example

This example reads lower tail probabilities for several beta distributions and calculates and prints the corresponding deviates.

### 9.1  Program Text

Program Text (g01tefe.f90)

### 9.2  Program Data

Program Data (g01tefe.d)

### 9.3  Program Results

Program Results (g01tefe.r)