Free cookie consent management tool by TermsFeed Policy Generator

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

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

minor speed tuning

File size: 3.3 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      double limit = GetVariableValue<DoubleData>("ClassSeparation", scope, false).Data;
49      if(estimated.Length != dataset.Rows) {
50        estimated = new double[dataset.Rows];
51        original = new double[dataset.Rows];
52      }
53      double positive = 0;
54      double negative = 0;
55      double targetMean = dataset.GetMean(targetVariable);
56      for(int sample = 0; sample < dataset.Rows; sample++) {
57        double est = functionTree.Evaluate(dataset, sample);
58        double orig = dataset.GetValue(sample, targetVariable);
59        if(double.IsNaN(est) || double.IsInfinity(est)) {
60          est = targetMean + maximumPunishment;
61        } else if(est > targetMean + maximumPunishment) {
62          est = targetMean + maximumPunishment;
63        } else if(est < targetMean - maximumPunishment) {
64          est = targetMean - maximumPunishment;
65        }
66        estimated[sample] = est;
67        original[sample] = orig;
68        if(orig >= limit) positive++;
69        else negative++;
70      }
71      Array.Sort(estimated, original);
72      double best_mcc = -1.0;
73      double tp = 0;
74      double fn = positive;
75      double tn = negative;
76      double fp = 0;
77      for(int i = original.Length-1; i >= 0 ; i--) {
78        if(original[i] >= limit) {
79          tp++; fn--;
80        } else {
81          tn--; fp++;
82        }
83        double mcc = (tp * tn - fp * fn) / Math.Sqrt(positive * (tp + fn) * (tn + fp) * negative);
84        if(best_mcc < mcc) {
85          best_mcc = mcc;
86        }
87      }
88      return best_mcc;
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.