Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.StructureIdentification/Evaluation/ClassificationMeanSquaredErrorEvaluator.cs @ 6593

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

improved the condition for the special case by removing the predicate that estimated is smaller then original or larger than original because this is always true when the error is <-1 or +1 resp.

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 ClassificationMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator {
34    private const double EPSILON = 1.0E-6;
35    private double[] classesArr;
36    public override string Description {
37      get {
38        return @"Evaluates 'FunctionTree' for all samples of 'DataSet' and calculates the mean-squared-error
39for the estimated values vs. the real values of 'TargetVariable'.";
40      }
41    }
42
43    public ClassificationMeanSquaredErrorEvaluator()
44      : base() {
45      AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));
46    }
47
48    public override IOperation Apply(IScope scope) {
49      ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
50      classesArr = new double[classes.Count];
51      for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;
52      Array.Sort(classesArr);
53      return base.Apply(scope);
54    }
55
56    public override void Evaluate(int start, int end) {
57      double errorsSquaredSum = 0;
58      for(int sample = start; sample < end; sample++) {
59        double estimated = GetEstimatedValue(sample);
60        double original = GetOriginalValue(sample);
61        SetOriginalValue(sample, estimated);
62        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
63          double error = estimated - original;
64          // between classes use squared error
65          // on the lower end and upper end only add linear error if the absolute error is larger than 1
66          // the error>1.0 constraint is needed for balance because in the interval ]-1, 1[ the squared error is smaller than the absolute error
67          if((IsEqual(original, classesArr[0]) && error < -1.0) ||
68            (IsEqual(original, classesArr[classesArr.Length - 1]) && error > 1.0)) {
69            errorsSquaredSum += Math.Abs(error); // only add linear error below the smallest class or above the largest class
70          } else {
71            errorsSquaredSum += error * error;
72          }
73        }
74      }
75
76      errorsSquaredSum /= (end - start);
77      if(double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) {
78        errorsSquaredSum = double.MaxValue;
79      }
80      mse.Data = errorsSquaredSum;
81    }
82
83    private bool IsEqual(double x, double y) {
84      return Math.Abs(x - y) < EPSILON;
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.