Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_stat_inv_cdf_gamma (g01ff)

## Purpose

nag_stat_inv_cdf_gamma (g01ff) returns the deviate associated with the given lower tail probability of the gamma distribution.

## Syntax

[result, ifail] = g01ff(p, a, b, 'tol', tol)
[result, ifail] = nag_stat_inv_cdf_gamma(p, a, b, 'tol', tol)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 23: tol now optional (default 0)
.

## Description

The deviate, gp${g}_{p}$, associated with the lower tail probability, p$p$, of the gamma distribution with shape parameter α$\alpha$ and scale parameter β$\beta$, is defined as the solution to
 gp P(G ≤ gp : α,β) = p = 1/(βαΓ(α)) ∫ e − G / βGα − 1dG,  0 ≤ gp < ∞;α,β > 0. 0
$P(G≤gp:α,β)=p=1βαΓ(α) ∫0gpe-G/βGα-1dG, 0≤gp<∞;α,β>0.$
The method used is described by Best and Roberts (1975) making use of the relationship between the gamma distribution and the χ2${\chi }^{2}$-distribution.
Let y = 2(gp)/β $y=2\frac{{g}_{p}}{\beta }$. The required y$y$ is found from the Taylor series expansion
 y = y0 + ∑r(Cr(y0))/(r ! ) (E/(φ(y0)))r, $y=y0+∑rCr(y0) r! (Eϕ(y0) ) r,$
where y0${y}_{0}$ is a starting approximation
• C1(u) = 1${C}_{1}\left(u\right)=1$,
• Cr + 1(u) = (rΨ + d/(du)) Cr(u)${C}_{r+1}\left(u\right)=\left(r\Psi +\frac{d}{du}\right){C}_{r}\left(u\right)$,
• Ψ = (1/2)(α1)/u $\Psi =\frac{1}{2}-\frac{\alpha -1}{u}$,
• E = p0y0φ(u)du$E=p-\underset{0}{\overset{{y}_{0}}{\int }}\varphi \left(u\right)du$,
• φ(u) = 1/(2αΓ (α) )eu / 2uα1$\varphi \left(u\right)=\frac{1}{{2}^{\alpha }\Gamma \left(\alpha \right)}{e}^{-u/2}{u}^{\alpha -1}$.
For most values of p$p$ and α$\alpha$ the starting value
 y01 = 2α (z×sqrt(1/(9α)) + 1 − 1/(9α))3 $y01=2α (z⁢19α +1-19α ) 3$
is used, where z$z$ is the deviate associated with a lower tail probability of p$p$ for the standard Normal distribution.
For p$p$ close to zero,
 y02 = (pα2αΓ(α))1 / α $y02= (pα2αΓ (α) ) 1/α$
is used.
For large p$p$ values, when y01 > 4.4α + 6.0${y}_{01}>4.4\alpha +6.0$,
 y03 = − 2[ln(1 − p) − (α − 1)ln((1/2)y01) + ln(Γ(α))] $y03=-2[ln(1-p)-(α-1)ln(12y01)+ln(Γ (α) ) ]$
is found to be a better starting value than y01${y}_{01}$.
For small α$\alpha$ (α0.16)$\left(\alpha \le 0.16\right)$, p$p$ is expressed in terms of an approximation to the exponential integral and y04${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.

## References

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

## Parameters

### Compulsory Input Parameters

1:     p – double scalar
p$p$, the lower tail probability from the required gamma distribution.
Constraint: 0.0p < 1.0$0.0\le {\mathbf{p}}<1.0$.
2:     a – double scalar
α$\alpha$, the shape parameter of the gamma distribution.
Constraint: 0.0 < a106$0.0<{\mathbf{a}}\le {10}^{6}$.
3:     b – double scalar
β$\beta$, the scale parameter of the gamma distribution.
Constraint: b > 0.0${\mathbf{b}}>0.0$.

### Optional Input Parameters

1:     tol – double scalar
The relative accuracy required by you in the results. The smallest recommended value is 50 × δ$50×\delta$, where δ = max (1018,machine precision). If nag_stat_inv_cdf_gamma (g01ff) is entered with tol less than 50 × δ$50×\delta$ or greater or equal to 1.0$1.0$, then 50 × δ$50×\delta$ is used instead.
Default: 0.0$0.0$

None.

### Output Parameters

1:     result – double scalar
The result of the function.
2:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Note: nag_stat_inv_cdf_gamma (g01ff) may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the function:
If on exit ${\mathbf{ifail}}={\mathbf{1}}$, 2${\mathbf{2}}$, 3${\mathbf{3}}$ or 5${\mathbf{5}}$, then nag_stat_inv_cdf_gamma (g01ff) returns 0.0$0.0$.

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

ifail = 1${\mathbf{ifail}}=1$
 On entry, p < 0.0${\mathbf{p}}<0.0$, or p ≥ 1.0${\mathbf{p}}\ge 1.0$,
ifail = 2${\mathbf{ifail}}=2$
 On entry, a ≤ 0.0${\mathbf{a}}\le 0.0$, or a > 106${\mathbf{a}}>{10}^{6}$, or b ≤ 0.0${\mathbf{b}}\le 0.0$
ifail = 3${\mathbf{ifail}}=3$
p is too close to 0.0$0.0$ or 1.0$1.0$ to enable the result to be calculated.
W ifail = 4${\mathbf{ifail}}=4$
The solution has failed to converge in 100$100$ iterations. A larger value of tol should be tried. The result may be a reasonable approximation.
ifail = 5${\mathbf{ifail}}=5$
The series to calculate the gamma function has failed to converge. This is an unlikely error exit.

## Accuracy

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

None.

## Example

```function nag_stat_inv_cdf_gamma_example
p = 0.01;
a = 1;
b = 20;
[result, ifail] = nag_stat_inv_cdf_gamma(p, a, b)
```
```

result =

0.2010

ifail =

0

```
```function g01ff_example
p = 0.01;
a = 1;
b = 20;
[result, ifail] = g01ff(p, a, b)
```
```

result =

0.2010

ifail =

0

```