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_pdf_gamma_vector (g01kk)

## Purpose

nag_stat_pdf_gamma_vector (g01kk) returns a number of values of the probability density function (PDF), or its logarithm, for the gamma distribution.

## Syntax

[pdf, ivalid, ifail] = g01kk(ilog, x, a, b, 'lx', lx, 'la', la, 'lb', lb)
[pdf, ivalid, ifail] = nag_stat_pdf_gamma_vector(ilog, x, a, b, 'lx', lx, 'la', la, 'lb', lb)

## Description

The gamma distribution with shape parameter ${\alpha }_{i}$ and scale parameter ${\beta }_{i}$ has PDF
 $f xi,αi,βi = 1 βi αi Γαi xi αi-1 e -xi / βi if ​ xi ≥ 0 ; αi , βi > 0 fxi,αi,βi=0 otherwise.$
If $0.01\le {x}_{i},{\alpha }_{i},{\beta }_{i}\le 100$ then an algorithm based directly on the gamma distribution's PDF is used. For values outside this range, the function is calculated via the Poisson distribution's PDF as described in Loader (2000) (see Further Comments).
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

Loader C (2000) Fast and accurate computation of binomial probabilities (not yet published)

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{ilog}$int64int32nag_int scalar
The value of ilog determines whether the logarithmic value is returned in pdf.
${\mathbf{ilog}}=0$
$f\left({x}_{i},{\alpha }_{i},{\beta }_{i}\right)$, the probability density function is returned.
${\mathbf{ilog}}=1$
$\mathrm{log}\left(f\left({x}_{i},{\alpha }_{i},{\beta }_{i}\right)\right)$, the logarithm of the probability density function is returned.
Constraint: ${\mathbf{ilog}}=0$ or $1$.
2:     $\mathrm{x}\left({\mathbf{lx}}\right)$ – double array
${x}_{i}$, the values at which the PDF is to be evaluated with ${x}_{i}={\mathbf{x}}\left(j\right)$, , for $i=1,2,\dots ,\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{lx}},{\mathbf{la}},{\mathbf{lb}}\right)$.
3:     $\mathrm{a}\left({\mathbf{la}}\right)$ – double array
${\alpha }_{i}$, the shape parameter 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
${\beta }_{i}$, the scale parameter 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{lx}$int64int32nag_int scalar
Default: the dimension of the array x.
The length of the array x.
Constraint: ${\mathbf{lx}}>0$.
2:     $\mathrm{la}$int64int32nag_int scalar
Default: the dimension of the array a.
The length of the array a.
Constraint: ${\mathbf{la}}>0$.
3:     $\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{pdf}\left(:\right)$ – double array
The dimension of the array pdf will be $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{lx}},{\mathbf{la}},{\mathbf{lb}}\right)$
$f\left({x}_{i},{\alpha }_{i},{\beta }_{i}\right)$ or $\mathrm{log}\left(f\left({x}_{i},{\alpha }_{i},{\beta }_{i}\right)\right)$.
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{lx}},{\mathbf{la}},{\mathbf{lb}}\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$
${\alpha }_{i}\le 0.0$.
${\mathbf{ivalid}}\left(i\right)=2$
${\beta }_{i}\le 0.0$.
${\mathbf{ivalid}}\left(i\right)=3$
$\frac{{x}_{i}}{{\beta }_{i}}$ overflows, the value returned should be a reasonable approximation.
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 x, a or b was invalid.
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{ilog}}=0$ or $1$.
${\mathbf{ifail}}=3$
Constraint: ${\mathbf{lx}}>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

Not applicable.

Due to the lack of a stable link to Loader (2000) paper, we give a brief overview of the method, as applied to the Poisson distribution. The Poisson distribution has a continuous mass function given by,
 $px;λ = λx x! e-λ .$ (1)
The usual way of computing this quantity would be to take the logarithm and calculate,
 $log p x;λ = x log⁡λ - log x! - λ .$
For large $x$ and $\lambda$, $x\mathrm{log}\lambda$ and $\mathrm{log}\left(x!\right)$ are very large, of the same order of magnitude and when calculated have rounding errors. The subtraction of these two terms can therefore result in a number, many orders of magnitude smaller and hence we lose accuracy due to subtraction errors. For example for $x=2×{10}^{6}$ and $\lambda =2×{10}^{6}$, $\mathrm{log}\left(x!\right)\approx 2.7×{10}^{7}$ and $\mathrm{log}\left(p\left(x;\lambda \right)\right)=-8.17326744645834$. But calculated with the method shown later we have $\mathrm{log}\left(p\left(x;\lambda \right)\right)=-8.1732674441334492$. The difference between these two results suggests a loss of about 7 significant figures of precision.
Loader introduces an alternative way of expressing (1) based on the saddle point expansion,
 $log p x;λ = log p x;x - Dx;λ ,$ (2)
where $D\left(x;\lambda \right)$, the deviance for the Poisson distribution is given by,
 $Dx;λ = log p x;x - log p x;λ , = λ D0 x λ ,$ (3)
and
 $D0 ε = ε log⁡ε + 1 - ε .$
For $\epsilon$ close to $1$, ${D}_{0}\left(\epsilon \right)$ can be evaluated through the series expansion
 $λ D0 x λ = x-λ 2 x+λ + 2x ∑ j=1 ∞ v 2j+1 2j+1 , where ​ v = x-λ x+λ ,$
otherwise ${D}_{0}\left(\epsilon \right)$ can be evaluated directly. In addition, Loader suggests evaluating $\mathrm{log}\left(x!\right)$ using the Stirling–De Moivre series,
 $logx! = 12 log⁡ 2πx + x logx -x + δx ,$ (4)
where the error $\delta \left(x\right)$ is given by
 $δx = 112x - 1 360x3 + 1 1260x5 + O x-7 .$
Finally $\mathrm{log}\left(p\left(x;\lambda \right)\right)$ can be evaluated by combining equations (1)(4) to get,
 $p x;λ = 1 2πx e - δx - λ D0 x/λ .$

## Example

This example prints the value of the gamma distribution PDF at six different points ${x}_{i}$ with differing ${\alpha }_{i}$ and ${\beta }_{i}$.
```function g01kk_example

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

x = [0.1,  3,  6,  4,    9,   16  ];
a = [3,   10,  5, 10,    9,    3.5];
b = [2,   11,  1,  0.1,  0.5,  2.5];

ilog = int64(0);
[pdf, ivalid, ifail] = g01kk( ...
ilog, x, a, b);

fprintf('     x       a       b       pdf     ivalid\n');
lx = numel(x);
la = numel(a);
lb = numel(b);
len = max ([lx, la, lb]);

for i=0:len-1
fprintf('%8.2f%8.2f%8.2f%12.4e%4d\n', x(mod(i,lx)+1), a(mod(i,la)+1), ...
b(mod(i,lb)+1), pdf(i+1), ivalid(i+1));
end

```
```g01kk example results

x       a       b       pdf     ivalid
0.10    3.00    2.00  5.9452e-04   0
3.00   10.00   11.00  1.5921e-12   0
6.00    5.00    1.00  1.3385e-01   0
4.00   10.00    0.10  3.0690e-08   0
9.00    9.00    0.50  8.3251e-03   0
16.00    3.50    2.50  2.0723e-02   0
```