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  private DoubleData theilInequaliy;


34  private DoubleData uBias;


35  private DoubleData uVariance;


36  private DoubleData uCovariance;


37 


38  public override string Description {


39  get {


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


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


42 


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


44 


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


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


47 


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


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


50  }


51  }


52 


53  public TheilInequalityCoefficientEvaluator()


54  : base() {


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


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


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


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


59  }


60 


61  public override IOperation Apply(IScope scope) {


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


63  if(theilInequaliy == null) {


64  theilInequaliy = new DoubleData();


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


66  }


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


68  if(uBias == null) {


69  uBias = new DoubleData();


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


71  }


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


73  if(uVariance == null) {


74  uVariance = new DoubleData();


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


76  }


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


78  if(uCovariance == null) {


79  uCovariance = new DoubleData();


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


81  }


82  return base.Apply(scope);


83  }


84 


85  public override void Evaluate(int start, int end) {


86  double errorsSquaredSum = 0.0;


87  double originalSquaredSum = 0.0;


88  double[] estimatedChanges = new double[endstart];


89  double[] originalChanges = new double[endstart];


90  int nSamples = 0;


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


92  double prevValue = GetOriginalValue(sample  1);


93  double estimatedChange = GetEstimatedValue(sample)  prevValue;


94  double originalChange = GetOriginalValue(sample)  prevValue;


95  SetOriginalValue(sample, estimatedChange + prevValue);


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


97  double error = estimatedChange  originalChange;


98  errorsSquaredSum += error * error;


99  originalSquaredSum += originalChange * originalChange;


100  estimatedChanges[sample  start] = estimatedChange;


101  originalChanges[sample  start] = originalChange;


102  nSamples++;


103  }


104  }


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


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


107  quality = double.MaxValue;


108  theilInequaliy.Data = quality; // U2


109 


110  // decomposition into U_bias + U_variance + U_covariance parts


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


112  bias *= bias; // squared


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


114 


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


116  variance *= variance; // squared


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


118 


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


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


121  }


122  }


123  }

