1 function[phi,LLF] = EMGrank(Pi,Rho,mini,maxi,X,Y,tau,rank)
7 % allocate output matrices
17 deltaPhiBufferSize = 20;
19 %main loop (at least mini iterations)
21 while ite<=mini || (ite<=maxi && sumDeltaPhi>tau)
23 %M step: Mise à jour de Beta (et donc phi)
28 %U,S,V = SVD of (t(Xr)Xr)^{-1} * t(Xr) * Yr
29 [U,S,V] = svd(pinv(transpose(X(Z==r,:))*X(Z==r,:))*transpose(X(Z==r,:))*Y(Z==r,:));
30 %Set m-rank(r) singular values to zero, and recompose
31 %best rank(r) approximation of the initial product
32 S(rank(r)+1:end,:) = 0;
33 phi(:,:,r) = U * S * transpose(V) * Rho(:,:,r);
36 %Etape E et calcul de LLF
42 dotProduct = (Y(i,:)*Rho(:,:,r)-X(i,:)*phi(:,:,r)) * transpose(Y(i,:)*Rho(:,:,r)-X(i,:)*phi(:,:,r));
43 logGamIR = log(Pi(r)) + log(det(Rho(:,:,r))) - 0.5*dotProduct;
44 %Z(i) = index of max (gam(i,:))
45 if logGamIR > maxLogGamIR
47 maxLogGamIR = logGamIR;
49 sumLLF1 = sumLLF1 + exp(logGamIR) / (2*pi)^(m/2);
51 sumLogLLF2 = sumLogLLF2 + log(sumLLF1);
54 LLF = -1/n * sumLogLLF2;
56 % update distance parameter to check algorithm convergence (delta(phi, Phi))
57 deltaPhi = [ deltaPhi, max(max(max((abs(phi-Phi))./(1+abs(phi))))) ];
58 if length(deltaPhi) > deltaPhiBufferSize
59 deltaPhi = deltaPhi(2:length(deltaPhi));
61 sumDeltaPhi = sum(abs(deltaPhi));
63 % update other local variables