-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstepwiseCPC.m
47 lines (40 loc) · 1.2 KB
/
stepwiseCPC.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function [Lambda,Q,qtilde] = stepwiseCPC(S,n,pmax,lmax)
%CPCSTEPWISE calculate the common pricinple components
%Input S=covariances matrices pxpxk with p number of variables,k number of
% covariance matrices
% n number of samples for each Cov 1Xk
% pmax number of common principle components
% lmax maximum number of components for convergance <p
%Output
% Lambda eigenvalues pXk
% Q eigenvectors pXp (CPC)
% Reference: Stepwise Common Principal Components Trendafilov
% Pedro, Andy
n=n(:)';
[p,~,k]=size(S);
n = reshape(n,[1,1,k]);
nt=sum(n);
Spooled=sum(S.*repmat(n./nt,[p,p,1]),3);
[qtilde,D]=eig(Spooled);
[B,I] = sort(diag(D),'descend');
qtilde = qtilde(:,I);
Pi=eye(p);
Lambda=zeros(p,k);
Q=zeros(p,p);
mu=zeros(1,1,k);
for j=1:pmax
x=qtilde(:,j);
x = x./sqrt(x'*x);
x=Pi*x;
for i=1:k, mu(1,1,i)=x'*S(:,:,i)*x; end %i
for l=1:lmax
Spooled=sum(S.*repmat(n./mu,p,p,1),3);
y=Pi*Spooled*x;
x=y./sqrt(y'*y);
for i=1:k, mu(1,1,i)=x'*S(:,:,i)*x; end%i
end%lmax
Q(:,j)=x;
Pi=Pi-x*x';
end %pmax
for i=1:k, Lambda(:,i)=diag(Q'*S(:,:,i)*Q); end%i
end