Changeset 695 for trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/TheilInequalityCoefficientEvaluator.cs
- Timestamp:
- 10/19/08 21:29:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/TheilInequalityCoefficientEvaluator.cs
r692 r695 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.GP.StructureIdentification; 29 using HeuristicLab.DataAnalysis; 29 30 30 31 namespace HeuristicLab.GP.StructureIdentification.TimeSeries { 31 32 public class TheilInequalityCoefficientEvaluator : GPEvaluatorBase { 32 33 private DoubleData theilInequaliy; 34 private DoubleData uBias; 35 private DoubleData uVariance; 36 private DoubleData uCovariance; 37 33 38 public override string Description { 34 39 get { 35 40 return @"Evaluates 'FunctionTree' for all samples of 'Dataset' and calculates 36 the 'Theil inequality coefficient (scale invariant)' of estimated values vs. real values of 'TargetVariable'."; 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_(t-1), A_t = y_t - y_(t-1)). 47 48 U2 is 0 for a perfect prediction and 1 for the naive model y'_t = y_(t-1). An U2 > 1 means the 49 model is worse than the naive model (=> model is useless)."; 37 50 } 38 51 } … … 40 53 public TheilInequalityCoefficientEvaluator() 41 54 : base() { 42 AddVariableInfo(new VariableInfo("TheilInequalityCoefficient", "Theil's inequality coefficient of the model", typeof(DoubleData), VariableKind.New)); 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)); 43 59 } 44 60 … … 49 65 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficient"), theilInequaliy)); 50 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 } 51 82 return base.Apply(scope); 52 83 } … … 55 86 double errorsSquaredSum = 0.0; 56 87 double originalSquaredSum = 0.0; 88 double[] estimatedChanges = new double[end-start]; 89 double[] originalChanges = new double[end-start]; 90 int nSamples = 0; 57 91 for(int sample = start; sample < end; sample++) { 58 92 double prevValue = GetOriginalValue(sample - 1); … … 64 98 errorsSquaredSum += error * error; 65 99 originalSquaredSum += originalChange * originalChange; 100 estimatedChanges[sample - start] = estimatedChange; 101 originalChanges[sample - start] = originalChange; 102 nSamples++; 66 103 } 67 104 } 68 int nSamples = end - start;69 105 double quality = Math.Sqrt(errorsSquaredSum / nSamples) / Math.Sqrt(originalSquaredSum / nSamples); 70 106 if(double.IsNaN(quality) || double.IsInfinity(quality)) 71 107 quality = double.MaxValue; 72 theilInequaliy.Data = quality; 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; 73 121 } 74 122 }
Note: See TracChangeset
for help on using the changeset viewer.