# NAG CL Interfaceg05sec (dist_​dirichlet)

Settings help

CL Name Style:

## 1Purpose

g05sec generates a vector of pseudorandom numbers taken from a Dirichlet distribution.

## 2Specification

 #include
 void g05sec (Nag_OrderType order, Integer n, Integer m, const double a[], Integer state[], double x[], Integer pdx, NagError *fail)
The function may be called by the names: g05sec, nag_rand_dist_dirichlet or nag_rand_dirichlet.

## 3Description

The distribution has PDF (probability density function)
 $f(x) = 1 B(α) ∏ i=1 m x i αi - 1 and B(α) = ∏ i=1 m Γ (αi) Γ (∑ i=1 m αi)$
where $x=\left\{{x}_{1},{x}_{2},\dots ,{x}_{m}\right\}$ is a vector of dimension $m$, such that ${x}_{i}>0$ for all $i$ and $\sum _{\mathit{i}=1}^{m}{x}_{i}=1$.
g05sec generates a draw from a Dirichlet distribution by first drawing $m$ independent samples, ${y}_{i}\sim \mathrm{gamma}\left({\alpha }_{i},1\right)$, i.e., independent draws from a gamma distribution with parameters ${\alpha }_{i}>0$ and one, and then setting ${x}_{i}={y}_{i}/\sum _{\mathit{j}=1}^{m}{y}_{j}$.
One of the initialization functions g05kfc (for a repeatable sequence if computed sequentially) or g05kgc (for a non-repeatable sequence) must be called prior to the first call to g05sec.

## 4References

Dagpunar J (1988) Principles of Random Variate Generation Oxford University Press
Hastings N A J and Peacock J B (1975) Statistical Distributions Butterworth

## 5Arguments

1: $\mathbf{order}$Nag_OrderType Input
On entry: the order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by ${\mathbf{order}}=\mathrm{Nag_RowMajor}$. See Section 3.1.3 in the Introduction to the NAG Library CL Interface for a more detailed explanation of the use of this argument.
Constraint: ${\mathbf{order}}=\mathrm{Nag_RowMajor}$ or $\mathrm{Nag_ColMajor}$.
2: $\mathbf{n}$Integer Input
On entry: $n$, the number of pseudorandom numbers to be generated.
Constraint: ${\mathbf{n}}\ge 0$.
3: $\mathbf{m}$Integer Input
On entry: $m$, the number of dimensions of the distribution.
Constraint: ${\mathbf{m}}>0$.
4: $\mathbf{a}\left[{\mathbf{m}}\right]$const double Input
On entry: the parameter vector for the distribution.
Constraint: ${\mathbf{a}}\left[\mathit{i}-1\right]>0.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{m}}$.
5: $\mathbf{state}\left[\mathit{dim}\right]$Integer Communication Array
Note: the dimension, $\mathit{dim}$, of this array is dictated by the requirements of associated functions that must have been previously called. This array MUST be the same array passed as argument state in the previous call to nag_rand_init_repeatable (g05kfc) or nag_rand_init_nonrepeatable (g05kgc).
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
6: $\mathbf{x}\left[\mathit{dim}\right]$double Output
Note: the dimension, dim, of the array x must be at least
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{pdx}}×{\mathbf{m}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}×{\mathbf{pdx}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
where ${\mathbf{X}}\left(i,j\right)$ appears in this document, it refers to the array element
• ${\mathbf{x}}\left[\left(j-1\right)×{\mathbf{pdx}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{x}}\left[\left(i-1\right)×{\mathbf{pdx}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On exit: the $n$ pseudorandom numbers from the specified Dirichlet distribution, with ${\mathbf{X}}\left(i,j\right)$ holding the $j$th dimension for the $i$th variate.
7: $\mathbf{pdx}$Integer Input
On entry: the stride separating row or column elements (depending on the value of order) in the array x.
Constraints:
• if ${\mathbf{order}}=\mathrm{Nag_ColMajor}$, ${\mathbf{pdx}}\ge {\mathbf{n}}$;
• if ${\mathbf{order}}=\mathrm{Nag_RowMajor}$, ${\mathbf{pdx}}\ge {\mathbf{m}}$.
8: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
On entry, argument $⟨\mathit{\text{value}}⟩$ had an illegal value.
NE_INT
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}>0$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 0$.
NE_INT_2
On entry, ${\mathbf{pdx}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{pdx}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{pdx}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{order}}=\mathrm{Nag_ColMajor}$ or ${\mathbf{pdx}}\ge {\mathbf{n}}$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_INVALID_STATE
On entry, state vector has been corrupted or not initialized.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_REAL_ARRAY
On entry, at least one ${\mathbf{a}}\left[i\right]\le 0$.

Not applicable.

## 8Parallelism and Performance

g05sec is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

None.

## 10Example

This example prints a set of five pseudorandom numbers from a Dirichlet distribution with parameters $m=4$ and $\alpha =\left\{2.0,2.0,2.0,2.0\right\}$, generated by a single call to g05sec, after initialization by g05kfc.

### 10.1Program Text

Program Text (g05sece.c)

None.

### 10.3Program Results

Program Results (g05sece.r)