﻿ g05rz Method
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

C#
```public static void g05rz(
int mode,
int n,
int m,
double[] xmu,
double[,] c,
double[] r,
G05..::..G05State g05state,
double[,] x,
out int ifail
)```
Visual Basic
```Public Shared Sub g05rz ( _
mode As Integer, _
n As Integer, _
m As Integer, _
xmu As Double(), _
c As Double(,), _
r As Double(), _
g05state As G05..::..G05State, _
x As Double(,), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g05rz(
int mode,
int n,
int m,
array<double>^ xmu,
array<double,2>^ c,
array<double>^ r,
G05..::..G05State^ g05state,
array<double,2>^ x,
[OutAttribute] int% ifail
)```
F#
```static member g05rz :
mode : int *
n : int *
m : int *
xmu : float[] *
c : float[,] *
r : float[] *
g05state : G05..::..G05State *
x : float[,] *
ifail : int byref -> unit
```

#### Parameters

mode
Type: System..::..Int32
On entry: a code for selecting the operation to be performed by the method.
${\mathbf{mode}}=0$
Set up reference vector only.
${\mathbf{mode}}=1$
Generate variates using reference vector set up in a prior call to g05rz.
${\mathbf{mode}}=2$
Set up reference vector and generate variates.
Constraint: ${\mathbf{mode}}=0$, $1$ or $2$.
n
Type: System..::..Int32
On entry: $n$, the number of random variates required.
Constraint: ${\mathbf{n}}\ge 0$.
m
Type: System..::..Int32
On entry: $m$, the number of dimensions of the distribution.
Constraint: ${\mathbf{m}}>0$.
xmu
Type: array<System..::..Double>[]()[][]
An array of size [m]
On entry: $a$, the vector of means of the distribution.
c
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, m]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{m}}$
On entry: the covariance matrix of the distribution. Only the upper triangle need be set.
Constraint: $C$ must be positive semidefinite to machine precision.
r
Type: array<System..::..Double>[]()[][]
An array of size [lr]
On entry: if ${\mathbf{mode}}=1$, the reference vector as set up by g05rz in a previous call with ${\mathbf{mode}}=0$ or $2$.
On exit: if ${\mathbf{mode}}=0$ or $2$, the reference vector that can be used in subsequent calls to g05rz with ${\mathbf{mode}}=1$.
g05state
Type: NagLibrary..::..G05..::..G05State
An Object of type G05.G05State.
x
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, _tdx]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
• if ${\mathbf{mode}}=1$ or $2$, $\mathrm{dim1}\ge {\mathbf{n}}$;
• if ${\mathbf{mode}}=3$ or $4$, $\mathrm{dim1}\ge {\mathbf{m}}$.
On exit: the array of pseudorandom multivariate Normal vectors generated by the method.
Two possible storage orders are available. If ${\mathbf{mode}}=1$ or $2$ then ${\mathbf{x}}\left[i-1,j-1\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-1,i-1\right]$ holds the $j$th dimension for the $i$th variate.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

When the covariance matrix is nonsingular (i.e., strictly positive definite), the distribution has probability density function
 $fx=C-12πmexp-12x-aTC-1x-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.
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 method 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 methods (G05KFF not in this release) (for a repeatable sequence if computed sequentially) or (G05KGF not in this release) (for a non-repeatable sequence) must be called prior to the first call to 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

# Error Indicators and Warnings

Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDC, LDX) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\mathbf{ifail}}=1$
On entry, ${\mathbf{mode}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mode}}=0$, $1$ or $2$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 0$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}>0$.
${\mathbf{ifail}}=5$
On entry, the covariance matrix $C$ is not positive semidefinite to machine precision.
${\mathbf{ifail}}=6$
On entry, ${\mathbf{ldc}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldc}}\ge {\mathbf{m}}$.
${\mathbf{ifail}}=7$
m is not the same as when r was set up in a previous call.
Previous value of ${\mathbf{m}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=8$
On entry, lr is not large enough, ${\mathbf{lr}}=〈\mathit{\text{value}}〉$: minimum length required $\text{}=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=9$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=11$
On entry, ${\mathbf{ldx}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{ldx}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=12$
On entry, $\mathbf{_tdx}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: $\mathbf{_sorder}\ne 1$ or $\mathbf{_tdx}\ge {\mathbf{m}}$.
${\mathbf{ifail}}=22$
On entry,$\mathbf{_tdx}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: $\mathbf{_sorder}=1$ or $\mathbf{_tdx}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-4000$
Invalid dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

Not applicable.

# Parallelism and Performance

None.

The time taken by 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.