double[] logTotals = new double[state.getNumTopics()];
Arrays.fill(logTotals, Double.NEGATIVE_INFINITY);
// Output sufficient statistics for each word. == pseudo-log counts.
DoubleWritable v = new DoubleWritable();
for (Iterator<Vector.Element> iter = wordCounts.iterateNonZero(); iter.hasNext();) {
Vector.Element e = iter.next();
int w = e.index();
for (int k = 0; k < state.getNumTopics(); ++k) {
v.set(doc.phi(k, w) + Math.log(e.get()));
IntPairWritable kw = new IntPairWritable(k, w);
// ouput (topic, word)'s logProb contribution
context.write(kw, v);
logTotals[k] = LDAUtil.logSum(logTotals[k], v.get());
}
}
// Output the totals for the statistics. This is to make
// normalizing a lot easier.
for (int k = 0; k < state.getNumTopics(); ++k) {
IntPairWritable kw = new IntPairWritable(k, LDADriver.TOPIC_SUM_KEY);
v.set(logTotals[k]);
assert !Double.isNaN(v.get());
context.write(kw, v);
}
IntPairWritable llk = new IntPairWritable(LDADriver.LOG_LIKELIHOOD_KEY, LDADriver.LOG_LIKELIHOOD_KEY);
// Output log-likelihoods.
v.set(doc.getLogLikelihood());
context.write(llk, v);
}