NAG CL Interface
g05pyc (matrix_​corr)

Settings help

CL Name Style:


1 Purpose

g05pyc generates a random correlation matrix with given eigenvalues.

2 Specification

#include <nag.h>
void  g05pyc (Integer n, const double d[], double eps, Integer state[], double c[], Integer pdc, NagError *fail)
The function may be called by the names: g05pyc, nag_rand_matrix_corr or nag_rand_corr_matrix.

3 Description

Given n eigenvalues, λ1,λ2,,λn, such that
i=1nλi=n  
and
λi 0,   i= 1,2,,n,  
g05pyc will generate a random correlation matrix, C, of dimension n, with eigenvalues λ1,λ2,,λn.
The method used is based on that described by Lin and Bendel (1985). Let D be the diagonal matrix with values λ1,λ2,,λn and let A be a random orthogonal matrix generated by g05pxc then the matrix C0=A D AT is a random covariance matrix with eigenvalues λ1,λ2,,λn. The matrix C0 is transformed into a correlation matrix by means of n-1 elementary rotation matrices Pi such that C = Pn-1 Pn-2 P1 C0 P1T Pn-2T Pn-1T . The restriction on the sum of eigenvalues implies that for any diagonal element of C0>1, there is another diagonal element <1. The Pi 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 ε.
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 Pi which are chosen as arbitrarily as possible.
One of the initialization functions g05kfc (for a repeatable sequence if computed sequentially) or g05kgc (for a non-repeatable sequence) must be called prior to the first call to g05pyc.

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: n Integer Input
On entry: n, the dimension of the correlation matrix to be generated.
Constraint: n1.
2: d[n] const double Input
On entry: the n eigenvalues, λi, for i=1,2,,n.
Constraints:
  • d[i-1]0.0, for i=1,2,,n;
  • i=1nd[i-1]=n to within eps.
3: eps double Input
On entry: the maximum acceptable error in the diagonal elements.
Suggested value: eps=0.00001.
Constraint: epsn×machine precision (see Chapter X02).
4: state[dim] Integer Communication Array
Note: the dimension, dim, of this array is dictated by the requirements of associated functions that must have been previously called. This array MUST be the same array passed as argument state in the previous call to nag_rand_init_repeatable (g05kfc) or nag_rand_init_nonrepeatable (g05kgc).
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[n×pdc] double Output
On exit: a random correlation matrix, C, of dimension n.
6: pdc Integer Input
On entry: the stride separating column elements of the matrix C in the array c.
Constraint: pdcn.
7: fail NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

6 Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_DIAG_ELEMENTS
The diagonals of the returned matrix are not unity, try increasing the value of eps, or rerun the code using a different seed.
NE_EIGVAL_SUM
On entry, the eigenvalues do not sum to n.
NE_INT
On entry, n=value.
Constraint: n1.
On entry, pdc=value.
Constraint: pdc>0.
NE_INT_2
On entry, pdc=value and n=value.
Constraint: pdcn.
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.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_INVALID_STATE
On entry, state vector has been corrupted or not initialized.
NE_NEGATIVE_EIGVAL
On entry, an eigenvalue is negative.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_REAL
On entry, eps=value.
Constraint: epsn×machine precision.

7 Accuracy

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

8 Parallelism and Performance

g05pyc is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g05pyc 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 function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9 Further Comments

The time taken by g05pyc is approximately proportional to n2.

10 Example

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

10.1 Program Text

Program Text (g05pyce.c)

10.2 Program Data

Program Data (g05pyce.d)

10.3 Program Results

Program Results (g05pyce.r)