-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpectationMaximization.java
283 lines (210 loc) · 7.66 KB
/
ExpectationMaximization.java
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
280
281
282
283
import org.apache.commons.math3.linear.*;
import org.apache.commons.math3.stat.StatUtils;
import java.io.File;
import static javafx.scene.input.KeyCode.R;
/**
* Created by ARPAN on 17-11-2016.
*/
class Model{
RealMatrix mu;
RealMatrix[] sigma;
RealVector w;
}
public class ExpectationMaximization {
static final double pi=3.14159265;
public static int[] EM(double[][] data,int[] label,int nData,int nDim,int init){
double tol=1E-6;
int maxitr=10;
double[] llh=new double[maxitr];
for(double i:llh)
i=Double.NEGATIVE_INFINITY;
RealMatrix X=new Array2DRowRealMatrix(data);
X=X.transpose();
//IO.display(X,"X");
RealMatrix R=initialization(nData,label,init);
//IO.display(label,"Label",nData);
//IO.display(R,"R");
for(int i=1;i<maxitr;i++){
//System.out.print("itr="+i+"\t"+":");
label=getLabels(R,nData,init);
//IO.display(label,"Label",nData);
Model model=maximization(X,R,nData,nDim,init);
R=expectation(X,model,nData,nDim,init,llh,i);
if(Math.abs(llh[i]-llh[i-1])<tol*Math.abs(llh[i])){
break;
}
}
return label;
}
public static RealMatrix initialization(int nData,int init){
int[] label=new int[nData];
for(int i:label)
i=(int)Math.ceil(init*Math.random());
double[][] R=new double[nData][init];
for(int i=0;i<nData;i++){
R[i][label[i]]=1;
}
return new Array2DRowRealMatrix(R);
}
public static RealMatrix initialization(int nData,int[] label,int init){
RealMatrix R=new Array2DRowRealMatrix(nData,init);
for(int i=0;i<nData;i++){
R.setEntry(i,label[i],1);
}
return R;
}
public static int[] getLabels(RealMatrix R,int nData,int k){
double max;int maxindex;
int[] label=new int[nData];
for(int i=0;i<nData;i++){
max=R.getEntry(i,0);
maxindex=0;
for(int j=1;j<k;j++){
if(R.getEntry(i,j)>max){
max=R.getEntry(i,j);
maxindex=j;
}
}
label[i]=maxindex;
}
return label;
}
public static Model maximization(RealMatrix X,RealMatrix R,int nData,int nDim,int k){
RealVector nk=new ArrayRealVector(k);
for(int i=0;i<k;i++){
nk.setEntry(i,StatUtils.sum(R.getColumn(i)));
}
RealVector w=new ArrayRealVector(k);
for(int i=0;i<k;i++){
w.setEntry(i,nk.getEntry(i)/nData);
}
RealMatrix XtimesR=R.preMultiply(X);
//mu: nDim x k
RealMatrix mu=new Array2DRowRealMatrix(nDim,k);
for(int i=0;i<nDim;i++){
RealVector row=new ArrayRealVector(XtimesR.getRowVector(i));
mu.setRowVector(i,row.ebeDivide(nk));
}
//IO.display(mu,"mu");
RealMatrix[] sigma=new RealMatrix[k];
for(int i=0;i<k;i++){
sigma[i]=new Array2DRowRealMatrix(nData,nDim);
}
//r: nData x k
RealMatrix r=new Array2DRowRealMatrix(nData,k);
for(int i=0;i<nData;i++){
for(int j=0;j<k;j++){
//r[i][j]=Math.sqrt(R.getEntry(i,j));
r.setEntry(i,j,Math.sqrt(R.getEntry(i,j)));
}
}
//X: nDim x nData
RealMatrix Xo=new Array2DRowRealMatrix(nDim,nData);
for(int i=0;i<k;i++){
RealVector cur_mu=mu.getColumnVector(i);
for(int j=0;j<nData;j++){
Xo.setColumnVector(j,X.getColumnVector(j).subtract(cur_mu));
}
RealVector cur_r=r.getColumnVector(i);
for(int j=0;j<nDim;j++){
Xo.setRowVector(j,Xo.getRowVector(j).ebeMultiply(cur_r));
}
//IO.display(Xo,"Xo");
RealMatrix Sigma=Xo.transpose().preMultiply(Xo);
for(int j=0;j<nDim;j++){
for(int l=0;l<nDim;l++){
Sigma.setEntry(j,l,(Sigma.getEntry(j,l)/nk.getEntry(i)));
//Sigma.setEntry(j,l,(Sigma.getEntry(j,l)/nk.getEntry(i))+1E-6);
}
}
//IO.display(Sigma,"Sigma");
sigma[i]=Sigma;
}
Model model=new Model();
model.mu=mu;
model.sigma=sigma;
model.w=w;
return model;
}
public static RealMatrix expectation(RealMatrix X,Model model,int nData,int nDim,int k,double[] llh,int iter){
RealMatrix mu=model.mu;
RealMatrix[] sigma=model.sigma;
RealVector w=model.w;
RealMatrix R1=new Array2DRowRealMatrix(nData,k);
for(int i=0;i<k;i++){
R1.setColumnVector(i,loggausspdf(X,mu.getColumnVector(i),sigma[i],nData,nDim));
}
for(int i=0;i<k;i++){
w.setEntry(i,Math.log(w.getEntry(i)));
}
for(int i=0;i<nData;i++){
R1.setRowVector(i,R1.getRowVector(i).add(w));
}
RealVector T=logsumexp(R1,nData,nDim,k);
llh[iter]=StatUtils.sum(T.toArray())/nData;
for(int i=0;i<k;i++){
R1.setColumnVector(i,R1.getColumnVector(i).subtract(T));
}
for(int i=0;i<nData;i++){
for(int j=0;j<k;j++){
R1.setEntry(i,j,Math.exp(R1.getEntry(i,j)));
}
}
return R1;
}
public static RealVector loggausspdf(RealMatrix X,RealVector mu,RealMatrix sigma,int nData,int nDim){
RealMatrix X_mu=new Array2DRowRealMatrix(nDim,nData);
for(int i=0;i<nData;i++){
X_mu.setColumnVector(i,X.getColumnVector(i).subtract(mu));
}
//IO.display(X_mu,"X_mu");
CholeskyDecomposition cholesky=new CholeskyDecomposition(sigma);
RealMatrix U=cholesky.getL();
//IO.display(U,"U");
RealMatrix invU = new LUDecomposition(U).getSolver().getInverse();
//IO.display(invU,"invU");
RealMatrix Q=X_mu.preMultiply(invU);
//IO.display(Q,"Q");
RealVector q=new ArrayRealVector(nData);
for(int i=0;i<nData;i++){
q.setEntry(i,Q.getColumnVector(i).dotProduct(Q.getColumnVector(i)));
}
//IO.display(q,"q");
double diagsum=0;
for(int i=0;i<nDim;i++){
diagsum+=Math.log(U.getEntry(i,i));
}
RealVector c=new ArrayRealVector(nData,nDim*Math.log(2*pi)+(2*diagsum));
//IO.display(c,"c");
RealVector y=c.add(q);
y.mapMultiplyToSelf(-0.5);
//IO.display(y,"y");
return y;
}
public static RealVector logsumexp(RealMatrix R,int nData,int nDim,int k){
RealMatrix y=new Array2DRowRealMatrix(nData,k);
double rowmax=Double.NEGATIVE_INFINITY,rowsum=0;
RealVector s=new ArrayRealVector(nData);
for(int i=0;i<nData;i++){
rowmax=StatUtils.max(R.getRow(i));
rowsum=0;
for(int j=0;j<k;j++){
y.setEntry(i,j,Math.exp(R.getEntry(i,j)-rowmax));
rowsum+=y.getEntry(i,j);
}
s.setEntry(i,Math.log(rowsum)+rowmax);
}
return s;
}
public static void main(String args[]){
int nData=150,nDim=4;
File file_in=new File("./data/LunarData/iris_dataset.txt");
File init_label=new File("./data/LunarData/iris_label.txt");
double[][] data=IO.readDoubleMat(file_in,nData,nDim);
int[] init=IO.readIntVec(init_label,nData);
//IO.display(data,"Iris dataset",nData,nDim);
//IO.display(init,"Initial label",nData);
int[] EMlabel=EM(data,init,nData,nDim,3);
IO.display(EMlabel,"EM Label",150);
}
}