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_multivar_normal (g05rz)

## Purpose

nag_rand_multivar_normal (g05rz) sets up a reference vector and generates an array of pseudorandom numbers from a multivariate Normal distribution with mean vector $a$ and covariance matrix $C$.

## Syntax

[r, state, x, ifail] = g05rz(mode, n, xmu, c, r, state, 'm', m, 'lr', lr)
[r, state, x, ifail] = nag_rand_multivar_normal(mode, n, xmu, c, r, state, 'm', m, 'lr', lr)

## Description

When the covariance matrix is nonsingular (i.e., strictly positive definite), the distribution has probability density function
 $fx = C-1 2πm exp - 12 x-aT C-1 x-a$
where $m$ is the number of dimensions, $C$ is the covariance matrix, $a$ is the vector of means and $x$ is the vector of positions.
Covariance matrices are symmetric and positive semidefinite. Given such a matrix $C$, there exists a lower triangular matrix $L$ such that $L{L}^{\mathrm{T}}=C$. $L$ is not unique, if $C$ is singular.
nag_rand_multivar_normal (g05rz) decomposes $C$ to find such an $L$. It then stores $m$, $a$ and $L$ in the reference vector $r$ which is used to generate a vector $x$ of independent standard Normal pseudorandom numbers. It then returns the vector $a+Lx$, which has the required multivariate Normal distribution.
It should be noted that this function will work with a singular covariance matrix $C$, provided $C$ is positive semidefinite, despite the fact that the above formula for the probability density function is not valid in that case. Wilkinson (1965) should be consulted if further information is required.
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_multivar_normal (g05rz).

## References

Knuth D E (1981) The Art of Computer Programming (Volume 2) (2nd Edition) Addison–Wesley
Wilkinson J H (1965) The Algebraic Eigenvalue Problem Oxford University Press, Oxford

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{mode}$int64int32nag_int scalar
A code for selecting the operation to be performed by the function.
${\mathbf{mode}}=0$
Set up reference vector only.
${\mathbf{mode}}=1$ or $3$
Generate variates using reference vector set up in a prior call to nag_rand_multivar_normal (g05rz).
${\mathbf{mode}}=2$ or $4$
Set up reference vector and generate variates.
The variates are stored differently in x for ${\mathbf{mode}}=1$ or $2$ compared with ${\mathbf{mode}}=3$ or $4$.
Constraint: ${\mathbf{mode}}=0$, $1$, $2$, $3$ or $4$.
2:     $\mathrm{n}$int64int32nag_int scalar
$n$, the number of random variates required.
Constraint: ${\mathbf{n}}\ge 0$.
3:     $\mathrm{xmu}\left({\mathbf{m}}\right)$ – double array
$a$, the vector of means of the distribution.
4:     $\mathrm{c}\left(\mathit{ldc},{\mathbf{m}}\right)$ – double array
ldc, the first dimension of the array, must satisfy the constraint $\mathit{ldc}\ge {\mathbf{m}}$.
The covariance matrix of the distribution. Only the upper triangle need be set.
Constraint: $C$ must be positive semidefinite to machine precision.
5:     $\mathrm{r}\left({\mathbf{lr}}\right)$ – double array
6:     $\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.

### Optional Input Parameters

1:     $\mathrm{m}$int64int32nag_int scalar
Default: the dimension of the array xmu and the first dimension of the array c and the second dimension of the array c. (An error is raised if these dimensions are not equal.)
$m$, the number of dimensions of the distribution.
Constraint: ${\mathbf{m}}>0$.
2:     $\mathrm{lr}$int64int32nag_int scalar
Default: the dimension of the array r.
The dimension of the array r. if ${\mathbf{mode}}=1$ or $3$, it must be the same as the value of lr specified in the prior call to nag_rand_multivar_normal (g05rz) with ${\mathbf{mode}}=0$, $2$ or $4$.
Constraint: ${\mathbf{lr}}\ge {\mathbf{m}}×\left({\mathbf{m}}+1\right)+1$.

### Output Parameters

