hide long namesshow long names
hide short namesshow short names
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)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

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 and scale parameter β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.01xi,αi,βi100 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:     ilog int64int32nag_int scalar
The value of ilog determines whether the logarithmic value is returned in pdf.
ilog=0
fxi,αi,βi, the probability density function is returned.
ilog=1
logfxi,αi,βi, the logarithm of the probability density function is returned.
Constraint: ilog=0 or 1.
2:     xlx – double array
xi, the values at which the PDF is to be evaluated with xi=xj, j=i-1 mod lx+1, for i=1,2,,maxlx,la,lb.
3:     ala – double array
αi, the shape parameter with αi=aj, j=i-1 mod la+1.
Constraint: aj>0.0, for j=1,2,,la.
4:     blb – double array
βi, the scale parameter with βi=bj, j=i-1 mod lb+1.
Constraint: bj>0.0, for j=1,2,,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.
2:     la int64int32nag_int scalar
Default: the dimension of the array a.
The length of the array a.
Constraint: la>0.
3:     lb int64int32nag_int scalar
Default: the dimension of the array b.
The length of the array b.
Constraint: lb>0.

Output Parameters

1:     pdf: – double array
The dimension of the array pdf will be maxlx,la,lb
fxi,αi,βi or logfxi,αi,βi.
2:     ivalid: int64int32nag_int array
The dimension of the array ivalid will be maxlx,la,lb
ivalidi indicates any errors with the input arguments, with
ivalidi=0
No error.
ivalidi=1
αi0.0.
ivalidi=2
βi0.0.
ivalidi=3
xiβi overflows, the value returned should be a reasonable approximation.
3:     ifail int64int32nag_int scalar
ifail=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
On entry, at least one value of x, a or b was invalid.
Check ivalid for more information.
   ifail=2
Constraint: ilog=0 or 1.
   ifail=3
Constraint: lx>0.
   ifail=4
Constraint: la>0.
   ifail=5
Constraint: lb>0.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

Not applicable.

Further Comments

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 λ, xlogλ and logx! 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 and λ=2×106, logx!2.7×107 and logpx;λ=-8.17326744645834. But calculated with the method shown later we have logpx;λ=-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 Dx;λ, 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 ε close to 1, D0ε 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 D0ε can be evaluated directly. In addition, Loader suggests evaluating logx! using the Stirling–De Moivre series,
logx! = 12 log 2πx + x logx -x + δx , (4)
where the error δx is given by
δx = 112x - 1 360x3 + 1 1260x5 + O x-7 .  
Finally logpx;λ 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 xi with differing αi and β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

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–2015