[669] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.GP.StructureIdentification.Classification {
|
---|
| 29 | public class ROCAnalyzer : OperatorBase {
|
---|
[678] | 30 | private ItemList myRocValues;
|
---|
| 31 | private ItemList<DoubleData> myAucValues;
|
---|
[669] | 32 |
|
---|
[678] | 33 |
|
---|
[669] | 34 | public override string Description {
|
---|
[696] | 35 | get { return @"Calculate TPR & FPR for various thresholds on dataset"; }
|
---|
[669] | 36 | }
|
---|
| 37 |
|
---|
| 38 | public ROCAnalyzer()
|
---|
| 39 | : base() {
|
---|
[696] | 40 | AddVariableInfo(new VariableInfo("Values", "Item list holding the estimated and original values for the ROCAnalyzer", typeof(ItemList), VariableKind.In));
|
---|
[678] | 41 | AddVariableInfo(new VariableInfo("ROCValues", "The values of the ROCAnalyzer, namely TPR & FPR", typeof(ItemList), VariableKind.New | VariableKind.Out));
|
---|
| 42 | AddVariableInfo(new VariableInfo("AUCValues", "The AUC Values for each ROC", typeof(ItemList<DoubleData>), VariableKind.New | VariableKind.Out));
|
---|
[669] | 43 | }
|
---|
| 44 |
|
---|
| 45 | public override IOperation Apply(IScope scope) {
|
---|
[678] | 46 | #region initialize HL-variables
|
---|
[669] | 47 | ItemList values = GetVariableValue<ItemList>("Values", scope, true);
|
---|
[678] | 48 | myRocValues = GetVariableValue<ItemList>("ROCValues", scope, false, false);
|
---|
| 49 | if (myRocValues == null) {
|
---|
| 50 | myRocValues = new ItemList();
|
---|
[669] | 51 | IVariableInfo info = GetVariableInfo("ROCValues");
|
---|
| 52 | if (info.Local)
|
---|
[678] | 53 | AddVariable(new HeuristicLab.Core.Variable(info.ActualName, myRocValues));
|
---|
[669] | 54 | else
|
---|
[678] | 55 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(info.FormalName), myRocValues));
|
---|
[672] | 56 | } else {
|
---|
[678] | 57 | myRocValues.Clear();
|
---|
[672] | 58 | }
|
---|
[669] | 59 |
|
---|
[678] | 60 | myAucValues = GetVariableValue<ItemList<DoubleData>>("AUCValues", scope, false, false);
|
---|
| 61 | if (myAucValues == null) {
|
---|
| 62 | myAucValues = new ItemList<DoubleData>();
|
---|
| 63 | IVariableInfo info = GetVariableInfo("AUCValues");
|
---|
| 64 | if (info.Local)
|
---|
| 65 | AddVariable(new HeuristicLab.Core.Variable(info.ActualName, myAucValues));
|
---|
| 66 | else
|
---|
| 67 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(info.FormalName), myAucValues));
|
---|
| 68 | } else {
|
---|
| 69 | myAucValues.Clear();
|
---|
| 70 | }
|
---|
| 71 | #endregion
|
---|
[669] | 72 |
|
---|
| 73 | //calculate new ROC Values
|
---|
[672] | 74 | double estimated = 0.0;
|
---|
| 75 | double original = 0.0;
|
---|
[669] | 76 |
|
---|
| 77 | //initialize classes dictionary
|
---|
[678] | 78 | SortedDictionary<double, List<double>> classes = new SortedDictionary<double, List<double>>();
|
---|
[669] | 79 | foreach (ItemList value in values) {
|
---|
| 80 | estimated = ((DoubleData)value[0]).Data;
|
---|
| 81 | original = ((DoubleData)value[1]).Data;
|
---|
| 82 | if (!classes.ContainsKey(original))
|
---|
| 83 | classes[original] = new List<double>();
|
---|
| 84 | classes[original].Add(estimated);
|
---|
| 85 | }
|
---|
[678] | 86 | foreach (double key in classes.Keys)
|
---|
| 87 | classes[key].Sort();
|
---|
[2222] | 88 |
|
---|
[678] | 89 | //calculate ROC Curve
|
---|
| 90 | foreach (double key in classes.Keys) {
|
---|
| 91 | CalculateBestROC(key, classes);
|
---|
| 92 | }
|
---|
[669] | 93 |
|
---|
[678] | 94 | return null;
|
---|
| 95 | }
|
---|
[669] | 96 |
|
---|
[678] | 97 | protected void CalculateBestROC(double positiveClassKey, SortedDictionary<double, List<double>> classes) {
|
---|
| 98 | List<KeyValuePair<double, double>> rocCharacteristics;
|
---|
| 99 | List<KeyValuePair<double, double>> bestROC;
|
---|
| 100 | List<KeyValuePair<double, double>> actROC;
|
---|
| 101 |
|
---|
| 102 | List<double> negatives = new List<double>();
|
---|
| 103 | foreach (double key in classes.Keys) {
|
---|
| 104 | if (key != positiveClassKey)
|
---|
| 105 | negatives.AddRange(classes[key]);
|
---|
| 106 | }
|
---|
| 107 | List<double> actNegatives = negatives.Where<double>(value => value < classes[positiveClassKey].Max<double>()).ToList<double>();
|
---|
| 108 | actNegatives.Add(classes[positiveClassKey].Max<double>());
|
---|
| 109 | actNegatives.Sort();
|
---|
| 110 | actNegatives = actNegatives.Reverse<double>().ToList<double>();
|
---|
| 111 |
|
---|
| 112 | double bestAUC = double.MinValue;
|
---|
| 113 | double actAUC = 0;
|
---|
| 114 | //first class
|
---|
| 115 | if (classes.Keys.ElementAt<double>(0) == positiveClassKey) {
|
---|
| 116 | rocCharacteristics = null;
|
---|
| 117 | CalculateROCValuesAndAUC(classes[positiveClassKey], actNegatives, negatives.Count, double.MinValue, ref rocCharacteristics, out actROC, out actAUC);
|
---|
| 118 | myAucValues.Add(new DoubleData(actAUC));
|
---|
| 119 | myRocValues.Add(Convert(actROC));
|
---|
| 120 | }
|
---|
| 121 | //middle classes
|
---|
| 122 | else if (classes.Keys.ElementAt<double>(classes.Keys.Count - 1) != positiveClassKey) {
|
---|
| 123 | rocCharacteristics = null;
|
---|
| 124 | bestROC = new List<KeyValuePair<double, double>>();
|
---|
[696] | 125 | foreach (double minThreshold in classes[positiveClassKey].Distinct<double>()) {
|
---|
| 126 | CalculateROCValuesAndAUC(classes[positiveClassKey], actNegatives, negatives.Count, minThreshold, ref rocCharacteristics, out actROC, out actAUC);
|
---|
[678] | 127 | if (actAUC > bestAUC) {
|
---|
| 128 | bestAUC = actAUC;
|
---|
| 129 | bestROC = actROC;
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
[2222] | 132 | myAucValues.Add(new DoubleData(bestAUC));
|
---|
| 133 | myRocValues.Add(Convert(bestROC));
|
---|
| 134 |
|
---|
[678] | 135 | } else { //last class
|
---|
| 136 | actNegatives = negatives.Where<double>(value => value > classes[positiveClassKey].Min<double>()).ToList<double>();
|
---|
| 137 | actNegatives.Add(classes[positiveClassKey].Min<double>());
|
---|
| 138 | actNegatives.Sort();
|
---|
| 139 | CalculateROCValuesAndAUCForLastClass(classes[positiveClassKey], actNegatives, negatives.Count, out bestROC, out bestAUC);
|
---|
| 140 | myAucValues.Add(new DoubleData(bestAUC));
|
---|
| 141 | myRocValues.Add(Convert(bestROC));
|
---|
| 142 |
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[696] | 147 | protected void CalculateROCValuesAndAUC(List<double> positives, List<double> negatives, int negativesCount, double minThreshold,
|
---|
[678] | 148 | ref List<KeyValuePair<double, double>> rocCharacteristics, out List<KeyValuePair<double, double>> roc, out double auc) {
|
---|
| 149 | double actTP = -1;
|
---|
| 150 | double actFP = -1;
|
---|
| 151 | double oldTP = -1;
|
---|
| 152 | double oldFP = -1;
|
---|
| 153 | auc = 0;
|
---|
| 154 | roc = new List<KeyValuePair<double, double>>();
|
---|
| 155 |
|
---|
[696] | 156 | actTP = positives.Count<double>(value => minThreshold <= value && value <= negatives.Max<double>());
|
---|
[2222] | 157 | actFP = negatives.Count<double>(value => minThreshold <= value);
|
---|
[678] | 158 | //add point (1,TPR) for AUC 'correct' calculation
|
---|
| 159 | roc.Add(new KeyValuePair<double, double>(1, actTP / positives.Count));
|
---|
| 160 | oldTP = actTP;
|
---|
| 161 | oldFP = negativesCount;
|
---|
| 162 | roc.Add(new KeyValuePair<double, double>(actFP / negativesCount, actTP / positives.Count));
|
---|
| 163 |
|
---|
| 164 | if (rocCharacteristics == null) {
|
---|
| 165 | rocCharacteristics = new List<KeyValuePair<double, double>>();
|
---|
[696] | 166 | foreach (double maxThreshold in negatives.Distinct<double>()) {
|
---|
[678] | 167 | auc += ((oldTP + actTP) / positives.Count) * ((oldFP - actFP) / negativesCount) / 2;
|
---|
| 168 | oldTP = actTP;
|
---|
| 169 | oldFP = actFP;
|
---|
[696] | 170 | actTP = positives.Count<double>(value => minThreshold <= value && value < maxThreshold);
|
---|
| 171 | actFP = negatives.Count<double>(value => minThreshold <= value && value < maxThreshold);
|
---|
[678] | 172 | rocCharacteristics.Add(new KeyValuePair<double, double>(oldTP - actTP, oldFP - actFP));
|
---|
| 173 | roc.Add(new KeyValuePair<double, double>(actFP / negativesCount, actTP / positives.Count));
|
---|
| 174 |
|
---|
| 175 | //stop calculation if truePositiveRate == 0 => straight line with y=0 & save runtime
|
---|
[696] | 176 | if ((actTP == 0) || (actFP == 0))
|
---|
[678] | 177 | break;
|
---|
| 178 | }
|
---|
| 179 | auc += ((oldTP + actTP) / positives.Count) * ((oldFP - actFP) / negativesCount) / 2;
|
---|
| 180 | } else { //characteristics of ROCs calculated
|
---|
| 181 | foreach (KeyValuePair<double, double> rocCharac in rocCharacteristics) {
|
---|
| 182 | auc += ((oldTP + actTP) / positives.Count) * ((oldFP - actFP) / negativesCount) / 2;
|
---|
| 183 | oldTP = actTP;
|
---|
| 184 | oldFP = actFP;
|
---|
| 185 | actTP = oldTP - rocCharac.Key;
|
---|
| 186 | actFP = oldFP - rocCharac.Value;
|
---|
| 187 | roc.Add(new KeyValuePair<double, double>(actFP / negativesCount, actTP / positives.Count));
|
---|
[696] | 188 | if ((actTP == 0) || (actFP == 0))
|
---|
[678] | 189 | break;
|
---|
| 190 | }
|
---|
| 191 | auc += ((oldTP + actTP) / positives.Count) * ((oldFP - actFP) / negativesCount) / 2;
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | protected void CalculateROCValuesAndAUCForLastClass(List<double> positives, List<double> negatives, int negativesCount,
|
---|
| 196 | out List<KeyValuePair<double, double>> roc, out double auc) {
|
---|
| 197 | double actTP = -1;
|
---|
| 198 | double actFP = -1;
|
---|
| 199 | double oldTP = -1;
|
---|
| 200 | double oldFP = -1;
|
---|
| 201 | auc = 0;
|
---|
| 202 | roc = new List<KeyValuePair<double, double>>();
|
---|
| 203 |
|
---|
| 204 | actTP = positives.Count<double>(value => value >= negatives.Min<double>());
|
---|
| 205 | actFP = negatives.Count<double>(value => value >= negatives.Min<double>());
|
---|
| 206 | //add point (1,TPR) for AUC 'correct' calculation
|
---|
| 207 | roc.Add(new KeyValuePair<double, double>(1, actTP / positives.Count));
|
---|
| 208 | oldTP = actTP;
|
---|
| 209 | oldFP = negativesCount;
|
---|
| 210 | roc.Add(new KeyValuePair<double, double>(actFP / negativesCount, actTP / positives.Count));
|
---|
| 211 |
|
---|
[696] | 212 | foreach (double minThreshold in negatives.Distinct<double>()) {
|
---|
[678] | 213 | auc += ((oldTP + actTP) / positives.Count) * ((oldFP - actFP) / negativesCount) / 2;
|
---|
| 214 | oldTP = actTP;
|
---|
| 215 | oldFP = actFP;
|
---|
[696] | 216 | actTP = positives.Count<double>(value => minThreshold < value);
|
---|
| 217 | actFP = negatives.Count<double>(value => minThreshold < value);
|
---|
[678] | 218 | roc.Add(new KeyValuePair<double, double>(actFP / negativesCount, actTP / positives.Count));
|
---|
| 219 |
|
---|
| 220 | //stop calculation if truePositiveRate == 0 => straight line with y=0 & save runtime
|
---|
[2222] | 221 | if (actTP == 0 || actFP == 0)
|
---|
[669] | 222 | break;
|
---|
| 223 | }
|
---|
[678] | 224 | auc += ((oldTP + actTP) / positives.Count) * ((oldFP - actFP) / negativesCount) / 2;
|
---|
[669] | 225 |
|
---|
[678] | 226 | }
|
---|
| 227 |
|
---|
| 228 | private ItemList Convert(List<KeyValuePair<double, double>> data) {
|
---|
| 229 | ItemList list = new ItemList();
|
---|
| 230 | ItemList row;
|
---|
| 231 | foreach (KeyValuePair<double, double> dataPoint in data) {
|
---|
| 232 | row = new ItemList();
|
---|
| 233 | row.Add(new DoubleData(dataPoint.Key));
|
---|
| 234 | row.Add(new DoubleData(dataPoint.Value));
|
---|
| 235 | list.Add(row);
|
---|
[669] | 236 | }
|
---|
[678] | 237 | return list;
|
---|
| 238 | }
|
---|
[669] | 239 |
|
---|
| 240 | }
|
---|
[678] | 241 |
|
---|
[669] | 242 | }
|
---|