Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/3.3/TheilInequalityCoefficientEvaluator.cs @ 2216

Last change on this file since 2216 was 2216, checked in by gkronber, 15 years ago

GP Refactoring #713

  • cleaned code
  • reintegrated GP.Boolean and GP.SantaFe
  • worked on serialization of function trees
File size: 5.9 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 HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.DataAnalysis;
26
27namespace HeuristicLab.GP.StructureIdentification.TimeSeries {
28  public class TheilInequalityCoefficientEvaluator : GPEvaluatorBase {
29    public override string Description {
30      get {
31        return @"Evaluates 'FunctionTree' for all samples of 'Dataset' and calculates
32the 'Theil inequality coefficient (Theil's U2 not U1!)' of estimated values vs. real values of 'TargetVariable'.
33
34U2 = Sqrt(1/N * Sum(P_t - A_t)^2 ) / Sqrt(1/N * Sum(A_t)^2 )
35
36where P_t is the predicted change of the target variable and A_t is the measured (original) change.
37(P_t = y'_t - y_(t-1), A_t = y_t - y_(t-1)).
38
39U2 is 0 for a perfect prediction and 1 for the naive model y'_t = y_(t-1). An U2 > 1 means the
40model is worse than the naive model (=> model is useless).";
41      }
42    }
43
44    public TheilInequalityCoefficientEvaluator()
45      : base() {
46      AddVariableInfo(new VariableInfo("TheilInequalityCoefficient", "Theil's inequality coefficient (U2) of the model", typeof(DoubleData), VariableKind.New));
47      AddVariableInfo(new VariableInfo("TheilInequalityCoefficientBias", "Bias proportion of Theil's inequality coefficient", typeof(DoubleData), VariableKind.New));
48      AddVariableInfo(new VariableInfo("TheilInequalityCoefficientVariance", "Variance proportion of Theil's inequality coefficient", typeof(DoubleData), VariableKind.New));
49      AddVariableInfo(new VariableInfo("TheilInequalityCoefficientCovariance", "Covariance proportion of Theil's inequality coefficient", typeof(DoubleData), VariableKind.New));
50    }
51
52    public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
53      #region create result variables
54      DoubleData theilInequaliy = GetVariableValue<DoubleData>("TheilInequalityCoefficient", scope, false, false);
55      if (theilInequaliy == null) {
56        theilInequaliy = new DoubleData();
57        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficient"), theilInequaliy));
58      }
59      DoubleData uBias = GetVariableValue<DoubleData>("TheilInequalityCoefficientBias", scope, false, false);
60      if (uBias == null) {
61        uBias = new DoubleData();
62        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientBias"), uBias));
63      }
64      DoubleData uVariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientVariance", scope, false, false);
65      if (uVariance == null) {
66        uVariance = new DoubleData();
67        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientVariance"), uVariance));
68      }
69      DoubleData uCovariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientCovariance", scope, false, false);
70      if (uCovariance == null) {
71        uCovariance = new DoubleData();
72        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientCovariance"), uCovariance));
73      }
74      #endregion
75
76      double errorsSquaredSum = 0.0;
77      double originalSquaredSum = 0.0;
78      double[] estimatedChanges = new double[end - start];
79      double[] originalChanges = new double[end - start];
80      int nSamples = 0;
81      for (int sample = start; sample < end; sample++) {
82        double prevValue = dataset.GetValue(sample - 1, targetVariable);
83        double estimatedChange = evaluator.Evaluate(sample) - prevValue;
84        double originalChange = dataset.GetValue(sample, targetVariable) - prevValue;
85        if (updateTargetValues) {
86          dataset.SetValue(sample, targetVariable, estimatedChange + prevValue);
87        }
88        if (!double.IsNaN(originalChange) && !double.IsInfinity(originalChange)) {
89          double error = estimatedChange - originalChange;
90          errorsSquaredSum += error * error;
91          originalSquaredSum += originalChange * originalChange;
92          estimatedChanges[sample - start] = estimatedChange;
93          originalChanges[sample - start] = originalChange;
94          nSamples++;
95        }
96      }
97      double quality = Math.Sqrt(errorsSquaredSum / nSamples) / Math.Sqrt(originalSquaredSum / nSamples);
98      if (double.IsNaN(quality) || double.IsInfinity(quality))
99        quality = double.MaxValue;
100      theilInequaliy.Data = quality; // U2
101
102      // decomposition into U_bias + U_variance + U_covariance parts
103      double bias = Statistics.Mean(estimatedChanges) - Statistics.Mean(originalChanges);
104      bias *= bias; // squared
105      uBias.Data = bias / (errorsSquaredSum / nSamples);
106
107      double variance = Statistics.StandardDeviation(estimatedChanges) - Statistics.StandardDeviation(originalChanges);
108      variance *= variance; // squared
109      uVariance.Data = variance / (errorsSquaredSum / nSamples);
110
111      // all parts add up to one so I don't have to calculate the correlation coefficient for the covariance proportion
112      uCovariance.Data = 1.0 - uBias.Data - uVariance.Data;
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.