| 1 |
#ifndef _CCA_h_ |
| 2 |
#define _CCA_h_ |
| 3 |
/* CCA.h |
| 4 |
* |
| 5 |
* Copyright (C) 1993-2023 David Weenink |
| 6 |
* |
| 7 |
* This code is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
/* |
| 22 |
djmw 2001 |
| 23 |
djmw 20020423 GPL header |
| 24 |
djmw 20110306 Latest modification. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "Eigen.h" |
| 28 |
#include "SSCP.h" |
| 29 |
#include "TableOfReal.h" |
| 30 |
|
| 31 |
#include "CCA_def.h" |
| 32 |
|
| 33 |
/* |
| 34 |
Class CCA represents the Canonical Correlation Analysis of two datasets |
| 35 |
(two tables with multivariate data, Table 1 was N rows x p columns, |
| 36 |
Table 2 was N rows x q columns, and p <= q). |
| 37 |
|
| 38 |
Interpretation: |
| 39 |
|
| 40 |
The eigenvectors v1 [i] en v2 [i] have the property that for the linear |
| 41 |
compounds |
| 42 |
|
| 43 |
c1 [1] = v1 [1]' . Table1 c2 [1]= v2 [1]' . Table2 |
| 44 |
.............................................. |
| 45 |
c1 [p] = v1 [p]' . Table1 c2 [p]= v2 [p]' . Table2 |
| 46 |
|
| 47 |
the sample correlation of c1 [1] and c2 [1] is greatest, the sample |
| 48 |
correlation of c1 [2] and c2 [2] is greatest among all linear compounds |
| 49 |
uncorrelated with c1 [1] and c2 [1], and so on, for all p possible pairs. |
| 50 |
*/ |
| 51 |
|
| 52 |
autoCCA CCA_create (integer numberOfCoefficients, integer ny, integer nx); |
| 53 |
|
| 54 |
void CCA_drawEigenvector (CCA me, Graphics g, int x_or_y, integer ivec, integer first, integer last, |
| 55 |
double ymin, double ymax, int weigh, double size_mm, conststring32 mark, int connect, bool garnish); |
| 56 |
|
| 57 |
double CCA_getEigenvectorElement (CCA me, int x_or_y, integer ivec, integer element); |
| 58 |
|
| 59 |
autoCCA TableOfReal_to_CCA (TableOfReal me, integer ny); |
| 60 |
/* |
| 61 |
Solves the canonical correlation analysis equations: |
| 62 |
|
| 63 |
(S12*inv(S22)*S12' - lambda S11)X1 = 0 (1) |
| 64 |
(S12'*inv(S11)*S12 - lambda S22)X2 = 0 (2) |
| 65 |
|
| 66 |
Where S12 = T1' * T2, S11 = T1' * T1 and S22 = T2' * T2. |
| 67 |
Given the following svd's: |
| 68 |
|
| 69 |
svd (T1) = U1 D1 V1' |
| 70 |
svd (T2) = U2 D2 V2' |
| 71 |
|
| 72 |
We can write down: |
| 73 |
|
| 74 |
inv(S11) = V1 * D1^-2 * V1' and inv(S22) = V2 * D2^-2 * V2', |
| 75 |
and S12*inv(S22)*S12' simplifies to: V1*D1*U1'*U2 * U2'*U1*D1*V1' |
| 76 |
|
| 77 |
and (1) becomes: |
| 78 |
|
| 79 |
(V1*D1*U1'*U2 * U2'*U1*D1*V1' -lambda V1*D1 * D1*V1')X1 = 0 |
| 80 |
|
| 81 |
This can be written as: |
| 82 |
|
| 83 |
(V1*D1*U1'*U2 * U2'*U1 -lambda V1*D1) D1*V1'*X1 = 0 |
| 84 |
|
| 85 |
multiplying from the left with: D1^-1*V1' results in |
| 86 |
|
| 87 |
(U1'*U2 * U2'*U1 -lambda) D1*V1'*X1 = 0 |
| 88 |
|
| 89 |
Taking the svd(U2'*U1) = U D V' we get: |
| 90 |
|
| 91 |
(D^2 -lambda)V'*D1*V1'*X1 = 0 |
| 92 |
|
| 93 |
The eigenvectors X1 can be formally written as: |
| 94 |
|
| 95 |
X1 = V1*inv(D1)*V |
| 96 |
|
| 97 |
Equation (2) results in: |
| 98 |
|
| 99 |
X2 = V2*inv(D2)*U |
| 100 |
*/ |
| 101 |
|
| 102 |
autoTableOfReal CCA_TableOfReal_scores (CCA me, TableOfReal thee, integer numberOfFactors); |
| 103 |
/* |
| 104 |
Return the factors in a table with 2*numberOfFactors columns. |
| 105 |
The first 'numberOfFactors' columns are the scores for the dependent part |
| 106 |
of the table the following 'numberOfFactors' columns are for the |
| 107 |
independent part. |
| 108 |
*/ |
| 109 |
|
| 110 |
autoTableOfReal CCA_TableOfReal_factorLoadings (CCA me, TableOfReal thee); |
| 111 |
/* |
| 112 |
Get the canonical factor loadings (also structure correlation coefficients), |
| 113 |
the correlation of a canonical variable with an original variable. |
| 114 |
*/ |
| 115 |
|
| 116 |
double CCA_getCorrelationCoefficient (CCA me, integer index); |
| 117 |
|
| 118 |
void CCA_getZeroCorrelationProbability (CCA me, integer index, double *out_prob, double *out_chisq, double *out_df); |
| 119 |
|
| 120 |
autoTableOfReal CCA_TableOfReal_predict (CCA me, TableOfReal thee, integer from); |
| 121 |
/* |
| 122 |
Given independent table, predict the dependent one, on the basis of |
| 123 |
the canonical correlations. |
| 124 |
*/ |
| 125 |
|
| 126 |
autoCCA SSCP_to_CCA (SSCP me, integer ny); |
| 127 |
|
| 128 |
#endif /* CCA.h */ |