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)

// 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);
}

Author comment

Began life as a copy of #1030114

download  show line numbers  debug dex   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

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: 22 / 27
Referenced in: [show references]

Formerly at http://tinybrain.de/1030130 & http://1030130.tinybrain.de