# NAG FL Interfaceg05rzf (multivar_​normal)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

g05rzf 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$.

## 2Specification

Fortran Interface
 Subroutine g05rzf ( mode, n, m, xmu, c, ldc, r, lr, x, ldx,
 Integer, Intent (In) :: mode, n, m, ldc, lr, ldx Integer, Intent (Inout) :: state(*), ifail Real (Kind=nag_wp), Intent (In) :: xmu(m), c(ldc,m) Real (Kind=nag_wp), Intent (Inout) :: r(lr), x(ldx,*)
#include <nag.h>
 void g05rzf_ (const Integer *mode, const Integer *n, const Integer *m, const double xmu[], const double c[], const Integer *ldc, double r[], const Integer *lr, Integer state[], double x[], const Integer *ldx, Integer *ifail)
The routine may be called by the names g05rzf or nagf_rand_multivar_normal.

## 3Description

When the covariance matrix is nonsingular (i.e., strictly positive definite), the distribution has probability density function
 $f(x) = |C-1| (2π)m exp(-12(x-a)TC-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.
g05rzf 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 routine 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 routines g05kff (for a repeatable sequence if computed sequentially) or g05kgf (for a non-repeatable sequence) must be called prior to the first call to g05rzf.

## 4References

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

## 5Arguments

1: $\mathbf{mode}$Integer Input
On entry: a code for selecting the operation to be performed by the routine.
${\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 g05rzf.
${\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: $\mathbf{n}$Integer Input
On entry: $n$, the number of random variates required.
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{xmu}\left({\mathbf{m}}\right)$Real (Kind=nag_wp) array Input
On entry: $a$, the vector of means of the distribution.
5: $\mathbf{c}\left({\mathbf{ldc}},{\mathbf{m}}\right)$Real (Kind=nag_wp) array Input
On entry: the covariance matrix of the distribution. Only the upper triangle need be set.
Constraint: $C$ must be positive semidefinite to machine precision.
6: $\mathbf{ldc}$Integer Input
On entry: the first dimension of the array c as declared in the (sub)program from which g05rzf is called.
Constraint: ${\mathbf{ldc}}\ge {\mathbf{m}}$.
7: $\mathbf{r}\left({\mathbf{lr}}\right)$Real (Kind=nag_wp) array Communication Array
On entry: if ${\mathbf{mode}}=1$ or $3$, the reference vector as set up by g05rzf in a previous call with ${\mathbf{mode}}=0$, $2$ or $4$.
On exit: if ${\mathbf{mode}}=0$ or $2$, the reference vector that can be used in subsequent calls to g05rzf with ${\mathbf{mode}}=1$ or $3$.
8: $\mathbf{lr}$Integer Input
On entry: the dimension of the array r as declared in the (sub)program from which g05rzf is called. If ${\mathbf{mode}}=1$ or $3$, it must be the same as the value of lr specified in the prior call to g05rzf with ${\mathbf{mode}}=0$, $2$ or $4$.
Constraint: ${\mathbf{lr}}\ge {\mathbf{m}}×\left({\mathbf{m}}+1\right)+1$.
9: $\mathbf{state}\left(*\right)$Integer array Communication Array
Note: the actual argument supplied must be the array state supplied to the initialization routines g05kff or g05kgf.
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
10: $\mathbf{x}\left({\mathbf{ldx}},*\right)$Real (Kind=nag_wp) array Output
Note: the second dimension of the array x must be at least ${\mathbf{m}}$ if ${\mathbf{mode}}=1$ or $2$ and at least ${\mathbf{n}}$ if ${\mathbf{mode}}=3$ or $4$.
On exit: the array of pseudorandom multivariate Normal vectors generated by the routine.
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.
11: $\mathbf{ldx}$Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which g05rzf is called.
Constraints:
• if ${\mathbf{mode}}=1$ or $2$, ${\mathbf{ldx}}\ge {\mathbf{n}}$;
• if ${\mathbf{mode}}=3$ or $4$, ${\mathbf{ldx}}\ge {\mathbf{m}}$.
12: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, invalid value for mode.
Constraint: ${\mathbf{mode}}=0$, $1$, $2$, $3$ or $4$.
${\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}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

Not applicable.

## 8Parallelism and Performance

g05rzf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g05rzf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

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

## 10Example

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 g05rzf. All ten observations are generated by a single call to g05rzf with ${\mathbf{mode}}=2$. The random number generator is initialized by g05kff.

### 10.1Program Text

Program Text (g05rzfe.f90)

### 10.2Program Data

Program Data (g05rzfe.d)

### 10.3Program Results

Program Results (g05rzfe.r)