IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
cca.m
Go to the documentation of this file.
1 %>@ingroup maths
2 %>@file
3 %>@brief Canonical Correlation Analysis
4 %>
5 %> <h3>Reference</h3>
6 %> Hastie et. al. 2001, The elements of statistical learning, exercise 3.18.
7 %
8 %> @param X "Input"
9 %> @param Y "Output"
10 %> @param P either the penalty coefficients of a penalty matrix itself
11 %> @return [A, B]
12 function [A, B] = cca(X, Y, P)
13 
14 [no, nf] = size(X);
15 
16 if ~exist('P', 'var')
17  P = 0;
18 else
19  [q, w] = size(P);
20  if q ~= nf || w ~= nf
21  % assumes the coefficients have been passed
22  P = no*penalty_matrix(nf, P);
23  else
24  end;
25 end;
26 
27 
28 Y = data_normalize(Y, 's');
29 X = data_normalize(X, 's'); % We need this otherwise the correlation X'*Y needs normalization
30 
31 
32 if 1
33  [A, B] = canoncorr(X, Y);
34 else
35  % Hastie et. al. 2001, The elements of statistical learning, exercise
36  % 3.18.
37 
38  % This algorithm is working fine
39 
40  p = min(size(X, 2), size(Y, 2))-1;
41 
42  M = (Y'*Y)^(-1/2)*(Y'*X)*(X'*X+P)^(-1/2);
43  [U, D, V] = svd(M);
44 
45  B = (Y'*Y)^(-1/2)*U(:, 1:p);
46  A = (X'*X+P)^(-1/2)*V(:, 1:p);
47  end;
function penalty_matrix(in nf, in dcoeff)
function cca(in X, in Y, in P)