G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

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.

## 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 ${n}_{x}$ variables in a data matrix $X$ and ${n}_{y}$ 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 $\Sigma$. 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 ${k}_{x}$ and the rank of the data matrix $Y$ is ${k}_{y}$, and both $X$ and $Y$ have had variable (column) means subtracted then the ${k}_{x}$ by ${k}_{y}$ matrix $V$ is given by:
 $V=QxTQy,$
where ${Q}_{x}$ is the first ${k}_{x}$ columns of the orthogonal matrix $Q$ either from the $QR$ decomposition of $X$ if $X$ is of full column rank, i.e., ${k}_{x}={n}_{x}$:
 $X=QxRx$
or from the SVD of $X$ if ${k}_{x}<{n}_{x}$:
 $X=QxDxPxT.$
Similarly ${Q}_{y}$ is the first ${k}_{y}$ columns of the orthogonal matrix $Q$ either from the $QR$ decomposition of $Y$ if $Y$ is of full column rank, i.e., ${k}_{y}={n}_{y}$:
 $Y=QyRy$
or from the SVD of $Y$ if ${k}_{y}<{n}_{y}$:
 $Y=QyDyPyT.$
Let the SVD of $V$ be:
 $V=UxΔUyT$
then the nonzero elements of the diagonal matrix $\Delta$, ${\delta }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,l$, are the $l$ canonical correlations associated with the $l$ canonical variates, where $l=\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({k}_{x},{k}_{y}\right)$.
The eigenvalues, ${\lambda }_{i}^{2}$, of the matrix $\Sigma$ are given by:
 $λi2 = δi2 .$
