2 EMGrank = function(Pi, Rho, mini, maxi, X, Y, tau, rank){
10 phi = array(0, dim=c(p,m,k))
16 Phi = array(0, dim=c(p,m,k))
19 deltaPhiBufferSize = 20
23 while(ite<=mini || (ite<=maxi && sumDeltaPhi>tau)){
24 #M step: Mise à jour de Beta (et donc phi)
26 Z_bin = valse:::vec_bin(Z,r)
27 Z_vec = Z_bin$vec #vecteur 0 et 1 aux endroits o? Z==r
28 Z_indice = Z_bin$indice
29 if(sum(Z_indice) == 0){
32 #U,S,V = SVD of (t(Xr)Xr)^{-1} * t(Xr) * Yr
33 sv = svd(ginv( crossprod(X[Z_indice,]) ) %*% crossprod(X[Z_indice,], Y[Z_indice,]) )
37 #Set m-rank(r) singular values to zero, and recompose
38 #best rank(r) approximation of the initial product
43 j_r_1 = c(rank[r]+1:length(S))
46 S = diag(S, nrow = ncol(U))
47 phi[,,r] = U %*% S %*% t(V) %*% Rho[,,r]
50 #Etape E et calcul de LLF
56 dotProduct = tcrossprod(Y[i,]%*%Rho[,,r]-X[i,]%*%phi[,,r])
57 logGamIR = log(Pi[r]) + log(det(Rho[,,r])) - 0.5*dotProduct
58 #Z[i] = index of max (gam[i,])
59 if(logGamIR > maxLogGamIR){
61 maxLogGamIR = logGamIR
63 sumLLF1 = sumLLF1 + exp(logGamIR) / (2*pi)^(m/2)
65 sumLogLLF2 = sumLogLLF2 + log(sumLLF1)
68 LLF = -1/n * sumLogLLF2
70 #update distance parameter to check algorithm convergence (delta(phi, Phi))
71 deltaPhi = c(deltaPhi, max(max(max((abs(phi-Phi))/(1+abs(phi))))) )
72 if(length(deltaPhi) > deltaPhiBufferSize){
73 l_1 = c(2:length(deltaPhi))
74 deltaPhi = deltaPhi[l_1]
76 sumDeltaPhi = sum(abs(deltaPhi))
78 #update other local variables
83 return(list(phi=phi, LLF=LLF))