1:     $\mathrm{r}\left({\mathbf{lr}}\right)$ – double array
2:     $\mathrm{state}\left(:\right)$int64int32nag_int array
Contains updated information on the state of the generator.
3:     $\mathrm{x}\left(\mathit{ldx},:\right)$ – double array
The first dimension, $\mathit{ldx}$, of the array x will be
• if ${\mathbf{mode}}=1$ or $2$, $\mathit{ldx}={\mathbf{n}}$;
• if ${\mathbf{mode}}=3$ or $4$, $\mathit{ldx}={\mathbf{m}}$.
The second dimension of the array x will be ${\mathbf{m}}$ if ${\mathbf{mode}}=1$ or $2$ and at least ${\mathbf{n}}$ if ${\mathbf{mode}}=3$ or $4$.
The array of pseudorandom multivariate Normal vectors generated by the function.
Two possible storage orders are available. If ${\mathbf{mode}}=1$ or $2$ then ${\mathbf{x}}\left(i,j\right)$ holds the $j$th dimension for the $i$th variate. If ${\mathbf{mode}}=3$ or $4$ this ordering is reversed and ${\mathbf{x}}\left(j,i\right)$ holds the $j$th dimension for the $i$th variate.
4:     $\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$
On entry, invalid value for mode.
Constraint: ${\mathbf{mode}}=0$, $1$, $2$, $3$ or $4$.
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{n}}\ge 0$.
${\mathbf{ifail}}=3$
Constraint: ${\mathbf{m}}>0$.
${\mathbf{ifail}}=5$
On entry, the covariance matrix $C$ is not positive semidefinite to machine precision.
${\mathbf{ifail}}=6$
Constraint: $\mathit{ldc}\ge {\mathbf{m}}$.
${\mathbf{ifail}}=7$
m is not the same as when r was set up in a previous call.
${\mathbf{ifail}}=8$
On entry, lr is not large enough, ${\mathbf{lr}}=_$: minimum length required .
${\mathbf{ifail}}=9$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=11$
Constraint: $\mathit{ldx}\ge {\mathbf{m}}$.
Constraint: $\mathit{ldx}\ge {\mathbf{n}}$.
${\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.

The time taken by nag_rand_multivar_normal (g05rz) is of order $n{m}^{3}$.
It is recommended that the diagonal elements of $C$ should not differ too widely in order of magnitude. This may be achieved by scaling the variables if necessary. The actual matrix decomposed is $C+E=L{L}^{\mathrm{T}}$, where $E$ is a diagonal matrix with small positive diagonal elements. This ensures that, even when $C$ is singular, or nearly singular, the Cholesky factor $L$ corresponds to a positive definite covariance matrix that agrees with $C$ within machine precision.

## Example

This example prints ten pseudorandom observations from a multivariate Normal distribution with means vector
 $1.0 2.0 -3.0 0.0$
and covariance matrix
 $1.69 0.39 -1.86 0.07 0.39 98.01 -7.07 -0.71 -1.86 -7.07 11.56 0.03 0.07 -0.71 0.03 0.01 ,$
generated by nag_rand_multivar_normal (g05rz). All ten observations are generated by a single call to nag_rand_multivar_normal (g05rz) with ${\mathbf{mode}}=2$. The random number generator is initialized by nag_rand_init_repeat (g05kf).
```function g05rz_example

fprintf('g05rz 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(10);

% Distribution means
xmu = [1; 2; -3; 0];

% Upper triangular part of covariance matrix
c = [ 1.69,  0.39, -1.86,  0.07;
0,    98.01, -7.07, -0.71;
0,     0,    11.56,  0.03;
0,     0,     0,     0.01];
m = size(c,1);

% Setup and generate in one go
mode = int64(2);

% Generate variates from a multivariate Normal distribution
lr = m*(m+1) + 2;
r = zeros(lr, 1);
[r, state, x, ifail] = g05rz( ...
mode, n, xmu, c, r, state);

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

```
```g05rz example results

Variates
1.4534  -14.1206   -3.7410    0.1184
-0.6191   -4.8000   -0.1473   -0.0304
1.8607    5.3206   -5.0753    0.0106
2.0861  -13.6996   -1.3451    0.1428
-0.6326    3.9729    0.5721   -0.0770
0.9754   -3.8162   -4.2978    0.0040
0.6174   -5.1573    2.5037    0.0772
2.0352   26.9359    2.2939   -0.0826
0.9941   14.7700   -1.0421   -0.0549
1.5780    2.8916   -2.1725   -0.0129

```