Commit | Line | Data |
---|---|---|
ef67d338 BA |
1 | EMGLLF = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,lambda,X,Y,tau) |
2 | { | |
83ed2c0a BG |
3 | #matrix dimensions |
4 | n = dim(X)[1] | |
c2028869 BG |
5 | p = dim(phiInit)[1] |
6 | m = dim(phiInit)[2] | |
7 | k = dim(phiInit)[3] | |
83ed2c0a BG |
8 | |
9 | #init outputs | |
10 | phi = phiInit | |
11 | rho = rhoInit | |
ef67d338 | 12 | pi = piInit |
83ed2c0a BG |
13 | LLF = rep(0, maxi) |
14 | S = array(0, dim=c(p,m,k)) | |
15 | ||
83ed2c0a BG |
16 | gam = gamInit |
17 | Gram2 = array(0, dim=c(p,p,k)) | |
18 | ps2 = array(0, dim=c(p,m,k)) | |
19 | b = rep(0, k) | |
20 | pen = matrix(0, maxi, k) | |
21 | X2 = array(0, dim=c(n,p,k)) | |
6e22eb7b | 22 | Y2 = array(0, dim=c(n,m,k)) |
83ed2c0a BG |
23 | dist = 0 |
24 | dist2 = 0 | |
25 | ite = 1 | |
ef67d338 | 26 | pi2 = rep(0, k) |
83ed2c0a BG |
27 | ps = matrix(0, m,k) |
28 | nY2 = matrix(0, m,k) | |
29 | ps1 = array(0, dim=c(n,m,k)) | |
30 | nY21 = array(0, dim=c(n,m,k)) | |
31 | Gam = matrix(0, n,k) | |
32 | EPS = 1E-15 | |
33 | ||
ef67d338 BA |
34 | while(ite <= mini || (ite<= maxi && (dist>= tau || dist2 >= sqrt(tau)))) |
35 | { | |
83ed2c0a BG |
36 | Phi = phi |
37 | Rho = rho | |
ef67d338 BA |
38 | Pi = pi |
39 | ||
83ed2c0a | 40 | #calcul associé à Y et X |
ef67d338 BA |
41 | for(r in 1:k) |
42 | { | |
43 | for (mm in 1:m) | |
44 | Y2[,mm,r] = sqrt(gam[,r]) * Y[,mm] | |
45 | for (i in 1:n) | |
46 | X2[i,,r] = sqrt(gam[i,r]) * X[i,] | |
47 | for (mm in 1:m) | |
83ed2c0a | 48 | ps2[,mm,r] = crossprod(X2[,,r],Y2[,mm,r]) |
ef67d338 BA |
49 | for (j in 1:p) |
50 | { | |
51 | for (s in 1:p) | |
6e22eb7b | 52 | Gram2[j,s,r] = crossprod(X2[,j,r], X2[,s,r]) |
83ed2c0a BG |
53 | } |
54 | } | |
55 | ||
56 | ########## | |
57 | #Etape M # | |
58 | ########## | |
59 | ||
60 | #pour pi | |
ef67d338 BA |
61 | for (r in 1:k) |
62 | b[r] = sum(abs(phi[,,r])) | |
87fea89a | 63 | gam2 = colSums(gam) |
ef67d338 | 64 | a = sum(gam %*% log(pi)) |
83ed2c0a BG |
65 | |
66 | #tant que les props sont negatives | |
67 | kk = 0 | |
68 | pi2AllPositive = FALSE | |
ef67d338 BA |
69 | while (!pi2AllPositive) |
70 | { | |
71 | pi2 = pi + 0.1^kk * ((1/n)*gam2 - pi) | |
72 | pi2AllPositive = all(pi2 >= 0) | |
83ed2c0a BG |
73 | kk = kk+1 |
74 | } | |
017063cd BA |
75 | |
76 | #if (ite==2) browser() | |
ef67d338 BA |
77 | #t[m] la plus grande valeur dans la grille O.1^k tel que ce soit décroissante ou constante |
78 | while( kk < 1000 && -a/n + lambda * sum(pi^gamma * b) < | |
79 | -sum(gam2 * log(pi2))/n + lambda * sum(pi2^gamma * b) ) | |
80 | { | |
81 | pi2 = pi + 0.1^kk * (1/n*gam2 - pi) | |
82 | kk = kk + 1 | |
83ed2c0a | 83 | } |
ef67d338 BA |
84 | t = 0.1^kk |
85 | pi = (pi + t*(pi2-pi)) / sum(pi + t*(pi2-pi)) | |
83ed2c0a BG |
86 | |
87 | #Pour phi et rho | |
ef67d338 BA |
88 | for (r in 1:k) |
89 | { | |
90 | for (mm in 1:m) | |
91 | { | |
92 | for (i in 1:n) | |
93 | { | |
94 | ps1[i,mm,r] = Y2[i,mm,r] * sum(X2[i,,r] * phi[,mm,r]) | |
95 | nY21[i,mm,r] = Y2[i,mm,r]^2 | |
83ed2c0a | 96 | } |
b45ba1b0 BG |
97 | ps[mm,r] = sum(ps1[,mm,r]) |
98 | nY2[mm,r] = sum(nY21[,mm,r]) | |
017063cd BA |
99 | |
100 | #TODO: debug rho computation | |
ef67d338 | 101 | rho[mm,mm,r] = (ps[mm,r]+sqrt(ps[mm,r]^2+4*nY2[mm,r]*gam2[r])) / (2*nY2[mm,r]) |
83ed2c0a BG |
102 | } |
103 | } | |
ef67d338 BA |
104 | for (r in 1:k) |
105 | { | |
106 | for (j in 1:p) | |
107 | { | |
108 | for (mm in 1:m) | |
109 | { | |
110 | S[j,mm,r] = -rho[mm,mm,r]*ps2[j,mm,r] + | |
111 | (if(j>1) sum(phi[1:(j-1),mm,r] * Gram2[j,1:(j-1),r]) else 0) + | |
112 | (if(j<p) sum(phi[(j+1):p,mm,r] * Gram2[j,(j+1):p,r]) else 0) | |
113 | if (abs(S[j,mm,r]) <= n*lambda*(pi[r]^gamma)) | |
83ed2c0a | 114 | phi[j,mm,r]=0 |
ef67d338 BA |
115 | else if(S[j,mm,r] > n*lambda*(pi[r]^gamma)) |
116 | phi[j,mm,r] = (n*lambda*(pi[r]^gamma)-S[j,mm,r]) / Gram2[j,j,r] | |
117 | else | |
118 | phi[j,mm,r] = -(n*lambda*(pi[r]^gamma)+S[j,mm,r]) / Gram2[j,j,r] | |
83ed2c0a BG |
119 | } |
120 | } | |
121 | } | |
ef67d338 | 122 | |
83ed2c0a BG |
123 | ########## |
124 | #Etape E # | |
125 | ########## | |
126 | sumLogLLF2 = 0 | |
ef67d338 BA |
127 | for (i in 1:n) |
128 | { | |
129 | #precompute sq norms to numerically adjust their values | |
130 | sqNorm2 = rep(0,k) | |
131 | for (r in 1:k) | |
132 | sqNorm2[r] = sum( (Y[i,]%*%rho[,,r]-X[i,]%*%phi[,,r])^2 ) | |
133 | shift = 0.5*min(sqNorm2) | |
134 | ||
83ed2c0a BG |
135 | #compute Gam(:,:) using shift determined above |
136 | sumLLF1 = 0.0; | |
ef67d338 BA |
137 | for (r in 1:k) |
138 | { | |
139 | #FIXME: numerical problems, because 0 < det(Rho[,,r] < EPS; what to do ?! | |
140 | # consequence: error in while() at line 77 | |
46a2e676 | 141 | Gam[i,r] = pi[r] * exp(-0.5*sqNorm2[r] + shift) #* det(rho[,,r]) |
ef67d338 | 142 | sumLLF1 = sumLLF1 + Gam[i,r] / (2*base::pi)^(m/2) |
83ed2c0a BG |
143 | } |
144 | sumLogLLF2 = sumLogLLF2 + log(sumLLF1) | |
145 | sumGamI = sum(Gam[i,]) | |
146 | if(sumGamI > EPS) | |
147 | gam[i,] = Gam[i,] / sumGamI | |
148 | else | |
ef67d338 | 149 | gam[i,] = rep(0,k) |
83ed2c0a | 150 | } |
ef67d338 BA |
151 | |
152 | sumPen = sum(pi^gamma * b) | |
153 | LLF[ite] = -sumLogLLF2/n + lambda*sumPen | |
154 | ||
155 | dist = ifelse( ite == 1, LLF[ite], (LLF[ite]-LLF[ite-1]) / (1+abs(LLF[ite])) ) | |
156 | ||
157 | Dist1 = max( (abs(phi-Phi)) / (1+abs(phi)) ) | |
158 | Dist2 = max( (abs(rho-Rho)) / (1+abs(rho)) ) | |
159 | Dist3 = max( (abs(pi-Pi)) / (1+abs(Pi)) ) | |
160 | dist2 = max(Dist1,Dist2,Dist3) | |
161 | ||
162 | ite = ite+1 | |
83ed2c0a | 163 | } |
ef67d338 BA |
164 | |
165 | return(list("phi"=phi, "rho"=rho, "pi"=pi, "LLF"=LLF, "S"=S)) | |
87fea89a | 166 | } |