hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_rand_matrix_corr (g05py)


    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example


nag_rand_matrix_corr (g05py) generates a random correlation matrix with given eigenvalues.


[state, c, ifail] = g05py(d, state, 'n', n, 'eps', eps)
[state, c, ifail] = nag_rand_matrix_corr(d, state, 'n', n, 'eps', eps)


Given n eigenvalues, λ1,λ2,,λn, such that
λi 0,   i= 1,2,,n,  
nag_rand_matrix_corr (g05py) 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 nag_rand_matrix_orthog (g05px) 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 nag_rand_init_repeat (g05kf) (for a repeatable sequence if computed sequentially) or nag_rand_init_nonrepeat (g05kg) (for a non-repeatable sequence) must be called prior to the first call to nag_rand_matrix_corr (g05py).


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


Compulsory Input Parameters

1:     dn – double array
The n eigenvalues, λi, for i=1,2,,n.
  • di0.0, for i=1,2,,n;
  • i=1ndi=n to within eps.
2:     state: int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains information on the selected base generator and its current state.

Optional Input Parameters

1:     n int64int32nag_int scalar
Default: the dimension of the array d.
n, the dimension of the correlation matrix to be generated.
Constraint: n1.
2:     eps – double scalar
Default: 0.00001
The maximum acceptable error in the diagonal elements.
Constraint: epsn×machine precision (see Chapter X02).

Output Parameters

1:     state: int64int32nag_int array
Contains updated information on the state of the generator.
2:     cldcn – double array
A random correlation matrix, C, of dimension n.
3:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:
Constraint: n1.
On entry, an eigenvalue is negative.
On entry, the eigenvalues do not sum to n.
Constraint: epsn×machine precision.
On entry, state vector has been corrupted or not initialized.
The diagonals of the returned matrix are not unity, try increasing the value of eps, or rerun the code using a different seed.
Constraint: ldcn.
An unexpected error has been triggered by this routine. Please contact NAG.
Your licence key may have expired or may not have been installed correctly.
Dynamic memory allocation failed.


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

Further Comments

The time taken by nag_rand_matrix_corr (g05py) is approximately proportional to n2.


Following initialization of the pseudorandom number generator by a call to nag_rand_init_repeat (g05kf), a 3 by 3 correlation matrix with eigenvalues of 0.7, 0.9 and 1.4 is generated and printed.
function g05py_example

fprintf('g05py example results\n\n');

% Initialize the base generator to a repeatable sequence
seed  = [int64(1762543)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
                        genid, subid, seed);

% Eigenvalues
d = [0.7; 0.9; 1.4];

% Generate the correlation matrix with eigenvalues d
[state, c, ifail] = g05py( ...
                           d, state);

disp('Correlation Matrix');

g05py example results

Correlation Matrix
    1.0000   -0.2549   -0.1004
   -0.2549    1.0000    0.2343
   -0.1004    0.2343    1.0000

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015