Changeset 7000 for branches/Benchmarking/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineDirectionalSymmetryCalculator.cs
- Timestamp:
- 11/16/11 15:07:36 (13 years ago)
- Location:
- branches/Benchmarking
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Benchmarking
- Property svn:mergeinfo changed
/trunk (added) merged: 6918-6919,6921-6923,6928-6930,6933,6935,6938-6939,6942,6944,6949-6954,6959,6961-6964,6974-6983,6985-6986,6993-6995,6997-6999
- Property svn:mergeinfo changed
-
branches/Benchmarking/sources
- Property svn:mergeinfo changed
/trunk/sources (added) merged: 6918-6919,6921-6923,6928-6930,6935,6938-6939,6942,6944,6949-6954,6959,6961-6964,6974-6983,6986,6993-6995,6997-6999
- Property svn:mergeinfo changed
-
branches/Benchmarking/sources/HeuristicLab.Problems.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis (added) merged: 6961,6963-6964,6974,6980,6982
- Property svn:mergeinfo changed
-
branches/Benchmarking/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineDirectionalSymmetryCalculator.cs
r6802 r7000 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 24 25 25 26 26 27 namespace HeuristicLab.Problems.DataAnalysis { 27 public class OnlineDirectionalSymmetryCalculator : IOnlineCalculator { 28 private double prevEstimated; 29 private double prevOriginal; 28 public class OnlineDirectionalSymmetryCalculator : IOnlineTimeSeriesCalculator { 30 29 private int n; 31 30 private int nCorrect; … … 33 32 public double DirectionalSymmetry { 34 33 get { 35 if (n < =1) return 0.0;36 return (double)nCorrect / (n - 1) * 100.0;34 if (n < 1) return 0.0; 35 return (double)nCorrect / n; 37 36 } 38 37 } … … 51 50 } 52 51 53 public void Add(double original, double estimated) {54 if (double.IsNaN( estimated) || double.IsInfinity(estimated) || double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {52 public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) { 53 if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) { 55 54 errorState = errorState | OnlineCalculatorError.InvalidValueAdded; 56 } else if (n == 0) {57 prevOriginal = original;58 prevEstimated = estimated;59 n++;60 55 } else { 61 if ((original - prevOriginal) * (estimated - prevEstimated) >= 0.0) { 62 nCorrect++; 56 var actualEnumerator = actualContinuation.GetEnumerator(); 57 var predictedEnumerator = predictedContinuation.GetEnumerator(); 58 double prevActual = startValue; 59 double prevPredicted = startValue; 60 while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & errorState != OnlineCalculatorError.InvalidValueAdded) { 61 double actual = actualEnumerator.Current; 62 double predicted = predictedEnumerator.Current; 63 if (double.IsNaN(actual) || double.IsNaN(predicted)) { 64 errorState = errorState | OnlineCalculatorError.InvalidValueAdded; 65 } else { 66 // count a prediction correct if the trend (positive/negative/no change) is predicted correctly 67 if ((actual - prevActual) * (predicted - prevPredicted) > 0.0 || 68 (actual - prevActual).IsAlmost(predicted - prevPredicted) 69 ) { 70 nCorrect++; 71 } 72 n++; 73 } 63 74 } 64 n++; 65 errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1 66 prevOriginal = original; 67 prevEstimated = estimated; 75 // check if both enumerators are at the end to make sure both enumerations have the same length 76 if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext()) { 77 errorState = errorState | OnlineCalculatorError.InvalidValueAdded; 78 } else { 79 errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1 80 } 68 81 } 69 82 } … … 72 85 n = 0; 73 86 nCorrect = 0; 74 prevOriginal = double.NaN;75 prevEstimated = double.NaN;76 87 errorState = OnlineCalculatorError.InsufficientElementsAdded; 77 88 } 78 89 79 90 80 public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) { 81 IEnumerator<double> firstEnumerator = first.GetEnumerator(); 82 IEnumerator<double> secondEnumerator = second.GetEnumerator(); 91 public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) { 92 IEnumerator<double> startValueEnumerator = startValues.GetEnumerator(); 93 IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator(); 94 IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator(); 83 95 OnlineDirectionalSymmetryCalculator dsCalculator = new OnlineDirectionalSymmetryCalculator(); 84 96 85 // add first element of time series as a reference point 86 firstEnumerator.MoveNext(); 87 secondEnumerator.MoveNext(); 88 dsCalculator.Add(firstEnumerator.Current, secondEnumerator.Current); 89 90 // always move forward both enumerators (do not use short-circuit evaluation!) 91 while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) { 92 double estimated = secondEnumerator.Current; 93 double original = firstEnumerator.Current; 94 dsCalculator.Add(original, estimated); 97 // always move forward all enumerators (do not use short-circuit evaluation!) 98 while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) { 99 dsCalculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current); 95 100 if (dsCalculator.ErrorState != OnlineCalculatorError.None) break; 96 101 } 97 102 98 // check if bothenumerators are at the end to make sure both enumerations have the same length103 // check if all enumerators are at the end to make sure both enumerations have the same length 99 104 if (dsCalculator.ErrorState == OnlineCalculatorError.None && 100 (s econdEnumerator.MoveNext() || firstEnumerator.MoveNext())) {101 throw new ArgumentException("Number of elements in first and second enumerationdoesn't match.");105 (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) { 106 throw new ArgumentException("Number of elements in startValues, actualContinuations and estimatedValues predictedContinuations doesn't match."); 102 107 } else { 103 108 errorState = dsCalculator.ErrorState;
Note: See TracChangeset
for help on using the changeset viewer.