Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

54
LINES

< > BotCompany Repo | #1030130 // findBestThreshold [working unoptimized backup]

JavaX fragment (include)

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  
}

Author comment

Began life as a copy of #1030114

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030130
Snippet name: findBestThreshold [working unoptimized backup]
Eternal ID of this version: #1030130/1
Text MD5: 4272190d84b982676a723c5b3510dc65
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-11-10 18:38:37
Source code size: 2349 bytes / 54 lines
Pitched / IR pitched: No / No
Views / Downloads: 155 / 181
Referenced in: [show references]