G03ADF (PDF version)
G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G03ADF

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.

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

G03ADF performs canonical correlation analysis upon input data matrices.

2  Specification

SUBROUTINE G03ADF ( WEIGHT, N, M, Z, LDZ, ISZ, NX, NY, WT, E, LDE, NCV, CVX, LDCVX, MCV, CVY, LDCVY, TOL, WK, IWK, IFAIL)
INTEGER  N, M, LDZ, ISZ(M), NX, NY, LDE, NCV, LDCVX, MCV, LDCVY, IWK, IFAIL
REAL (KIND=nag_wp)  Z(LDZ,M), WT(*), E(LDE,6), CVX(LDCVX,MCV), CVY(LDCVY,MCV), TOL, WK(IWK)
CHARACTER(1)  WEIGHT

3  Description

Let there be two sets of variables, x and y. For a sample of n observations on nx variables in a data matrix X and ny variables in a data matrix Y, canonical correlation analysis seeks to find a small number of linear combinations of each set of variables in order to explain or summarise the relationships between them. The variables thus formed are known as canonical variates.
Let the variance-covariance matrix of the two datasets be
Sxx Sxy Syx Syy
and let
Σ=Syy -1SyxSxx -1Sxy
then the canonical correlations can be calculated from the eigenvalues of the matrix Σ. However, G03ADF calculates the canonical correlations by means of a singular value decomposition (SVD) of a matrix V. If the rank of the data matrix X is kx and the rank of the data matrix Y is ky, and both X and Y have had variable (column) means subtracted then the kx by ky matrix V is given by:
V=QxTQy,
where Qx is the first kx columns of the orthogonal matrix Q either from the QR decomposition of X if X is of full column rank, i.e., kx=nx:
X=QxRx
or from the SVD of X if kx<nx:
X=QxDxPxT.
Similarly Qy is the first ky columns of the orthogonal matrix Q either from the QR decomposition of Y if Y is of full column rank, i.e., ky=ny:
Y=QyRy
or from the SVD of Y if ky<ny:
Y=QyDyPyT.
Let the SVD of V be:
V=UxΔUyT
then the nonzero elements of the diagonal matrix Δ, δi, for i=1,2,,l, are the l canonical correlations associated with the l canonical variates, where l = minkx,ky .
The eigenvalues, λi2, of the matrix Σ are given by:
λi2 = δi2 .
The value of πi=λi2/λi2 gives the proportion of variation explained by the ith canonical variate. The values of the πi's give an indication as to how many canonical variates are needed to adequately describe the data, i.e., the dimensionality of the problem.
To test for a significant dimensionality greater than i the χ2 statistic:
n - 12 kx + ky + 3 j=i+1 l log 1 - δj2
can be used. This is asymptotically distributed as a χ2-distribution with kx-iky-i degrees of freedom. If the test for i=kmin is not significant, then the remaining tests for i>kmin should be ignored.
The loadings for the canonical variates are calculated from the matrices Ux and Uy respectively. These matrices are scaled so that the canonical variates have unit variance.

4  References

Hastings N A J and Peacock J B (1975) Statistical Distributions Butterworth
Kendall M G and Stuart A (1976) The Advanced Theory of Statistics (Volume 3) (3rd Edition) Griffin
Morrison D F (1967) Multivariate Statistical Methods McGraw–Hill

5  Parameters

1:     WEIGHT – CHARACTER(1)Input
On entry: indicates if weights are to be used.
WEIGHT='U'
No weights are used.
WEIGHT='W'
Weights are used and must be supplied in WT.
Constraint: WEIGHT='U' or 'W'.
2:     N – INTEGERInput
On entry: n, the number of observations.
Constraint: N>NX+NY.
3:     M – INTEGERInput
On entry: m, the total number of variables.
Constraint: MNX+NY.
4:     Z(LDZ,M) – REAL (KIND=nag_wp) arrayInput
On entry: Zij must contain the ith observation for the jth variable, for i=1,2,,n and j=1,2,,m.
Both x and y variables are to be included in Z, the indicator array, ISZ, being used to assign the variables in Z to the x or y sets as appropriate.
5:     LDZ – INTEGERInput
On entry: the first dimension of the array Z as declared in the (sub)program from which G03ADF is called.
Constraint: LDZN.
6:     ISZ(M) – INTEGER arrayInput
On entry: ISZj indicates whether or not the jth variable is included in the analysis and to which set of variables it belongs.
ISZj>0
The variable contained in the jth column of Z is included as an x variable in the analysis.
ISZj<0
The variable contained in the jth column of Z is included as a y variable in the analysis.
ISZj=0
The variable contained in the jth column of Z is not included in the analysis.
Constraint: only NX elements of ISZ can be >0 and only NY elements of ISZ can be <0.
7:     NX – INTEGERInput
On entry: the number of x variables in the analysis, nx.
Constraint: NX1.
8:     NY – INTEGERInput
On entry: the number of y variables in the analysis, ny.
Constraint: NY1.
9:     WT(*) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least N if WEIGHT='W', and at least 1 otherwise.
On entry: if WEIGHT='W', the first n elements of WT must contain the weights to be used in the analysis.
If WTi=0.0, the ith observation is not included in the analysis. The effective number of observations is the sum of weights.
If WEIGHT='U', WT is not referenced and the effective number of observations is n.
Constraints:
  • WTi0.0, for i=1,2,,n;
  • the sum of weightsNX+NY+1.
