forked from docteurZ/cohsmix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathestimation_model_3.R
More file actions
executable file
·279 lines (263 loc) · 9.47 KB
/
estimation_model_3.R
File metadata and controls
executable file
·279 lines (263 loc) · 9.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
## __________________________________________________________
##
## EMalgorithmNodes
##
## INPUT: Tau -> Initial classification matrix
## Y -> Matrix of the covariates
## X -> Adjacency matrix
## NbIteration -> Number of iteration
## SelfLoop -> Equal to FALSE if the self loops
## are not considered
##
## OUTPUT: Mu -> Matrix of the estimation of the mean
## VarCovEstimated -> Estimation variance-covariance matrix
## PIEstimated -> Estimation of the connectivity matrix
## AlphaEstimated -> Estimation of the probability
## for a node i to belong to class q
## TauEstimated -> Estimation of the variational paramater
## EJ -> Value of the expected value of J for
## each iteration
##
## => Plot of EJ curve and network with the estimated classe
##
## __________________________________________________________
EMalgorithmNodes <-function(Tau,Y, X, NbIteration, Plot=TRUE, SelfLoop=FALSE){
############# Notations ##################
nbGroup <- dim(Tau)[2];
nbNodes <- dim(X)[1];
nbCov <- dim(Y)[2];
AlphaEstimated <- vector(length=nbGroup);
facteur <- matrix(1/nbNodes,nrow=1,ncol=nbNodes);
EJ <- 0;
TauEstimated <- Tau;
EJnew<-0;
Mu<-matrix(0,nrow=nbCov,ncol=nbGroup);
##########################################
# ALGO #
##########################################
cat("Iteration: ");
for (i in 1:NbIteration) {
cat(i," ");
TauPrim <- t(TauEstimated);
if (SelfLoop == FALSE){
div = t(t(as.vector(colSums(TauEstimated))))%*%colSums(TauEstimated)-(TauPrim%*%TauEstimated);
} else {
div = t(t(as.vector(colSums(TauEstimated))))%*%colSums(TauEstimated);
}
####### Estimation of the mean Mu for the Q groups#######
for (q in 1:nbGroup) {
Mu[,q]<- colSums(Y*TauEstimated[,q])/sum(TauEstimated[,q])
}
####### Estimation of PI #######
PItemp <- (TauPrim%*%X%*%TauEstimated);
PIEstimated <- PItemp/(div);
PIEstimated[is.nan(PIEstimated) ==TRUE] <- exp(mincut);
PIEstimated[PIEstimated == 'Inf'] <- exp(mincut);
PIEstimated[PIEstimated == 1] <- (1-exp(mincut));
PIEstimated[PIEstimated < exp(mincut)] <- exp(mincut);
####### Estimation of Alpha #######
AlphaEstimated <- facteur %*% TauEstimated;
####### Estimation of the variance-covariance matrix #######
sigma2<-0;
for (q in 1:nbGroup) {
sigma2 <- sigma2 + (1/sum(TauEstimated[,q])) * sum(TauEstimated[,q] * apply((t(Y) - Mu[,q])^2,2, sum));
}
sigma2<-sigma2/nbCov;
VarCovEstimated<-diag(rep(sigma2,nbCov));
####### Expected value of J #######
EJnew<-ExpectedJNodes(TauEstimated,Y,X,Mu,PIEstimated,AlphaEstimated,VarCovEstimated,SelfLoop=SelfLoop);
#print("NEw");
#print(EJnew);
EJ <- c(EJ,EJnew);
EJold<-EJ[i];
#print("old");
#print(EJold)
if (EJold!=0){
critere <- abs((EJnew-EJold)/EJold);
print(critere);
if (critere<=0.0000001){
break();
}
}
####### Estimation of Tau #######
Tautemp <- EstimTauNodes(TauEstimated,Y,X,Mu,PIEstimated,AlphaEstimated,VarCovEstimated,SelfLoop=SelfLoop);
TauEstimated <- Tautemp$TauEstimated;
}
cat("\n");
if (Plot==TRUE){
#################### Representation #######################
#x11()
#par(mfrow = c(1,2))
#cat("Plotting the convergence criterion...","\n")
#plot(EJ[2:NbIteration])
#title("Expected value of J: Convergence criterion")
#cat("Plotting the estimated network structure...","\n")
#map=MAP(TauEstimated)
#gplot(Adjacente,vertex.col=map$node.classes+2)
#title("Network with estimated classes")
############################################################
}
return(list(Mu=Mu,VarCovEstimated=VarCovEstimated,
PIEstimated=PIEstimated,
AlphaEstimated=AlphaEstimated,
TauEstimated=TauEstimated,
EJ=EJ[2:length(EJ)]));
}
## __________________________________________________________
##
## EstimTauNodes
##
## INPUT: Tau -> Initial classification matrix
## Y -> Matrix of the covariates
## X -> Adjacency matrix
## Mu -> Matrix of the estimations of the
## means
## PI -> Connectivity matrix
## Alpha -> Vector of the probability
## for a node i to belong to class q
## VarCov -> Variance-Covariance matrix
## SelfLoop -> Equal to FALSE if the self loops
## are not considered
##
## OUTPUT : TauEstimated -> Estimation of the variational paramater
## according to the inputs
## __________________________________________________________
EstimTauNodes <-function(Tau, Y, X, Mu, PI, Alpha, VarCov, SelfLoop = FALSE){
nbGroup <- dim(Tau)[2];
nbNodes <- dim(Y)[1];
TauPrim <- t(Tau);
TauEstimated <- Tau;
LogTauEstimated <- matrix(0,nrow=nbNodes,ncol=nbGroup);
PIPrim <- t(PI);
H <- 1-PI;
HPrim <- t(H);
VarCovInv <- ginv(VarCov);
detVar <- det(VarCov);
maxcut <- log(.Machine$double.xmax) - log(nbGroup);
mincut <- log(.Machine$double.xmin);
for (q in 1:nbGroup){
HPrim[q,] <- pmin(HPrim[q, ], exp(maxcut));
HPrim[q,] <- pmax(HPrim[q, ], exp(mincut));
PIPrim[q,] <- pmin(PIPrim[q, ], exp(maxcut));
PIPrim[q,] <- pmax(PIPrim[q, ], exp(mincut));
Alpha[q] <- pmin(Alpha[q], exp(maxcut));
Alpha[q] <- pmax(Alpha[q], exp(mincut));
}
HPrim[is.nan(HPrim) == TRUE] <- exp(mincut);
HPrim[HPrim < exp(mincut) ] <- exp(mincut);
H[is.nan(H) == TRUE] <- exp(mincut);
H[H < exp(mincut) ] <- exp(mincut);
LogAlpha <- log(Alpha);
LogPIPrim <- log(PIPrim);
LogHPrim <- log(HPrim);
BernMatrix = (((X%*%Tau)%*%(LogPIPrim)) + (((1-X)%*%Tau)%*%(LogHPrim)));
TauLogHPrim <- Tau%*%LogHPrim;
for (i in 1:nbNodes){
for (q in 1:nbGroup){
Bern <- BernMatrix[i,q];
Norm <- log(1/(2*pi^(nbNodes/2)*(detVar^(1/2))))-1/2 *(Y[i,] - Mu[,q])%*%VarCovInv%*%t(t((Y[i,] - Mu[,q])));
EgaliteIJ <- - TauLogHPrim[i,q];
if (SelfLoop == FALSE){
LogTauEstimated[i,q] <- LogAlpha[q] + Bern + Norm + EgaliteIJ;
} else {
LogTauEstimated[i,q] <- LogAlpha[q] + Bern + Norm;
}
if (is.nan(LogTauEstimated[i,q])==TRUE){
LogTauEstimated[i,q]<-mincut;
}
if (LogTauEstimated[i,q]=='Inf'){
LogTauEstimated[i,q]<-mincut;
}
}
LogTauEstimated[i, ] <- pmin(LogTauEstimated[i, ], maxcut);
LogTauEstimated[i, ] <- pmax(LogTauEstimated[i, ], mincut);
TauEstimated[i, ] <- exp(LogTauEstimated[i, ]);
Normalize <- 1/sum(TauEstimated[i,]);
TauEstimated[i,] <- TauEstimated[i,] * Normalize;
TauEstimated[i, ][TauEstimated[i, ] < .Machine$double.xmin] <- .Machine$double.xmin;
}
return( list(TauEstimated=TauEstimated));
}
## __________________________________________________________
##
## ExpectedJNodes
##
## INPUT: Tau -> Initial classification matrix
## Y -> Matrix of the covariates
## X -> Adjacency matrix
## Mu -> Matrix of the estimations of the
## means
## PI -> Connectivity matrix
## Alpha -> Vector of the probability
## for a node i to belong to class q
## VarCov -> Variance-Covariance matrix
## SelfLoop -> Equal to FALSE if the self loops
## are not considered
##
## OUTPUT : Expected -> Expected value of J
##
## __________________________________________________________
ExpectedJNodes <-function(Tau, Y, X, Mu, PI, Alpha, VarCov, SelfLoop=FALSE){
nbGroup <- dim(Tau)[2];
nbNodes <- dim(Y)[1];
entropieTemp <- 0;
PIPrim <- t(PI);
H <- 1-PI;
HPrim <- t(H);
maxcut <- log(.Machine$double.xmax) - log(nbGroup);
mincut <- log(.Machine$double.xmin);
VarCovInv <- ginv(VarCov);
detVar <- det(VarCov);
Norm<-0;
################ Entropie #############################
for(q in 1:nbGroup){
HPrim[q,] <- pmin(HPrim[q, ], exp(maxcut));
HPrim[q,] <- pmax(HPrim[q, ], exp(mincut));
PIPrim[q,] <- pmin(PIPrim[q, ], exp(maxcut));
PIPrim[q,] <- pmax(PIPrim[q, ], exp(mincut));
Alpha[q] <- pmin(Alpha[q], exp(maxcut));
Alpha[q] <- pmax(Alpha[q], exp(mincut));
PI[q,] <- pmin(PI[q,], exp(maxcut));
PI[q,] <- pmax(PI[q,], exp(mincut));
H[q,] <- pmin(H[q,], exp(maxcut));
H[q,] <- pmax(H[q,], exp(mincut));
}
HPrim[is.nan(HPrim) == TRUE] <- exp(mincut);
HPrim[HPrim < exp(mincut) ] <- exp(mincut);
H[is.nan(H) == TRUE] <- exp(mincut);
H[H < exp(mincut) ] <- exp(mincut);
for(i in 1:nbNodes){
Tau[i, ] <- pmin(Tau[i, ], exp(maxcut));
Tau[i, ] <- pmax(Tau[i, ], exp(mincut));
for (q in 1:nbGroup){
entropieTemp <- c(entropieTemp,Tau[i,q]*log(Tau[i,q]));
tmpNorm <- Tau[i,q]*(log(1/(2*pi^(nbNodes/2)*detVar^(1/2)))-1/2 *((Y[i,] - Mu[,q])%*%VarCovInv)%*%t(t((Y[i,] - Mu[,q]))));
if (tmpNorm=='Inf'){
tmpNorm<-mincut;
}
Norm <- c(Norm, tmpNorm);
}
}
entropie <- sum(entropieTemp);
AlphaPrim <- t(Alpha);
TauPrim <- t(Tau);
Bern1 <- TauPrim%*%X%*%Tau;
Bern2 <- TauPrim%*%(1-X)%*%Tau;
#print(Norm)
#print(AlphaPrim)
#print(TauPrim)
if (SelfLoop == FALSE){
Expected <- sum(Tau%*%AlphaPrim) + sum(Bern1*log(PI) + Bern2*log(H))-sum((TauPrim%*%Tau)*log(H)) + sum(Norm) - entropie;
print("alpha");
print(sum(Tau%*%AlphaPrim));
print("pi");
print(sum(Bern1*log(PI) + Bern2*log(H)) -sum((TauPrim%*%Tau)*log(H)));
print("norm");
print( sum(Norm));
print("entropie")
print(entropie);
} else {
Expected <- sum(Tau%*%AlphaPrim) + sum(Bern1*log(PI) + Bern2*log(H)) +sum(Norm)- entropie;
}
return(Expected);
}