Free cookie consent management tool by TermsFeed Policy Generator

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/HeuristicLab.Modeling/3.2
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • 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  }
Note: See TracChangeset for help on using the changeset viewer.