Free cookie consent management tool by TermsFeed Policy Generator

Changeset 695


Ignore:
Timestamp:
10/19/08 21:29:00 (16 years ago)
Author:
gkronber
Message:

implemented #324 (Bias, variance and covariance decomposition of theil's inequality)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/TheilInequalityCoefficientEvaluator.cs

    r692 r695  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.GP.StructureIdentification;
     29using HeuristicLab.DataAnalysis;
    2930
    3031namespace HeuristicLab.GP.StructureIdentification.TimeSeries {
    3132  public class TheilInequalityCoefficientEvaluator : GPEvaluatorBase {
    3233    private DoubleData theilInequaliy;
     34    private DoubleData uBias;
     35    private DoubleData uVariance;
     36    private DoubleData uCovariance;
     37
    3338    public override string Description {
    3439      get {
    3540        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'.";
     41the 'Theil inequality coefficient (Theil's U2 not U1!)' of estimated values vs. real values of 'TargetVariable'.
     42
     43U2 = Sqrt(1/N * Sum(P_t - A_t)^2 ) / Sqrt(1/N * Sum(A_t)^2 )
     44
     45where 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
     48U2 is 0 for a perfect prediction and 1 for the naive model y'_t = y_(t-1). An U2 > 1 means the
     49model is worse than the naive model (=> model is useless).";
    3750      }
    3851    }
     
    4053    public TheilInequalityCoefficientEvaluator()
    4154      : 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));
    4359    }
    4460
     
    4965        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficient"), theilInequaliy));
    5066      }
     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      }
    5182      return base.Apply(scope);
    5283    }
     
    5586      double errorsSquaredSum = 0.0;
    5687      double originalSquaredSum = 0.0;
     88      double[] estimatedChanges = new double[end-start];
     89      double[] originalChanges = new double[end-start];
     90      int nSamples = 0;
    5791      for(int sample = start; sample < end; sample++) {
    5892        double prevValue = GetOriginalValue(sample - 1);
     
    6498          errorsSquaredSum += error * error;
    6599          originalSquaredSum += originalChange * originalChange;
     100          estimatedChanges[sample - start] = estimatedChange;
     101          originalChanges[sample - start] = originalChange;
     102          nSamples++;
    66103        }
    67104      }
    68       int nSamples = end - start;
    69105      double quality = Math.Sqrt(errorsSquaredSum / nSamples) / Math.Sqrt(originalSquaredSum / nSamples);
    70106      if(double.IsNaN(quality) || double.IsInfinity(quality))
    71107        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;
    73121    }
    74122  }
Note: See TracChangeset for help on using the changeset viewer.