forked from xxw1122/mPCCA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcca.m
235 lines (210 loc) · 6.78 KB
/
mcca.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
%X (nn x N) : input data 1
%Y (mm x N) : input data 2
%N: the number of training pairs
%d: dimension of z (after reduction)
%K: number of mixtures
% cyc - maximum number of cycles of EM (default 100)
% tol - termination tolerance (prop change in likelihood) (default 0.0001)
%x = Wx * z + Mux
%y = Wy * z + Muy
% Wx (K x nn x d): transformation matrix for x
% Wy (K x mm x d): transformation matrix for y
% Mux ((K x nn): mean for transformation x
% Muy ((K x mm): mean for transformation y
% Psix ( nn x nn x K): covariance for transformation x
% Psiy ( mm x mm x K): covariance for transformation y
% Wi (K x 1): - priors
% LL - log likelihood curve
% Iterates until a proportional change < tolerance(tol) in the log likelihood
% or cycles(cyc) steps of EM
function [Wx, Wy, Mux, Muy, Psix, Psiy, Wi, LL] = mcca(X, Y, d,K,cyc,tol, diagV, sharCov)
if nargin<8 sharCov=0; end; %% using shared covariance matrices
if nargin<7 diagV=1; end; %% using diagonal covariance matrices Psix Psiy
if nargin<6 tol=0.0001; end;
if nargin<5 cyc=100; end;
if nargin<4 K=2; end;
if nargin<3 d=1; end;
[nn, N]= size(X);
mm = size(Y, 1);
%% control randomness
rng('default') ;
%% diagonal augment for singular covariance matrix
tiny=exp(-200);
Ttd1 = eye(d+1)*tiny;
%% log likelihood
LL=[];
Id=eye(d);
%% initialize parameters: Wx, Wy, Mux, Muy, Psix, Psiy, Wi
mX = mean(X, 2);
cX = cov(X');
mY = mean(Y, 2);
cY = cov(Y');
scale=exp(2*sum(log(diag(chol(cX))))/nn);
Wx=randn(K , nn , d)*sqrt(scale/d);
scale=exp(2*sum(log(diag(chol(cY))))/mm);
Wy=randn(K , mm , d)*sqrt(scale/d);
Psix = zeros(nn , nn , K);
Psiy = zeros(mm , mm , K);
for i=1 : K
Mux(i, :) = mX;
Muy(i, :) = mY;
Psix(:,:,i) = diag(diag(cX))+tiny;
Psiy(:,:,i) = diag(diag(cY))+tiny;
end
Mux = Mux + randn(K, nn)*sqrtm(cX);
Muy = Muy + randn(K, mm)*sqrtm(cY);
%% initialize hidden parameters
G = zeros(N, K); %% posterior probabilties p(k|x_i)
Ez = zeros(N,d,K); %% E(z_{i,k})
% Ezz = zeros(d,d, K); % E(z_{i,k}z_{i,k}')
% Vzz = zeros(d,d,K);
Vxy = zeros(mm+nn, mm+nn, K);
Wi = ones(K,1)/K;
XY =[X; Y]';
likbase = 0;
%% EM Training of CCA
fprintf('\n Begin EN training\n');
%% iterations
for jj = 1 : cyc
fprintf(' EM Step %d \n', jj);
%% initialize total mean
Muz = [Mux, Muy];
%% initialize p(v|k)
Pr = zeros(K, N);
%% initialize variance matrix for every component
parfor kk = 1 : K
%% transformation for x -> z
Wxk = squeeze(Wx(kk,:,:));
%% transformation for y -> z
Wyk = squeeze(Wy(kk,:,:));
%% VxyPar is an ad-hoc designed solution for parallel running
VxyPar = zeros(mm+nn) ;
VxyPar(1:nn,1:nn) = Wxk*Wxk' + squeeze(Psix(:,:,kk));
VxyPar(1+nn:mm+nn,1+nn:mm+nn) = Wyk*Wyk' + squeeze(Psiy(:,:,kk));
VxyPar(1:nn,1+nn:mm+nn) = Wxk*Wyk';
VxyPar(1+nn:mm+nn,1:nn) = Wyk*Wxk';
%% update Vxy: variance for kth component
Vxy(:,:,kk) = VxyPar ;
%% p(v_{n}|k)
Pr(kk, :) = mvnpdf(XY, Muz(kk,:,:), squeeze(Vxy(:,:,kk)));
%% ensure the validity of Vxy: semi-definite and symmetric
%% but don't change the eigenvectors
% if det(Vxy(:,:,kk)) < eps
% rowSum = sum(Vxy(:,:,kk), 2) ;
% increment = -min(rowSum) ;
% if increment > 0
% Vxy(:,:,kk) = Vxy(:,:,kk) + increment * eye(mm+nn) + eps * eye(mm+nn);
% end
% end
end
%calculate likelihood
sW = repmat(Wi, 1, N);
Pr = Pr.*sW;
clear sW;
lik = sum(log(sum(Pr)));
fprintf(' Likelihood %f (Cycle %d) \n', lik, jj);
LL =[ LL ,lik ];
oldlik = likbase;
if (jj<=1)
likbase=lik;
elseif (jj<=2)
likbase=lik;
elseif (lik<oldlik)
fprintf(' violation');
elseif ((lik-likbase)<(1 + tol)*(oldlik-likbase)|~isfinite(lik))
break;
end;
sPr = sum(Pr, 1);
sPr = 1./sPr;
sPr = repmat(sPr, K ,1);
G = sPr.*Pr;
G = G'; %G(i,k)=p(k|v_i) size N x K
clear sPr Pr;
% M-step:
Wi = sum(G, 1)'/N; %calculate w_k
if sharCov==1
PsixT = zeros(nn);
PsiyT = zeros(mm);
end
for kk=1:K
fprintf('\t EM-iteration: %03d/%03d\n', kk, K);
Wxk = squeeze(Wx(kk,:,:));
Wyk = squeeze(Wy(kk,:,:));
Wk = [Wxk ; Wyk];
Pki = inv(squeeze(Vxy(1:mm+nn,1:mm+nn,kk)));
WPik = Wk'*Pki;
vzik = Id- WPik*Wk;%covariance for for p(z|x_i,y_i,k)
Gk = G(:, kk);
sGk = sum(Gk);
GkM = repmat(Gk, 1, d+1);
Gzz = zeros(d+1, d+1) ;
parfor indexSample = 1 : N
%% E-step %%
zik = WPik*[X(:, indexSample)- Mux(kk,:)'; Y(:, indexSample)- Muy(kk,:)']; %mean for p(z|x_i,y_i,k)
Ez(indexSample,:,kk) = zik;
% Ezz(:,:,kk) = vzik+zik*zik'; % E((zz'|x_i,y_i,k)
%% M-step %%
GzzSample = zeros(d+1, d+1) ;
%% for the sake of parallel computing
%% omit the use of explicit Ezz
GzzSample(1:d, 1:d) = Gk(indexSample) * (vzik+zik*zik') ; % Ezz(:,:,kk) ;
GzzSample(1+d, 1:d) = Gk(indexSample) * Ez(indexSample, :, kk) ;
GzzSample(1:d, 1+d) = Gk(indexSample) * Ez(indexSample, :, kk) ;
GzzSample(1+d, 1+d) = Gk(indexSample) ;
Gzz = Gzz + GzzSample;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Gzzi = inv(Gzz+ Ttd1);
Ezka = [squeeze(Ez(:,:,kk)),ones(N,1)]; % N x (d+1)
Gxz = X*(Ezka.*GkM); % nn x (d+1)
WMux = Gxz * Gzzi; % nn x (d+1);
% WMux = Gxz /(Gzz+ Ttd1);
Wx(kk,:,:) = WMux(:, 1:d);
Mux(kk,:) = WMux(:, 1+d);
Gyz = Y*(Ezka.*GkM); % mm x (d+1)
WMuy = Gyz * Gzzi; % mm x (d+1)
Wy(kk,:,:) = WMuy(:, 1:d);
Muy(kk,:) = WMuy(:, 1+d);
clear GkM Gxz Gyz Gzz Gzzi ;
GkM = repmat(Gk', nn, 1); %nn x N
Xh = X - WMux*Ezka'; %nn x N
if sharCov==0
if diagV ==1
Psix(:,:,kk) = diag(diag((X.*GkM)*Xh'/sGk));%+eye(mm)*tiny
else
Psix(:,:,kk) = (X.*GkM)*Xh'/sGk;%+eye(mm)*tiny
end
else
if diagV ==1
PsixT = PsixT+ diag(diag((X.*GkM)*Xh'));%+eye(mm)*tiny
else
PsixT = PsixT+ (X.*GkM)*Xh';%+eye(mm)*tiny
end
end
clear GkM Xh;
GkM = repmat(Gk', mm, 1); %mm x N
Yh = Y - WMuy*Ezka'; %mm x N
if sharCov==0
if diagV ==1
Psiy(:,:,kk) = diag(diag((Y.*GkM)*Yh'/sGk));%+eye(mm)*tiny
else
Psiy(:,:,kk) = (Y.*GkM)*Yh'/sGk;%+eye(mm)*tiny
end
else
if diagV ==1
PsiyT = PsiyT + diag(diag((Y.*GkM)*Yh'));%+eye(mm)*tiny
else
PsiyT = PsiyT + (Y.*GkM)*Yh';%+eye(mm)*tiny
end
end
clear GkM Yh;
end
if sharCov==1
PsixT = PsixT/N;
PsiyT = PsiyT/N;
for kk=1:K
Psix(:,:,kk) = PsixT;
Psiy(:,:,kk) = PsiyT;
end
end
end