Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/MCCEvaluator.cs @ 668

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

namespaces changed to HeuristicLab.GP.StructureIdentification.Classification
(ticket #177)

File size: 3.5 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.GP.StructureIdentification;
29
30namespace HeuristicLab.GP.StructureIdentification.Classification {
31  public class MCCEvaluator : GPEvaluatorBase {
32    private double limit;
33    private double[] original = new double[1];
34    private double[] estimated = new double[1];
35    private DoubleData mcc;
36    public override string Description {
37      get {
38        return @"Calculates the matthews correlation coefficient for a given model and class separation threshold";
39      }
40    }
41    public MCCEvaluator()
42      : base() {
43      AddVariableInfo(new VariableInfo("ClassSeparation", "The value of separation between negative and positive target classification values (for instance 0.5 if negative=0 and positive=1).", typeof(DoubleData), VariableKind.In));
44      AddVariableInfo(new VariableInfo("MCC", "The matthews correlation coefficient of the model", typeof(DoubleData), VariableKind.New));
45    }
46
47    public override IOperation Apply(IScope scope) {
48      mcc = GetVariableValue<DoubleData>("MCC", scope, false, false);
49      if(mcc == null) {
50        mcc = new DoubleData();
51        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MCC"), mcc));
52      }
53      limit = GetVariableValue<DoubleData>("ClassSeparation", scope, true).Data;
54      return base.Apply(scope);
55    }
56
57    public override void Evaluate(int start, int end) {
58      int nSamples = end - start;
59      if(estimated.Length != nSamples) {
60        estimated = new double[nSamples];
61        original = new double[nSamples];
62      }
63
64      double positive = 0;
65      double negative = 0;
66      for(int sample = start; sample < end; sample++) {
67        double est = GetEstimatedValue(sample);
68        double orig = GetOriginalValue(sample);
69        SetOriginalValue(sample, est);
70        estimated[sample - start] = est;
71        original[sample - start] = orig;
72        if(orig >= limit) positive++;
73        else negative++;
74      }
75      Array.Sort(estimated, original);
76      double best_mcc = -1.0;
77      double tp = 0;
78      double fn = positive;
79      double tn = negative;
80      double fp = 0;
81      for(int i = original.Length - 1; i >= 0; i--) {
82        if(original[i] >= limit) {
83          tp++; fn--;
84        } else {
85          tn--; fp++;
86        }
87        double mcc = (tp * tn - fp * fn) / Math.Sqrt(positive * (tp + fn) * (tn + fp) * negative);
88        if(best_mcc < mcc) {
89          best_mcc = mcc;
90        }
91      }
92      this.mcc.Data = best_mcc;
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.