int instanceIndex = 0;
for (Instance instance: trainingList) {
FeatureVector multinomialValues = (FeatureVector) instance.getTarget();
if (multinomialValues == null) { continue; }
// Get the predicted probability of each class
// under the current model parameters
this.classifier.getUnnormalizedClassificationScores(instance, scores);
double sumScores = 0.0;
// Exponentiate the scores
for (int i=0; i<scores.length; i++) {
// Due to underflow, it's very likely that some of these scores will be 0.0.
scores[i] = Math.exp(scores[i]);
sumScores += scores[i];
}
FeatureVector features = (FeatureVector) instance.getData();
double totalLength = 0;
for (double count : multinomialValues.getValues()) {
totalLength += count;
}
double digammaDifferenceForSums =
Dirichlet.digamma(sumScores + totalLength) -
Dirichlet.digamma(sumScores);
for (int loc = 0; loc < features.numLocations(); loc++) {
int index = features.indexAtLocation(loc);
double value = features.valueAtLocation(loc);
if (value == 0.0) { continue; }
// In a FeatureVector, there's no easy way to say "do you know
// about this id?" so I've broken this into two for loops,