Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/ROCAnalyzer.cs @ 669

Last change on this file since 669 was 669, checked in by mkommend, 16 years ago

ROCAnalyser renamed to ROCAnalyzer

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.DataAnalysis;
29
30
31namespace HeuristicLab.GP.StructureIdentification.Classification {
32  public class ROCAnalyzer : OperatorBase {
33
34    public override string Description {
35      get { return @"Calculate TPR & FPR for various tresholds on dataset"; }
36    }
37
38    public ROCAnalyzer()
39      : base() {
40      AddVariableInfo(new VariableInfo("Values", "Item list holding the estimated and orignial values for the ROCAnalyzer", typeof(ItemList), VariableKind.In));
41      AddVariableInfo(new VariableInfo("ROCValues", "The values of the ROCAnalyzer, namely TPR & FPR", typeof(ItemList), VariableKind.New | VariableKind.Out));
42    }
43
44    public override IOperation Apply(IScope scope) {
45      ItemList values = GetVariableValue<ItemList>("Values", scope, true);
46      ItemList rocValues = GetVariableValue<ItemList>("ROCValues", scope, false, false);
47      if (rocValues == null) {
48        rocValues = new ItemList();
49        IVariableInfo info = GetVariableInfo("ROCValues");
50        if (info.Local)
51          AddVariable(new HeuristicLab.Core.Variable(info.ActualName, rocValues));
52        else
53          scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(info.FormalName), rocValues));
54      } else
55        rocValues.Clear();
56
57      //ROC Curve starts at 0,0
58      ItemList row = new ItemList();
59      row.Add(new DoubleData(0));
60      row.Add(new DoubleData(0));
61      rocValues.Add(row);
62
63      //calculate new ROC Values
64      double estimated=0.0;
65      double original=0.0;
66      double positiveClassKey;
67      double negativeClassKey;
68      double truePositiveRate=0.0;
69      double falsePositiveRate=0.0;
70
71      //initialize classes dictionary
72      Dictionary<double, List<double>> classes = new Dictionary<double, List<double>>();
73      foreach (ItemList value in values) {
74        estimated = ((DoubleData)value[0]).Data;
75        original = ((DoubleData)value[1]).Data;
76        if (!classes.ContainsKey(original))
77          classes[original] = new List<double>();
78        classes[original].Add(estimated);
79      }
80
81      //check for 2 classes classification problem
82      if (classes.Keys.Count != 2)
83        throw new Exception("ROCAnalyser only handles  2 class classification problems");
84
85      //sort estimated values in classes dictionary
86      foreach (List<double> estimatedValues in classes.Values)
87        estimatedValues.Sort();
88
89      //calculate truePosivite- & falsePositiveRate
90      positiveClassKey = classes.Keys.Min<double>();
91      negativeClassKey = classes.Keys.Max<double>();
92      for (int i = 0; i < classes[negativeClassKey].Count; i++) {
93        truePositiveRate = classes[positiveClassKey].Count<double>(value => value < classes[negativeClassKey][i]) / classes[positiveClassKey].Count;
94        falsePositiveRate = i / classes[negativeClassKey].Count;
95        row = new ItemList();
96        row.Add(new DoubleData(falsePositiveRate));
97        row.Add(new DoubleData(truePositiveRate));
98        rocValues.Add(row);
99
100        //stop calculation if truePositiveRate = 1; save runtime
101        if (truePositiveRate == 1)
102          break;
103      }
104
105      //add case when treshold == max negative class value => falsePositiveRate ==1
106      if (truePositiveRate != 1.0) {
107       
108        truePositiveRate = classes[positiveClassKey].Count<double>(value => value <= classes[negativeClassKey][classes[negativeClassKey].Count - 1]) / classes[positiveClassKey].Count;
109        falsePositiveRate = 1;
110        row = new ItemList();
111        row.Add(new DoubleData(falsePositiveRate));
112        row.Add(new DoubleData(truePositiveRate));
113        rocValues.Add(row);
114      } else {
115        //ROC ends at 1,1
116        row = new ItemList();
117        row.Add(new DoubleData(1));
118        row.Add(new DoubleData(1));
119        rocValues.Add(row);
120      }
121
122      return null;
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.