G05PYF (PDF version)
G05 Chapter Contents
G05 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG05PYF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G05PYF generates a random correlation matrix with given eigenvalues.

## 2  Specification

 SUBROUTINE G05PYF ( N, D, EPS, STATE, C, LDC, IFAIL)
 INTEGER N, STATE(*), LDC, IFAIL REAL (KIND=nag_wp) D(N), EPS, C(LDC,N)

## 3  Description

Given $n$ eigenvalues, ${\lambda }_{1},{\lambda }_{2},\dots ,{\lambda }_{n}$, such that
 $∑i=1nλi=n$
and
 $λi≥ 0, i= 1,2,…,n,$
G05PYF will generate a random correlation matrix, $C$, of dimension $n$, with eigenvalues ${\lambda }_{1},{\lambda }_{2},\dots ,{\lambda }_{n}$.
The method used is based on that described by Lin and Bendel (1985). Let $D$ be the diagonal matrix with values ${\lambda }_{1},{\lambda }_{2},\dots ,{\lambda }_{n}$ and let $A$ be a random orthogonal matrix generated by G05PXF then the matrix ${C}_{0}=AD{A}^{\mathrm{T}}$ is a random covariance matrix with eigenvalues ${\lambda }_{1},{\lambda }_{2},\dots ,{\lambda }_{n}$. The matrix ${C}_{0}$ is transformed into a correlation matrix by means of $n-1$ elementary rotation matrices ${P}_{i}$ such that $C={P}_{n-1}{P}_{n-2}\dots {P}_{1}{C}_{0}{P}_{1}^{\mathrm{T}}\dots {P}_{n-2}^{\mathrm{T}}{P}_{n-1}^{\mathrm{T}}$. The restriction on the sum of eigenvalues implies that for any diagonal element of ${C}_{0}>1$, there is another diagonal element $\text{}<1$. The ${P}_{i}$ are constructed from such pairs, chosen at random, to produce a unit diagonal element corresponding to the first element. This is repeated until all diagonal elements are $1$ to within a given tolerance $\epsilon$.
The randomness of $C$ should be interpreted only to the extent that $A$ is a random orthogonal matrix and $C$ is computed from $A$ using the ${P}_{i}$ which are chosen as arbitrarily as possible.
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 G05PYF.

## 4  References

Lin S P and Bendel R B (1985) Algorithm AS 213: Generation of population correlation on matrices with specified eigenvalues Appl. Statist. 34 193–198

## 5  Parameters

1:     N – INTEGERInput
On entry: $n$, the dimension of the correlation matrix to be generated.
Constraint: ${\mathbf{N}}\ge 1$.
2:     D(N) – REAL (KIND=nag_wp) arrayInput
On entry: the $n$ eigenvalues, ${\lambda }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
Constraints:
• ${\mathbf{D}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=1,2,\dots ,n$;
• $\sum _{i=1}^{n}{\mathbf{D}}\left(i\right)=n$ to within EPS.
3:     EPS – REAL (KIND=nag_wp)Input
On entry: the maximum acceptable error in the diagonal elements.
Suggested value: ${\mathbf{EPS}}=0.00001$.
Constraint:  (see Chapter X02).
4:     STATE($*$) – INTEGER arrayCommunication 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.
5:     C(LDC,N) – REAL (KIND=nag_wp) arrayOutput
On exit: a random correlation matrix, $C$, of dimension $n$.
6:     LDC – INTEGERInput
On entry: the first dimension of the array C as declared in the (sub)program from which G05PYF is called.
Constraint: ${\mathbf{LDC}}\ge {\mathbf{N}}$.
7:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{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, ${\mathbf{N}}<1$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{D}}\left(i\right)<0.0$ for some $i$, or $\sum _{i=1}^{n}{\mathbf{D}}\left(i\right)\ne n$.
${\mathbf{IFAIL}}=3$
 On entry, .
${\mathbf{IFAIL}}=4$
 On entry, STATE vector was not initialized or has been corrupted.
${\mathbf{IFAIL}}=5$
The error in a diagonal element is greater than EPS. The value of EPS should be increased. Otherwise the program could be rerun with a different value used for the seed of the random number generator, see G05KFF or G05KGF.
${\mathbf{IFAIL}}=6$
 On entry, ${\mathbf{LDC}}<{\mathbf{N}}$.

## 7  Accuracy

The maximum error in a diagonal element is given by EPS.

## 8  Further Comments

The time taken by G05PYF is approximately proportional to ${n}^{2}$.

## 9  Example

Following initialization of the pseudorandom number generator by a call to G05KFF, a $3$ by $3$ correlation matrix with eigenvalues of $0.7$, $0.9$ and $1.4$ is generated and printed.

### 9.1  Program Text

Program Text (g05pyfe.f90)

### 9.2  Program Data

Program Data (g05pyfe.d)

### 9.3  Program Results

Program Results (g05pyfe.r)

G05PYF (PDF version)
G05 Chapter Contents
G05 Chapter Introduction
NAG Library Manual