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_prob_gamma_vector (g01sf)

## Purpose

nag_stat_prob_gamma_vector (g01sf) returns a number of lower or upper tail probabilities for the gamma distribution.

## Syntax

[p, ivalid, ifail] = g01sf(tail, g, a, b, 'ltail', ltail, 'lg', lg, 'la', la, 'lb', lb)
[p, ivalid, ifail] = nag_stat_prob_gamma_vector(tail, g, a, b, 'ltail', ltail, 'lg', lg, 'la', la, 'lb', lb)

## Description

The lower tail probability for the gamma distribution with parameters ${\alpha }_{i}$ and ${\beta }_{i}$, $P\left({G}_{i}\le {g}_{i}\right)$, is defined by:
 $P Gi ≤ gi :αi,βi = 1 βi αi Γ αi ∫ 0 gi Gi αi-1 e -Gi/βi dGi , αi>0.0 , ​ βi>0.0 .$
The mean of the distribution is ${\alpha }_{i}{\beta }_{i}$ and its variance is ${\alpha }_{i}{{\beta }_{i}}^{2}$. The transformation ${Z}_{i}=\frac{{G}_{i}}{{\beta }_{i}}$ is applied to yield the following incomplete gamma function in normalized form,
 $P Gi ≤ gi :αi,βi = P Zi ≤ gi / βi :αi,1.0 = 1 Γ αi ∫ 0 gi / βi Zi αi-1 e -Zi dZi .$
This is then evaluated using nag_specfun_gamma_incomplete (s14ba).
The input arrays to this function are designed to allow maximum flexibility in the supply of vector arguments by re-using elements of any arrays that are shorter than the total number of evaluations required. See Vectorized Routines in the G01 Chapter Introduction for further information.

## References

Hastings N A J and Peacock J B (1975) Statistical Distributions Butterworth

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{tail}\left({\mathbf{ltail}}\right)$ – cell array of strings
Indicates whether a lower or upper tail probability is required. For , for $\mathit{i}=1,2,\dots ,\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ltail}},{\mathbf{lg}},{\mathbf{la}},{\mathbf{lb}}\right)$:
${\mathbf{tail}}\left(j\right)=\text{'L'}$
The lower tail probability is returned, i.e., ${p}_{i}=P\left({G}_{i}\le {g}_{i}:{\alpha }_{i},{\beta }_{i}\right)$.
${\mathbf{tail}}\left(j\right)=\text{'U'}$
The upper tail probability is returned, i.e., ${p}_{i}=P\left({G}_{i}\ge {g}_{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}}$.
2:     $\mathrm{g}\left({\mathbf{lg}}\right)$ – double array
${g}_{i}$, the value of the gamma variate with ${g}_{i}={\mathbf{g}}\left(j\right)$, .
Constraint: ${\mathbf{g}}\left(\mathit{j}\right)\ge 0.0$, for $\mathit{j}=1,2,\dots ,{\mathbf{lg}}$.
3:     $\mathrm{a}\left({\mathbf{la}}\right)$ – double array
The parameter ${\alpha }_{i}$ of the gamma distribution 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}}$.
4:     $\mathrm{b}\left({\mathbf{lb}}\right)$ – double array
The parameter ${\beta }_{i}$ of the 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}}$.

### Optional Input Parameters

1:     $\mathrm{ltail}$int64int32nag_int scalar
Default: the dimension of the array tail.
The length of the array tail.
Constraint: ${\mathbf{ltail}}>0$.
2:     $\mathrm{lg}$int64int32nag_int scalar
Default: the dimension of the array g.
The length of the array g.
Constraint: ${\mathbf{lg}}>0$.
3:     $\mathrm{la}$int64int32nag_int scalar
Default: the dimension of the array a.
The length of the array a.
Constraint: ${\mathbf{la}}>0$.
4:     $\mathrm{lb}$int64int32nag_int scalar
Default: the dimension of the array b.
The length of the array b.
Constraint: ${\mathbf{lb}}>0$.

### Output Parameters

1:     $\mathrm{p}\left(:\right)$ – double array
The dimension of the array p will be $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{lg}},{\mathbf{la}},{\mathbf{lb}},{\mathbf{ltail}}\right)$
${p}_{i}$, the probabilities of the beta distribution.
2:     $\mathrm{ivalid}\left(:\right)$int64int32nag_int array
The dimension of the array ivalid will be $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{lg}},{\mathbf{la}},{\mathbf{lb}},{\mathbf{ltail}}\right)$
${\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 ${p}_{i}$.
${\mathbf{ivalid}}\left(i\right)=2$
 On entry, ${g}_{i}<0.0$.
${\mathbf{ivalid}}\left(i\right)=3$
 On entry, ${\alpha }_{i}\le 0.0$, or ${\beta }_{i}\le 0.0$.
${\mathbf{ivalid}}\left(i\right)=4$
The solution did not converge in $600$ iterations, see nag_specfun_gamma_incomplete (s14ba) for details. The probability returned should be a reasonable approximation to the solution.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:

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

W  ${\mathbf{ifail}}=1$
On entry, at least one value of g, a, b or tail was invalid, or the solution did not converge.
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{ltail}}>0$.
${\mathbf{ifail}}=3$
Constraint: ${\mathbf{lg}}>0$.
${\mathbf{ifail}}=4$
Constraint: ${\mathbf{la}}>0$.
${\mathbf{ifail}}=5$
Constraint: ${\mathbf{lb}}>0$.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

The result should have a relative accuracy of machine precision. There are rare occasions when the relative accuracy attained is somewhat less than machine precision but the error should not exceed more than $1$ or $2$ decimal places.

The time taken by nag_stat_prob_gamma_vector (g01sf) to calculate each probability varies slightly with the input arguments ${g}_{i}$, ${\alpha }_{i}$ and ${\beta }_{i}$.

## Example

This example reads in values from a number of gamma distributions and computes the associated lower tail probabilities.
```function g01sf_example

fprintf('g01sf example results\n\n');

tail = {'L'};
g = [15.5; 0.5; 10; 5];
a = [4; 4; 1; 2];
b = [2; 1; 2; 2];
% calculate probability
[p, ivalid, ifail] = g01sf(tail, g, a, b);

fprintf('Gamma deviate    Alpha     Beta    Probability\n');
lg    = numel(g);
la    = numel(a);
lb    = numel(b);
ltail = numel(tail);
len   = max ([lg, la, lb, ltail]);
for i=0:len-1
fprintf('%9.2f%13.2f%9.2f%10.4f\n', g(mod(i,lg)+1), a(mod(i,la)+1), ...
b(mod(i,lb)+1), p(i+1));
end

```
```g01sf example results

Gamma deviate    Alpha     Beta    Probability
15.50         4.00     2.00    0.9499
0.50         4.00     1.00    0.0018
10.00         1.00     2.00    0.9933
5.00         2.00     2.00    0.7127
```