float maxAnotherScore = 0.f;
float maxAnotherVariance = 0.f;
if(nonZeroVariance && label2model.isEmpty()) {// for initial call
float var = 2.f * calcVariance(features);
return new Margin(correctScore, maxAnotherLabel, maxAnotherScore).variance(var);
}
for(Map.Entry<Object, PredictionModel> label2map : label2model.entrySet()) {// for each class
Object label = label2map.getKey();
PredictionModel model = label2map.getValue();
PredictionResult predicted = calcScoreAndVariance(model, features);
float score = predicted.getScore();
if(label.equals(actual_label)) {
correctScore = score;
correctVariance = predicted.getVariance();
} else {
if(maxAnotherLabel == null || score > maxAnotherScore) {
maxAnotherLabel = label;
maxAnotherScore = score;
maxAnotherVariance = predicted.getVariance();
}
}
}
float var = correctVariance + maxAnotherVariance;
return new Margin(correctScore, maxAnotherLabel, maxAnotherScore).variance(var);
}