Libraryless. Click here for Pure Java version (2777L/17K).
// 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<Double, Int, Int> findBestThreshold(Cl<Double> list1, Cl<Double> list2) { if (empty(list1) || empty(list2)) null; // get basic treshold & number of false positives/negatives T3<Double, Int, Int> t = findThreshold_unmoved(list1, list2); double threshold = t.a; int falsePositives = t.b, falseNegatives = t.c; // get all values in a sorted list L<Double> 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); }
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt
No comments. add comment
Snippet ID: | #1030114 |
Snippet name: | findBestThreshold [OK, could be optimized a bit more] |
Eternal ID of this version: | #1030114/24 |
Text MD5: | 4272190d84b982676a723c5b3510dc65 |
Transpilation MD5: | 927dbf964c9285876d25a252be82674c |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-11-10 09:38:30 |
Source code size: | 2349 bytes / 54 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 260 / 371 |
Version history: | 23 change(s) |
Referenced in: | [show references] |