final Map.Entry<Serializable, Double> minorityEntry = MapUtils.getEntryWithLowestValue(classificationProportions).get();
Serializable majorityClassification = majorityEntry.getKey();
final double majorityProportion = majorityEntry.getValue();
final double naturalMinorityProportion = 1.0 - majorityProportion;
if (naturalMinorityProportion >= targetMinorityProportion) {
final Classifier wrappedPredictiveModel = predictiveModelBuilder.buildPredictiveModel(trainingData);
return new DownsamplingClassifier(wrappedPredictiveModel, majorityClassification, minorityEntry.getKey(), 0);
}
final double dropProbability = (naturalMinorityProportion > targetMinorityProportion)? 0 : 1.0 - ((naturalMinorityProportion - targetMinorityProportion*naturalMinorityProportion) / (targetMinorityProportion - targetMinorityProportion *naturalMinorityProportion));
Iterable<? extends Instance<AttributesMap>> downsampledTrainingData = Iterables.filter(trainingData, new RandomDroppingInstanceFilter(majorityClassification, dropProbability));
final Classifier wrappedPredictiveModel = predictiveModelBuilder.buildPredictiveModel(downsampledTrainingData);
return new DownsamplingClassifier(wrappedPredictiveModel, majorityClassification, minorityEntry.getKey(), dropProbability);
}