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