Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.StructureIdentification/Evaluation/MCCEvaluator.cs @ 3494

Last change on this file since 3494 was 482, checked in by gkronber, 16 years ago

made a few more improvements in the GP evaluators (ticket #242 All GP evaluators should support the 'UseEstimatedTargetValues' switch for autoregressive modelling)

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