Libraryless. Click here for Pure Java version (2777L/17K).
1 | // find best threshold value to separate list1 (smaller values) from list2 (bigger values), minimizing false positives + false negatives |
2 | // returns (threshold, false positives, false negatives) |
3 | static T3<Double, Int, Int> findBestThreshold(Cl<Double> list1, Cl<Double> list2) { |
4 | if (empty(list1) || empty(list2)) null; |
5 | // get basic treshold & number of false positives/negatives |
6 | T3<Double, Int, Int> t = findThreshold_unmoved(list1, list2); |
7 | double threshold = t.a; |
8 | int falsePositives = t.b, falseNegatives = t.c; |
9 | |
10 | // get all values in a sorted list |
11 | L<Double> l = sorted(concatLists(list1, list2)); |
12 | int i = binarySearch_insertionPoint(l, threshold); |
13 | ifdef findBestThreshold_debug |
14 | printVars_str("Initial threshold.", +i, l := l(l), +threshold, +falsePositives, +falseNegatives); |
15 | endifdef |
16 | |
17 | while (falsePositives <= falseNegatives && i > 0) { |
18 | int i2 = i-1; |
19 | double threshold2 = avg(l.get(i2), l.get(i2+1)); |
20 | int falsePositives2 = countPred(list1, d -> d > threshold2); // this is unoptimized |
21 | int falseNegatives2 = countPred(list2, d -> d <= threshold2); |
22 | ifdef findBestThreshold_debug |
23 | printVars_str("Stepped down.", +i2, l := l(l), +threshold2, +falsePositives2, +falseNegatives2); |
24 | endifdef |
25 | |
26 | // is new result better than old result? |
27 | if (falsePositives2+falseNegatives2 <= falsePositives+falseNegatives) { // not getting worse |
28 | i = i2; |
29 | threshold = threshold2; |
30 | falsePositives = falsePositives2; |
31 | falseNegatives = falseNegatives2; |
32 | } else break; |
33 | } |
34 | |
35 | while (falseNegatives <= falsePositives && i+2 < l(l)) { |
36 | int i2 = i+1; |
37 | double threshold2 = avg(l.get(i2), l.get(i2+1)); |
38 | int falsePositives2 = countPred(list1, d -> d > threshold2); |
39 | int falseNegatives2 = countPred(list2, d -> d <= threshold2); |
40 | ifdef findBestThreshold_debug |
41 | printVars_str("Stepped up.", +i2, l := l(l), +threshold2, +falsePositives2, +falseNegatives2); |
42 | endifdef |
43 | |
44 | // is new result better than old result? |
45 | if (falsePositives2+falseNegatives2 <= falsePositives+falseNegatives) { // not getting worse |
46 | i = i2; |
47 | threshold = threshold2; |
48 | falsePositives = falsePositives2; |
49 | falseNegatives = falseNegatives2; |
50 | } else break; |
51 | } |
52 | |
53 | ret t3(threshold, falsePositives, falseNegatives); |
54 | } |
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: | 262 / 374 |
Version history: | 23 change(s) |
Referenced in: | [show references] |