Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_rand_dist_gamma (g05sj)

## Purpose

nag_rand_dist_gamma (g05sj) generates a vector of pseudorandom numbers taken from a gamma distribution with parameters a$a$ and b$b$.

## Syntax

[state, x, ifail] = g05sj(n, a, b, state)
[state, x, ifail] = nag_rand_dist_gamma(n, a, b, state)

## Description

The gamma distribution has PDF (probability density function)
 f(x) = 1/(baΓ(a))xa − 1e − x / b if ​x ≤ 0;  a,b > 0 f(x) = 0 otherwise.
$f(x)= 1baΓ(a) xa-1e-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$a$:
 (i) if a < 1$a<1$, a switching algorithm described by Dagpunar (1988) (called G6) is used. The target distributions are f1(x) = caxa − 1 / ta${f}_{1}\left(x\right)=ca{x}^{a-1}/{t}^{a}$ and f2(x) = (1 − c)e − (x − t)${f}_{2}\left(x\right)=\left(1-c\right){e}^{-\left(x-t\right)}$, where c = t / (t + ae − t)$c=t/\left(t+a{e}^{-t}\right)$, and the switching parameter, t$t$, is taken as 1 − a$1-a$. This is similar to Ahrens and Dieter's GS algorithm (see Ahrens and Dieter (1974)) in which t = 1$t=1$; (ii) if a = 1$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; (iii) if a > 1$a>1$, the algorithm given by Best (1978) is used. This is based on using a Student's t$t$-distribution with two degrees of freedom as the target distribution in an envelope rejection method.
One of the initialization functions nag_rand_init_repeat (g05kf) (for a repeatable sequence if computed sequentially) or nag_rand_init_nonrepeat (g05kg) (for a non-repeatable sequence) must be called prior to the first call to nag_rand_dist_gamma (g05sj).

## 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

## Parameters

### Compulsory Input Parameters

1:     n – int64int32nag_int scalar
n$n$, the number of pseudorandom numbers to be generated.
Constraint: n0${\mathbf{n}}\ge 0$.
2:     a – double scalar
a$a$, the parameter of the gamma distribution.
Constraint: a > 0.0${\mathbf{a}}>0.0$.
3:     b – double scalar
b$b$, the parameter of the gamma distribution.
Constraint: b > 0.0${\mathbf{b}}>0.0$.
4:     state( : $:$) – int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains information on the selected base generator and its current state.

None.

None.

### Output Parameters

1:     state( : $:$) – int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains updated information on the state of the generator.
2:     x(n) – double array
The n$n$ pseudorandom numbers from the specified gamma distribution.
3:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
On entry, n < 0${\mathbf{n}}<0$.
ifail = 2${\mathbf{ifail}}=2$
On entry, a0.0${\mathbf{a}}\le 0.0$.
ifail = 3${\mathbf{ifail}}=3$
On entry, b0.0${\mathbf{b}}\le 0.0$.
ifail = 4${\mathbf{ifail}}=4$
 On entry, state vector was not initialized or has been corrupted.

Not applicable.

None.

## Example

```function nag_rand_dist_gamma_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
n = int64(5);
a = 5;
b = 1;
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
[state, x, ifail] = nag_rand_dist_gamma(n, a, b, state)
```
```

state =

17
1234
1
0
3990
775
3088
31015
17917
13895
19930
8
0
1234
1
1
1234

x =

5.0702
6.1337
3.1018
3.9863
4.9648

ifail =

0

```
```function g05sj_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
n = int64(5);
a = 5;
b = 1;
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
[state, x, ifail] = g05sj(n, a, b, state)
```
```

state =

17
1234
1
0
3990
775
3088
31015
17917
13895
19930
8
0
1234
1
1
1234

x =

5.0702
6.1337
3.1018
3.9863
4.9648

ifail =

0

```