nag_random_gamma (g05ffc) (PDF version)
g05 Chapter Contents
g05 Chapter Introduction
NAG C Library Manual

NAG Library Function Document

nag_random_gamma (g05ffc)

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

nag_random_gamma (g05ffc) generates a vector of pseudorandom variates from a gamma distribution with arguments a  and b .

2  Specification

#include <nag.h>
#include <nagg05.h>
void  nag_random_gamma (double a, double b, Integer n, double x[], NagError *fail)

3  Description

The gamma distribution has PDF (probability density function):
f x = 1 b a Γ a x a-1 e - x / b if ​ x 0 ; a , b > 0 f x = 0 otherwise.
One of three algorithms is used to generate the variates depending upon the value of a :
If a<1  a switching algorithm described by Dagpunar (1988) (called G6), is used. The target distributions are f 1 x = cax a-1 / t a  and f 2 x = 1-c e - x-t , where c = t t + a e -t , and the switching argument, t , is taken as 1-a . This is similar to GS algorithm of Ahrens and Dieter (1974) in which t=1 .
If a=1  the gamma distribution reduces to the exponential distribution and the method based on the logarithmic transformation of a uniform random variate is used.
If a>1  the algorithm given by Best (1978) is used. This is based on using a Student's t -distribution with two degrees of freedom as the target distribution in an envelope rejection method.

4  References

Ahrens J H and Dieter U (1974) Computer methods for sampling from gamma, beta, Poisson and binomial distributions Computing 12 223–46
Best D J (1978) Letter to the Editor Appl. Statist. 27 181
Dagpunar J (1988) Principles of Random Variate Generation Oxford University Press
Hastings N A J and Peacock J B (1975) Statistical Distributions Butterworth

5  Arguments

1:     adoubleInput
On entry: the argument, a , of the gamma distribution.
Constraint: a>0.0 .
2:     bdoubleInput
On entry: the argument, b , of the gamma distribution.
Constraint: b>0.0 .
3:     nIntegerInput
On entry: the number, n , of pseudorandom numbers to be generated.
Constraint: n1 .
4:     x[n]doubleOutput
On exit: the n  pseudorandom variates from the specified gamma distribution.
5:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

NE_INT_ARG_LE
On entry, n=value.
Constraint: n>0.
NE_REAL_ARG_LE
On entry, a must not be less than or equal to 0.0: a=value .
On entry, b must not be less than or equal to 0.0: b=value .

7  Accuracy

Not applicable.

8  Further Comments

To generate an observation from the χ 2  distribution with v  degrees of freedom generate an observation from a gamma distribution with arguments a = v / 2 , b=2 .
To generate an observation, y , from a Student's t -distribution with degrees of freedom v  generate an observation, x , from a gamma distribution with arguments a = v / 2  and b=2  and an observation, z , from a standard Normal distribution (see nag_random_normal (g05ddc)) and use the transformation y = z / x .

9  Example

The example program prints a set of five pseudorandom variates from a gamma distribution with arguments a=5.0  and b=1.0 , generated by nag_random_gamma (g05ffc) after initialization by nag_random_init_repeatable (g05cbc).

9.1  Program Text

Program Text (g05ffce.c)

9.2  Program Data

None.

9.3  Program Results

Program Results (g05ffce.r)


nag_random_gamma (g05ffc) (PDF version)
g05 Chapter Contents
g05 Chapter Introduction
NAG C Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012