The value of ${\pi }_{i}={\lambda }_{i}^{2}/\sum {\lambda }_{i}^{2}$ gives the proportion of variation explained by the $i$th canonical variate. The values of the ${\pi }_{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 ${\chi }^{2}$ statistic:
 $n - 12 kx + ky + 3 ∑ j=i+1 l log 1 - δj2$
can be used. This is asymptotically distributed as a ${\chi }^{2}$-distribution with $\left({k}_{x}-i\right)\left({k}_{y}-i\right)$ degrees of freedom. If the test for $i={k}_{\mathrm{min}}$ is not significant, then the remaining tests for $i>{k}_{\mathrm{min}}$ should be ignored.
The loadings for the canonical variates are calculated from the matrices ${U}_{x}$ and ${U}_{y}$ respectively. These matrices are scaled so that the canonical variates have unit variance.
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.
${\mathbf{WEIGHT}}=\text{'U'}$
No weights are used.
${\mathbf{WEIGHT}}=\text{'W'}$
Weights are used and must be supplied in WT.
Constraint: ${\mathbf{WEIGHT}}=\text{'U'}$ or $\text{'W'}$.
2:     N – INTEGERInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{N}}>{\mathbf{NX}}+{\mathbf{NY}}$.
3:     M – INTEGERInput
On entry: $m$, the total number of variables.
Constraint: ${\mathbf{M}}\ge {\mathbf{NX}}+{\mathbf{NY}}$.
4:     Z(LDZ,M) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{Z}}\left(\mathit{i},\mathit{j}\right)$ must contain the $\mathit{i}$th observation for the $\mathit{j}$th variable, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,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: ${\mathbf{LDZ}}\ge {\mathbf{N}}$.
6:     ISZ(M) – INTEGER arrayInput
On entry: ${\mathbf{ISZ}}\left(j\right)$ indicates whether or not the $j$th variable is included in the analysis and to which set of variables it belongs.
${\mathbf{ISZ}}\left(j\right)>0$
The variable contained in the $j$th column of Z is included as an $x$ variable in the analysis.
${\mathbf{ISZ}}\left(j\right)<0$
The variable contained in the $j$th column of Z is included as a $y$ variable in the analysis.
${\mathbf{ISZ}}\left(j\right)=0$
The variable contained in the $j$th column of Z is not included in the analysis.
Constraint: only NX elements of ISZ can be $\text{}>0$ and only NY elements of ISZ can be $\text{}<0$.
7:     NX – INTEGERInput
On entry: the number of $x$ variables in the analysis, ${n}_{x}$.
Constraint: ${\mathbf{NX}}\ge 1$.
8:     NY – INTEGERInput
On entry: the number of $y$ variables in the analysis, ${n}_{y}$.
Constraint: ${\mathbf{NY}}\ge 1$.
9:     WT($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least ${\mathbf{N}}$ if ${\mathbf{WEIGHT}}=\text{'W'}$, and at least $1$ otherwise.
On entry: if ${\mathbf{WEIGHT}}=\text{'W'}$, the first $n$ elements of WT must contain the weights to be used in the analysis.
If ${\mathbf{WT}}\left(i\right)=0.0$, the $i$th observation is not included in the analysis. The effective number of observations is the sum of weights.
If ${\mathbf{WEIGHT}}=\text{'U'}$, WT is not referenced and the effective number of observations is $n$.
Constraints:
• ${\mathbf{WT}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=1,2,\dots ,n$;
• the $\text{sum of weights}\ge {\mathbf{NX}}+{\mathbf{NY}}+1$.
10:   E(LDE,$6$) – REAL (KIND=nag_wp) arrayOutput
On exit: the statistics of the canonical variate analysis.
${\mathbf{E}}\left(i,1\right)$
The canonical correlations, ${\delta }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,l$.
${\mathbf{E}}\left(i,2\right)$
The eigenvalues of $\Sigma$, ${\lambda }_{\mathit{i}}^{2}$, for $\mathit{i}=1,2,\dots ,l$.
${\mathbf{E}}\left(i,3\right)$
The proportion of variation explained by the $\mathit{i}$th canonical variate, for $\mathit{i}=1,2,\dots ,l$.
${\mathbf{E}}\left(i,4\right)$
The ${\chi }^{2}$ statistic for the $\mathit{i}$th canonical variate, for $\mathit{i}=1,2,\dots ,l$.
${\mathbf{E}}\left(i,5\right)$
The degrees of freedom for ${\chi }^{2}$ statistic for the $\mathit{i}$th canonical variate, for $\mathit{i}=1,2,\dots ,l$.
${\mathbf{E}}\left(i,6\right)$
The significance level for the ${\chi }^{2}$ statistic for the $\mathit{i}$th canonical variate, for $\mathit{i}=1,2,\dots ,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: ${\mathbf{LDE}}\ge \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{NX}},{\mathbf{NY}}\right)$.
12:   NCV – INTEGEROutput
On exit: the number of canonical correlations, $l$. This will be the minimum of the rank of $\mathrm{X}$ and the rank of $\mathrm{Y}$.
13:   CVX(LDCVX,MCV) – REAL (KIND=nag_wp) arrayOutput
On exit: the canonical variate loadings for the $x$ variables. ${\mathbf{CVX}}\left(i,j\right)$ contains the loading coefficient for the $i$th $x$ variable on the $j$th 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: ${\mathbf{LDCVX}}\ge {\mathbf{NX}}$.
15:   MCV – INTEGERInput
On entry: an upper limit to the number of canonical variates.
Constraint: ${\mathbf{MCV}}\ge \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{NX}},{\mathbf{NY}}\right)$.
16:   CVY(LDCVY,MCV) – REAL (KIND=nag_wp) arrayOutput
On exit: the canonical variate loadings for the $y$ variables. ${\mathbf{CVY}}\left(i,j\right)$ contains the loading coefficient for the $i$th $y$ variable on the $j$th 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: ${\mathbf{LDCVY}}\ge {\mathbf{NY}}$.
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: ${\mathbf{TOL}}\ge 0.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 ${\mathbf{NX}}\ge {\mathbf{NY}}$, ${\mathbf{IWK}}\ge {\mathbf{N}}×{\mathbf{NX}}+{\mathbf{NX}}+{\mathbf{NY}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(\left(5×\left({\mathbf{NX}}-1\right)+{\mathbf{NX}}×{\mathbf{NX}}\right),{\mathbf{N}}×{\mathbf{NY}}\right)+1$;
• if ${\mathbf{NX}}<{\mathbf{NY}}$, ${\mathbf{IWK}}\ge {\mathbf{N}}×{\mathbf{NY}}+{\mathbf{NX}}+{\mathbf{NY}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(\left(5×\left({\mathbf{NY}}-1\right)+{\mathbf{NY}}×{\mathbf{NY}}\right),{\mathbf{N}}×{\mathbf{NX}}\right)+1$.
21:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ 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\text{​ 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 $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{NX}}<1$, or ${\mathbf{NY}}<1$, or ${\mathbf{M}}<{\mathbf{NX}}+{\mathbf{NY}}$, or ${\mathbf{N}}\le {\mathbf{NX}}+{\mathbf{NY}}$, or ${\mathbf{MCV}}<\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{NX}},{\mathbf{NY}}\right)$, or ${\mathbf{LDZ}}<{\mathbf{N}}$, or ${\mathbf{LDCVX}}<{\mathbf{NX}}$, or ${\mathbf{LDCVY}}<{\mathbf{NY}}$, or ${\mathbf{LDE}}<\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{NX}},{\mathbf{NY}}\right)$, or ${\mathbf{NX}}\ge {\mathbf{NY}}$ and ${\mathbf{IWK}}<{\mathbf{N}}×{\mathbf{NX}}+{\mathbf{NX}}+{\mathbf{NY}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(\left(5×\left({\mathbf{NX}}-1\right)+{\mathbf{NX}}×{\mathbf{NX}}\right),{\mathbf{N}}×{\mathbf{NY}}\right)$, or ${\mathbf{NX}}<{\mathbf{NY}}$ and ${\mathbf{IWK}}<{\mathbf{N}}×{\mathbf{NY}}+{\mathbf{NX}}+{\mathbf{NY}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(\left(5×\left({\mathbf{NY}}-1\right)+{\mathbf{NY}}×{\mathbf{NY}}\right),{\mathbf{N}}×{\mathbf{NX}}\right)$, or ${\mathbf{WEIGHT}}\ne \text{'U'}$ or $\text{'W'}$, or ${\mathbf{TOL}}<0.0$.
${\mathbf{IFAIL}}=2$
 On entry, a ${\mathbf{WEIGHT}}=\text{'W'}$ and value of ${\mathbf{WT}}<0.0$.
${\mathbf{IFAIL}}=3$
 On entry, the number of $x$ variables to be included in the analysis as indicated by ISZ is not equal to NX. or the number of $y$ variables to be included in the analysis as indicated by ISZ is not equal to NY.
${\mathbf{IFAIL}}=4$
 On entry, the effective number of observations is less than ${\mathbf{NX}}+{\mathbf{NY}}+1$.
${\mathbf{IFAIL}}=5$
A singular value decomposition has failed to converge. See F02WUF. This is an unlikely error exit.
${\mathbf{IFAIL}}=6$
A canonical correlation is equal to $1$. This will happen if the $x$ and $y$ variables are perfectly correlated.
${\mathbf{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.

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.