| 1 |
/* CCA.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1993-2020 David Weenink |
| 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 20020402 GPL header |
| 22 |
djme 20021008 removed SVD_sort |
| 23 |
djmw 20031023 Removed one argument from CCA_and_TableOfReal_scores |
| 24 |
djmw 20031106 Removed bug from CCA_and_TableOfReal_scores |
| 25 |
djmw 20031221 Removed bug: CCA_TableOfReal_scores (wrong dimensions to Eigen_project_into). |
| 26 |
djmw 20061212 Changed info to Melder_writeLine<x> format. |
| 27 |
djmw 20071012 Added: o_CAN_WRITE_AS_ENCODING.h |
| 28 |
djmw 20081119 Check in TableOfReal_to_CCA if TableOfReal_areAllCellsDefined |
| 29 |
*/ |
| 30 |
|
| 31 |
#include "CCA_and_Correlation.h" |
| 32 |
#include "NUM2.h" |
| 33 |
#include "NUMlapack.h" |
| 34 |
#include "SVD.h" |
| 35 |
#include "Strings_extensions.h" |
| 36 |
#include "TableOfReal_extensions.h" |
| 37 |
#include "Eigen_and_TableOfReal.h" |
| 38 |
|
| 39 |
#include "oo_DESTROY.h" |
| 40 |
#include "CCA_def.h" |
| 41 |
#include "oo_COPY.h" |
| 42 |
#include "CCA_def.h" |
| 43 |
#include "oo_EQUAL.h" |
| 44 |
#include "CCA_def.h" |
| 45 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 46 |
#include "CCA_def.h" |
| 47 |
#include "oo_WRITE_TEXT.h" |
| 48 |
#include "CCA_def.h" |
| 49 |
#include "oo_WRITE_BINARY.h" |
| 50 |
#include "CCA_def.h" |
| 51 |
#include "oo_READ_TEXT.h" |
| 52 |
#include "CCA_def.h" |
| 53 |
#include "oo_READ_BINARY.h" |
| 54 |
#include "CCA_def.h" |
| 55 |
#include "oo_DESCRIPTION.h" |
| 56 |
#include "CCA_def.h" |
| 57 |
|
| 58 |
void structCCA :: v1_info () { |
| 59 |
structDaata :: v1_info (); |
| 60 |
MelderInfo_writeLine (U"Number of coefficients: ", numberOfCoefficients); |
| 61 |
MelderInfo_writeLine (U"ny: ", y -> dimension); |
| 62 |
MelderInfo_writeLine (U"nx: ", x -> dimension); |
| 63 |
} |
| 64 |
|
| 65 |
Thing_implement (CCA, Daata, 1); |
| 66 |
|
| 67 |
autoCCA CCA_create (integer numberOfCoefficients, integer ny, integer nx) { |
| 68 |
try { |
| 69 |
autoCCA me = Thing_new (CCA); |
| 70 |
my numberOfCoefficients = numberOfCoefficients; |
| 71 |
my yLabels = Thing_new (Strings); |
| 72 |
my xLabels = Thing_new (Strings); |
| 73 |
my y = Eigen_create (numberOfCoefficients, ny); |
| 74 |
my x = Eigen_create (numberOfCoefficients, nx); |
| 75 |
return me; |
| 76 |
} catch (MelderError) { |
| 77 |
Melder_throw (U"CCA not created."); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
void CCA_drawEigenvector (CCA me, Graphics g, int x_or_y, integer ivec, integer first, integer last, double ymin, double ymax, int weigh, double size_mm, conststring32 mark, int connect, bool garnish) { |
| 82 |
Eigen e = my x.get(); |
| 83 |
Strings labels = my xLabels.get(); |
| 84 |
if (x_or_y == 1) { |
| 85 |
e = my y.get(); |
| 86 |
labels = my yLabels.get(); |
| 87 |
} |
| 88 |
Eigen_drawEigenvector (e, g, ivec, first, last, ymin, ymax, weigh, size_mm, mark, connect, labels -> strings.peek2(), garnish); |
| 89 |
} |
| 90 |
|
| 91 |
double CCA_getEigenvectorElement (CCA me, int x_or_y, integer ivec, integer element) { |
| 92 |
Eigen e = ( x_or_y == 1 ? my y.get() : my x.get() ); |
| 93 |
return Eigen_getEigenvectorElement (e, ivec, element); |
| 94 |
} |
| 95 |
|
| 96 |
autoCCA TableOfReal_to_CCA (TableOfReal me, integer numberOfDependents) { |
| 97 |
try { |
| 98 |
const integer numberOfObservations = my numberOfRows; |
| 99 |
const integer numberOfIndependents = my numberOfColumns - numberOfDependents; |
| 100 |
Melder_require (numberOfDependents >= 1 && numberOfDependents < my numberOfColumns, |
| 101 |
U"Number of dependents not correct."); |
| 102 |
Melder_require (numberOfDependents <= numberOfIndependents, |
| 103 |
U"The number of dependents (", numberOfDependents, U") should not exceed the number of independents (", numberOfIndependents, U")."); |
| 104 |
Melder_require (numberOfObservations >= numberOfIndependents, |
| 105 |
U"The number of observations (", numberOfObservations, U") should be at least the number of independents (", numberOfIndependents, U")."); |
| 106 |
Melder_require (NUMdefined (my data.get()), |
| 107 |
U"At least one of the table's elements is undefined."); |
| 108 |
|
| 109 |
// Use svd as (temporary) storage, and copy data |
| 110 |
|
| 111 |
autoSVD svdy = SVD_create (numberOfObservations, numberOfDependents); // numberOfObservations >= numberOfDependents, hence no transposition |
| 112 |
autoSVD svdx = SVD_create (numberOfObservations, numberOfIndependents); // numberOfObservations >= numberOfIndependents, hence no transposition |
| 113 |
svdy -> u.all() <<= my data.verticalBand (1, numberOfDependents); |
| 114 |
svdx -> u.all() <<= my data.verticalBand (numberOfDependents + 1, my numberOfColumns); |
| 115 |
const double fnormy = NUMnorm (svdy -> u.get(), 2.0); |
| 116 |
const double fnormx = NUMnorm (svdx -> u.get(), 2.0); |
| 117 |
|
| 118 |
Melder_require (fnormy > 0.0 && fnormx > 0.0, |
| 119 |
U"One of the parts of the table contains only zeros."); |
| 120 |
|
| 121 |
// Centre the data and svd it. |
| 122 |
|
| 123 |
centreEachColumn_MAT_inout (svdy -> u.get()); |
| 124 |
centreEachColumn_MAT_inout (svdx -> u.get()); |
| 125 |
|
| 126 |
SVD_compute (svdy.get()); |
| 127 |
SVD_compute (svdx.get()); |
| 128 |
|
| 129 |
const integer numberOfZeroedy = SVD_zeroSmallSingularValues (svdy.get(), 0.0); |
| 130 |
const integer numberOfZeroedx = SVD_zeroSmallSingularValues (svdx.get(), 0.0); |
| 131 |
|
| 132 |
// Form the matrix C = ux' uy (use svd-object storage) |
| 133 |
|
| 134 |
autoSVD svdc = SVD_create (numberOfIndependents, numberOfDependents); |
| 135 |
mul_fast_MAT_out (svdc -> u.get(), svdx -> u.transpose(), svdy -> u.get()); |
| 136 |
SVD_compute (svdc.get()); |
| 137 |
const integer numberOfZeroedc = SVD_zeroSmallSingularValues (svdc.get(), 0.0); |
| 138 |
const integer numberOfCoefficients = numberOfDependents - numberOfZeroedc; |
| 139 |
|
| 140 |
autoCCA thee = CCA_create (numberOfCoefficients, numberOfDependents, numberOfIndependents); |
| 141 |
thy yLabels = Strings_createFromSTRVEC (my columnLabels.part (1, numberOfDependents)); |
| 142 |
thy xLabels = Strings_createFromSTRVEC (my columnLabels.part (numberOfDependents + 1, my numberOfColumns)); |
| 143 |
thy numberOfObservations = numberOfObservations; |
| 144 |
|
| 145 |
/* |
| 146 |
Y = Vy * inv(Dy) * Vc |
| 147 |
X = Vx * inv(Dx) * Uc |
| 148 |
For the eigenvectors we want a row representation: |
| 149 |
colums(Y) = rows(Y') = rows(Vc' * inv(Dy) * Vy') |
| 150 |
colums(X) = rows(X') = rows(Uc' * inv(Dx) * Vx') |
| 151 |
rows(Y') = evecy [i] [j] = Vc [k] [i] * Vy [j] [k] / Dy [k] |
| 152 |
rows(X') = evecx [i] [j] = Uc [k] [i] * Vx [j] [k] / Dx [k] |
| 153 |
*/ |
| 154 |
for (integer icoef = 1; icoef <= numberOfCoefficients; icoef ++) { |
| 155 |
const double ccc = svdc -> d [icoef]; |
| 156 |
thy y -> eigenvalues [icoef] = thy x -> eigenvalues [icoef] = ccc * ccc; |
| 157 |
for (integer idep = 1; idep <= numberOfDependents; idep ++) { |
| 158 |
longdouble t = 0.0; |
| 159 |
for (integer q = 1; q <= numberOfIndependents - numberOfZeroedy; q ++) |
| 160 |
t += svdc -> v [q] [icoef] * svdy -> v [idep] [q] / svdy -> d [q]; |
| 161 |
thy y -> eigenvectors [icoef] [idep] = double (t); |
| 162 |
} |
| 163 |
for (integer iindep = 1; iindep <= numberOfIndependents; iindep ++) { |
| 164 |
longdouble t = 0.0; |
| 165 |
for (integer q = 1; q <= numberOfDependents - numberOfZeroedx; q ++) |
| 166 |
t += svdc -> u [q] [icoef] * svdx -> v [iindep] [q] / svdx -> d [q]; |
| 167 |
thy x -> eigenvectors [icoef] [iindep] = double (t); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
// Normalize eigenvectors. |
| 172 |
|
| 173 |
MATnormalizeRows_inplace (thy y -> eigenvectors.get(), 2.0, 1.0); |
| 174 |
MATnormalizeRows_inplace (thy x -> eigenvectors.get(), 2.0, 1.0); |
| 175 |
Melder_assert (thy x -> dimension == thy xLabels -> numberOfStrings && |
| 176 |
thy y -> dimension == thy yLabels -> numberOfStrings); |
| 177 |
return thee; |
| 178 |
} catch (MelderError) { |
| 179 |
Melder_throw (me, U": CCA not created."); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
autoTableOfReal CCA_TableOfReal_scores (CCA me, TableOfReal thee, integer numberOfFactors) { |
| 184 |
try { |
| 185 |
const integer n = thy numberOfRows; |
| 186 |
const integer nx = my x -> dimension, ny = my y -> dimension; |
| 187 |
|
| 188 |
Melder_require (ny + nx == thy numberOfColumns, |
| 189 |
U"The number of columns in the table (", thy numberOfColumns, |
| 190 |
U") should agree with the dimensions of the CCA object (ny + nx = ", ny, U" + ", nx, U")."); |
| 191 |
|
| 192 |
if (numberOfFactors == 0) |
| 193 |
numberOfFactors = my numberOfCoefficients; |
| 194 |
|
| 195 |
Melder_require (numberOfFactors > 0 && numberOfFactors <= my numberOfCoefficients, |
| 196 |
U"The number of factors should be in interval [1, ", my numberOfCoefficients, U"]."); |
| 197 |
|
| 198 |
autoTableOfReal him = TableOfReal_create (n, 2 * numberOfFactors); |
| 199 |
his rowLabels.all() <<= thy rowLabels.all(); |
| 200 |
|
| 201 |
mul_MAT_out (his data.verticalBand (1, numberOfFactors), thy data.verticalBand (1, nx), my y -> eigenvectors.horizontalBand(1, numberOfFactors).transpose ()); |
| 202 |
mul_MAT_out (his data.verticalBand (numberOfFactors + 1, 2 * numberOfFactors), thy data.verticalBand (nx + 1, nx + ny), my x -> eigenvectors.horizontalBand(1, numberOfFactors).transpose()); |
| 203 |
|
| 204 |
TableOfReal_setSequentialColumnLabels (him.get(), 1, numberOfFactors, U"y_", 1, 1); |
| 205 |
TableOfReal_setSequentialColumnLabels (him.get(), numberOfFactors + 1, his numberOfColumns, U"x_", 1, 1); |
| 206 |
return him; |
| 207 |
} catch (MelderError) { |
| 208 |
Melder_throw (me, U": no TableOfReal with scores created."); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
autoTableOfReal CCA_TableOfReal_predict (CCA me, TableOfReal thee, integer from) { |
| 213 |
try { |
| 214 |
const integer ny = my y -> dimension, nx = my x -> dimension; |
| 215 |
const integer nev = my y -> numberOfEigenvalues; |
| 216 |
|
| 217 |
/* |
| 218 |
We can only predict when we have the largest dimension as input |
| 219 |
and the number of coefficients equals the dimension of the smallest. |
| 220 |
*/ |
| 221 |
|
| 222 |
Melder_require (ny == nev, |
| 223 |
U"There are not enough correlations present for prediction."); |
| 224 |
|
| 225 |
if (from == 0) |
| 226 |
from = 1; |
| 227 |
|
| 228 |
const integer ncols = thy numberOfColumns - from + 1; |
| 229 |
Melder_require (from > 0 && ncols == nx, |
| 230 |
U"The number of columns to analyze should be equal to ", nx, U"."); |
| 231 |
|
| 232 |
autoTableOfReal him = TableOfReal_create (thy numberOfRows, ny); |
| 233 |
mul_MAT_out (his data.get(), thy data.verticalBand (from, thy numberOfColumns), my x -> eigenvectors.transpose()); |
| 234 |
his rowLabels.all() <<= thy rowLabels.all(); |
| 235 |
|
| 236 |
autoVEC buf = raw_VEC (ny); |
| 237 |
|
| 238 |
// u = V a -> a = V'u |
| 239 |
|
| 240 |
for (integer i = 1; i <= thy numberOfRows; i ++) { |
| 241 |
buf.all() <<= his data.row (i).part (1, ny); |
| 242 |
for (integer j = 1; j <= ny; j ++) { |
| 243 |
longdouble t = 0.0; |
| 244 |
for (integer k = 1; k <= ny; k ++) { |
| 245 |
t += sqrt (my y -> eigenvalues [k]) * my y -> eigenvectors [k] [j] * buf [k]; |
| 246 |
} |
| 247 |
his data [i] [j] = (double) t; |
| 248 |
} |
| 249 |
} |
| 250 |
return him; |
| 251 |
} catch (MelderError) { |
| 252 |
Melder_throw (me, U": no predictions created."); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
autoTableOfReal CCA_TableOfReal_factorLoadings (CCA me, TableOfReal thee) { |
| 257 |
try { |
| 258 |
autoCorrelation c = TableOfReal_to_Correlation (thee); |
| 259 |
autoTableOfReal him = CCA_Correlation_factorLoadings (me, c.get()); |
| 260 |
return him; |
| 261 |
} catch (MelderError) { |
| 262 |
Melder_throw (me, U": no factor loadings created."); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
double CCA_getCorrelationCoefficient (CCA me, integer index) { |
| 267 |
if (index < 1 || index > my numberOfCoefficients) |
| 268 |
return undefined; |
| 269 |
return sqrt (my y -> eigenvalues [index]); |
| 270 |
} |
| 271 |
|
| 272 |
void CCA_getZeroCorrelationProbability (CCA me, integer eigenvalueNumber, double *out_prob, double *out_chisq, double *out_df) { |
| 273 |
constVEC eigenvalues = my y -> eigenvalues.get(); |
| 274 |
const integer numberOfEigenvalues = my y -> numberOfEigenvalues; |
| 275 |
const integer ny = my y -> dimension, nx = my x -> dimension; |
| 276 |
|
| 277 |
double chisq = undefined, prob = undefined, df = undefined; |
| 278 |
|
| 279 |
if (eigenvalueNumber >= 1 && eigenvalueNumber <= numberOfEigenvalues) { |
| 280 |
double lambda = 1.0; |
| 281 |
for (integer ieigen = eigenvalueNumber; ieigen <= numberOfEigenvalues; ieigen ++) |
| 282 |
lambda *= 1.0 - eigenvalues [ieigen]; |
| 283 |
df = (ny - eigenvalueNumber + 1) * (nx - eigenvalueNumber + 1); |
| 284 |
chisq = - (my numberOfObservations - (ny + nx + 3.0) / 2.0) * log (lambda); |
| 285 |
prob = NUMchiSquareQ (chisq, df); |
| 286 |
} |
| 287 |
|
| 288 |
if (out_chisq) |
| 289 |
*out_chisq = chisq; |
| 290 |
if (out_df) |
| 291 |
*out_df = df; |
| 292 |
if (out_prob) |
| 293 |
*out_prob = prob; |
| 294 |
} |
| 295 |
|
| 296 |
autoCCA SSCP_to_CCA (SSCP me, integer ny) { |
| 297 |
try { |
| 298 |
integer info; |
| 299 |
Melder_require (ny > 0 && ny < my numberOfRows, |
| 300 |
U"Invalid split."); |
| 301 |
Melder_require (my numberOfRows > 1, |
| 302 |
U"Matrix should not be diagonal."); |
| 303 |
|
| 304 |
integer nx = my numberOfRows - ny; |
| 305 |
const bool xy_interchanged = ( nx < ny ); |
| 306 |
integer yof = 1, xof = ny + 1; |
| 307 |
if (xy_interchanged) { |
| 308 |
yof = ny + 1; |
| 309 |
xof = 1; |
| 310 |
nx = ny; |
| 311 |
ny = my numberOfRows - nx; |
| 312 |
} |
| 313 |
/* |
| 314 |
Copy Syy and Sxx into upper part of syy and sxx matrices. |
| 315 |
*/ |
| 316 |
autoMAT syy = part_MAT (my data.get(), yof, yof + ny - 1, yof, yof + ny - 1); |
| 317 |
autoMAT sxx = part_MAT (my data.get(), xof, xof + nx - 1, xof, xof + nx - 1); |
| 318 |
autoMAT syx = part_MAT (my data.get(), yof, yof + ny - 1, xof, xof + nx - 1); |
| 319 |
/* |
| 320 |
Cholesky decomposition: Syy = Uy'*Uy and Sxx = Ux'*Ux. |
| 321 |
(Pretend as if colum-major storage) |
| 322 |
*/ |
| 323 |
(void) NUMlapack_dpotf2_ ("L", ny, & syy [1] [1], ny, & info); |
| 324 |
Melder_require (info == 0, |
| 325 |
U"The leading minor of order ", info, U" is not positive definite, and the " |
| 326 |
U"factorization of Syy could not be completed."); |
| 327 |
|
| 328 |
(void) NUMlapack_dpotf2_ ("L", nx, & sxx [1] [1], nx, & info); |
| 329 |
Melder_require (info == 0, |
| 330 |
U"The leading minor of order ", info, U" is not positive definite, and the " |
| 331 |
U"factorization of Sxx could not be completed."); |
| 332 |
/* |
| 333 |
With Cholesky decomps Sxx = Ux'* Ux, Syy = Uy * Uy' |
| 334 |
Sxx**-1 = Uxi * Uxi' and Syy**-1 = Uyi * Uyi', where |
| 335 |
Uxi = Ux**-1 and Uyi = Uy**-1, the equations |
| 336 |
(1) (Syx * Sxx**-1 * Syx' - lambda Syy) y = 0 |
| 337 |
(1') (Syx' * Syy**-1 * Syx - lambda Sxx) x = 0 |
| 338 |
can be written as: |
| 339 |
(2) (Syx * Uxi * Uxi' * Syx' - lambda Uy' * Uy) y = 0 |
| 340 |
(2') (Syx' * Uyi * Uyi' * Syx - lambda Ux' * Ux) x = 0 |
| 341 |
More explicitly as: |
| 342 |
(3) (Uxi' * Syx')' * (Uxi' * Syx') - lambda Uy' * Uy) y = 0 |
| 343 |
(3') (Uyi' * Syx )' * (Uyi' * Syx ) - lambda Ux' * Ux) x = 0 |
| 344 |
They are now in the form (A'A - lambda B'B) x = 0 and both can be solved with the GSVD. |
| 345 |
However, these equations are not independent. Both have the same |
| 346 |
eigenvalues and given the eigenvectors for one, the eigenvectors for |
| 347 |
the other can be calculated. |
| 348 |
If nx >= ny use eq. (3) |
| 349 |
GSVD (Uxi' * Syx', Uy) gives lambda's and y. |
| 350 |
To get x multiply (1) from the left by Syx'*Syy**-1 |
| 351 |
(4) (Syx'*Syy**-1*Syx * Sxx**-1 - lambda ) Syx' * y = 0 |
| 352 |
Split off Sxx**-1 |
| 353 |
(5) (Syx'*Syy**-1*Syx -lambda Sxx) * Sxx**-1 * Syx' * y = 0 |
| 354 |
It follows that x = Sxx**-1 * Syx' * y = Uxi * Uxi' * Sxy * y |
| 355 |
If ny > nx use eq. (3') |
| 356 |
We switch the role of x and y. |
| 357 |
*/ |
| 358 |
|
| 359 |
/* |
| 360 |
Uxi = inverse(Ux) |
| 361 |
*/ |
| 362 |
(void) NUMlapack_dtrti2_ ("L", "N", nx, & sxx [1] [1], nx, & info); |
| 363 |
Melder_require (info == 0, |
| 364 |
U"Error in inverse for Sxx."); |
| 365 |
/* |
| 366 |
Prepare Uxi' * Syx' = (Syx * Uxi)' |
| 367 |
*/ |
| 368 |
autoMAT a = mul_MAT (sxx.transpose(), syx.transpose()); |
| 369 |
Melder_assert (a.nrow == nx && a.ncol == ny); |
| 370 |
|
| 371 |
autoGSVD gsvd = GSVD_create (a.get(), syy.get()); |
| 372 |
autoMAT ri = copy_MAT (gsvd -> r.get()); |
| 373 |
|
| 374 |
autoCCA thee = Thing_new (CCA); |
| 375 |
|
| 376 |
thy y = Eigen_create (gsvd -> numberOfColumns, gsvd -> numberOfColumns); |
| 377 |
thy x = Eigen_create (thy y -> numberOfEigenvalues, nx); |
| 378 |
/* |
| 379 |
Get X=Q*R**-1 |
| 380 |
*/ |
| 381 |
NUMlapack_dtrti2_ ("L", "N", gsvd -> numberOfColumns, & ri [1] [1], gsvd -> numberOfColumns, & info); |
| 382 |
Melder_require (info == 0, |
| 383 |
U"Error in inverse for R."); |
| 384 |
|
| 385 |
for (integer i = 1; i <= gsvd -> numberOfColumns; i ++) { |
| 386 |
const double t = gsvd -> d1 [i] / gsvd -> d2 [i]; |
| 387 |
thy y -> eigenvalues [i] = t * t; |
| 388 |
for (integer j = 1; j <= gsvd -> numberOfColumns; j ++) { |
| 389 |
const double sum = NUMinner (gsvd -> q.row (i).part (1, j), ri.column (j).part (1, j)); |
| 390 |
thy y -> eigenvectors [j] [i] = sum; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
MATnormalizeRows_inplace (thy y -> eigenvectors.get(), 2.0, 1.0); |
| 395 |
|
| 396 |
thy numberOfCoefficients = thy y -> numberOfEigenvalues; |
| 397 |
thy numberOfObservations = Melder_ifloor (my numberOfObservations); |
| 398 |
|
| 399 |
// x = Sxx**-1 * Syx' * y |
| 400 |
|
| 401 |
for (integer i = 1; i <= thy numberOfCoefficients; i ++) { |
| 402 |
constVEC evecy = thy y -> eigenvectors.row (i); |
| 403 |
VEC evecx = thy x -> eigenvectors.row (i); |
| 404 |
for (integer j = 1; j <= nx; j ++) { |
| 405 |
longdouble t = 0.0; |
| 406 |
for (integer k = j; k <= nx; k ++) { |
| 407 |
for (integer l = 1; l <= nx; l ++) { |
| 408 |
for (integer n = 1; n <= ny; n ++) { |
| 409 |
t += sxx [j] [k] * sxx [l] [k] * syx [n] [l] * evecy [n]; |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
evecx [j] = double (t); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
MATnormalizeRows_inplace (thy x -> eigenvectors.get(), 2.0, 1.0); |
| 418 |
|
| 419 |
if (ny < nx) { |
| 420 |
autoEigen t = thy x.move(); |
| 421 |
thy x = thy y.move(); |
| 422 |
thy y = t.move(); |
| 423 |
} |
| 424 |
return thee; |
| 425 |
} catch (MelderError) { |
| 426 |
Melder_throw (me, U": CCA not created."); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
/* End of file CCA.c */ |