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

PDF version (NAG web site, 64-bit version, 64-bit version)
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 αi${\alpha }_{i}$ and scale parameter βi${\beta }_{i}$ has PDF
 f (xi,αi,βi) = 1/( βiαi Γ(αi) ) xiαi − 1 e − xi / βi if ​ xi ≥ 0 ;   αi , βi > 0 f(xi,αi,βi) = 0 otherwise.
$f (xi,αi,βi) = 1 βi αi Γ(αi) xi αi-1 e -xi / βi if ​ xi ≥ 0 ; αi , βi > 0 f(xi,αi,βi)=0 otherwise.$
If 0.01xi,αi,βi100$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 Section [Further Comments]).
The input arrays to this function 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 [Vectorized s] 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:     ilog – int64int32nag_int scalar
The value of ilog determines whether the logarithmic value is returned in pdf.
ilog = 0${\mathbf{ilog}}=0$
f(xi,αi,βi)$f\left({x}_{i},{\alpha }_{i},{\beta }_{i}\right)$, the probability density function is returned.
ilog = 1${\mathbf{ilog}}=1$
log(f(xi,αi,βi))$\mathrm{log}\left(f\left({x}_{i},{\alpha }_{i},{\beta }_{i}\right)\right)$, the logarithm of the probability density function is returned.
Constraint: ilog = 0${\mathbf{ilog}}=0$ or 1$1$.
2:     x(lx) – double array
lx, the dimension of the array, must satisfy the constraint lx > 0${\mathbf{lx}}>0$.
xi${x}_{i}$, the values at which the PDF is to be evaluated with xi = x(j)${x}_{i}={\mathbf{x}}\left(j\right)$, j = ((i1)  mod  lx) + 1, for i = 1,2,,max (lx,la,lb)$i=1,2,\dots ,\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{lx}},{\mathbf{la}},{\mathbf{lb}}\right)$.
3:     a(la) – double array
la, the dimension of the array, must satisfy the constraint la > 0${\mathbf{la}}>0$.
αi${\alpha }_{i}$, the shape parameter with αi = a(j)${\alpha }_{i}={\mathbf{a}}\left(j\right)$, j = ((i1)  mod  la) + 1.
Constraint: a(j) > 0.0${\mathbf{a}}\left(\mathit{j}\right)>0.0$, for j = 1,2,,la$\mathit{j}=1,2,\dots ,{\mathbf{la}}$.
4:     b(lb) – double array
lb, the dimension of the array, must satisfy the constraint lb > 0${\mathbf{lb}}>0$.
βi${\beta }_{i}$, the scale parameter with βi = b(j)${\beta }_{i}={\mathbf{b}}\left(j\right)$, j = ((i1)  mod  lb) + 1.
Constraint: b(j) > 0.0${\mathbf{b}}\left(\mathit{j}\right)>0.0$, for j = 1,2,,lb$\mathit{j}=1,2,\dots ,{\mathbf{lb}}$.

### Optional Input Parameters

1:     lx – int64int32nag_int scalar
Default: The dimension of the array x.
The length of the array x.
Constraint: lx > 0${\mathbf{lx}}>0$.
2:     la – int64int32nag_int scalar
Default: The dimension of the array a.
The length of the array a.
Constraint: la > 0${\mathbf{la}}>0$.
3:     lb – int64int32nag_int scalar
Default: The dimension of the array b.
The length of the array b.
Constraint: lb > 0${\mathbf{lb}}>0$.

None.

### Output Parameters

1:     pdf( : $:$) – double array
Note: the dimension of the array pdf must be at least max (lx,la,lb)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{lx}},{\mathbf{la}},{\mathbf{lb}}\right)$.
f(xi,αi,βi)$f\left({x}_{i},{\alpha }_{i},{\beta }_{i}\right)$ or log(f(xi,αi,βi))$\mathrm{log}\left(f\left({x}_{i},{\alpha }_{i},{\beta }_{i}\right)\right)$.
2:     ivalid( : $:$) – int64int32nag_int array
Note: the dimension of the array ivalid must be at least max (lx,la,lb)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{lx}},{\mathbf{la}},{\mathbf{lb}}\right)$.
ivalid(i)${\mathbf{ivalid}}\left(i\right)$ indicates any errors with the input arguments, with
ivalid(i) = 0${\mathbf{ivalid}}\left(i\right)=0$
No error.
ivalid(i) = 1${\mathbf{ivalid}}\left(i\right)=1$
αi0.0${\alpha }_{i}\le 0.0$.
ivalid(i) = 2${\mathbf{ivalid}}\left(i\right)=2$
βi0.0${\beta }_{i}\le 0.0$.
ivalid(i) = 3${\mathbf{ivalid}}\left(i\right)=3$
(xi)/(βi)$\frac{{x}_{i}}{{\beta }_{i}}$ overflows, the value returned should be a reasonable approximation.
3:     ifail – int64int32nag_int scalar
${\mathrm{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 ifail = 1${\mathbf{ifail}}=1$
On entry, at least one value of x, a or b was invalid.
ifail = 2${\mathbf{ifail}}=2$
Constraint: ilog = 0${\mathbf{ilog}}=0$ or 1$1$.
ifail = 3${\mathbf{ifail}}=3$
Constraint: lx > 0${\mathbf{lx}}>0$.
ifail = 4${\mathbf{ifail}}=4$
Constraint: la > 0${\mathbf{la}}>0$.
ifail = 5${\mathbf{ifail}}=5$
Constraint: lb > 0${\mathbf{lb}}>0$.

## 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,
 p(x ; λ) = (λx)/(x ! ) e − λ . $p(x;λ) = λ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 ! ) − λ . $log( p (x;λ) ) = x log⁡λ - log( x! ) - λ .$
For large x$x$ and λ$\lambda$, xlogλ$x\mathrm{log}\lambda$ and log(x ! )$\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 × 106$x=2×{10}^{6}$ and λ = 2 × 106$\lambda =2×{10}^{6}$, log(x ! )2.7 × 107$\mathrm{log}\left(x!\right)\approx 2.7×{10}^{7}$ and log(p(x ; λ)) = 8.17326744645834$\mathrm{log}\left(p\left(x;\lambda \right)\right)=-8.17326744645834$. But calculated with the method shown later we have log(p(x ; λ)) = 8.1732674441334492$\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)) − D(x ; λ) , $log( p (x;λ) ) = log( p (x;x) ) - D(x;λ) ,$ (2)
where D(x ; λ)$D\left(x;\lambda \right)$, the deviance for the Poisson distribution is given by,
 D(x ; λ) = log(p(x ; x)) − log(p(x ; λ)) , = λ D0 (x/λ) ,
