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