Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MCCEvaluator.cs @ 381

Last change on this file since 381 was 367, checked in by gkronber, 16 years ago

fixed small bugs in GP evaluators

File size: 3.6 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    public override string Description {
35      get {
36        return @"TASK";
37      }
38    }
39
40    public MCCEvaluator()
41      : base() {
42      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));
43    }
44
45    private double[] original = new double[1];
46    private double[] estimated = new double[1];
47    public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {
48      int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
49      int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
50      int nSamples = trainingEnd-trainingStart;
51      double limit = GetVariableValue<DoubleData>("ClassSeparation", scope, false).Data;
52      if(estimated.Length != nSamples) {
53        estimated = new double[nSamples];
54        original = new double[nSamples];
55      }
56
57      double positive = 0;
58      double negative = 0;
59      double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
60      functionTree.PrepareEvaluation(dataset);
61      for(int sample = trainingStart; sample < trainingEnd; sample++) {
62        double est = functionTree.Evaluate(sample);
63        double orig = dataset.GetValue(sample, targetVariable);
64        if(double.IsNaN(est) || double.IsInfinity(est)) {
65          est = targetMean + maximumPunishment;
66        } else if(est > targetMean + maximumPunishment) {
67          est = targetMean + maximumPunishment;
68        } else if(est < targetMean - maximumPunishment) {
69          est = targetMean - maximumPunishment;
70        }
71        estimated[sample-trainingStart] = est;
72        original[sample-trainingStart] = orig;
73        if(orig >= limit) positive++;
74        else negative++;
75      }
76      Array.Sort(estimated, original);
77      double best_mcc = -1.0;
78      double tp = 0;
79      double fn = positive;
80      double tn = negative;
81      double fp = 0;
82      for(int i = original.Length-1; i >= 0 ; i--) {
83        if(original[i] >= limit) {
84          tp++; fn--;
85        } else {
86          tn--; fp++;
87        }
88        double mcc = (tp * tn - fp * fn) / Math.Sqrt(positive * (tp + fn) * (tn + fp) * negative);
89        if(best_mcc < mcc) {
90          best_mcc = mcc;
91        }
92      }
93      return best_mcc;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.