g05 Chapter Contents
g05 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_random_gamma (g05ffc)

## 1  Purpose

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

## 2  Specification

 #include #include
 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}\left(x\right)={cax}^{a-1}/{t}^{a}$ and ${f}_{2}\left(x\right)=\left(1-c\right){e}^{-\left(x-t\right)}$, where $c=t\left(t+a{e}^{-t}\right)$, 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

On entry: the argument, $a$, of the gamma distribution.
Constraint: ${\mathbf{a}}>0.0$.
2:     bdoubleInput
On entry: the argument, $b$, of the gamma distribution.
Constraint: ${\mathbf{b}}>0.0$.
3:     nIntegerInput
On entry: the number, $n$, of pseudorandom numbers to be generated.
Constraint: ${\mathbf{n}}\ge 1$.
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, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}>0$.
NE_REAL_ARG_LE
On entry, a must not be less than or equal to 0.0: ${\mathbf{a}}=〈\mathit{\text{value}}〉$.
On entry, b must not be less than or equal to 0.0: ${\mathbf{b}}=〈\mathit{\text{value}}〉$.

## 7  Accuracy

Not applicable.

To generate an observation from the ${\chi }^{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/\sqrt{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)

None.

### 9.3  Program Results

Program Results (g05ffce.r)