Changeset 7085 for branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators
- Timestamp:
- 11/28/11 13:47:28 (13 years ago)
- Location:
- branches/RegressionBenchmarks
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/RegressionBenchmarks
- Property svn:ignore
-
old new 4 4 *.suo 5 5 *.vsp 6 Doxygen 6 7 Google.ProtocolBuffers-0.9.1.dll 7 8 HeuristicLab 3.3.5.1.ReSharper.user 8 9 HeuristicLab 3.3.6.0.ReSharper.user 9 10 HeuristicLab.4.5.resharper.user 11 HeuristicLab.ExtLibs.6.0.ReSharper.user 10 12 HeuristicLab.resharper.user 11 13 ProtoGen.exe … … 16 18 bin 17 19 protoc.exe 18 HeuristicLab.ExtLibs.6.0.ReSharper.user19 Doxygen
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis (added) merged: 6961,6963-6964,6974,6980,6982,7011,7017,7021,7023,7043,7049
- Property svn:mergeinfo changed
-
branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/NormalizedGiniCalculator.cs
r6913 r7085 28 28 public class NormalizedGiniCalculator { 29 29 30 public static double Calculate(IEnumerable<double> original, IEnumerable<double> estimated, out OnlineCalculatorError errorState) { 31 if (original.Count() != estimated.Count()) { 32 throw new ArgumentException("Number of elements in first and second enumeration doesn't match."); 30 public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 31 var originalValuesArr = originalValues.ToArray(); 32 var estimatedValuesArr = estimatedValues.ToArray(); 33 if (originalValuesArr.Count() != estimatedValuesArr.Count()) { 34 throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match."); 33 35 } 34 double oe = Gini(original , estimated, out errorState);36 double oe = Gini(originalValuesArr, estimatedValuesArr, out errorState); 35 37 if (errorState != OnlineCalculatorError.None) return double.NaN; 36 38 37 return oe / (Gini(original , original, out errorState));39 return oe / (Gini(originalValuesArr, originalValuesArr, out errorState)); 38 40 } 39 41 -
branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineAccuracyCalculator.cs
r5945 r7085 68 68 #endregion 69 69 70 public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) {71 IEnumerator<double> firstEnumerator = first.GetEnumerator();72 IEnumerator<double> secondEnumerator = second.GetEnumerator();70 public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 71 IEnumerator<double> originalEnumerator = originalValues.GetEnumerator(); 72 IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator(); 73 73 OnlineAccuracyCalculator accuracyCalculator = new OnlineAccuracyCalculator(); 74 74 75 75 // always move forward both enumerators (do not use short-circuit evaluation!) 76 while ( firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {77 double estimated = secondEnumerator.Current;78 double original = firstEnumerator.Current;76 while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) { 77 double original = originalEnumerator.Current; 78 double estimated = estimatedEnumerator.Current; 79 79 accuracyCalculator.Add(original, estimated); 80 80 if (accuracyCalculator.ErrorState != OnlineCalculatorError.None) break; … … 83 83 // check if both enumerators are at the end to make sure both enumerations have the same length 84 84 if (accuracyCalculator.ErrorState == OnlineCalculatorError.None && 85 ( secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {86 throw new ArgumentException("Number of elements in first and second enumerationdoesn't match.");85 (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) { 86 throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match."); 87 87 } else { 88 88 errorState = accuracyCalculator.ErrorState; -
branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineCovarianceCalcualtor.cs
r5945 r7085 26 26 public class OnlineCovarianceCalculator : IOnlineCalculator { 27 27 28 private double originalMean, estimatedMean, Cn;28 private double xMean, yMean, Cn; 29 29 private int n; 30 30 public double Covariance { … … 49 49 n = 0; 50 50 Cn = 0.0; 51 originalMean = 0.0;52 estimatedMean = 0.0;51 xMean = 0.0; 52 yMean = 0.0; 53 53 errorState = OnlineCalculatorError.InsufficientElementsAdded; 54 54 } 55 55 56 public void Add(double original, double estimated) {57 if (double.IsNaN( estimated) || double.IsInfinity(estimated) || double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {56 public void Add(double x, double y) { 57 if (double.IsNaN(y) || double.IsInfinity(y) || double.IsNaN(x) || double.IsInfinity(x) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) { 58 58 errorState = errorState | OnlineCalculatorError.InvalidValueAdded; 59 59 } else { … … 62 62 63 63 // online calculation of tMean 64 originalMean = originalMean + (original - originalMean) / n;65 double delta = estimated - estimatedMean; // delta = (y - yMean(n-1))66 estimatedMean = estimatedMean + delta / n;64 xMean = xMean + (x - xMean) / n; 65 double delta = y - yMean; // delta = (y - yMean(n-1)) 66 yMean = yMean + delta / n; 67 67 68 68 // online calculation of covariance 69 Cn = Cn + delta * ( original - originalMean); // C(n) = C(n-1) + (y - yMean(n-1)) (t - tMean(n))69 Cn = Cn + delta * (x - xMean); // C(n) = C(n-1) + (y - yMean(n-1)) (t - tMean(n)) 70 70 } 71 71 } … … 79 79 // always move forward both enumerators (do not use short-circuit evaluation!) 80 80 while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) { 81 double estimated= secondEnumerator.Current;82 double original= firstEnumerator.Current;83 covarianceCalculator.Add( original, estimated);81 double x = secondEnumerator.Current; 82 double y = firstEnumerator.Current; 83 covarianceCalculator.Add(x, y); 84 84 if (covarianceCalculator.ErrorState != OnlineCalculatorError.None) break; 85 85 } -
branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineDirectionalSymmetryCalculator.cs
r6802 r7085 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; -
branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanAbsoluteErrorCalculator.cs
r6643 r7085 65 65 #endregion 66 66 67 public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) {68 IEnumerator<double> firstEnumerator = first.GetEnumerator();69 IEnumerator<double> secondEnumerator = second.GetEnumerator();67 public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 68 IEnumerator<double> originalEnumerator = originalValues.GetEnumerator(); 69 IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator(); 70 70 OnlineMeanAbsoluteErrorCalculator maeCalculator = new OnlineMeanAbsoluteErrorCalculator(); 71 71 72 72 // always move forward both enumerators (do not use short-circuit evaluation!) 73 while ( firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {74 double estimated = secondEnumerator.Current;75 double original = firstEnumerator.Current;73 while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) { 74 double original = originalEnumerator.Current; 75 double estimated = estimatedEnumerator.Current; 76 76 maeCalculator.Add(original, estimated); 77 77 if (maeCalculator.ErrorState != OnlineCalculatorError.None) break; … … 80 80 // check if both enumerators are at the end to make sure both enumerations have the same length 81 81 if (maeCalculator.ErrorState == OnlineCalculatorError.None && 82 ( secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {83 throw new ArgumentException("Number of elements in first and second enumerationdoesn't match.");82 (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) { 83 throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match."); 84 84 } else { 85 85 errorState = maeCalculator.ErrorState; -
branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanAbsolutePercentageErrorCalculator.cs
r5945 r7085 67 67 #endregion 68 68 69 public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) {70 IEnumerator<double> firstEnumerator = first.GetEnumerator();71 IEnumerator<double> secondEnumerator = second.GetEnumerator();69 public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 70 IEnumerator<double> originalEnumerator = originalValues.GetEnumerator(); 71 IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator(); 72 72 OnlineMeanAbsolutePercentageErrorCalculator calculator = new OnlineMeanAbsolutePercentageErrorCalculator(); 73 73 74 74 // always move forward both enumerators (do not use short-circuit evaluation!) 75 while ( firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {76 double estimated = secondEnumerator.Current;77 double original = firstEnumerator.Current;75 while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) { 76 double original = originalEnumerator.Current; 77 double estimated = estimatedEnumerator.Current; 78 78 calculator.Add(original, estimated); 79 79 if (calculator.ErrorState != OnlineCalculatorError.None) break; … … 82 82 // check if both enumerators are at the end to make sure both enumerations have the same length 83 83 if (calculator.ErrorState == OnlineCalculatorError.None && 84 ( secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {85 throw new ArgumentException("Number of elements in first and second enumerationdoesn't match.");84 (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) { 85 throw new ArgumentException("Number of elements in originalValues and second estimatedValues doesn't match."); 86 86 } else { 87 87 errorState = calculator.ErrorState; -
branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanSquaredErrorCalculator.cs
r5945 r7085 65 65 #endregion 66 66 67 public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) {68 IEnumerator<double> firstEnumerator = first.GetEnumerator();69 IEnumerator<double> secondEnumerator = second.GetEnumerator();67 public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 68 IEnumerator<double> originalEnumerator = originalValues.GetEnumerator(); 69 IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator(); 70 70 OnlineMeanSquaredErrorCalculator mseCalculator = new OnlineMeanSquaredErrorCalculator(); 71 71 72 72 // always move forward both enumerators (do not use short-circuit evaluation!) 73 while ( firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {74 double estimated = secondEnumerator.Current;75 double original = firstEnumerator.Current;73 while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) { 74 double original = originalEnumerator.Current; 75 double estimated = estimatedEnumerator.Current; 76 76 mseCalculator.Add(original, estimated); 77 77 if (mseCalculator.ErrorState != OnlineCalculatorError.None) break; … … 80 80 // check if both enumerators are at the end to make sure both enumerations have the same length 81 81 if (mseCalculator.ErrorState == OnlineCalculatorError.None && 82 ( secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {83 throw new ArgumentException("Number of elements in first and second enumerationdoesn't match.");82 (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) { 83 throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match."); 84 84 } else { 85 85 errorState = mseCalculator.ErrorState; -
branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineNormalizedMeanSquaredErrorCalculator.cs
r5962 r7085 63 63 #endregion 64 64 65 public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) {66 IEnumerator<double> firstEnumerator = first.GetEnumerator();67 IEnumerator<double> secondEnumerator = second.GetEnumerator();65 public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 66 IEnumerator<double> originalEnumerator = originalValues.GetEnumerator(); 67 IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator(); 68 68 OnlineNormalizedMeanSquaredErrorCalculator normalizedMSECalculator = new OnlineNormalizedMeanSquaredErrorCalculator(); 69 69 70 70 //needed because otherwise the normalizedMSECalculator is in ErrorState.InsufficientValuesAdded 71 if ( firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {72 double estimated = secondEnumerator.Current;73 double original = firstEnumerator.Current;71 if (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) { 72 double original = originalEnumerator.Current; 73 double estimated = estimatedEnumerator.Current; 74 74 normalizedMSECalculator.Add(original, estimated); 75 75 } 76 76 77 77 // always move forward both enumerators (do not use short-circuit evaluation!) 78 while ( firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {79 double estimated = secondEnumerator.Current;80 double original = firstEnumerator.Current;78 while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) { 79 double original = originalEnumerator.Current; 80 double estimated = estimatedEnumerator.Current; 81 81 normalizedMSECalculator.Add(original, estimated); 82 82 if (normalizedMSECalculator.ErrorState != OnlineCalculatorError.None) break; … … 85 85 // check if both enumerators are at the end to make sure both enumerations have the same length 86 86 if (normalizedMSECalculator.ErrorState == OnlineCalculatorError.None && 87 ( secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {88 throw new ArgumentException("Number of elements in first and secondenumeration doesn't match.");87 (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) { 88 throw new ArgumentException("Number of elements in originalValues and estimatedValues enumeration doesn't match."); 89 89 } else { 90 90 errorState = normalizedMSECalculator.ErrorState; -
branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlinePearsonsRSquaredCalculator.cs
r5945 r7085 74 74 // always move forward both enumerators (do not use short-circuit evaluation!) 75 75 while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) { 76 double original = firstEnumerator.Current; 76 77 double estimated = secondEnumerator.Current; 77 double original = firstEnumerator.Current;78 78 rSquaredCalculator.Add(original, estimated); 79 79 if (rSquaredCalculator.ErrorState != OnlineCalculatorError.None) break; -
branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineTheilsUStatisticCalculator.cs
r6807 r7085 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 24 25 25 26 namespace HeuristicLab.Problems.DataAnalysis { 26 public class OnlineTheilsUStatisticCalculator : IOnline Calculator {27 public class OnlineTheilsUStatisticCalculator : IOnlineTimeSeriesCalculator { 27 28 private OnlineMeanAndVarianceCalculator squaredErrorMeanCalculator; 28 29 private OnlineMeanAndVarianceCalculator unbiasedEstimatorMeanCalculator; 29 private double prevOriginal;30 private int n;31 30 32 31 public double TheilsUStatistic { … … 52 51 } 53 52 54 public void Add(double original, double estimated) {55 if (double.IsNaN( estimated) || double.IsInfinity(estimated) || double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {53 public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) { 54 if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) { 56 55 errorState = errorState | OnlineCalculatorError.InvalidValueAdded; 57 } else if (n == 0) {58 prevOriginal = original;59 n++;60 56 } else { 61 // error of predicted change 62 double errorEstimatedChange = (estimated - original); 63 squaredErrorMeanCalculator.Add(errorEstimatedChange * errorEstimatedChange); 57 var actualEnumerator = actualContinuation.GetEnumerator(); 58 var predictedEnumerator = predictedContinuation.GetEnumerator(); 59 while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & ErrorState != OnlineCalculatorError.InvalidValueAdded) { 60 double actual = actualEnumerator.Current; 61 double predicted = predictedEnumerator.Current; 62 if (double.IsNaN(actual) || double.IsNaN(predicted)) { 63 errorState = errorState | OnlineCalculatorError.InvalidValueAdded; 64 } else { 65 // error of predicted change 66 double errorPredictedChange = (predicted - startValue) - (actual - startValue); 67 squaredErrorMeanCalculator.Add(errorPredictedChange * errorPredictedChange); 64 68 65 double errorNoChange = (original - prevOriginal); 66 unbiasedEstimatorMeanCalculator.Add(errorNoChange * errorNoChange); 67 errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1 68 prevOriginal = original; 69 n++; 69 double errorNoChange = (actual - startValue); 70 unbiasedEstimatorMeanCalculator.Add(errorNoChange * errorNoChange); 71 } 72 } 73 // check if both enumerators are at the end to make sure both enumerations have the same length 74 if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext()) { 75 errorState = errorState | OnlineCalculatorError.InvalidValueAdded; 76 } else { 77 errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1 78 } 70 79 } 71 80 } 72 81 73 74 82 public void Reset() { 75 prevOriginal = double.NaN;76 n = 0;77 83 squaredErrorMeanCalculator.Reset(); 78 84 unbiasedEstimatorMeanCalculator.Reset(); … … 82 88 #endregion 83 89 84 public static double Calculate(IEnumerable<double> estimatedValues, IEnumerable<double> originalValues, out OnlineCalculatorError errorState) { 85 IEnumerator<double> originalValuesEnumerator = originalValues.GetEnumerator(); 86 IEnumerator<double> estimatedValuesEnumerator = estimatedValues.GetEnumerator(); 90 public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) { 91 IEnumerator<double> startValueEnumerator = startValues.GetEnumerator(); 92 IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator(); 93 IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator(); 87 94 OnlineTheilsUStatisticCalculator calculator = new OnlineTheilsUStatisticCalculator(); 88 95 89 // add first element of time series as a reference point 90 originalValuesEnumerator.MoveNext(); 91 estimatedValuesEnumerator.MoveNext(); 92 calculator.Add(originalValuesEnumerator.Current, estimatedValuesEnumerator.Current); 93 94 // always move forward both enumerators (do not use short-circuit evaluation!) 95 while (originalValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) { 96 double estimated = estimatedValuesEnumerator.Current; 97 double original = originalValuesEnumerator.Current; 98 calculator.Add(original, estimated); 96 // always move forward all enumerators (do not use short-circuit evaluation!) 97 while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) { 98 calculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current); 99 99 if (calculator.ErrorState != OnlineCalculatorError.None) break; 100 100 } 101 101 102 // check if bothenumerators are at the end to make sure both enumerations have the same length102 // check if all enumerators are at the end to make sure both enumerations have the same length 103 103 if (calculator.ErrorState == OnlineCalculatorError.None && 104 ( estimatedValuesEnumerator.MoveNext() || originalValuesEnumerator.MoveNext())) {105 throw new ArgumentException("Number of elements in first and second enumerationdoesn't match.");104 (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) { 105 throw new ArgumentException("Number of elements in startValues, actualContinuations and estimatedValues predictedContinuations doesn't match."); 106 106 } else { 107 107 errorState = calculator.ErrorState; -
branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineWeightedDirectionalSymmetryCalculator.cs
r6807 r7085 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 OnlineWeightedDirectionalSymmetryCalculator : IOnlineCalculator { 28 private double prevEstimated; 29 private double prevOriginal; 28 public class OnlineWeightedDirectionalSymmetryCalculator : IOnlineTimeSeriesCalculator { 30 29 private int n; 31 30 private double correctSum; … … 52 51 } 53 52 54 public void Add(double original, double estimated) {55 if (double.IsNaN( estimated) || double.IsInfinity(estimated) || double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {53 public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) { 54 if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) { 56 55 errorState = errorState | OnlineCalculatorError.InvalidValueAdded; 57 } else if (n == 0) {58 prevOriginal = original;59 prevEstimated = estimated;60 n++;61 56 } else { 62 double err = Math.Abs(original - estimated); 63 if ((original - prevOriginal) * (estimated - prevEstimated) >= 0.0) { 64 correctSum += err; 57 var actualEnumerator = actualContinuation.GetEnumerator(); 58 var predictedEnumerator = predictedContinuation.GetEnumerator(); 59 while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & errorState != OnlineCalculatorError.InvalidValueAdded) { 60 double actual = actualEnumerator.Current; 61 double predicted = predictedEnumerator.Current; 62 if (double.IsNaN(actual) || double.IsNaN(predicted)) { 63 errorState = errorState | OnlineCalculatorError.InvalidValueAdded; 64 } else { 65 double err = Math.Abs(actual - predicted); 66 // count as correct only if the trend (positive/negative/no change) is predicted correctly 67 if ((actual - startValue) * (predicted - startValue) > 0.0 || 68 (actual - startValue).IsAlmost(predicted - startValue)) { 69 correctSum += err; 70 } else { 71 incorrectSum += err; 72 } 73 n++; 74 } 75 } 76 // check if both enumerators are at the end to make sure both enumerations have the same length 77 if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext()) { 78 errorState = errorState | OnlineCalculatorError.InvalidValueAdded; 65 79 } else { 66 incorrectSum += err;80 errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1 67 81 } 68 n++;69 errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 170 prevOriginal = original;71 prevEstimated = estimated;72 82 } 73 83 } … … 77 87 correctSum = 0; 78 88 incorrectSum = 0; 79 prevOriginal = double.NaN;80 prevEstimated = double.NaN;81 89 errorState = OnlineCalculatorError.InsufficientElementsAdded; 82 90 } 83 91 84 92 85 public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) { 86 IEnumerator<double> firstEnumerator = first.GetEnumerator(); 87 IEnumerator<double> secondEnumerator = second.GetEnumerator(); 88 OnlineWeightedDirectionalSymmetryCalculator dsCalculator = new OnlineWeightedDirectionalSymmetryCalculator(); 89 90 // add first element of time series as a reference point 91 firstEnumerator.MoveNext(); 92 secondEnumerator.MoveNext(); 93 dsCalculator.Add(firstEnumerator.Current, secondEnumerator.Current); 93 public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) { 94 IEnumerator<double> startValueEnumerator = startValues.GetEnumerator(); 95 IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator(); 96 IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator(); 97 OnlineWeightedDirectionalSymmetryCalculator calculator = new OnlineWeightedDirectionalSymmetryCalculator(); 94 98 95 // always move forward both enumerators (do not use short-circuit evaluation!) 96 while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) { 97 double estimated = secondEnumerator.Current; 98 double original = firstEnumerator.Current; 99 dsCalculator.Add(original, estimated); 100 if (dsCalculator.ErrorState != OnlineCalculatorError.None) break; 99 // always move forward all enumerators (do not use short-circuit evaluation!) 100 while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) { 101 calculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current); 102 if (calculator.ErrorState != OnlineCalculatorError.None) break; 101 103 } 102 104 103 // check if bothenumerators are at the end to make sure both enumerations have the same length104 if ( dsCalculator.ErrorState == OnlineCalculatorError.None &&105 (s econdEnumerator.MoveNext() || firstEnumerator.MoveNext())) {106 throw new ArgumentException("Number of elements in first and second enumerationdoesn't match.");105 // check if all enumerators are at the end to make sure both enumerations have the same length 106 if (calculator.ErrorState == OnlineCalculatorError.None && 107 (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) { 108 throw new ArgumentException("Number of elements in startValues, actualContinuations and estimatedValues predictedContinuations doesn't match."); 107 109 } else { 108 errorState = dsCalculator.ErrorState;109 return dsCalculator.WeightedDirectionalSymmetry;110 errorState = calculator.ErrorState; 111 return calculator.WeightedDirectionalSymmetry; 110 112 } 111 113 }
Note: See TracChangeset
for help on using the changeset viewer.