Changeset 3452 for trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Evaluators/SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.cs
- Timestamp:
- 04/20/10 20:31:23 (14 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Evaluators/SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.cs
r3441 r3452 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-20 08Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 27 27 using HeuristicLab.Common; 28 28 using HeuristicLab.Data; 29 using HeuristicLab. DataAnalysis;29 using HeuristicLab.Parameters; 30 30 31 namespace HeuristicLab.Modeling { 32 public class SimpleMeanAbsolutePercentageOfRangeErrorEvaluator : SimpleEvaluatorBase { 33 public override string OutputVariableName { 34 get { 35 return "MAPRE"; 36 } 31 namespace HeuristicLab.Problems.DataAnalysis.Evaluators { 32 public class SimpleMeanAbsolutePercentageOfRangeErrorEvaluator : SimpleEvaluator { 33 34 public ILookupParameter<PercentValue> AveragePercentageOfRangeErrorParameter { 35 get { return (ILookupParameter<PercentValue>)Parameters["AveragePercentageOfRangeError"]; } 37 36 } 38 37 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.")); 46 40 } 47 41 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) { 49 51 double errorsSum = 0.0; 50 52 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(); 57 55 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; 61 59 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); 65 70 errorsSum += percent_error; 66 71 n++; 67 72 } 68 73 } 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; 71 78 } else { 72 79 return errorsSum / n;
Note: See TracChangeset
for help on using the changeset viewer.