Debian Codegraph @

dwtools/CCA_and_Correlation.cpp

raw symbols SCIP
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"cxx . . $ `<file>/dwtools/CCA_and_Correlation.h`/
27 #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/
28
29 autoTableOfRealcxx . . $ autoTableOfReal# CCA_Correlation_factorLoadingscxx . . $ CCA_Correlation_factorLoadings(1b2e604189241b50). (CCAcxx . . $ CCA# me, Correlationcxx . . $ Correlation# thee) {
30 try {
31 const integercxx . . $ integer# ny = mycxx . . $ `melder/melder.h:44:9`! ycxx . . $ structCCA#y. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dimensioncxx . . $ structEigen#dimension., nx = mycxx . . $ `melder/melder.h:44:9`! xcxx . . $ structCCA#x. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dimensioncxx . . $ structEigen#dimension.;
32 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (ny + nx == thycxx . . $ `melder/melder.h:45:9`! numberOfColumns,
33 U"The number of columns in the Correlation should equal the sum of the dimensions in the CCA object.");
34
35 autoTableOfRealcxx . . $ autoTableOfReal# him = TableOfReal_createcxx . . $ TableOfReal_create(e573ed3514e79d3). (2 * mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structCCA#numberOfCoefficients., thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.);
36 hiscxx . . $ `melder/melder.h:47:9`! columnLabelscxx . . $ structTableOfReal#columnLabels..allcxx . . $ _autostringautovector#all(58997016dad794db).() <<=cxx . . $ `operator<<=`(5bcbc9f49fd394e1). thycxx . . $ `melder/melder.h:45:9`! columnLabelscxx . . $ structTableOfReal#columnLabels..allcxx . . $ _autostringautovector#all(58997016dad794db).();
37 TableOfReal_setSequentialRowLabelscxx . . $ TableOfReal_setSequentialRowLabels(8dc3139cdc86d712). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 1, mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structCCA#numberOfCoefficients., U"dv", 1, 1);
38 TableOfReal_setSequentialRowLabelscxx . . $ TableOfReal_setSequentialRowLabels(8dc3139cdc86d712). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structCCA#numberOfCoefficients. + 1, 2 * mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structCCA#numberOfCoefficients., U"iv", 1, 1);
39
40 for (integercxx . . $ integer# irow = 1; irow <= thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; irow ++) {
41 for (integercxx . . $ integer# j = 1; j <= mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structCCA#numberOfCoefficients.; j ++) {
42 hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).irow] = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (irow).partcxx . . $ vector#part(ae267db9e2c04be7). (1, ny), mycxx . . $ `melder/melder.h:44:9`! ycxx . . $ structCCA#y. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvectorscxx . . $ structEigen#eigenvectors..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (j));
43 }
44 for (integercxx . . $ integer# j = 1; j <= mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structCCA#numberOfCoefficients.; j ++) {
45 hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structCCA#numberOfCoefficients. + j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).irow] = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (irow).partcxx . . $ vector#part(ae267db9e2c04be7). (ny + 1, ny + nx), mycxx . . $ `melder/melder.h:44:9`! xcxx . . $ structCCA#x. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvectorscxx . . $ structEigen#eigenvectors..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (j));
46 }
47 }
48 return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
49 } catch (MelderErrorcxx . . $ MelderError#) {
50 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"TableOfReal not created from CCA & Correlation.");
51 }
52 }
53
54 static void _CCA_Correlation_checkcxx . . $ _CCA_Correlation_check(1818f5136b4b1c98). (CCAcxx . . $ CCA# me, Correlationcxx . . $ Correlation# thee, integercxx . . $ integer# canonicalVariate_from, integercxx . . $ integer# canonicalVariate_to) {
55 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! y -> dimension + mycxx . . $ `melder/melder.h:44:9`! x -> dimension == thycxx . . $ `melder/melder.h:45:9`! numberOfColumns,
56 U"The number of columns in the Correlation object should equal the sum of the dimensions in the CCA object.");
57 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (canonicalVariate_to >= canonicalVariate_from,
58 U"The second value in the \"Canonical variate range\" should be equal or larger than the first.");
59 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (canonicalVariate_from > 0 && canonicalVariate_to <= mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficients,
60 U"The \"Canonical variate range\" should be within the interval [1, ", mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficients, U"].");
61 }
62
63 double CCA_Correlation_getVarianceFractioncxx . . $ CCA_Correlation_getVarianceFraction(14a1b5ce4319b765). (CCAcxx . . $ CCA# me, Correlationcxx . . $ Correlation# thee, int x_or_y, integercxx . . $ integer# canonicalVariate_from, integercxx . . $ integer# canonicalVariate_to) {
64 _CCA_Correlation_checkcxx . . $ _CCA_Correlation_check(1818f5136b4b1c98). (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 longdoublecxx . . $ longdouble# varianceFraction = 0.0;
82 for (integercxx . . $ integer# icv = canonicalVariate_from; icv <= canonicalVariate_to; icv ++) {
83 longdoublecxx . . $ longdouble# variance = 0.0, varianceScaling = 0.0;
84 if (x_or_y == 1) { /* y: dependent set */
85 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! ycxx . . $ structCCA#y. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dimensioncxx . . $ structEigen#dimension.; i ++) {
86 const double si = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (i).partcxx . . $ vector#part(ae267db9e2c04be7). (1, mycxx . . $ `melder/melder.h:44:9`! ycxx . . $ structCCA#y. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dimensioncxx . . $ structEigen#dimension.), mycxx . . $ `melder/melder.h:44:9`! ycxx . . $ structCCA#y. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvectorscxx . . $ structEigen#eigenvectors..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (icv));
87 variance += si * si; /* (Rxx.e)'(Rxx.e) = e'.Rxx'.Rxx.e */
88 varianceScaling += mycxx . . $ `melder/melder.h:44:9`! ycxx . . $ structCCA#y. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvectorscxx . . $ structEigen#eigenvectors. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).icv] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * si; /* e'.Rxx.e*/
89 }
90 varianceFraction += (variance / varianceScaling) / mycxx . . $ `melder/melder.h:44:9`! ycxx . . $ structCCA#y. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dimensioncxx . . $ structEigen#dimension.;
91 } else {
92 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! xcxx . . $ structCCA#x. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dimensioncxx . . $ structEigen#dimension.; i ++) {
93 const double si = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (mycxx . . $ `melder/melder.h:44:9`! ycxx . . $ structCCA#y. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dimensioncxx . . $ structEigen#dimension. + i).partcxx . . $ vector#part(ae267db9e2c04be7). (mycxx . . $ `melder/melder.h:44:9`! ycxx . . $ structCCA#y. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dimensioncxx . . $ structEigen#dimension. + 1, thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..ncolcxx . . $ automatrix#ncol.), mycxx . . $ `melder/melder.h:44:9`! xcxx . . $ structCCA#x. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvectorscxx . . $ structEigen#eigenvectors..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (icv));
94 variance += si * si; /* (Rxx.e)'(Rxx.e) = e'.Rxx'.Rxx.e */
95 varianceScaling += mycxx . . $ `melder/melder.h:44:9`! xcxx . . $ structCCA#x. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvectorscxx . . $ structEigen#eigenvectors. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).icv] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * si; /* e'.Rxx.e*/
96 }
97 varianceFraction += (variance / varianceScaling) / mycxx . . $ `melder/melder.h:44:9`! ycxx . . $ structCCA#y. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dimensioncxx . . $ structEigen#dimension.;
98 }
99 }
100 return (double) varianceFraction;
101 }
102
103 double CCA_Correlation_getRedundancy_slcxx . . $ CCA_Correlation_getRedundancy_sl(14a1b5ce4319b765). (CCAcxx . . $ CCA# me, Correlationcxx . . $ Correlation# thee, int x_or_y, integercxx . . $ integer# canonicalVariate_from, integercxx . . $ integer# canonicalVariate_to) {
104 _CCA_Correlation_checkcxx . . $ _CCA_Correlation_check(1818f5136b4b1c98). (me, thee, canonicalVariate_from, canonicalVariate_to);
105
106 longdoublecxx . . $ longdouble# redundancy = 0.0;
107 for (integercxx . . $ integer# icv = canonicalVariate_from; icv <= canonicalVariate_to; icv ++) {
108 const double varianceFraction = CCA_Correlation_getVarianceFractioncxx . . $ CCA_Correlation_getVarianceFraction(14a1b5ce4319b765). (me, thee, x_or_y, icv, icv);
109 if (isundefcxx . . $ isundef(f8197d536af80c96). (varianceFraction))
110 return undefinedcxx . . $ undefined.;
111 redundancy += varianceFraction * mycxx . . $ `melder/melder.h:44:9`! ycxx . . $ structCCA#y. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvaluescxx . . $ structEigen#eigenvalues. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icv]; // eigenvalue == (coefficient)^2
112 }
113
114 return (double) redundancy;
115 }
116
117 /* End of file CCA_and_Correlation.cpp */