| 1 | #cxx . . $ `<file>/dwtools/CCA.h`/ifndef _CCA_h_ |
| 2 | #define _CCA_h_cxx . . $ `dwtools/CCA.h:2:9`! |
| 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"cxx . . $ `<file>/dwsys/Eigen.h`/ |
| 28 | #include "SSCP.h"cxx . . $ `<file>/dwtools/SSCP.h`/ |
| 29 | #include "TableOfReal.h"cxx . . $ `<file>/stat/TableOfReal.h`/ |
| 30 | |
| 31 | #include "CCA_def.h"cxx . . $ `<file>/dwtools/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 | autoCCAcxx . . $ autoCCA# CCA_createcxx . . $ CCA_create(76f6eb1c73d3899f). (integercxx . . $ integer# numberOfCoefficients, integercxx . . $ integer# ny, integercxx . . $ integer# nx); |
| 53 | |
| 54 | void CCA_drawEigenvectorcxx . . $ CCA_drawEigenvector(56dcdd8d34d668bf). (CCAcxx . . $ CCA# me, Graphicscxx . . $ Graphics# g, int x_or_y, integercxx . . $ integer# ivec, integercxx . . $ integer# first, integercxx . . $ integer# last, |
| 55 | double ymin, double ymax, int weigh, double size_mm, conststring32cxx . . $ conststring32# mark, int connect, bool garnish); |
| 56 | |
| 57 | double CCA_getEigenvectorElementcxx . . $ CCA_getEigenvectorElement(165274c1d9e25241). (CCAcxx . . $ CCA# me, int x_or_y, integercxx . . $ integer# ivec, integercxx . . $ integer# element); |
| 58 | |
| 59 | autoCCAcxx . . $ autoCCA# TableOfReal_to_CCAcxx . . $ TableOfReal_to_CCA(4453f70e552bae90). (TableOfRealcxx . . $ TableOfReal# me, integercxx . . $ 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 | autoTableOfRealcxx . . $ autoTableOfReal# CCA_TableOfReal_scorescxx . . $ CCA_TableOfReal_scores(4e4d732bab4facb5). (CCAcxx . . $ CCA# me, TableOfRealcxx . . $ TableOfReal# thee, integercxx . . $ 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 | autoTableOfRealcxx . . $ autoTableOfReal# CCA_TableOfReal_factorLoadingscxx . . $ CCA_TableOfReal_factorLoadings(10a85cc69d31a415). (CCAcxx . . $ CCA# me, TableOfRealcxx . . $ 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_getCorrelationCoefficientcxx . . $ CCA_getCorrelationCoefficient(d4d0b1accb937bb9). (CCAcxx . . $ CCA# me, integercxx . . $ integer# index); |
| 117 | |
| 118 | void CCA_getZeroCorrelationProbabilitycxx . . $ CCA_getZeroCorrelationProbability(92eabff09359fd6e). (CCAcxx . . $ CCA# me, integercxx . . $ integer# index, double *out_prob, double *out_chisq, double *out_df); |
| 119 | |
| 120 | autoTableOfRealcxx . . $ autoTableOfReal# CCA_TableOfReal_predictcxx . . $ CCA_TableOfReal_predict(4e4d732bab4facb5). (CCAcxx . . $ CCA# me, TableOfRealcxx . . $ TableOfReal# thee, integercxx . . $ integer# from); |
| 121 | /* |
| 122 | Given independent table, predict the dependent one, on the basis of |
| 123 | the canonical correlations. |
| 124 | */ |
| 125 | |
| 126 | autoCCAcxx . . $ autoCCA# SSCP_to_CCAcxx . . $ SSCP_to_CCA(b54d57be900501a9). (SSCPcxx . . $ SSCP# me, integercxx . . $ integer# ny); |
| 127 | |
| 128 | #endif /* CCA.h */ |