// find best threshold value to separate list1 (smaller values) from list2 (bigger values), minimizing false positives + false negatives // returns (threshold, false positives, false negatives) static T3 findBestThreshold(Cl list1, Cl list2) { if (empty(list1) || empty(list2)) null; // get basic treshold & number of false positives/negatives T3 t = findThreshold_unmoved(list1, list2); double threshold = t.a; int falsePositives = t.b, falseNegatives = t.c; // get all values in a sorted list L l = sorted(concatLists(list1, list2)); int i = binarySearch_insertionPoint(l, threshold); ifdef findBestThreshold_debug printVars_str("Initial threshold.", +i, l := l(l), +threshold, +falsePositives, +falseNegatives); endifdef while (falsePositives <= falseNegatives && i > 0) { int i2 = i-1; double threshold2 = avg(l.get(i2), l.get(i2+1)); int falsePositives2 = countPred(list1, d -> d > threshold2); // this is unoptimized int falseNegatives2 = countPred(list2, d -> d <= threshold2); ifdef findBestThreshold_debug printVars_str("Stepped down.", +i2, l := l(l), +threshold2, +falsePositives2, +falseNegatives2); endifdef // is new result better than old result? if (falsePositives2+falseNegatives2 <= falsePositives+falseNegatives) { // not getting worse i = i2; threshold = threshold2; falsePositives = falsePositives2; falseNegatives = falseNegatives2; } else break; } while (falseNegatives <= falsePositives && i+2 < l(l)) { int i2 = i+1; double threshold2 = avg(l.get(i2), l.get(i2+1)); int falsePositives2 = countPred(list1, d -> d > threshold2); int falseNegatives2 = countPred(list2, d -> d <= threshold2); ifdef findBestThreshold_debug printVars_str("Stepped up.", +i2, l := l(l), +threshold2, +falsePositives2, +falseNegatives2); endifdef // is new result better than old result? if (falsePositives2+falseNegatives2 <= falsePositives+falseNegatives) { // not getting worse i = i2; threshold = threshold2; falsePositives = falsePositives2; falseNegatives = falseNegatives2; } else break; } ret t3(threshold, falsePositives, falseNegatives); }