PDF version (NAG web site
, 64-bit version, 64-bit version)
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
and scale parameter
has PDF
If
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:
– int64int32nag_int scalar
-
The value of
ilog determines whether the logarithmic value is returned in
pdf.
- , the probability density function is returned.
- , the logarithm of the probability density function is returned.
Constraint:
or .
- 2:
– double array
-
, the values at which the PDF is to be evaluated with , , for .
- 3:
– double array
-
, the shape parameter with , .
Constraint:
, for .
- 4:
– double array
-
, the scale parameter with , .
Constraint:
, for .
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the array
x.
The length of the array
x.
Constraint:
.
- 2:
– int64int32nag_int scalar
-
Default:
the dimension of the array
a.
The length of the array
a.
Constraint:
.
- 3:
– int64int32nag_int scalar
-
Default:
the dimension of the array
b.
The length of the array
b.
Constraint:
.
Output Parameters
- 1:
– double array
-
The dimension of the array
pdf will be
or .
- 2:
– int64int32nag_int array
-
The dimension of the array
ivalid will be
indicates any errors with the input arguments, with
- No error.
- .
- .
- overflows, the value returned should be a reasonable approximation.
- 3:
– int64int32nag_int scalar
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
-
On entry, at least one value of
x,
a or
b was invalid.
Check
ivalid for more information.
-
-
Constraint: or .
-
-
Constraint: .
-
-
Constraint: .
-
-
Constraint: .
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
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,
The usual way of computing this quantity would be to take the logarithm and calculate,
For large and , and 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 and , and . But calculated with the method shown later we have . 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,
where
, the deviance for the Poisson distribution is given by,
and
For
close to
,
can be evaluated through the series expansion
otherwise
can be evaluated directly. In addition, Loader suggests evaluating
using the Stirling–De Moivre series,
where the error
is given by
Finally
can be evaluated by combining equations
(1)–
(4) to get,
Example
This example prints the value of the gamma distribution PDF at six different points with differing and .
Open in the MATLAB editor:
g01kk_example
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)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015