-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockHMM.java
500 lines (387 loc) · 11.1 KB
/
BlockHMM.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Random;
import org.apache.commons.math.special.Gamma;
public class BlockHMM extends TopicModel {
public HashMap<Integer,Integer> docMap;
public HashMap<String,Integer> wordMap;
public HashMap<Integer,String> wordMapInv;
public String[] docsID;
public int[] docsPrev;
public int[][] docsNext;
public int[][] docs;
public int[] docsZ;
public int[][] docsX;
public int[][] nTZ;
public int[] nT;
public int[][] nZW;
public int[] nZ;
public int[] nBW;
public int nB;
public int[] nX;
public int D;
public int W;
public int Z;
public double omega;
public double[] alpha;
public double gamma0;
public double gamma1;
public Random r = new Random();
public BlockHMM(int z, double g0, double g1) {
gamma0 = g0;
gamma1 = g1;
Z = z;
alpha = new double[Z];
for (int i = 0; i < Z; i++) {
alpha[i] = 0.1; // initial value
}
}
public void initialize() {
System.out.println("Initializing...");
omega = 0.01; // initial value
docsZ = new int[D];
docsX = new int[D][];
nTZ = new int[Z+1][Z];
nT = new int[Z+1];
nZW = new int[Z][W];
nZ = new int[Z];
nBW = new int[W];
nB = 0;
nX = new int[2];
for (int d = 0; d < D; d++) {
int z = r.nextInt(Z); // select random z value in {0...Z-1}
docsZ[d] = z;
docsX[d] = new int[docs[d].length];
for (int n = 0; n < docs[d].length; n++) {
int w = docs[d][n];
//int x = r.nextInt(2); // select x uniformly
int x = 0;
double u = r.nextDouble(); // select random x value in {0,1}
u *= (double)(gamma0+gamma1); // from distribution given by prior
if (u > gamma0) x = 1;
//x = 1;
docsX[d][n] = x;
// update counts
nX[x] += 1;
if (x == 0) {
nBW[w] += 1;
nB += 1;
}
else {
nZW[z][w] += 1;
nZ[z] += 1;
}
}
}
for (int d = 0; d < D; d++) {
int z = docsZ[d];
int zP = docsPrev[d] == -1 ? Z : docsZ[docsPrev[d]];
nTZ[zP][z] += 1;
nT[zP] += 1;
}
}
public void updateOmega()
{
double LLold = 0;
double LLnew = 0;
double omegaSum = omega*(double)W;
double omegaNew = Math.exp(Math.log(omega) + r.nextGaussian());
double omegaSumNew = omegaNew * (double)W;
for (int z = 0; z < Z; z++) {
LLold += Gamma.logGamma(omegaSum) - Gamma.logGamma(nZ[z] + omegaSum);
LLnew += Gamma.logGamma(omegaSumNew) - Gamma.logGamma(nZ[z] + omegaSumNew);
for (int w = 0; w < W; w++) {
LLold += Gamma.logGamma(nZW[z][w] + omega) - Gamma.logGamma(omega);
LLnew += Gamma.logGamma(nZW[z][w] + omegaNew) - Gamma.logGamma(omegaNew);
}
}
double ratio = Math.exp(LLnew - LLold);
boolean accept = false;
if (r.nextDouble() < ratio) accept = true;
System.out.println("omega: proposed "+omegaNew);
System.out.println(" (ratio = "+ratio);
if (accept) {
omega = omegaNew;
System.out.println("Accepted");
} else {
System.out.println("Rejected");
}
System.out.println("omega: "+omega);
}
public void doSampling() {
for (int d = 0; d < D; d++) {
for (int n = 0; n < docs[d].length; n++) {
sample(d, n);
}
sampleD(d);
}
// update hyperparams every 10 iterations
if (iter > 100 && iter % 10 == 0) {
// update omega
updateOmega();
// update alpha (Tom Minka 03 method)
double[] alphaNew = new double[Z];
double alphaNorm = 0;
for (int z = 0; z < Z; z++) alphaNorm += alpha[z];
double denom = 0;
for (int zP = 0; zP < Z+1; zP++) {
for (int z = 0; z < Z; z++) {
alphaNew[z] += Gamma.digamma(nTZ[zP][z] + alpha[z]) - Gamma.digamma(alpha[z]);
}
denom += Gamma.digamma(nT[zP] + alphaNorm) - Gamma.digamma(alphaNorm);
}
for (int z = 0; z < Z; z++) {
alpha[z] = alpha[z] * (alphaNew[z] / denom);
alpha[z] += 0.1; // hack
System.out.println("alpha_"+z+" "+alpha[z]);
}
}
}
public void sampleD(int d) {
int topic = docsZ[d];
int topicPrev = docsPrev[d] == -1 ? Z : docsZ[docsPrev[d]];
// decrement counts
nTZ[topicPrev][topic] -= 1;
nT[topicPrev] -= 1;
for (int e = 0; e < docsNext[d].length; e++) {
nTZ[topic][docsZ[docsNext[d][e]]] -= 1;
nT[topic] -= 1;
}
for (int n = 0; n < docs[d].length; n++) {
int w = docs[d][n];
int level = docsX[d][n];
if (level == 1) {
nZW[topic][w] -= 1;
nZ[topic] -= 1;
}
}
double[] logp = new double[Z+1];
double alphaNorm = 0;
for (int z = 0; z < Z; z++) alphaNorm += alpha[z];
double omegaNorm = W * omega;
// word probabilties
for (int n = 0; n < docs[d].length; n++) {
int w = docs[d][n];
int level = docsX[d][n];
if (level == 1) {
for (int z = 0; z < Z; z++) {
double prob = (nZW[z][w] + omega) / (nZ[z] + omegaNorm);
logp[z] += Math.log(prob);
}
}
}
// transition probabilities
for (int z = 0; z < Z; z++) {
double prob = (nTZ[topicPrev][z] + alpha[z]) / (nT[topicPrev] + alphaNorm);
logp[z] += Math.log(prob);
for (int e = 0; e < docsNext[d].length; e++) {
int topicNext = docsZ[docsNext[d][e]];
prob = (nTZ[z][topicNext] + alpha[topicNext]) / (nT[z] + alphaNorm);
logp[z] += Math.log(prob);
}
}
// sampled from unnormalized ratios
double[] p = new double[Z];
p[0] = 1.0;
double pTotal = p[0];
for (int z = 1; z < Z; z++) { //Z+1 if we include bg
p[z] = p[z-1] * Math.exp(logp[z] - logp[z-1]);
pTotal += p[z];
}
double u = r.nextDouble() * pTotal;
double v = 0.0;
for (int z = 0; z < Z; z++) {
v += p[z];
if (v > u) {
topic = z;
break;
}
}
// increment counts
nTZ[topicPrev][topic] += 1;
nT[topicPrev] += 1;
for (int e = 0; e < docsNext[d].length; e++) {
nTZ[topic][docsZ[docsNext[d][e]]] += 1;
nT[topic] += 1;
}
for (int n = 0; n < docs[d].length; n++) {
int w = docs[d][n];
int level = docsX[d][n];
if (level == 1) {
nZW[topic][w] += 1;
nZ[topic] += 1;
}
}
docsZ[d] = topic;
}
public void sample(int d, int n) {
int w = docs[d][n];
int topic = docsZ[d];
int level = docsX[d][n];
// decrement counts
nX[level] -= 1;
if (level == 0) {
nBW[w] -= 1;
nB -= 1;
} else {
nZW[topic][w] -= 1;
nZ[topic] -= 1;
}
double omegaNorm = W * omega;
// sample new value for level
double p0 = (nX[0] + gamma0) * // background
(nBW[w] + omega) / (nB + omegaNorm);
double p1 = (nX[1] + gamma1) * // topic
(nZW[topic][w] + omega) / (nZ[topic] + omegaNorm);
double pTotal = p0 + p1;
double u = r.nextDouble() * pTotal;
if (u > p0) level = 1;
else level = 0;
// increment counts
nX[level] += 1;
if (level == 0) {
nBW[w] += 1;
nB += 1;
} else {
nZW[topic][w] += 1;
nZ[topic] += 1;
}
// set new assignments
docsX[d][n] = level;
}
public void readDocs(String filename) throws Exception {
System.out.println("Reading input...");
docMap = new HashMap<Integer,Integer>();
wordMap = new HashMap<String,Integer>();
wordMapInv = new HashMap<Integer,String>();
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String s;
D = 0;
int dj = 0;
while((s = br.readLine()) != null) {
D++;
}
docsID = new String[D];
docs = new int[D][];
docsPrev = new int[D];
docsNext = new int[D][];
int[] countNext = new int[D];
fr = new FileReader(filename);
br = new BufferedReader(fr);
while ((s = br.readLine()) != null) {
String[] tokens0 = s.split("\\s+");
String [] tokens = new String[tokens0.length-3];
for (int n = 3; n < tokens0.length; n++) tokens[n-3] = tokens0[n];
int d0 = Integer.parseInt(tokens0[0]);
if (!docMap.containsKey(d0)) {
docMap.put(d0, docMap.size());
}
}
fr.close();
br.close();
fr = new FileReader(filename);
br = new BufferedReader(fr);
int d = 0;
int di = 0;
while ((s = br.readLine()) != null) {
String[] tokens0 = s.split("\\s+");
String [] tokens = new String[tokens0.length-3];
for (int n = 3; n < tokens0.length; n++) tokens[n-3] = tokens0[n];
int N = tokens.length;
int d0 = Integer.parseInt(tokens0[0]);
d = docMap.get(d0);
docsPrev[d] = Integer.parseInt(tokens0[1]);
docsID[d] = tokens0[2];
if (docsPrev[d] > -1) {
docsPrev[d] = docMap.get(docsPrev[d]); // if not -1, convert to internal ID
countNext[docsPrev[d]] += 1;
}
docs[d] = new int[N];
for (int n = 0; n < N; n++) {
String word = tokens[n];
int key = wordMap.size();
if (!wordMap.containsKey(word)) {
wordMap.put(word, new Integer(key));
wordMapInv.put(new Integer(key), word);
}
else {
key = ((Integer) wordMap.get(word)).intValue();
}
docs[d][n] = key;
}
}
for (int i = 0; i < D; i++) docsNext[i] = new int[countNext[i]];
countNext = new int[D];
for (int i = 0; i < D; i++) {
int prev = docsPrev[i];
if (prev != -1) {
docsNext[prev][countNext[prev]] = i;
countNext[prev]++;
}
}
br.close();
fr.close();
W = wordMap.size();
System.out.println(D+" documents");
System.out.println(W+" word types");
}
public void writeOutput(String filename) throws Exception {
System.out.println("Writing output...");
FileWriter fw = new FileWriter(filename+".assign");
BufferedWriter bw = new BufferedWriter(fw);
for (int d = 0; d < D; d++) {
bw.write(d+" "+docsPrev[d]+" "+docsID[d]+" "+docsZ[d]+" ");
for (int n = 0; n < docs[d].length; n++) {
String word = wordMapInv.get(docs[d][n]);
bw.write(word+":"+docsX[d][n]+" ");
}
bw.newLine();
}
bw.close();
fw.close();
fw = new FileWriter(filename+".alpha");
bw = new BufferedWriter(fw);
for (int z = 0; z < Z; z++) {
bw.write(alpha[z]+" ");
}
bw.close();
fw.close();
fw = new FileWriter(filename+".omega");
bw = new BufferedWriter(fw);
bw.write(""+omega);
bw.close();
fw.close();
// estimate transition matrix
double alphaNorm = 0;
for (int z = 0; z < Z; z++) alphaNorm += alpha[z];
fw = new FileWriter(filename+".pi");
bw = new BufferedWriter(fw);
for (int i = 0; i < Z; i++) {
for (int j = 0; j < Z; j++) {
double prob = (nTZ[i][j] + alpha[j]) / (nT[i] + alphaNorm);
bw.write(prob+" ");
}
bw.newLine();
}
bw.close();
fw.close();
}
/*
public static double digamma(double x)
{
double r = 0.0;
while (x <= 5.0) {
r -= 1.0 / x;
x += 1.0;
}
double f = 1.0 / (x * x);
double t = f * (-1 / 12.0 + f * (1 / 120.0 + f * (-1 / 252.0 + f * (1 / 240.0 + f * (-1 / 132.0 + f * (691 / 32760.0 + f * (-1 / 12.0
+ f * 3617.0 / 8160.0)))))));
return r + Math.log(x) - 0.5 / x + t;
}
*/
}