$D(x;λ) = log( p (x;x) ) - log( p (x;λ) ) , = λ D0 ( x λ ) ,$
(3)
and
 D0 (ε) = ε logε + 1 − ε . $D0 (ε) = ε log⁡ε + 1 - ε .$
For ε$\epsilon$ close to 1$1$, D0(ε)${D}_{0}\left(\epsilon \right)$ can be evaluated through the series expansion
 ∞ λD0(x/λ) = ((x − λ)2)/(x + λ) + 2x ∑ (v2j + 1)/(2j + 1) ,  where ​v = (x − λ)/(x + λ), j = 1
$λ D0 ( x λ ) = (x-λ) 2 x+λ + 2x ∑ j=1 ∞ v 2j+1 2j+1 , where ​ v = x-λ x+λ ,$
otherwise D0(ε)${D}_{0}\left(\epsilon \right)$ can be evaluated directly. In addition, Loader suggests evaluating log(x ! )$\mathrm{log}\left(x!\right)$ using the Stirling–De Moivre series,
 log(x ! ) = (1/2) log (2πx) + x log(x) − x + δ(x) , $log(x!) = 12 log⁡ (2πx) + x log(x) -x + δ(x) ,$ (4)
where the error δ(x)$\delta \left(x\right)$ is given by
 δ(x) = 1/(12x) − 1/(360x3) + 1/(1260x5) + O (x − 7) . $δ(x) = 112x - 1 360x3 + 1 1260x5 + O ( x-7 ) .$
Finally log(p(x ; λ))$\mathrm{log}\left(p\left(x;\lambda \right)\right)$ can be evaluated by combining equations (1)(4) to get,
 p (x ; λ) = 1/(sqrt(2πx)) e − δ(x) − λ D0 (x / λ) . $p (x;λ) = 1 2πx e - δ(x) - λ D0 ( x/λ ) .$

## Example

```function nag_stat_pdf_gamma_vector_example
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] = nag_stat_pdf_gamma_vector(ilog, x, a, b);

fprintf('\n  x             a             b             result\n');
lx = numel(x);
la = numel(a);
lb = numel(b);
len = max ([lx, la, lb]);
for i=0:len-1
fprintf('%13.5e %13.5e %13.5e %13.5e %3d\n', x(mod(i,lx)+1), a(mod(i,la)+1), ...
b(mod(i,lb)+1), pdf(i+1), ivalid(i+1));
end
```
```

x             a             b             result
1.00000e-01   3.00000e+00   2.00000e+00   5.94518e-04   0
3.00000e+00   1.00000e+01   1.10000e+01   1.59205e-12   0
6.00000e+00   5.00000e+00   1.00000e+00   1.33853e-01   0
4.00000e+00   1.00000e+01   1.00000e-01   3.06901e-08   0
9.00000e+00   9.00000e+00   5.00000e-01   8.32509e-03   0
1.60000e+01   3.50000e+00   2.50000e+00   2.07228e-02   0

```
```function g01kk_example
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('\n  x             a             b             result\n');
lx = numel(x);
la = numel(a);
lb = numel(b);
len = max ([lx, la, lb]);
for i=0:len-1
fprintf('%13.5e %13.5e %13.5e %13.5e %3d\n', x(mod(i,lx)+1), a(mod(i,la)+1), ...
b(mod(i,lb)+1), pdf(i+1), ivalid(i+1));
end
```
```

x             a             b             result
1.00000e-01   3.00000e+00   2.00000e+00   5.94518e-04   0
3.00000e+00   1.00000e+01   1.10000e+01   1.59205e-12   0
6.00000e+00   5.00000e+00   1.00000e+00   1.33853e-01   0
4.00000e+00   1.00000e+01   1.00000e-01   3.06901e-08   0
9.00000e+00   9.00000e+00   5.00000e-01   8.32509e-03   0
1.60000e+01   3.50000e+00   2.50000e+00   2.07228e-02   0

```

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013