Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/05/10 17:14:22 (14 years ago)
Author:
gkronber
Message:

Improved efficiency of analyzers and evaluators for regression problems. #1074

Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Evaluators
Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Evaluators/SimpleMSEEvaluator.cs

    r3462 r3996  
    4545
    4646    public static double Calculate(IEnumerable<double> original, IEnumerable<double> estimated) {
    47       double sse = 0.0;
    48       int cnt = 0;
     47      var onlineMseEvaluator = new OnlineMeanSquaredErrorEvaluator();
    4948      var originalEnumerator = original.GetEnumerator();
    5049      var estimatedEnumerator = estimated.GetEnumerator();
     
    5251        double e = estimatedEnumerator.Current;
    5352        double o = originalEnumerator.Current;
    54         if (!double.IsNaN(e) && !double.IsInfinity(e) &&
    55             !double.IsNaN(o) && !double.IsInfinity(o)) {
    56           double error = e - o;
    57           sse += error * error;
    58           cnt++;
    59         }
     53        onlineMseEvaluator.Add(o, e);
    6054      }
    6155      if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) {
    6256        throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match.");
    63       } else if (cnt == 0) {
    64         throw new ArgumentException("Mean squared errors is not defined for input vectors of NaN or Inf");
    6557      } else {
    66         double mse = sse / cnt;
    67         return mse;
     58        return onlineMseEvaluator.MeanSquaredError;
    6859      }
    6960    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Evaluators/SimpleMeanAbsolutePercentageErrorEvaluator.cs

    r3980 r3996  
    4848
    4949    public static double Calculate(IEnumerable<double> original, IEnumerable<double> estimated) {
    50       double sre = 0;
    51       int cnt = 0;
     50      OnlineMeanAbsolutePercentageErrorEvaluator onlineEvaluator = new OnlineMeanAbsolutePercentageErrorEvaluator();
    5251      var originalEnumerator = original.GetEnumerator();
    5352      var estimatedEnumerator = estimated.GetEnumerator();
     
    5554        double e = estimatedEnumerator.Current;
    5655        double o = originalEnumerator.Current;
    57         if (!double.IsNaN(e) && !double.IsInfinity(e) &&
    58             !double.IsNaN(o) && !double.IsInfinity(o) && !o.IsAlmost(0.0)) {
    59           sre += Math.Abs((e - o) / o);
    60           cnt++;
    61         }
     56        onlineEvaluator.Add(o, e);
    6257      }
    6358      if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) {
    6459        throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match.");
    65       } else if (cnt == 0) {
    66         throw new ArgumentException("Average relative error is not defined for input vectors of NaN or Inf");
    6760      } else {
    68         return sre / cnt;
     61        return onlineEvaluator.MeanAbsolutePercentageError;
    6962      }
    7063    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Evaluators/SimpleRSquaredEvaluator.cs

    r3452 r3996  
    4949
    5050    public static double Calculate(IEnumerable<double> original, IEnumerable<double> estimated) {
     51      var onlinePearsonRSquaredEvaluator = new OnlinePearsonsRSquaredEvaluator();
    5152      var originalEnumerator = original.GetEnumerator();
    5253      var estimatedEnumerator = estimated.GetEnumerator();
    53       originalEnumerator.MoveNext();
    54       estimatedEnumerator.MoveNext();
    55       double e = estimatedEnumerator.Current;
    56       double o = originalEnumerator.Current;
    5754
    58       // stable and iterative calculation of R² in one pass over original and estimated
    59       double sum_sq_x = 0.0;
    60       double sum_sq_y = 0.0;
    61       double sum_coproduct = 0.0;
    62       if (IsInvalidValue(o) || IsInvalidValue(e)) {
    63         throw new ArgumentException("R² is not defined for variables with NaN or infinity values.");
    64       }
    65       double mean_x = o;
    66       double mean_y = e;
    67       int n = 1;
    6855      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
    69         e = estimatedEnumerator.Current;
    70         o = originalEnumerator.Current;
    71         double sweep = (n - 1.0) / n;
    72         if (IsInvalidValue(o) || IsInvalidValue(e)) {
    73           throw new ArgumentException("Correlation coefficient is not defined for variables with NaN or infinity values.");
    74         }
    75         double delta_x = o - mean_x;
    76         double delta_y = e - mean_y;
    77         sum_sq_x += delta_x * delta_x * sweep;
    78         sum_sq_y += delta_y * delta_y * sweep;
    79         sum_coproduct += delta_x * delta_y * sweep;
    80         mean_x += delta_x / n;
    81         mean_y += delta_y / n;
    82         n++;
     56        double e = estimatedEnumerator.Current;
     57        double o = originalEnumerator.Current;
     58        onlinePearsonRSquaredEvaluator.Add(o, e);
    8359      }
    8460      if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) {
    8561        throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match.");
    8662      } else {
    87         double pop_sd_x = Math.Sqrt(sum_sq_x / n);
    88         double pop_sd_y = Math.Sqrt(sum_sq_y / n);
    89         double cov_x_y = sum_coproduct / n;
    90 
    91         if (pop_sd_x.IsAlmost(0.0) || pop_sd_y.IsAlmost(0.0))
    92           return 0.0;
    93         else {
    94           double r = cov_x_y / (pop_sd_x * pop_sd_y);
    95           return r * r;
    96         }
     63        return onlinePearsonRSquaredEvaluator.RSquared;
    9764      }
    9865    }
Note: See TracChangeset for help on using the changeset viewer.