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