NAG FL Interface
g03adf (canon_​corr)

Settings help

FL Name Style:


FL Specification Language:


1 Purpose

g03adf performs canonical correlation analysis upon input data matrices.

2 Specification

Fortran Interface
Subroutine g03adf ( weight, n, m, z, ldz, isz, nx, ny, wt, e, lde, ncv, cvx, ldcvx, mcv, cvy, ldcvy, tol, wk, iwk, ifail)
Integer, Intent (In) :: n, m, ldz, isz(m), nx, ny, lde, ldcvx, mcv, ldcvy, iwk
Integer, Intent (Inout) :: ifail
Integer, Intent (Out) :: ncv
Real (Kind=nag_wp), Intent (In) :: z(ldz,m), wt(*), tol
Real (Kind=nag_wp), Intent (Inout) :: e(lde,6), cvx(ldcvx,mcv), cvy(ldcvy,mcv)
Real (Kind=nag_wp), Intent (Out) :: wk(iwk)
Character (1), Intent (In) :: weight
C Header Interface
#include <nag.h>
void  g03adf_ (const char *weight, const Integer *n, const Integer *m, const double z[], const Integer *ldz, const Integer isz[], const Integer *nx, const Integer *ny, const double wt[], double e[], const Integer *lde, Integer *ncv, double cvx[], const Integer *ldcvx, const Integer *mcv, double cvy[], const Integer *ldcvy, const double *tol, double wk[], const Integer *iwk, Integer *ifail, const Charlen length_weight)
The routine may be called by the names g03adf or nagf_mv_canon_corr.

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×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 = min(kx,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-i)(ky-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 Arguments

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 Integer Input
On entry: n, the number of observations.
Constraint: n>nx+ny.
3: m Integer Input
On entry: m, the total number of variables.
Constraint: mnx+ny.
4: z(ldz,m) Real (Kind=nag_wp) array Input
On entry: z(i,j) 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 Integer Input
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 array Input
On entry: isz(j) indicates whether or not the jth variable is included in the analysis and to which set of variables it belongs.
isz(j)>0
The variable contained in the jth column of z is included as an x variable in the analysis.
isz(j)<0
The variable contained in the jth column of z is included as a y variable in the analysis.
isz(j)=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 Integer Input
On entry: the number of x variables in the analysis, nx.
Constraint: nx1.
8: ny Integer Input
On entry: the number of y variables in the analysis, ny.
Constraint: ny1.
9: wt(*) Real (Kind=nag_wp) array Input
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 wt(i)=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:
  • wt(i)0.0, for i=1,2,,n;
  • the sum of weightsnx+ny+1.
10: e(lde,6) Real (Kind=nag_wp) array Output
On exit: the statistics of the canonical variate analysis.
e(i,1)
The canonical correlations, δi, for i=1,2,,l.
e(i,2)
The eigenvalues of Σ, λi2, for i=1,2,,l.
e(i,3)
The proportion of variation explained by the ith canonical variate, for i=1,2,,l.
e(i,4)
The χ2 statistic for the ith canonical variate, for i=1,2,,l.
e(i,5)
The degrees of freedom for χ2 statistic for the ith canonical variate, for i=1,2,,l.
e(i,6)
The significance level for the χ2 statistic for the ith canonical variate, for i=1,2,,l.
11: lde Integer Input
On entry: the first dimension of the array e as declared in the (sub)program from which g03adf is called.
Constraint: ldemin(nx,ny).
12: ncv Integer Output
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) array Output
On exit: the canonical variate loadings for the x variables. cvx(i,j) contains the loading coefficient for the ith x variable on the jth canonical variate.
14: ldcvx Integer Input
On entry: the first dimension of the array cvx as declared in the (sub)program from which g03adf is called.
Constraint: ldcvxnx.
15: mcv Integer Input
On entry: an upper limit to the number of canonical variates.
Constraint: mcvmin(nx,ny).
16: cvy(ldcvy,mcv) Real (Kind=nag_wp) array Output
On exit: the canonical variate loadings for the y variables. cvy(i,j) contains the loading coefficient for the ith y variable on the jth canonical variate.
17: ldcvy Integer Input
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) array Workspace
20: iwk Integer Input
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+max((5×(nx-1)+nx×nx),n×ny)+1;
  • if nx<ny, iwkn×ny+nx+ny+max((5×(ny-1)+ny×ny),n×nx)+1.
21: 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 -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, iwk=value
Constraint: iwkvalue.
On entry, ldcvx=value and nx=value.
Constraint: ldcvxnx.
On entry, ldcvy=value and ny=value.
Constraint: ldcvyny.
On entry, lde=value and min(nx,ny)=value.
Constraint: ldemin(nx,ny).
On entry, ldz=value and n=value.
Constraint: ldzn.
On entry, m=value and nx+ny=value.
Constraint: mnx+ny.
On entry, mcv=value and min(nx,ny)=value.
Constraint: mcvmin(nx,ny).
On entry, n=value and nx+ny=value.
Constraint: n>nx+ny.
On entry, nx=value.
Constraint: nx1.
On entry, ny=value.
Constraint: ny1.
On entry, tol=value.
Constraint: tol0.0.
On entry, weight=value.
Constraint: weight='U' or 'W'.
ifail=2
On entry, i=value and wt(i)<0.0.
Constraint: wt(i)0.0.
ifail=3
On entry, nx=value, expected value=value.
Constraint: nx must be consistent with isz.
On entry, ny=value, expected value=value.
Constraint: ny must be consistent with isz.
ifail=4
On entry, the effective number of observations is less than nx+ny+1.
ifail=5
The singular value decomposition has failed to converge. This is an unlikely error exit.
ifail=6
A canonical correlation is equal to 1.0. This will happen if the x and y variables are perfectly correlated.
ifail=7
On entry, the rank of the X matrix is 0.
On entry, the rank of the Y matrix is 0.
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.
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.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

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 Parallelism and Performance

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

9 Further Comments

None.

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

10.1 Program Text

Program Text (g03adfe.f90)

10.2 Program Data

Program Data (g03adfe.d)

10.3 Program Results

Program Results (g03adfe.r)