1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022008 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 


22  using System;


23  using System.Collections.Generic;


24  using System.Linq;


25  using System.Text;


26  using HeuristicLab.Core;


27  using HeuristicLab.Data;


28  using HeuristicLab.GP.StructureIdentification;


29  using HeuristicLab.DataAnalysis;


30 


31  namespace HeuristicLab.GP.StructureIdentification.TimeSeries {


32  public class TheilInequalityCoefficientEvaluator : GPEvaluatorBase {


33  public override string Description {


34  get {


35  return @"Evaluates 'FunctionTree' for all samples of 'Dataset' and calculates


36  the 'Theil inequality coefficient (Theil's U2 not U1!)' of estimated values vs. real values of 'TargetVariable'.


37 


38  U2 = Sqrt(1/N * Sum(P_t  A_t)^2 ) / Sqrt(1/N * Sum(A_t)^2 )


39 


40  where P_t is the predicted change of the target variable and A_t is the measured (original) change.


41  (P_t = y'_t  y_(t1), A_t = y_t  y_(t1)).


42 


43  U2 is 0 for a perfect prediction and 1 for the naive model y'_t = y_(t1). An U2 > 1 means the


44  model is worse than the naive model (=> model is useless).";


45  }


46  }


47 


48  public TheilInequalityCoefficientEvaluator()


49  : base() {


50  AddVariableInfo(new VariableInfo("TheilInequalityCoefficient", "Theil's inequality coefficient (U2) of the model", typeof(DoubleData), VariableKind.New));


51  AddVariableInfo(new VariableInfo("TheilInequalityCoefficientBias", "Bias proportion of Theil's inequality coefficient", typeof(DoubleData), VariableKind.New));


52  AddVariableInfo(new VariableInfo("TheilInequalityCoefficientVariance", "Variance proportion of Theil's inequality coefficient", typeof(DoubleData), VariableKind.New));


53  AddVariableInfo(new VariableInfo("TheilInequalityCoefficientCovariance", "Covariance proportion of Theil's inequality coefficient", typeof(DoubleData), VariableKind.New));


54  }


55 


56  public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {


57  #region create result variables


58  DoubleData theilInequaliy = GetVariableValue<DoubleData>("TheilInequalityCoefficient", scope, false, false);


59  if (theilInequaliy == null) {


60  theilInequaliy = new DoubleData();


61  scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficient"), theilInequaliy));


62  }


63  DoubleData uBias = GetVariableValue<DoubleData>("TheilInequalityCoefficientBias", scope, false, false);


64  if (uBias == null) {


65  uBias = new DoubleData();


66  scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientBias"), uBias));


67  }


68  DoubleData uVariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientVariance", scope, false, false);


69  if (uVariance == null) {


70  uVariance = new DoubleData();


71  scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientVariance"), uVariance));


72  }


73  DoubleData uCovariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientCovariance", scope, false, false);


74  if (uCovariance == null) {


75  uCovariance = new DoubleData();


76  scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientCovariance"), uCovariance));


77  }


78  #endregion


79 


80  double errorsSquaredSum = 0.0;


81  double originalSquaredSum = 0.0;


82  double[] estimatedChanges = new double[end  start];


83  double[] originalChanges = new double[end  start];


84  int nSamples = 0;


85  for (int sample = start; sample < end; sample++) {


86  double prevValue = dataset.GetValue(sample  1, targetVariable);


87  double estimatedChange = evaluator.Evaluate(sample)  prevValue;


88  double originalChange = dataset.GetValue(sample, targetVariable)  prevValue;


89  if (updateTargetValues) {


90  dataset.SetValue(sample, targetVariable, estimatedChange + prevValue);


91  }


92  if (!double.IsNaN(originalChange) && !double.IsInfinity(originalChange)) {


93  double error = estimatedChange  originalChange;


94  errorsSquaredSum += error * error;


95  originalSquaredSum += originalChange * originalChange;


96  estimatedChanges[sample  start] = estimatedChange;


97  originalChanges[sample  start] = originalChange;


98  nSamples++;


99  }


100  }


101  double quality = Math.Sqrt(errorsSquaredSum / nSamples) / Math.Sqrt(originalSquaredSum / nSamples);


102  if (double.IsNaN(quality)  double.IsInfinity(quality))


103  quality = double.MaxValue;


104  theilInequaliy.Data = quality; // U2


105 


106  // decomposition into U_bias + U_variance + U_covariance parts


107  double bias = Statistics.Mean(estimatedChanges)  Statistics.Mean(originalChanges);


108  bias *= bias; // squared


109  uBias.Data = bias / (errorsSquaredSum / nSamples);


110 


111  double variance = Statistics.StandardDeviation(estimatedChanges)  Statistics.StandardDeviation(originalChanges);


112  variance *= variance; // squared


113  uVariance.Data = variance / (errorsSquaredSum / nSamples);


114 


115  // all parts add up to one so I don't have to calculate the correlation coefficient for the covariance propotion


116  uCovariance.Data = 1.0  uBias.Data  uVariance.Data;


117  }


118  }


119  }

