Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/20/10 20:31:23 (14 years ago)
Author:
gkronber
Message:

Included tracking of best of run solution (based on validation set) and calculation of MSE, R² and rel. Error on training and test sets. #938 (Data types and operators for regression problems)

File:
1 copied

Legend:

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

    r3441 r3452  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2727using HeuristicLab.Common;
    2828using HeuristicLab.Data;
    29 using HeuristicLab.DataAnalysis;
     29using HeuristicLab.Parameters;
    3030
    31 namespace HeuristicLab.Modeling {
    32   public class SimpleMeanAbsolutePercentageOfRangeErrorEvaluator : SimpleEvaluatorBase {
    33     public override string OutputVariableName {
    34       get {
    35         return "MAPRE";
    36       }
     31namespace HeuristicLab.Problems.DataAnalysis.Evaluators {
     32  public class SimpleMeanAbsolutePercentageOfRangeErrorEvaluator : SimpleEvaluator {
     33
     34    public ILookupParameter<PercentValue> AveragePercentageOfRangeErrorParameter {
     35      get { return (ILookupParameter<PercentValue>)Parameters["AveragePercentageOfRangeError"]; }
    3736    }
    3837
    39     public override double Evaluate(double[,] values) {
    40       try {
    41         return Calculate(values);
    42       }
    43       catch (ArgumentException) {
    44         return double.PositiveInfinity;
    45       }
     38    public SimpleMeanAbsolutePercentageOfRangeErrorEvaluator() {
     39      Parameters.Add(new LookupParameter<PercentValue>("AveragePercentageOfRangeError", "The average relative (percentage of range) error of estimated values."));
    4640    }
    4741
    48     public static double Calculate(double[,] values) {
     42    protected override void Apply(DoubleMatrix values) {
     43      var original = from i in Enumerable.Range(0, values.Rows)
     44                     select values[i, ORIGINAL_INDEX];
     45      var estimated = from i in Enumerable.Range(0, values.Rows)
     46                      select values[i, ESTIMATION_INDEX];
     47      AveragePercentageOfRangeErrorParameter.ActualValue = new PercentValue(Calculate(original, estimated));
     48    }
     49
     50    public static double Calculate(IEnumerable<double> original, IEnumerable<double> estimated) {
    4951      double errorsSum = 0.0;
    5052      int n = 0;
    51       // copy to one-dimensional array for range calculation
    52       double[] originalValues = new double[values.GetLength(0)];
    53       for (int i = 0; i < originalValues.Length; i++) originalValues[i] = values[i, ORIGINAL_INDEX];
    54       double range = Statistics.Range(originalValues);
    55       if (double.IsInfinity(range)) throw new ArgumentException("Range of elements in values is infinity");
    56       if (range.IsAlmost(0.0)) throw new ArgumentException("Range of elements in values is zero");
     53      IList<double> originalList = original as IList<double>;
     54      if (originalList == null) originalList = original.ToList();
    5755
    58       for (int i = 0; i < values.GetLength(0); i++) {
    59         double estimated = values[i, ESTIMATION_INDEX];
    60         double original = values[i, ORIGINAL_INDEX];
     56      double range = originalList.Max() - originalList.Min();
     57      if (double.IsInfinity(range)) return double.MaxValue;
     58      if (range.IsAlmost(0.0)) return double.MaxValue;
    6159
    62         if (!double.IsNaN(estimated) && !double.IsInfinity(estimated) &&
    63           !double.IsNaN(original) && !double.IsInfinity(original) && original != 0.0) {
    64           double percent_error = Math.Abs((estimated - original) / range);
     60
     61      var originalEnumerator = original.GetEnumerator();
     62      var estimatedEnumerator = estimated.GetEnumerator();
     63      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
     64        double e = estimatedEnumerator.Current;
     65        double o = originalEnumerator.Current;
     66
     67        if (!double.IsNaN(e) && !double.IsInfinity(e) &&
     68          !double.IsNaN(o) && !double.IsInfinity(o) && !o.IsAlmost(0.0)) {
     69          double percent_error = Math.Abs((e - o) / range);
    6570          errorsSum += percent_error;
    6671          n++;
    6772        }
    6873      }
    69       if (double.IsInfinity(range) || n == 0) {
    70         throw new ArgumentException("Mean of absolute percentage of range error is not defined for input vectors of NaN or Inf");
     74      if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) {
     75        throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match.");
     76      } else if (n == 0) {
     77        return double.MaxValue;
    7178      } else {
    7279        return errorsSum / n;
Note: See TracChangeset for help on using the changeset viewer.