# NAG FL Interfaceg05pyf (matrix_​corr)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

g05pyf generates a random correlation matrix with given eigenvalues.

## 2Specification

Fortran Interface
 Subroutine g05pyf ( n, d, eps, c, ldc,
 Integer, Intent (In) :: n, ldc Integer, Intent (Inout) :: state(*), ifail Real (Kind=nag_wp), Intent (In) :: d(n), eps Real (Kind=nag_wp), Intent (Inout) :: c(ldc,n)
C Header Interface
#include <nag.h>
 void g05pyf_ (const Integer *n, const double d[], const double *eps, Integer state[], double c[], const Integer *ldc, Integer *ifail)
The routine may be called by the names g05pyf or nagf_rand_matrix_corr.

## 3Description

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

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: $n$, the dimension of the correlation matrix to be generated.
Constraint: ${\mathbf{n}}\ge 1$.
2: $\mathbf{d}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
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: $\mathbf{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: $\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.
5: $\mathbf{c}\left({\mathbf{ldc}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: a random correlation matrix, $C$, of dimension $n$.
6: $\mathbf{ldc}$Integer Input
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: $\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, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=2$
On entry, an eigenvalue is negative.
On entry, the eigenvalues do not sum to n.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{eps}}=⟨\mathit{\text{value}}⟩$.
Constraint: .
${\mathbf{ifail}}=4$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=5$
The diagonals of the returned matrix are not unity, try increasing the value of eps, or rerun the code using a different seed.
${\mathbf{ifail}}=6$
On entry, ${\mathbf{ldc}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldc}}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
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.

## 7Accuracy

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

## 8Parallelism and Performance

g05pyf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g05pyf 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.

## 9Further Comments

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

## 10Example

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

### 10.1Program Text

Program Text (g05pyfe.f90)

### 10.2Program Data

Program Data (g05pyfe.d)

### 10.3Program Results

Program Results (g05pyfe.r)