Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StructureIdentification/Evaluation/VarianceAccountedForEvaluator.cs @ 77

Last change on this file since 77 was 2, checked in by swagner, 16 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 6.4 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.Functions;
31
32namespace HeuristicLab.StructureIdentification {
33  public class VarianceAccountedForEvaluator : OperatorBase {
34    public override string Description {
35      get { return @"Evaluates 'OperatorTree' for samples 'FirstSampleIndex' - 'LastSampleIndex' (inclusive) and calculates
36the variance-accounted-for quality measure for the estimated values vs. the real values of 'TargetVariable'.
37
38The Variance Accounted For (VAF) function is computed as
39VAF(y,y') = ( 1 - var(y-y')/var(y) )
40where y' denotes the predicted / modelled values for y and var(x) the variance of a signal x."; }
41    }
42
43    /// <summary>
44    /// The Variance Accounted For (VAF) function calculates is computed as
45    /// VAF(y,y') = ( 1 - var(y-y')/var(y) )
46    /// where y' denotes the predicted / modelled values for y and var(x) the variance of a signal x.
47    /// </summary>
48    public VarianceAccountedForEvaluator()
49      : base() {
50      AddVariableInfo(new VariableInfo("OperatorTree", "The function tree that should be evaluated", typeof(IFunction), VariableKind.In));
51      AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In));
52      AddVariableInfo(new VariableInfo("TargetVariable", "Index of the target variable in the dataset", typeof(IntData), VariableKind.In));
53      AddVariableInfo(new VariableInfo("FirstSampleIndex", "Index of the first row of the dataset on which the function should be evaluated", typeof(IntData), VariableKind.In));
54      AddVariableInfo(new VariableInfo("LastSampleIndex", "Index of the last row of the dataset on which the function should be evaluated (inclusive)", typeof(IntData), VariableKind.In));
55      AddVariableInfo(new VariableInfo("PunishmentFactor", "Punishment factor for invalid estimations", typeof(DoubleData), VariableKind.In));
56      AddVariableInfo(new VariableInfo("UseEstimatedTargetValues", "When the function tree contains the target variable this variable determines " +
57      "if we should use the estimated or the original values of the target variable in the evaluation", typeof(BoolData), VariableKind.In));
58      AddVariableInfo(new VariableInfo("Quality", "Variance accounted for quality of the model", typeof(DoubleData), VariableKind.New));
59
60    }
61
62
63    private double[] originalTargetVariableValues = new double[1];
64    private double[] errors = new double[1];
65
66    public override IOperation Apply(IScope scope) {
67
68      int firstSampleIndex = GetVariableValue<IntData>("FirstSampleIndex", scope, true).Data;
69      int lastSampleIndex = GetVariableValue<IntData>("LastSampleIndex", scope, true).Data;
70
71      if(lastSampleIndex < firstSampleIndex) {
72        throw new InvalidProgramException();
73      }
74
75      IFunction function = GetVariableValue<IFunction>("OperatorTree", scope, true);
76
77      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
78
79      int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
80      bool useEstimatedTargetValues = GetVariableValue<BoolData>("UseEstimatedTargetValues", scope, true).Data;
81      double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
82
83      if(originalTargetVariableValues.Length != lastSampleIndex - firstSampleIndex + 1) {
84        originalTargetVariableValues = new double[lastSampleIndex - firstSampleIndex + 1];
85        errors = new double[lastSampleIndex - firstSampleIndex + 1];
86      }
87
88      double maximumPunishment = punishmentFactor * dataset.GetRange(targetVariable, firstSampleIndex, lastSampleIndex);
89
90      double targetMean = dataset.GetMean(targetVariable, firstSampleIndex, lastSampleIndex);
91
92      for(int sample = firstSampleIndex; sample <= lastSampleIndex; sample++) {
93
94        double estimated = function.Evaluate(dataset, sample);
95        double original =  dataset.GetValue(sample, targetVariable);
96
97        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
98          if(double.IsNaN(estimated) || double.IsInfinity(estimated))
99            estimated = targetMean + maximumPunishment;
100          else if(estimated > (targetMean + maximumPunishment))
101            estimated = targetMean + maximumPunishment;
102          else if(estimated < (targetMean - maximumPunishment))
103            estimated = targetMean - maximumPunishment;
104        }
105
106        errors[sample-firstSampleIndex] = original - estimated;
107        originalTargetVariableValues[sample-firstSampleIndex] = original;
108        if(useEstimatedTargetValues) {
109          dataset.SetValue(sample, targetVariable, estimated);
110        }
111      }
112
113      double errorsVariance = Statistics.Variance(errors);
114      double originalsVariance = Statistics.Variance(originalTargetVariableValues);
115      double quality = 1 - errorsVariance / originalsVariance;
116
117      if(double.IsNaN(quality) || double.IsInfinity(quality)) {
118        quality = double.MaxValue;
119      }
120
121      if(useEstimatedTargetValues) {
122        // restore original values of the target variable
123        for(int sample = firstSampleIndex; sample <= lastSampleIndex; sample++) {
124          dataset.SetValue(sample, targetVariable, originalTargetVariableValues[sample - firstSampleIndex]);
125        }
126      }
127
128      scope.AddVariable(new HeuristicLab.Core.Variable("Quality", new DoubleData(quality)));
129      return null;
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.