Commit | Line | Data |
---|---|---|
c2028869 BG |
1 | EMGLLF = function(Pi, Rho, mini, maxi, X, Y, tau, rank){ |
2 | #matrix dimensions | |
3 | n = dim(X)[1] | |
4 | p = dim(X)[2] | |
5 | m = dim(Rho)[2] | |
6 | k = dim(Rho)[3] | |
7 | ||
8 | #init outputs | |
9 | phi = array(0, dim=c(p,m,k)) | |
10 | Z = rep(1, n) | |
11 | Pi = piInit | |
12 | LLF = 0 | |
13 | ||
14 | #local variables | |
15 | Phi = array(0, dim=c(p,m,k)) | |
16 | deltaPhi = c(0) | |
17 | sumDeltaPhi = 0 | |
18 | deltaPhiBufferSize = 20 | |
19 | ||
20 | #main loop | |
21 | ite = 1 | |
22 | while(ite<=mini || (ite<=maxi && sumDeltaPhi>tau)){ | |
23 | #M step: Mise à jour de Beta (et donc phi) | |
24 | for(r in 1:k){ | |
25 | Z_bin = vec_bin(Z,r) | |
26 | Z_vec = Z_bin$vec #vecteur 0 et 1 aux endroits o? Z==r | |
27 | Z_indice = Z_bin$indice | |
28 | if(sum(Z_indice) == 0){ | |
29 | next | |
30 | } | |
31 | #U,S,V = SVD of (t(Xr)Xr)^{-1} * t(Xr) * Yr | |
32 | [U,S,V] = svd(ginv(crossprod(X[Z_indice,]))%*% (X[Z_indice,])%*%Y[Z_indice,] ) | |
33 | #Set m-rank(r) singular values to zero, and recompose | |
34 | #best rank(r) approximation of the initial product | |
35 | S[rank(r)+1:end,] = 0 | |
36 | phi[,,r] = U %*%S%*%t(V)%*%Rho[,,r] | |
37 | } | |
38 | ||
39 | #Etape E et calcul de LLF | |
40 | sumLogLLF2 = 0 | |
41 | for(i in 1:n){ | |
42 | sumLLF1 = 0 | |
43 | maxLogGamIR = -Inf | |
44 | for(r in 1:k){ | |
45 | dotProduct = tcrossprod(Y[i,]%*%Rho[,,r]-X[i,]%*%phi[,,r]) | |
46 | logGamIR = log(Pi[r]) + log(det(Rho[,,r])) - 0.5*dotProduct | |
47 | #Z[i] = index of max (gam[i,]) | |
48 | if(logGamIR > maxLogGamIR){ | |
49 | Z[i] = r | |
50 | maxLogGamIR = logGamIR | |
51 | } | |
52 | sumLLF1 = sumLLF1 + exp(logGamIR) / (2*pi)^(m/2) | |
53 | } | |
54 | sumLogLLF2 = sumLogLLF2 + log(sumLLF1) | |
55 | } | |
56 | ||
57 | LLF = -1/n * sumLogLLF2 | |
58 | ||
59 | #update distance parameter to check algorithm convergence (delta(phi, Phi)) | |
60 | deltaPhi = c(deltaPhi, max(max(max((abs(phi-Phi))/(1+abs(phi))))) ) | |
61 | if(length(deltaPhi) > deltaPhiBufferSize){ | |
62 | deltaPhi = deltaPhi[2:length(deltaPhi)] | |
63 | } | |
64 | sumDeltaPhi = sum(abs(deltaPhi)) | |
65 | ||
66 | #update other local variables | |
67 | Phi = phi | |
68 | ite = ite+1 | |
69 | ||
70 | } | |
71 | return(list(phi=phi, LLF=LLF)) | |
72 | } |