Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/UncertainMeanSquaredErrorEvaluator.cs @ 828

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

implemented crude version of an MSE evaluator that also calculates confidence bounds and stops evaluation early if it seems that the MSE is significantly better or worse than the quality-limit (based on parent quality). #366

File size: 5.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.DataAnalysis;
30using HeuristicLab.Random;
31
32namespace HeuristicLab.GP.StructureIdentification {
33  public class UncertainMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator {
34    public override string Description {
35      get {
36        return @"Evaluates 'FunctionTree' for all samples of the dataset and calculates the mean-squared-error
37for the estimated values vs. the real values of 'TargetVariable'.
38This operator stops the computation as soon as an upper limit for the mean-squared-error is reached.";
39      }
40    }
41
42    public UncertainMeanSquaredErrorEvaluator()
43      : base() {
44      AddVariableInfo(new VariableInfo("Random", "", typeof(MersenneTwister), VariableKind.In));
45      AddVariableInfo(new VariableInfo("MinEvaluatedSamples", "", typeof(IntData), VariableKind.In));
46      AddVariableInfo(new VariableInfo("QualityLimit", "The upper limit of the MSE which is used as early stopping criterion.", typeof(DoubleData), VariableKind.In));
47      AddVariableInfo(new VariableInfo("ConfidenceBounds", "Confidence bounds of the calculated MSE", typeof(DoubleData), VariableKind.New | VariableKind.Out));
48      AddVariableInfo(new VariableInfo("ActuallyEvaluatedSamples", "", typeof(IntData), VariableKind.New | VariableKind.Out));
49    }
50
51    // evaluates the function-tree for the given target-variable and the whole dataset and returns the MSE
52    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
53      double qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, false).Data;
54      int minSamples = GetVariableValue<IntData>("MinEvaluatedSamples", scope, true).Data;
55      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
56      DoubleData mse = GetVariableValue<DoubleData>("MSE", scope, false, false);
57      if (mse == null) {
58        mse = new DoubleData();
59        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse));
60      }
61      DoubleData confidenceBounds = GetVariableValue<DoubleData>("ConfidenceBounds", scope, false, false);
62      if (confidenceBounds == null) {
63        confidenceBounds = new DoubleData();
64        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ConfidenceBounds"), confidenceBounds));
65      }
66      IntData evaluatedSamples = GetVariableValue<IntData>("ActuallyEvaluatedSamples", scope, false, false);
67      if (evaluatedSamples == null) {
68        evaluatedSamples = new IntData();
69        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ActuallyEvaluatedSamples"), evaluatedSamples));
70      }
71
72      int rows = end - start;
73      double mean = 0;
74      double stdDev = 0;
75      double confidenceInterval = 0;
76      double m2 = 0;
77      int[] indexes = InitIndexes(mt, start, end);
78      int n = 0;
79      for (int sample = 0; sample < rows; sample++) {
80        double estimated = evaluator.Evaluate(indexes[sample]);
81        double original = dataset.GetValue(indexes[sample], targetVariable);
82        if (!double.IsNaN(original) && !double.IsInfinity(original)) {
83          n++;
84          double error = estimated - original;
85          double squaredError = error * error;
86          double delta = squaredError - mean;
87          mean = mean + delta / n;
88          m2 = m2 + delta * (squaredError - mean);
89
90          if (n > minSamples && n % minSamples == 0) {
91            stdDev = Math.Sqrt(Math.Sqrt(m2 / (n - 1)));
92            confidenceInterval = 2.364 * stdDev / Math.Sqrt(n);
93            if (qualityLimit < mean - confidenceInterval || qualityLimit > mean + confidenceInterval) {
94              break;
95            }
96          }
97        }
98      }
99
100      evaluatedSamples.Data = n;
101      mse.Data = mean;
102      stdDev = Math.Sqrt(Math.Sqrt(m2 / (n - 1)));
103      confidenceBounds.Data = 2.364 * stdDev / Math.Sqrt(n);
104    }
105
106    private int[] InitIndexes(MersenneTwister mt, int start, int end) {
107      int n = end - start;
108      int[] indexes = new int[n];
109      for (int i = 0; i < n; i++) indexes[i] = i + start;
110      for (int i = 0; i < n - 1; i++) {
111        int j = mt.Next(i, n);
112        int tmp = indexes[j];
113        indexes[j] = indexes[i];
114        indexes[i] = tmp;
115      }
116      return indexes;
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.