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.


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++
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
static member g05rz : 
        mode : int * 
        n : int * 
        m : int * 
        xmu : float[] * 
        c : float[,] * 
        r : float[] * 
        g05state : G05..::..G05State * 
        x : float[,] * 
        ifail : int byref -> unit 


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


When the covariance matrix is nonsingular (i.e., strictly positive definite), the distribution has probability density function
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 LLT=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.


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.
On entry, mode=value.
Constraint: mode=0, 1 or 2.
On entry, n=value.
Constraint: n0.
On entry, m=value.
Constraint: m>0.
On entry, the covariance matrix C is not positive semidefinite to machine precision.
On entry, ldc=value and m=value.
Constraint: ldcm.
m is not the same as when r was set up in a previous call.
Previous value of m=value and m=value.
On entry, lr is not large enough, lr=value: minimum length required =value.
On entry, state vector has been corrupted or not initialized.
On entry, ldx=value and m=value.
Constraint: ldxm.
On entry, ldx=value and n=value.
Constraint: ldxn.
On entry, _tdx=value and m=value.
Constraint: _sorder1 or _tdxm.
On entry,_tdx=value and n=value.
Constraint: _sorder=1 or _tdxn.
An error occured, see message report.
Invalid Parameters value
Invalid dimension for array value
Negative dimension for array value
Invalid Parameters value


Not applicable.

Parallelism and Performance


Further Comments

The time taken by g05rz is of order nm3.
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=LLT, 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.


See Also