10:   E(LDE,6) – REAL (KIND=nag_wp) arrayOutput
On exit: the statistics of the canonical variate analysis.
Ei1
The canonical correlations, δi, for i=1,2,,l.
Ei2
The eigenvalues of Σ, λi2, for i=1,2,,l.
Ei3
The proportion of variation explained by the ith canonical variate, for i=1,2,,l.
Ei4
The χ2 statistic for the ith canonical variate, for i=1,2,,l.
Ei5
The degrees of freedom for χ2 statistic for the ith canonical variate, for i=1,2,,l.
Ei6
The significance level for the χ2 statistic for the ith canonical variate, for i=1,2,,l.
11:   LDE – INTEGERInput
On entry: the first dimension of the array E as declared in the (sub)program from which G03ADF is called.
Constraint: LDEminNX,NY.
12:   NCV – INTEGEROutput
On exit: the number of canonical correlations, l. This will be the minimum of the rank of X and the rank of Y.
13:   CVX(LDCVX,MCV) – REAL (KIND=nag_wp) arrayOutput
On exit: the canonical variate loadings for the x variables. CVXij contains the loading coefficient for the ith x variable on the jth canonical variate.
14:   LDCVX – INTEGERInput
On entry: the first dimension of the array CVX as declared in the (sub)program from which G03ADF is called.
Constraint: LDCVXNX.
15:   MCV – INTEGERInput
On entry: an upper limit to the number of canonical variates.
Constraint: MCVminNX,NY.
16:   CVY(LDCVY,MCV) – REAL (KIND=nag_wp) arrayOutput
On exit: the canonical variate loadings for the y variables. CVYij contains the loading coefficient for the ith y variable on the jth canonical variate.
17:   LDCVY – INTEGERInput
On entry: the first dimension of the array CVY as declared in the (sub)program from which G03ADF is called.
Constraint: LDCVYNY.
18:   TOL – REAL (KIND=nag_wp)Input
On entry: the value of TOL is used to decide if the variables are of full rank and, if not, what is the rank of the variables. The smaller the value of TOL the stricter the criterion for selecting the singular value decomposition. If a non-negative value of TOL less than machine precision is entered, the square root of machine precision is used instead.
Constraint: TOL0.0.
19:   WK(IWK) – REAL (KIND=nag_wp) arrayWorkspace
20:   IWK – INTEGERInput
On entry: the dimension of the array WK as declared in the (sub)program from which G03ADF is called.
Constraints:
  • if NXNY, IWKN×NX+NX+NY+max5×NX-1+NX×NX,N×NY+1;
  • if NX<NY, IWKN×NY+NX+NY+max5×NY-1+NY×NY,N×NX+1.
21:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ 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​ 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 -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry 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:
IFAIL=1
On entry,NX<1,
orNY<1,
orM<NX+NY,
orNNX+NY,
or MCV < minNX,NY ,
orLDZ<N,
orLDCVX<NX,
orLDCVY<NY,
or LDE < minNX,NY ,
orNXNY and
IWK<N×NX+NX+NY+max5×NX-1+NX×NX,N×NY,
orNX<NY and
IWK<N×NY+NX+NY+max5×NY-1+NY×NY,N×NX,
orWEIGHT'U' or 'W',
orTOL<0.0.
IFAIL=2
On entry,a WEIGHT='W' and value of WT<0.0.
IFAIL=3
On entry,the number of x variables to be included in the analysis as indicated by ISZ is not equal to NX.
orthe number of y variables to be included in the analysis as indicated by ISZ is not equal to NY.
IFAIL=4
On entry,the effective number of observations is less than NX+NY+1.
IFAIL=5
A singular value decomposition has failed to converge. See F02WUF. This is an unlikely error exit.
IFAIL=6
A canonical correlation is equal to 1. This will happen if the x and y variables are perfectly correlated.
IFAIL=7
On entry, the rank of the X matrix or the rank of the Y matrix is 0. This will happen if all the x or y variables are constants.

7  Accuracy

As the computation involves the use of orthogonal matrices and a singular value decomposition rather than the traditional computing of a sum of squares matrix and the use of an eigenvalue decomposition, G03ADF should be less affected by ill-conditioned problems.

8  Further Comments

None.

9  Example

This example has nine observations and two variables in each set of the four variables read in, the second and third are x variables while the first and last are y variables. Canonical variate analysis is performed and the results printed.

9.1  Program Text

Program Text (g03adfe.f90)

9.2  Program Data

Program Data (g03adfe.d)

9.3  Program Results

Program Results (g03adfe.r)


G03ADF (PDF version)
G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012