Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2136


Ignore:
Timestamp:
07/06/09 17:12:37 (15 years ago)
Author:
gkronber
Message:

Improved handling of exceptional cases in data-based modeling evaluators. #688 (SimpleEvaluators should handle exceptional cases more gracefully)

Location:
trunk/sources
Files:
20 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.DataAnalysis/3.2/Statistics.cs

    r1788 r2136  
    119119
    120120    /// <summary>
    121     /// calculates the sum of all values.
    122     /// </summary>
    123     /// <param name="values"></param>
    124     /// <returns></returns>
    125     public static double Sum(double[] values) {
    126       int n = values.Length;
    127       double sum = 0.0;
    128       for (int i = 0; i < n; i++) {
    129         if (double.IsNaN(values[i])) {
    130           throw new NotFiniteNumberException();
    131         } else {
    132           sum += values[i];
    133         }
    134       }
    135       return sum;
    136     }
    137 
    138     /// <summary>
    139121    /// Calculates the mean of all values.
    140122    /// </summary>
     
    158140    /// <returns></returns>
    159141    public static double Mean(double[] values, int start, int end) {
    160       if (values.Length == 0) throw new InvalidOperationException();
     142      if (values.Length == 0) throw new ArgumentException("Values is empty.");
     143      if(end <=start) throw new ArgumentException("End is smaller or equal start");
    161144      double sum = 0.0;
    162145      int n = 0;
     
    167150        }
    168151      }
    169       if (n == 0) throw new InvalidOperationException();
    170       return sum / n;
     152      if (n > 0)
     153        return sum / n;
     154      else throw new ArgumentException("Only NaN elements in values");
    171155    }
    172156
     
    223207    /// <returns></returns>
    224208    public static double Variance(double[] values, int start, int end) {
     209      if (values.Length == 0) throw new ArgumentException("Values is empty.");
     210      if (end <= start) throw new ArgumentException("End is smaller or equal start");
    225211      if (end - start == 1)
    226212        return 0.0;
     
    238224      }
    239225      if (n < 2) {
    240         throw new InvalidOperationException();
     226        throw new ArgumentException("Only one non-NaN element in values");
    241227      }
    242228      return squaredErrorsSum / (n - 1);
  • trunk/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3/ConditionalCoefficientOfDeterminationEvaluator.cs

    r1902 r2136  
    3636      }
    3737    }
    38    
     38
    3939    public override string Description {
    4040      get {
     
    4545
    4646    public override double Evaluate(double[,] values) {
    47       double quality = SimpleR2Evaluator.Calculate(values);
    48       if (double.IsNaN(quality) || double.IsInfinity(quality))
    49         quality = double.MaxValue;
    50 
    51       return quality;
     47      try { return SimpleR2Evaluator.Calculate(values); }
     48      catch (ArgumentException) {
     49        return double.NegativeInfinity;
     50      }
    5251    }
    5352  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3/ConditionalMeanAbsolutePercentageErrorEvaluator.cs

    r1902 r2136  
    4545
    4646    public override double Evaluate(double[,] values) {
    47       double quality = SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(values);
    48       if (double.IsNaN(quality) || double.IsInfinity(quality))
    49         quality = double.MaxValue;
    50 
    51       return quality;
     47      try { return SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(values); }
     48      catch (ArgumentException) {
     49        return double.PositiveInfinity;
     50      }
    5251    }
    5352  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3/ConditionalMeanAbsolutePercentageOfRangeErrorEvaluator.cs

    r1902 r2136  
    4444
    4545    public override double Evaluate(double[,] values) {
    46       double quality = SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.Calculate(values);
    47       if (double.IsNaN(quality) || double.IsInfinity(quality))
    48         quality = double.MaxValue;
    49 
    50       return quality;
     46      try {
     47        return SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.Calculate(values);
     48      }
     49      catch (ArgumentException) {
     50        return double.PositiveInfinity;
     51      }
    5152    }
    5253  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3/ConditionalMeanSquaredErrorEvaluator.cs

    r1902 r2136  
    4444
    4545    public override double Evaluate(double[,] values) {
    46       double quality = SimpleMSEEvaluator.Calculate(values);
    47 
    48       if (double.IsNaN(quality) || double.IsInfinity(quality)) {
    49         quality = double.MaxValue;
     46      try {
     47        return SimpleMSEEvaluator.Calculate(values);
    5048      }
    51 
    52       return quality;
     49      catch (ArgumentException) {
     50        return double.PositiveInfinity;
     51      }
    5352    }
    5453  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3/ConditionalVarianceAccountedForEvaluator.cs

    r1902 r2136  
    5353
    5454    public override double Evaluate(double[,] values) {
    55       double quality = SimpleVarianceAccountedForEvaluator.Calculate(values);
    56 
    57       if (double.IsNaN(quality) || double.IsInfinity(quality)) {
    58         quality = double.MaxValue;
     55      try { return SimpleVarianceAccountedForEvaluator.Calculate(values); }
     56      catch (ArgumentException) {
     57        return double.NegativeInfinity;
    5958      }
    60       return quality;
    6159    }
    6260  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/CoefficientOfDeterminationEvaluator.cs

    r1894 r2136  
    3737      }
    3838    }
    39    
     39
    4040    public override string Description {
    4141      get {
     
    4646
    4747    public override double Evaluate(double[,] values) {
    48 
    49       double quality = SimpleR2Evaluator.Calculate(values);
    50       if (double.IsNaN(quality) || double.IsInfinity(quality))
    51         quality = double.MaxValue;
    52 
    53       return quality;
     48      try {
     49        return SimpleR2Evaluator.Calculate(values);
     50      }
     51      catch (ArgumentException) {
     52        return double.NegativeInfinity;
     53      }
    5454    }
    5555  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanAbsolutePercentageErrorEvaluator.cs

    r1894 r2136  
    4646
    4747    public override double Evaluate(double[,] values) {
    48       double quality = SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(values);
    49       if (double.IsNaN(quality) || double.IsInfinity(quality))
    50         quality = double.MaxValue;
    51 
    52       return quality;
     48      try {
     49        return SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(values);
     50      }
     51      catch (ArgumentException) {
     52        return double.PositiveInfinity;
     53      }
    5354    }
    5455  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanAbsolutePercentageOfRangeErrorEvaluator.cs

    r1894 r2136  
    4545
    4646    public override double Evaluate(double[,] values) {
    47       double quality = SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.Calculate(values);
    48       if (double.IsNaN(quality) || double.IsInfinity(quality))
    49         quality = double.MaxValue;
    50 
    51       return quality;
     47      try { return SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.Calculate(values); }
     48      catch (ArgumentException) {
     49        return double.PositiveInfinity;
     50      }
    5251    }
    5352  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanSquaredErrorEvaluator.cs

    r1894 r2136  
    4545
    4646    public override double Evaluate(double[,] values) {
    47       double quality = SimpleMSEEvaluator.Calculate(values);
    48 
    49       if (double.IsNaN(quality) || double.IsInfinity(quality)) {
    50         quality = double.MaxValue;
     47      try { return SimpleMSEEvaluator.Calculate(values); }
     48      catch (ArgumentException) {
     49        return double.PositiveInfinity;
    5150      }
    52 
    53       return quality;
    5451    }
    5552  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VariableQualityImpactCalculator.cs

    r2043 r2136  
    4646      evaluator.PrepareForEvaluation(dataset, targetVariable, start, end, punishmentFactor, tree);
    4747
    48       double[,] result = new double[end - start,2];
     48      double[,] result = new double[end - start, 2];
    4949      for (int i = start; i < end; i++) {
    5050        result[i - start, 0] = dataset.GetValue(i, targetVariable);
    51         result[i - start,1] = evaluator.Evaluate(i);
     51        result[i - start, 1] = evaluator.Evaluate(i);
    5252      }
    5353
    54       return HeuristicLab.Modeling.SimpleMSEEvaluator.Calculate(result);
     54      try {
     55        return HeuristicLab.Modeling.SimpleMSEEvaluator.Calculate(result);
     56      }
     57      catch (ArgumentException) {
     58        return double.PositiveInfinity;
     59      }
    5560    }
    5661  }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VarianceAccountedForEvaluator.cs

    r1894 r2136  
    5454
    5555    public override double Evaluate(double[,] values) {
    56       double quality = SimpleVarianceAccountedForEvaluator.Calculate(values);
    57 
    58       if (double.IsNaN(quality) || double.IsInfinity(quality)) {
    59         quality = double.MaxValue;
     56      try { return SimpleVarianceAccountedForEvaluator.Calculate(values); }
     57      catch (ArgumentException) {
     58        return double.NegativeInfinity;
    6059      }
    61       return quality;
    6260    }
    6361  }
  • trunk/sources/HeuristicLab.Modeling/3.2/SimpleEvaluatorBase.cs

    r1888 r2136  
    3131
    3232    public abstract double Evaluate(double[,] values);
     33
     34    protected static bool IsAlmost(double x, double y) {
     35      return Math.Abs(x - y) < 1.0E-12;
     36    }
    3337  }
    3438}
  • trunk/sources/HeuristicLab.Modeling/3.2/SimpleMSEEvaluator.cs

    r1888 r2136  
    1616    }
    1717    public override double Evaluate(double[,] values) {
    18       return Calculate(values);
     18      try {
     19        return Calculate(values);
     20      }
     21      catch (ArgumentException) {
     22        return double.PositiveInfinity;
     23      }
    1924    }
    2025
     
    3237        }
    3338      }
    34 
    35       double mse = sse / cnt;
    36       return mse;
     39      if (cnt > 0) {
     40        double mse = sse / cnt;
     41        return mse;
     42      } else {
     43        throw new ArgumentException("Mean squared errors is not defined for input vectors of NaN or Inf");
     44      }
    3745    }
    3846  }
  • trunk/sources/HeuristicLab.Modeling/3.2/SimpleMeanAbsolutePercentageErrorEvaluator.cs

    r1888 r2136  
    3838
    3939    public override double Evaluate(double[,] values) {
    40       return Calculate(values);
     40      try {
     41        return Calculate(values);
     42      }
     43      catch (ArgumentException) {
     44        return double.PositiveInfinity;
     45      }
    4146    }
    4247
     
    5560        }
    5661      }
    57       return errorsSum / n;
     62      if (n > 0) {
     63        return errorsSum / n;
     64      } else throw new ArgumentException("Mean of absolute percentage error is not defined for input vectors of NaN or Inf");
    5865    }
    5966  }
  • trunk/sources/HeuristicLab.Modeling/3.2/SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.cs

    r1888 r2136  
    3737
    3838    public override double Evaluate(double[,] values) {
    39       return Calculate(values);
     39      try {
     40        return Calculate(values);
     41      }
     42      catch (ArgumentException) {
     43        return double.PositiveInfinity;
     44      }
    4045    }
    4146
     
    4752      for (int i = 0; i < originalValues.Length; i++) originalValues[i] = values[i, 1];
    4853      double range = Statistics.Range(originalValues);
     54      if (double.IsInfinity(range)) throw new ArgumentException("Range of elements in values is infinity");
     55      if (IsAlmost(range, 0.0)) throw new ArgumentException("Range of elements in values is zero");
    4956
    5057      for (int i = 0; i < values.GetLength(0); i++) {
     
    5966        }
    6067      }
    61       return errorsSum / n;
     68      if (double.IsInfinity(range) || n == 0) {
     69        throw new ArgumentException("Mean of absolute percentage of range error is not defined for input vectors of NaN or Inf");
     70      } else {
     71        return errorsSum / n;
     72      }
    6273    }
    6374  }
  • trunk/sources/HeuristicLab.Modeling/3.2/SimpleR2Evaluator.cs

    r1888 r2136  
    1717
    1818    public override double Evaluate(double[,] values) {
    19       return Calculate(values);
     19      try {
     20        return Calculate(values);
     21      }
     22      catch (ArgumentException) {
     23        return double.NegativeInfinity;
     24      }
    2025    }
    2126
     
    3540        }
    3641      }
    37       targetMean /= cnt;
    3842
    39       double targetDeviationTotalSumOfSquares = 0;
    40       for (int i = 0; i < values.GetLength(0); i++) {
    41         double target = values[i, 1];
    42         if (!double.IsNaN(target) && !double.IsInfinity(target)) {
    43           target = target - targetMean;
    44           target = target * target;
    45           targetDeviationTotalSumOfSquares += target;
     43      if (cnt > 0) {
     44        targetMean /= cnt;
     45
     46        double targetDeviationTotalSumOfSquares = 0;
     47        for (int i = 0; i < values.GetLength(0); i++) {
     48          double target = values[i, 1];
     49          if (!double.IsNaN(target) && !double.IsInfinity(target)) {
     50            target = target - targetMean;
     51            target = target * target;
     52            targetDeviationTotalSumOfSquares += target;
     53          }
    4654        }
     55        double quality = 1 - sse / targetDeviationTotalSumOfSquares;
     56        if (quality > 1)
     57          throw new InvalidProgramException();
     58
     59        return quality;
     60      } else {
     61        throw new ArgumentException("Coefficient of determination is not defined for input vectors of NaN or Inf");
    4762      }
    48       double quality = 1 - sse / targetDeviationTotalSumOfSquares;
    49       if (quality > 1)
    50         throw new InvalidProgramException();
    51 
    52       return quality;
    5363    }
    5464  }
  • trunk/sources/HeuristicLab.Modeling/3.2/SimpleVarianceAccountedForEvaluator.cs

    r1888 r2136  
    4343
    4444    public override double Evaluate(double[,] values) {
    45       return Calculate(values);
     45      try {
     46        return Calculate(values);
     47      }
     48      catch (ArgumentException) {
     49        return double.NegativeInfinity;
     50      }
    4651    }
    4752
     
    6469      double errorsVariance = Statistics.Variance(errors);
    6570      double originalsVariance = Statistics.Variance(originalTargetVariableValues);
    66       double quality = 1 - errorsVariance / originalsVariance;
    67 
    68       return quality;
     71      if (IsAlmost(originalsVariance, 0.0))
     72        if (IsAlmost(errorsVariance, 0.0)) {
     73          return 1.0;
     74        } else {
     75          throw new ArgumentException("Variance of original values is zero");
     76        } else {
     77        return 1.0 - errorsVariance / originalsVariance;
     78      }
    6979    }
    7080  }
  • trunk/sources/HeuristicLab.Modeling/3.2/VariableEvaluationImpactCalculator.cs

    r2043 r2136  
    5050
    5151    protected override double CalculateImpact(double[] referenceValue, double[] newValue) {
    52       return SimpleMSEEvaluator.Calculate(CombineOutputs(referenceValue, newValue));
     52      try {
     53        return SimpleMSEEvaluator.Calculate(CombineOutputs(referenceValue, newValue));
     54      }
     55      catch (ArgumentException) {
     56        return double.PositiveInfinity;
     57      }
    5358    }
    5459
     
    5964    protected override double[] PostProcessImpacts(double[] impacts) {
    6065      double mseSum = impacts.Sum();
    61       if (mseSum == 0.0) mseSum = 1.0;
     66      if (IsAlmost(mseSum, 0.0)) mseSum = 1.0;
    6267      for (int i = 0; i < impacts.Length; i++) {
    6368        impacts[i] = impacts[i] / mseSum;
     
    6671    }
    6772
     73    private bool IsAlmost(double x, double y) {
     74      return Math.Abs(x - y) < 1.0E-12;
     75    }
     76
    6877    protected abstract double[] GetOutputs(IScope scope, Dataset dataset, int targetVariable, ItemList<IntData> allowedFeatures, int start, int end);
    6978  }
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/VariableQualityImpactCalculator.cs

    r2043 r2136  
    4545      for (int i = 0; i < end - start; i++) {
    4646        values[i, 0] = SVM.Prediction.Predict(model.Model, scaledProblem.X[i]);
    47         values[i, 1] = dataset.GetValue(start + i,targetVariable);
     47        values[i, 1] = dataset.GetValue(start + i, targetVariable);
    4848      }
    4949
    50       return HeuristicLab.Modeling.SimpleMSEEvaluator.Calculate(values);
     50      try { return HeuristicLab.Modeling.SimpleMSEEvaluator.Calculate(values); }
     51      catch (ArgumentException) { return double.PositiveInfinity; }
    5152    }
    5253  }
Note: See TracChangeset for help on using the changeset viewer.