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_beta (g05sb)

## Purpose

nag_rand_dist_beta (g05sb) generates a vector of pseudorandom numbers taken from a beta distribution with parameters $a$ and $b$.

## Syntax

[state, x, ifail] = g05sb(n, a, b, state)
[state, x, ifail] = nag_rand_dist_beta(n, a, b, state)

## Description

The beta distribution has PDF (probability density function)
One of four algorithms is used to generate the variates depending on the values of $a$ and $b$. Let $\alpha$ be the maximum and $\beta$ be the minimum of $a$ and $b$. Then the algorithms are as follows:
 (i) if $\alpha <0.5$, Johnk's algorithm is used, see for example Dagpunar (1988). This generates the beta variate as ${u}_{1}^{1/a}/\left(\begin{array}{c}{u}_{1}^{1/a}+{u}_{2}^{1/b}\end{array}\right)$, where ${u}_{1}$ and ${u}_{2}$ are uniformly distributed random variates; (ii) if $\beta >1$, the algorithm BB given by Cheng (1978) is used. This involves the generation of an observation from a beta distribution of the second kind by the envelope rejection method using a log-logistic target distribution and then transforming it to a beta variate; (iii) if $\alpha >1$ and $\beta <1$, the switching algorithm given by Atkinson (1979) is used. The two target distributions used are ${f}_{1}\left(x\right)=\beta {x}^{\beta }$ and ${f}_{2}\left(x\right)=\alpha {\left(1-x\right)}^{\beta -1}$, along with the approximation to the switching argument of $t=\left(1-\beta \right)/\left(\alpha +1-\beta \right)$; (iv) in all other cases, Cheng's BC algorithm (see Cheng (1978)) is used with modifications suggested by Dagpunar (1988). This algorithm is similar to BB, used when $\beta >1$, but is tuned for small values of $a$ and $b$.
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_beta (g05sb).

## References

Atkinson A C (1979) A family of switching algorithms for the computer generation of beta random variates Biometrika 66 141–5
Cheng R C H (1978) Generating beta variates with nonintegral shape parameters Comm. ACM 21 317–322
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:     $\mathrm{n}$int64int32nag_int scalar
$n$, the number of pseudorandom numbers to be generated.
Constraint: ${\mathbf{n}}\ge 0$.
2:     $\mathrm{a}$ – double scalar
$a$, the parameter of the beta distribution.
Constraint: ${\mathbf{a}}>0.0$.
3:     $\mathrm{b}$ – double scalar
$b$, the parameter of the beta distribution.
Constraint: ${\mathbf{b}}>0.0$.
4:     $\mathrm{state}\left(:\right)$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.

### Output Parameters

1:     $\mathrm{state}\left(:\right)$int64int32nag_int array
Contains updated information on the state of the generator.
2:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
The $n$ pseudorandom numbers from the specified beta distribution.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=1$
Constraint: ${\mathbf{n}}\ge 0$.
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{a}}>0.0$.
${\mathbf{ifail}}=3$
Constraint: ${\mathbf{b}}>0.0$.
${\mathbf{ifail}}=4$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

Not applicable.

To generate an observation, $y$, from the beta distribution of the second kind from an observation, $x$, generated by nag_rand_dist_beta (g05sb) the transformation, $y=x/\left(1-x\right)$, may be used.

## Example

This example prints a set of five pseudorandom numbers from a beta distribution with parameters $a=2.0$ and $b=2.0$, generated by a single call to nag_rand_dist_beta (g05sb), after initialization by nag_rand_init_repeat (g05kf).
```function g05sb_example

fprintf('g05sb example results\n\n');

% Initialize the base generator to a repeatable sequence
seed  = [int64(1762543)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
genid, subid, seed);

% Number of variates
n = int64(5);

% Parameters
a = 2;
b = 2;

% Generate variates from beta distribution
[state, x, ifail] = g05sb( ...
n, a, b, state);

disp('Variates');
disp(x);

```
```g05sb example results

Variates
0.5977
0.6818
0.1797
0.4174
0.4987

```