| 1 |
/* CCA_and_Correlation.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1993-2021 David Weenink, 2017 Paul Boersma |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
djmw 2001 |
| 21 |
djmw 20020525 GPL header. |
| 22 |
djmw 20060323 Stewart-Love redundancy added. |
| 23 |
djmw 20071022 Melder_error<n> |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "CCA_and_Correlation.h" |
| 27 |
#include "NUM2.h" |
| 28 |
|
| 29 |
autoTableOfReal CCA_Correlation_factorLoadings (CCA me, Correlation thee) { |
| 30 |
try { |
| 31 |
const integer ny = my y -> dimension, nx = my x -> dimension; |
| 32 |
Melder_require (ny + nx == thy numberOfColumns, |
| 33 |
U"The number of columns in the Correlation should equal the sum of the dimensions in the CCA object."); |
| 34 |
|
| 35 |
autoTableOfReal him = TableOfReal_create (2 * my numberOfCoefficients, thy numberOfColumns); |
| 36 |
his columnLabels.all() <<= thy columnLabels.all(); |
| 37 |
TableOfReal_setSequentialRowLabels (him.get(), 1, my numberOfCoefficients, U"dv", 1, 1); |
| 38 |
TableOfReal_setSequentialRowLabels (him.get(), my numberOfCoefficients + 1, 2 * my numberOfCoefficients, U"iv", 1, 1); |
| 39 |
|
| 40 |
for (integer irow = 1; irow <= thy numberOfRows; irow ++) { |
| 41 |
for (integer j = 1; j <= my numberOfCoefficients; j ++) { |
| 42 |
his data [j] [irow] = NUMinner (thy data.row (irow).part (1, ny), my y -> eigenvectors.row (j)); |
| 43 |
} |
| 44 |
for (integer j = 1; j <= my numberOfCoefficients; j ++) { |
| 45 |
his data [my numberOfCoefficients + j] [irow] = NUMinner (thy data.row (irow).part (ny + 1, ny + nx), my x -> eigenvectors.row (j)); |
| 46 |
} |
| 47 |
} |
| 48 |
return him; |
| 49 |
} catch (MelderError) { |
| 50 |
Melder_throw (U"TableOfReal not created from CCA & Correlation."); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
static void _CCA_Correlation_check (CCA me, Correlation thee, integer canonicalVariate_from, integer canonicalVariate_to) { |
| 55 |
Melder_require (my y -> dimension + my x -> dimension == thy numberOfColumns, |
| 56 |
U"The number of columns in the Correlation object should equal the sum of the dimensions in the CCA object."); |
| 57 |
Melder_require (canonicalVariate_to >= canonicalVariate_from, |
| 58 |
U"The second value in the \"Canonical variate range\" should be equal or larger than the first."); |
| 59 |
Melder_require (canonicalVariate_from > 0 && canonicalVariate_to <= my numberOfCoefficients, |
| 60 |
U"The \"Canonical variate range\" should be within the interval [1, ", my numberOfCoefficients, U"]."); |
| 61 |
} |
| 62 |
|
| 63 |
double CCA_Correlation_getVarianceFraction (CCA me, Correlation thee, int x_or_y, integer canonicalVariate_from, integer canonicalVariate_to) { |
| 64 |
_CCA_Correlation_check (me, thee, canonicalVariate_from, canonicalVariate_to); |
| 65 |
|
| 66 |
/* For the formulas see: |
| 67 |
William W. Cooley & Paul R. Lohnes (1971), Multivariate data Analysis, John Wiley & Sons, pag 170-... |
| 68 |
varianceFraction = s'.s / n, |
| 69 |
where e.g. for the independent set x: |
| 70 |
s = Rxx . c, |
| 71 |
and Rxx is the correlation matrix of x, |
| 72 |
c is the factor coefficient for x, |
| 73 |
nx is the dimension of x, |
| 74 |
The factor coefficient c is the eigenvector e for x scaled by the st.dev of the component, |
| 75 |
i.e. c = e / sqrt (e'.R.e) (pag 32-33). |
| 76 |
Therefore: |
| 77 |
varianceFraction = s'.s / n = c'Rxx' Rxx c/n = (e'.Rxx' Rxx.e) /(e'.Rxx.e) * 1/n |
| 78 |
(for one can. variate) |
| 79 |
*/ |
| 80 |
|
| 81 |
longdouble varianceFraction = 0.0; |
| 82 |
for (integer icv = canonicalVariate_from; icv <= canonicalVariate_to; icv ++) { |
| 83 |
longdouble variance = 0.0, varianceScaling = 0.0; |
| 84 |
if (x_or_y == 1) { /* y: dependent set */ |
| 85 |
for (integer i = 1; i <= my y -> dimension; i ++) { |
| 86 |
const double si = NUMinner (thy data.row (i).part (1, my y -> dimension), my y -> eigenvectors.row (icv)); |
| 87 |
variance += si * si; /* (Rxx.e)'(Rxx.e) = e'.Rxx'.Rxx.e */ |
| 88 |
varianceScaling += my y -> eigenvectors [icv] [i] * si; /* e'.Rxx.e*/ |
| 89 |
} |
| 90 |
varianceFraction += (variance / varianceScaling) / my y -> dimension; |
| 91 |
} else { |
| 92 |
for (integer i = 1; i <= my x -> dimension; i ++) { |
| 93 |
const double si = NUMinner (thy data.row (my y -> dimension + i).part (my y -> dimension + 1, thy data.ncol), my x -> eigenvectors.row (icv)); |
| 94 |
variance += si * si; /* (Rxx.e)'(Rxx.e) = e'.Rxx'.Rxx.e */ |
| 95 |
varianceScaling += my x -> eigenvectors [icv] [i] * si; /* e'.Rxx.e*/ |
| 96 |
} |
| 97 |
varianceFraction += (variance / varianceScaling) / my y -> dimension; |
| 98 |
} |
| 99 |
} |
| 100 |
return (double) varianceFraction; |
| 101 |
} |
| 102 |
|
| 103 |
double CCA_Correlation_getRedundancy_sl (CCA me, Correlation thee, int x_or_y, integer canonicalVariate_from, integer canonicalVariate_to) { |
| 104 |
_CCA_Correlation_check (me, thee, canonicalVariate_from, canonicalVariate_to); |
| 105 |
|
| 106 |
longdouble redundancy = 0.0; |
| 107 |
for (integer icv = canonicalVariate_from; icv <= canonicalVariate_to; icv ++) { |
| 108 |
const double varianceFraction = CCA_Correlation_getVarianceFraction (me, thee, x_or_y, icv, icv); |
| 109 |
if (isundef (varianceFraction)) |
| 110 |
return undefined; |
| 111 |
redundancy += varianceFraction * my y -> eigenvalues [icv]; // eigenvalue == (coefficient)^2 |
| 112 |
} |
| 113 |
|
| 114 |
return (double) redundancy; |
| 115 |
} |
| 116 |
|
| 117 |
/* End of file CCA_and_Correlation.cpp */ |