g05 Chapter Contents
g05 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_rngs_corr_matrix (g05qbc)

## 1  Purpose

nag_rngs_corr_matrix (g05qbc) generates a random correlation matrix with given eigenvalues.

## 2  Specification

 #include #include
 void nag_rngs_corr_matrix (Nag_OrderType order, Integer n, const double d[], double c[], Integer pdc, double eps, Integer igen, Integer iseed[], NagError *fail)

## 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,$
nag_rngs_corr_matrix (g05qbc) 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 nag_rngs_orthog_matrix (g05qac) 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 functions nag_rngs_init_repeatable (g05kbc) (for a repeatable sequence if computed sequentially) or nag_rngs_init_nonrepeatable (g05kcc) (for a non-repeatable sequence) must be called prior to the first call to nag_rngs_corr_matrix (g05qbc).

## 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  Arguments

1:     orderNag_OrderTypeInput
On entry: the order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by ${\mathbf{order}}=\mathrm{Nag_RowMajor}$. See Section 3.2.1.3 in the Essential Introduction for a more detailed explanation of the use of this argument.
Constraint: ${\mathbf{order}}=\mathrm{Nag_RowMajor}$ or Nag_ColMajor.
2:     nIntegerInput
On entry: $n$, the dimension of the correlation matrix to be generated.
Constraint: ${\mathbf{n}}\ge 1$.
3:     d[n]const doubleInput
On entry: the $n$ eigenvalues, ${\lambda }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
Constraints:
• ${\mathbf{d}}\left[\mathit{i}-1\right]\ge 0.0$, for $\mathit{i}=1,2,\dots ,n$;
• $\sum _{i=1}^{n}{\mathbf{d}}\left[i-1\right]=n$ to within eps.
4:     c[$\mathit{dim}$]doubleOutput
Note: the dimension, dim, of the array c must be at least ${\mathbf{pdc}}×{\mathbf{n}}$.
The $\left(i,j\right)$th element of the matrix $C$ is stored in
• ${\mathbf{c}}\left[\left(j-1\right)×{\mathbf{pdc}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{c}}\left[\left(i-1\right)×{\mathbf{pdc}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On exit: a random correlation matrix, $C$, of dimension $n$.
5:     pdcIntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array c.
Constraint: ${\mathbf{pdc}}\ge {\mathbf{n}}$.
6:     epsdoubleInput
On entry: $\epsilon$the maximum acceptable error in the diagonal elements.
Suggested value: ${\mathbf{eps}}=0.00001$.
Constraint:  (see Chapter x02).
7:     igenIntegerInput
On entry: must contain the identification number for the generator to be used to return a pseudorandom number and should remain unchanged following initialization by a prior call to nag_rngs_init_repeatable (g05kbc) or nag_rngs_init_nonrepeatable (g05kcc).
8:     iseed[$4$]IntegerCommunication Array
On entry: contains values which define the current state of the selected generator.
On exit: contains updated values defining the new state of the selected generator.
9:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_DIAG_ELEMENTS
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 nag_rngs_init_repeatable (g05kbc) or nag_rngs_init_nonrepeatable (g05kcc).
NE_EIGVAL_SUM
On entry, the eigenvalues do not sum to n.
NE_INT
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.
On entry, ${\mathbf{pdc}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdc}}>0$.
NE_INT_2
On entry, ${\mathbf{pdc}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdc}}\ge {\mathbf{n}}$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_NEGATIVE_EIGVAL
On entry, an eigenvalue is negative.
NE_REAL
On entry, ${\mathbf{eps}}=〈\mathit{\text{value}}〉$.
Constraint: .

## 7  Accuracy

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

The time taken by nag_rngs_corr_matrix (g05qbc) is approximately proportional to ${n}^{2}$.

## 9  Example

Following initialization of the pseudorandom number generator by a call to nag_rngs_init_repeatable (g05kbc), 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 (g05qbce.c)

### 9.2  Program Data

Program Data (g05qbce.d)

### 9.3  Program Results

Program Results (g05qbce.r)