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)

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αi and scale parameter βiβi has PDF
f (xi,αi,βi) = 1/( βiαi Γ(αi) ) xiαi1 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,βi1000.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 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 = 0ilog=0
f(xi,αi,βi)f(xi,αi,βi), the probability density function is returned.
ilog = 1ilog=1
log(f(xi,αi,βi))log(f(xi,αi,βi)), the logarithm of the probability density function is returned.
Constraint: ilog = 0ilog=0 or 11.
2:     x(lx) – double array
lx, the dimension of the array, must satisfy the constraint lx > 0lx>0.
xixi, the values at which the PDF is to be evaluated with xi = x(j)xi=xj, j = ((i1)  mod  lx) + 1j=((i-1) mod lx)+1, for i = 1,2,,max (lx,la,lb)i=1,2,,max(lx,la,lb).
3:     a(la) – double array
la, the dimension of the array, must satisfy the constraint la > 0la>0.
αiαi, the shape parameter with αi = a(j)αi=aj, j = ((i1)  mod  la) + 1j=((i-1) mod la)+1.
Constraint: a(j) > 0.0aj>0.0, for j = 1,2,,laj=1,2,,la.
4:     b(lb) – double array
lb, the dimension of the array, must satisfy the constraint lb > 0lb>0.
βiβi, the scale parameter with βi = b(j)βi=bj, j = ((i1)  mod  lb) + 1j=((i-1) mod lb)+1.
Constraint: b(j) > 0.0bj>0.0, for j = 1,2,,lbj=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 > 0lx>0.
2:     la – int64int32nag_int scalar
Default: The dimension of the array a.
The length of the array a.
Constraint: la > 0la>0.
3:     lb – int64int32nag_int scalar
Default: The dimension of the array b.
The length of the array b.
Constraint: lb > 0lb>0.

Input Parameters Omitted from the MATLAB Interface

None.

Output Parameters

1:     pdf( : :) – double array
Note: the dimension of the array pdf must be at least max (lx,la,lb)max(lx,la,lb).
f(xi,αi,βi)f(xi,αi,βi) or log(f(xi,αi,βi))log(f(xi,αi,βi)).
2:     ivalid( : :) – int64int32nag_int array
Note: the dimension of the array ivalid must be at least max (lx,la,lb)max(lx,la,lb).
ivalid(i)ivalidi indicates any errors with the input arguments, with
ivalid(i) = 0ivalidi=0
No error.
ivalid(i) = 1ivalidi=1
αi0.0αi0.0.
ivalid(i) = 2ivalidi=2
βi0.0βi0.0.
ivalid(i) = 3ivalidi=3
(xi)/(βi)xiβi overflows, the value returned should be a reasonable approximation.
3:     ifail – int64int32nag_int scalar
ifail = 0ifail=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 = 1ifail=1
On entry, at least one value of x, a or b was invalid.
Check ivalid for more information.
  ifail = 2ifail=2
Constraint: ilog = 0ilog=0 or 11.
  ifail = 3ifail=3
Constraint: lx > 0lx>0.
  ifail = 4ifail=4
Constraint: la > 0la>0.
  ifail = 5ifail=5
Constraint: lb > 0lb>0.

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,
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 xx and λλ, xlogλxlogλ and log(x ! )log(x!) 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 × 106x=2×106 and λ = 2 × 106λ=2×106, log(x ! )2.7 × 107log(x!)2.7×107 and log(p(x ; λ)) = 8.17326744645834log(p(x;λ))=-8.17326744645834. But calculated with the method shown later we have log(p(x ; λ)) = 8.1732674441334492log(p(x;λ))=-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(x;λ), 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 εε close to 11, D0(ε)D0(ε) 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(ε)D0(ε) can be evaluated directly. In addition, Loader suggests evaluating log(x ! )log(x!) 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)δ(x) is given by
δ(x) = 1/(12x) 1/(360x3) + 1/(1260x5) + O (x7) .
δ(x) = 112x - 1 360x3 + 1 1260x5 + O ( x-7 ) .
Finally log(p(x ; λ))log(p(x;λ)) 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