1 EMGLLF_R = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,lambda,X,Y,tau)
14 S = array(0, dim=c(p,m,k))
18 Gram2 = array(0, dim=c(p,p,k))
19 ps2 = array(0, dim=c(p,m,k))
21 X2 = array(0, dim=c(n,p,k))
22 Y2 = array(0, dim=c(n,m,k))
27 # Remember last pi,rho,phi values for exit condition in the end of loop
32 # Calcul associé à Y et X
36 Y2[,mm,r] = sqrt(gam[,r]) * Y[,mm]
38 X2[i,,r] = sqrt(gam[i,r]) * X[i,]
40 ps2[,mm,r] = crossprod(X2[,,r],Y2[,mm,r])
44 Gram2[j,s,r] = crossprod(X2[,j,r], X2[,s,r])
53 b = sapply( 1:k, function(r) sum(abs(phi[,,r])) )
55 a = sum(gam %*% log(pi))
57 # Tant que les props sont negatives
59 pi2AllPositive = FALSE
60 while (!pi2AllPositive)
62 pi2 = pi + 0.1^kk * ((1/n)*gam2 - pi)
63 pi2AllPositive = all(pi2 >= 0)
67 # t(m) la plus grande valeur dans la grille O.1^k tel que ce soit décroissante ou constante
68 while( kk < 1000 && -a/n + lambda * sum(pi^gamma * b) <
69 -sum(gam2 * log(pi2))/n + lambda * sum(pi2^gamma * b) )
71 pi2 = pi + 0.1^kk * (1/n*gam2 - pi)
75 pi = (pi + t*(pi2-pi)) / sum(pi + t*(pi2-pi))
84 ps = ps + Y2[i,mm,r] * sum(X2[i,,r] * phi[,mm,r])
85 nY2 = sum(Y2[,mm,r]^2)
86 rho[mm,mm,r] = (ps+sqrt(ps^2+4*nY2*gam2[r])) / (2*nY2)
96 S[j,mm,r] = -rho[mm,mm,r]*ps2[j,mm,r] + sum(phi[-j,mm,r] * Gram2[j,-j,r])
97 if (abs(S[j,mm,r]) <= n*lambda*(pi[r]^gamma))
99 else if(S[j,mm,r] > n*lambda*(pi[r]^gamma))
100 phi[j,mm,r] = (n*lambda*(pi[r]^gamma)-S[j,mm,r]) / Gram2[j,j,r]
102 phi[j,mm,r] = -(n*lambda*(pi[r]^gamma)+S[j,mm,r]) / Gram2[j,j,r]
119 gam[i,r] = pi[r] * exp(-0.5*sum( (Y[i,]%*%rho[,,r]-X[i,]%*%phi[,,r])^2 ))
121 sumLLH1 = sumLLH1 + gam[i,r] / (2*base::pi)^(m/2)
122 sumGamI = sumGamI + gam[i,r]
124 sumLogLLH2 = sumLogLLH2 + log(sumLLH1)
125 if(sumGamI > EPS) #else: gam[i,] is already ~=0
126 gam[i,] = gam[i,] / sumGamI
129 sumPen = sum(pi^gamma * b)
131 llh = -sumLogLLH2/n + lambda*sumPen
132 dist = ifelse( ite == 1, llh, (llh-last_llh) / (1+abs(llh)) )
133 Dist1 = max( (abs(phi-Phi)) / (1+abs(phi)) )
134 Dist2 = max( (abs(rho-Rho)) / (1+abs(rho)) )
135 Dist3 = max( (abs(pi-Pi)) / (1+abs(Pi)) )
136 dist2 = max(Dist1,Dist2,Dist3)
138 if (ite>=mini && (dist>= tau || dist2 >= sqrt(tau)))
142 affec = apply(gam, 1, which.max)
143 list( "phi"=phi, "rho"=rho, "pi"=pi, "llh"=llh, "S"=S, "affec"=affec )