CCA.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef _CCA_h_
  2. #define _CCA_h_
  3. /* CCA.h
  4. *
  5. * Copyright (C) 1993-2018 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. djmw 2001
  22. djmw 20020423 GPL header
  23. djmw 20110306 Latest modification.
  24. */
  25. #include "Eigen.h"
  26. #include "TableOfReal.h"
  27. #include "CCA_def.h"
  28. /*
  29. Class CCA represents the Canonical Correlation Analysis of two datasets
  30. (two tables with multivariate data, Table 1 was N rows x p columns,
  31. Table 2 was N rows x q columns, and p <= q).
  32. Interpretation:
  33. The eigenvectors v1[i] en v2[i] have the property that for the linear
  34. compounds
  35. c1[1] = v1[1]' . Table1 c2[1]= v2[1]' . Table2
  36. ..............................................
  37. c1[p] = v1[p]' . Table1 c2[p]= v2[p]' . Table2
  38. the sample correlation of c1[1] and c2[1] is greatest, the sample
  39. correlation of c1[2] and c2[2] is greatest amoung all linear compounds
  40. uncorrelated with c1[1] and c2[1], and so on, for all p possible pairs.
  41. */
  42. autoCCA CCA_create (integer numberOfCoefficients, integer ny, integer nx);
  43. void CCA_drawEigenvector (CCA me, Graphics g, int x_or_y, integer ivec, integer first, integer last,
  44. double ymin, double ymax, int weigh, double size_mm, conststring32 mark, int connect, int garnish);
  45. double CCA_getEigenvectorElement (CCA me, int x_or_y, integer ivec, integer element);
  46. autoCCA TableOfReal_to_CCA (TableOfReal me, integer ny);
  47. /*
  48. Solves the canonical correlation analysis equations:
  49. (S12*inv(S22)*S12' - lambda S11)X1 = 0 (1)
  50. (S12'*inv(S11)*S12 - lambda S22)X2 = 0 (2)
  51. Where S12 = T1' * T2, S11 = T1' * T1 and S22 = T2' * T2.
  52. Given the following svd's:
  53. svd (T1) = U1 D1 V1'
  54. svd (T2) = U2 D2 V2'
  55. We can write down:
  56. inv(S11) = V1 * D1^-2 * V1' and inv(S22) = V2 * D2^-2 * V2',
  57. and S12*inv(S22)*S12' simplifies to: V1*D1*U1'*U2 * U2'*U1*D1*V1'
  58. and (1) becomes:
  59. (V1*D1*U1'*U2 * U2'*U1*D1*V1' -lambda V1*D1 * D1*V1')X1 = 0
  60. This can be written as:
  61. (V1*D1*U1'*U2 * U2'*U1 -lambda V1*D1) D1*V1'*X1 = 0
  62. multiplying from the left with: D1^-1*V1' results in
  63. (U1'*U2 * U2'*U1 -lambda) D1*V1'*X1 = 0
  64. Taking the svd(U2'*U1) = U D V' we get:
  65. (D^2 -lambda)V'*D1*V1'*X1 = 0
  66. The eigenvectors X1 can be formally written as:
  67. X1 = V1*inv(D1)*V
  68. Equation (2) results in:
  69. X2 = V2*inv(D2)*U
  70. */
  71. autoTableOfReal CCA_TableOfReal_scores (CCA me, TableOfReal thee, integer numberOfFactors);
  72. /*
  73. Return the factors in a table with 2*numberOfFactors columns.
  74. The first 'numberOfFactors' columns are the scores for the dependent part
  75. of the table the following 'numberOfFactors' columns are for the
  76. independent part.
  77. */
  78. autoTableOfReal CCA_TableOfReal_factorLoadings (CCA me, TableOfReal thee);
  79. /*
  80. Get the canonical factor loadings (also structure correlation coefficients),
  81. the correlation of a canonical variable with an original variable.
  82. */
  83. double CCA_getCorrelationCoefficient (CCA me, integer index);
  84. void CCA_getZeroCorrelationProbability (CCA me, integer index, double *out_prob, double *out_chisq, double *out_df);
  85. autoTableOfReal CCA_TableOfReal_predict (CCA me, TableOfReal thee, integer from);
  86. /*
  87. Given independent table, predict the dependent one, on the basis of
  88. the canonical correlations.
  89. */
  90. #endif /* CCA.h */