Free cookie consent management tool by TermsFeed Policy Generator

Changeset 696


Ignore:
Timestamp:
10/20/08 09:47:32 (16 years ago)
Author:
mkommend
Message:

source improved through codereview with GK (ticket #308)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/ROCAnalyzer.cs

    r678 r696  
    3636
    3737    public override string Description {
    38       get { return @"Calculate TPR & FPR for various tresholds on dataset"; }
     38      get { return @"Calculate TPR & FPR for various thresholds on dataset"; }
    3939    }
    4040
    4141    public ROCAnalyzer()
    4242      : base() {
    43       AddVariableInfo(new VariableInfo("Values", "Item list holding the estimated and orignial values for the ROCAnalyzer", typeof(ItemList), VariableKind.In));
     43      AddVariableInfo(new VariableInfo("Values", "Item list holding the estimated and original values for the ROCAnalyzer", typeof(ItemList), VariableKind.In));
    4444      AddVariableInfo(new VariableInfo("ROCValues", "The values of the ROCAnalyzer, namely TPR & FPR", typeof(ItemList), VariableKind.New | VariableKind.Out));
    4545      AddVariableInfo(new VariableInfo("AUCValues", "The AUC Values for each ROC", typeof(ItemList<DoubleData>), VariableKind.New | VariableKind.Out));
     
    8989      foreach (double key in classes.Keys)
    9090        classes[key].Sort();
    91 
    92       //check for 2 classes classification problem
    93       //if (classes.Keys.Count != 2)
    94       //  throw new Exception("ROCAnalyser only handles  2 class classification problems");
    95 
     91 
    9692      //calculate ROC Curve
    9793      foreach (double key in classes.Keys) {
     
    10399
    104100    protected void CalculateBestROC(double positiveClassKey, SortedDictionary<double, List<double>> classes) {
    105 
    106       int rocIndex = myRocValues.Count - 1;
    107101      List<KeyValuePair<double, double>> rocCharacteristics;
    108102      List<KeyValuePair<double, double>> bestROC;
     
    132126        rocCharacteristics = null;
    133127        bestROC = new List<KeyValuePair<double, double>>();
    134         foreach (double minTreshold in classes[positiveClassKey].Distinct<double>()) {
    135           CalculateROCValuesAndAUC(classes[positiveClassKey], actNegatives, negatives.Count, minTreshold, ref rocCharacteristics, out  actROC, out actAUC);
     128        foreach (double minThreshold in classes[positiveClassKey].Distinct<double>()) {
     129          CalculateROCValuesAndAUC(classes[positiveClassKey], actNegatives, negatives.Count, minThreshold, ref rocCharacteristics, out  actROC, out actAUC);
    136130          if (actAUC > bestAUC) {
    137131            bestAUC = actAUC;
     
    154148    }
    155149
    156     protected void CalculateROCValuesAndAUC(List<double> positives, List<double> negatives, int negativesCount, double minTreshold,
     150    protected void CalculateROCValuesAndAUC(List<double> positives, List<double> negatives, int negativesCount, double minThreshold,
    157151      ref List<KeyValuePair<double, double>> rocCharacteristics, out List<KeyValuePair<double, double>> roc, out double auc) {
    158152      double actTP = -1;
     
    163157      roc = new List<KeyValuePair<double, double>>();
    164158
    165       actTP = positives.Count<double>(value => minTreshold <= value && value <= negatives.Max<double>());
    166       actFP = negatives.Count<double>(value => minTreshold <= value && value <= negatives.Max<double>());
     159      actTP = positives.Count<double>(value => minThreshold <= value && value <= negatives.Max<double>());
     160      actFP = negatives.Count<double>(value => minThreshold <= value );
    167161      //add point (1,TPR) for AUC 'correct' calculation
    168162      roc.Add(new KeyValuePair<double, double>(1, actTP / positives.Count));
     
    173167      if (rocCharacteristics == null) {
    174168        rocCharacteristics = new List<KeyValuePair<double, double>>();
    175         foreach (double maxTreshold in negatives.Distinct<double>()) {
     169        foreach (double maxThreshold in negatives.Distinct<double>()) {
    176170          auc += ((oldTP + actTP) / positives.Count) * ((oldFP - actFP) / negativesCount) / 2;
    177171          oldTP = actTP;
    178172          oldFP = actFP;
    179           actTP = positives.Count<double>(value => minTreshold <= value && value < maxTreshold);
    180           actFP = negatives.Count<double>(value => minTreshold <= value && value < maxTreshold);
     173          actTP = positives.Count<double>(value => minThreshold <= value && value < maxThreshold);
     174          actFP = negatives.Count<double>(value => minThreshold <= value && value < maxThreshold);
    181175          rocCharacteristics.Add(new KeyValuePair<double, double>(oldTP - actTP, oldFP - actFP));
    182176          roc.Add(new KeyValuePair<double, double>(actFP / negativesCount, actTP / positives.Count));
    183177
    184178          //stop calculation if truePositiveRate == 0 => straight line with y=0 & save runtime
    185           if ((actTP / positives.Count == 0) || (actFP / negatives.Count == 0))
     179          if ((actTP == 0) || (actFP == 0))
    186180            break;
    187181        }
     
    195189          actFP = oldFP - rocCharac.Value;
    196190          roc.Add(new KeyValuePair<double, double>(actFP / negativesCount, actTP / positives.Count));
    197           if (actTP / positives.Count == 0)
     191          if ((actTP == 0) || (actFP == 0))
    198192            break;
    199193        }
     
    219213      roc.Add(new KeyValuePair<double, double>(actFP / negativesCount, actTP / positives.Count));
    220214
    221       foreach (double minTreshold in negatives.Distinct<double>()) {
     215      foreach (double minThreshold in negatives.Distinct<double>()) {
    222216        auc += ((oldTP + actTP) / positives.Count) * ((oldFP - actFP) / negativesCount) / 2;
    223217        oldTP = actTP;
    224218        oldFP = actFP;
    225         actTP = positives.Count<double>(value => minTreshold <= value);
    226         actFP = negatives.Count<double>(value => minTreshold <= value);
     219        actTP = positives.Count<double>(value => minThreshold < value);
     220        actFP = negatives.Count<double>(value => minThreshold < value);
    227221        roc.Add(new KeyValuePair<double, double>(actFP / negativesCount, actTP / positives.Count));
    228222
    229223        //stop calculation if truePositiveRate == 0 => straight line with y=0 & save runtime
    230         if ((actTP / positives.Count == 0) || (actFP / negatives.Count == 0))
     224        if (actTP == 0 || actFP==0)
    231225          break;
    232226      }
Note: See TracChangeset for help on using the changeset viewer.