1 source("/home/goehry/Documents/valse/valse/R/vec_bin.R")
3 EMGrank = function(Pi, Rho, mini, maxi, X, Y, tau, rank){
11 phi = array(0, dim=c(p,m,k))
17 Phi = array(0, dim=c(p,m,k))
20 deltaPhiBufferSize = 20
24 while(ite<=mini || (ite<=maxi && sumDeltaPhi>tau)){
25 #M step: Mise à jour de Beta (et donc phi)
28 Z_vec = Z_bin$vec #vecteur 0 et 1 aux endroits o? Z==r
29 Z_indice = Z_bin$indice
30 if(sum(Z_indice) == 0){
33 #U,S,V = SVD of (t(Xr)Xr)^{-1} * t(Xr) * Yr
34 sv = svd(ginv( crossprod(X[Z_indice,]) ) %*% crossprod(X[Z_indice,], Y[Z_indice,]) )
38 #Set m-rank(r) singular values to zero, and recompose
39 #best rank(r) approximation of the initial product
44 j_r_1 = c(rank[r]+1:length(S))
47 S = diag(S, nrow = ncol(U))
48 phi[,,r] = U %*% S %*% t(V) %*% Rho[,,r]
51 #Etape E et calcul de LLF
57 dotProduct = tcrossprod(Y[i,]%*%Rho[,,r]-X[i,]%*%phi[,,r])
58 logGamIR = log(Pi[r]) + log(det(Rho[,,r])) - 0.5*dotProduct
59 #Z[i] = index of max (gam[i,])
60 if(logGamIR > maxLogGamIR){
62 maxLogGamIR = logGamIR
64 sumLLF1 = sumLLF1 + exp(logGamIR) / (2*pi)^(m/2)
66 sumLogLLF2 = sumLogLLF2 + log(sumLLF1)
69 LLF = -1/n * sumLogLLF2
71 #update distance parameter to check algorithm convergence (delta(phi, Phi))
72 deltaPhi = c(deltaPhi, max(max(max((abs(phi-Phi))/(1+abs(phi))))) )
73 if(length(deltaPhi) > deltaPhiBufferSize){
74 l_1 = c(2:length(deltaPhi))
75 deltaPhi = deltaPhi[l_1]
77 sumDeltaPhi = sum(abs(deltaPhi))
79 #update other local variables
84 return(list(phi=phi, LLF=LLF))