Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/28/11 13:47:28 (13 years ago)
Author:
sforsten
Message:

#1669: branch has been merged with the trunk in revision 7081 and methods in RegressionBenchmark have been renamed.

Location:
branches/RegressionBenchmarks
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • branches/RegressionBenchmarks

  • branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis

  • branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/NormalizedGiniCalculator.cs

    r6913 r7085  
    2828  public class NormalizedGiniCalculator {
    2929
    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.");
    3335      }
    34       double oe = Gini(original, estimated, out errorState);
     36      double oe = Gini(originalValuesArr, estimatedValuesArr, out errorState);
    3537      if (errorState != OnlineCalculatorError.None) return double.NaN;
    3638
    37       return oe / (Gini(original, original, out errorState));
     39      return oe / (Gini(originalValuesArr, originalValuesArr, out errorState));
    3840    }
    3941
  • branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineAccuracyCalculator.cs

    r5945 r7085  
    6868    #endregion
    6969
    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();
    7373      OnlineAccuracyCalculator accuracyCalculator = new OnlineAccuracyCalculator();
    7474
    7575      // 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;
    7979        accuracyCalculator.Add(original, estimated);
    8080        if (accuracyCalculator.ErrorState != OnlineCalculatorError.None) break;
     
    8383      // check if both enumerators are at the end to make sure both enumerations have the same length
    8484      if (accuracyCalculator.ErrorState == OnlineCalculatorError.None &&
    85           (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
    86         throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
     85          (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
     86        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
    8787      } else {
    8888        errorState = accuracyCalculator.ErrorState;
  • branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineCovarianceCalcualtor.cs

    r5945 r7085  
    2626  public class OnlineCovarianceCalculator : IOnlineCalculator {
    2727
    28     private double originalMean, estimatedMean, Cn;
     28    private double xMean, yMean, Cn;
    2929    private int n;
    3030    public double Covariance {
     
    4949      n = 0;
    5050      Cn = 0.0;
    51       originalMean = 0.0;
    52       estimatedMean = 0.0;
     51      xMean = 0.0;
     52      yMean = 0.0;
    5353      errorState = OnlineCalculatorError.InsufficientElementsAdded;
    5454    }
    5555
    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) {
    5858        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
    5959      } else {
     
    6262
    6363        // 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;
    6767
    6868        // 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))       
    7070      }
    7171    }
     
    7979      // always move forward both enumerators (do not use short-circuit evaluation!)
    8080      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);
    8484        if (covarianceCalculator.ErrorState != OnlineCalculatorError.None) break;
    8585      }
  • branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineDirectionalSymmetryCalculator.cs

    r6802 r7085  
    2222using System;
    2323using System.Collections.Generic;
     24using HeuristicLab.Common;
    2425
    2526
    2627namespace HeuristicLab.Problems.DataAnalysis {
    27   public class OnlineDirectionalSymmetryCalculator : IOnlineCalculator {
    28     private double prevEstimated;
    29     private double prevOriginal;
     28  public class OnlineDirectionalSymmetryCalculator : IOnlineTimeSeriesCalculator {
    3029    private int n;
    3130    private int nCorrect;
     
    3332    public double DirectionalSymmetry {
    3433      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;
    3736      }
    3837    }
     
    5150    }
    5251
    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) {
    5554        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
    56       } else if (n == 0) {
    57         prevOriginal = original;
    58         prevEstimated = estimated;
    59         n++;
    6055      } 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          }
    6374        }
    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        }
    6881      }
    6982    }
     
    7285      n = 0;
    7386      nCorrect = 0;
    74       prevOriginal = double.NaN;
    75       prevEstimated = double.NaN;
    7687      errorState = OnlineCalculatorError.InsufficientElementsAdded;
    7788    }
    7889
    7990
    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();
    8395      OnlineDirectionalSymmetryCalculator dsCalculator = new OnlineDirectionalSymmetryCalculator();
    8496
    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);
    95100        if (dsCalculator.ErrorState != OnlineCalculatorError.None) break;
    96101      }
    97102
    98       // check if both enumerators are at the end to make sure both enumerations have the same length
     103      // check if all enumerators are at the end to make sure both enumerations have the same length
    99104      if (dsCalculator.ErrorState == OnlineCalculatorError.None &&
    100           (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
    101         throw new ArgumentException("Number of elements in first and second enumeration doesn'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.");
    102107      } else {
    103108        errorState = dsCalculator.ErrorState;
  • branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanAbsoluteErrorCalculator.cs

    r6643 r7085  
    6565    #endregion
    6666
    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();
    7070      OnlineMeanAbsoluteErrorCalculator maeCalculator = new OnlineMeanAbsoluteErrorCalculator();
    7171
    7272      // 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;
    7676        maeCalculator.Add(original, estimated);
    7777        if (maeCalculator.ErrorState != OnlineCalculatorError.None) break;
     
    8080      // check if both enumerators are at the end to make sure both enumerations have the same length
    8181      if (maeCalculator.ErrorState == OnlineCalculatorError.None &&
    82          (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
    83         throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
     82         (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
     83        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
    8484      } else {
    8585        errorState = maeCalculator.ErrorState;
  • branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanAbsolutePercentageErrorCalculator.cs

    r5945 r7085  
    6767    #endregion
    6868
    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();
    7272      OnlineMeanAbsolutePercentageErrorCalculator calculator = new OnlineMeanAbsolutePercentageErrorCalculator();
    7373
    7474      // 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;
    7878        calculator.Add(original, estimated);
    7979        if (calculator.ErrorState != OnlineCalculatorError.None) break;
     
    8282      // check if both enumerators are at the end to make sure both enumerations have the same length
    8383      if (calculator.ErrorState == OnlineCalculatorError.None &&
    84             (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
    85         throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
     84            (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
     85        throw new ArgumentException("Number of elements in originalValues and second estimatedValues doesn't match.");
    8686      } else {
    8787        errorState = calculator.ErrorState;
  • branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanSquaredErrorCalculator.cs

    r5945 r7085  
    6565    #endregion
    6666
    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();
    7070      OnlineMeanSquaredErrorCalculator mseCalculator = new OnlineMeanSquaredErrorCalculator();
    7171
    7272      // 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;
    7676        mseCalculator.Add(original, estimated);
    7777        if (mseCalculator.ErrorState != OnlineCalculatorError.None) break;
     
    8080      // check if both enumerators are at the end to make sure both enumerations have the same length
    8181      if (mseCalculator.ErrorState == OnlineCalculatorError.None &&
    82          (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
    83         throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
     82         (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
     83        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
    8484      } else {
    8585        errorState = mseCalculator.ErrorState;
  • branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineNormalizedMeanSquaredErrorCalculator.cs

    r5962 r7085  
    6363    #endregion
    6464
    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();
    6868      OnlineNormalizedMeanSquaredErrorCalculator normalizedMSECalculator = new OnlineNormalizedMeanSquaredErrorCalculator();
    6969
    7070      //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;
    7474        normalizedMSECalculator.Add(original, estimated);
    7575      }
    7676
    7777      // 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;
    8181        normalizedMSECalculator.Add(original, estimated);
    8282        if (normalizedMSECalculator.ErrorState != OnlineCalculatorError.None) break;
     
    8585      // check if both enumerators are at the end to make sure both enumerations have the same length
    8686      if (normalizedMSECalculator.ErrorState == OnlineCalculatorError.None &&
    87            (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
    88         throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
     87           (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
     88        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumeration doesn't match.");
    8989      } else {
    9090        errorState = normalizedMSECalculator.ErrorState;
  • branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlinePearsonsRSquaredCalculator.cs

    r5945 r7085  
    7474      // always move forward both enumerators (do not use short-circuit evaluation!)
    7575      while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
     76        double original = firstEnumerator.Current;
    7677        double estimated = secondEnumerator.Current;
    77         double original = firstEnumerator.Current;
    7878        rSquaredCalculator.Add(original, estimated);
    7979        if (rSquaredCalculator.ErrorState != OnlineCalculatorError.None) break;
  • branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineTheilsUStatisticCalculator.cs

    r6807 r7085  
    2222using System;
    2323using System.Collections.Generic;
     24using HeuristicLab.Common;
    2425
    2526namespace HeuristicLab.Problems.DataAnalysis {
    26   public class OnlineTheilsUStatisticCalculator : IOnlineCalculator {
     27  public class OnlineTheilsUStatisticCalculator : IOnlineTimeSeriesCalculator {
    2728    private OnlineMeanAndVarianceCalculator squaredErrorMeanCalculator;
    2829    private OnlineMeanAndVarianceCalculator unbiasedEstimatorMeanCalculator;
    29     private double prevOriginal;
    30     private int n;
    3130
    3231    public double TheilsUStatistic {
     
    5251    }
    5352
    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) {
    5655        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
    57       } else if (n == 0) {
    58         prevOriginal = original;
    59         n++;
    6056      } 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);
    6468
    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        }
    7079      }
    7180    }
    7281
    73 
    7482    public void Reset() {
    75       prevOriginal = double.NaN;
    76       n = 0;
    7783      squaredErrorMeanCalculator.Reset();
    7884      unbiasedEstimatorMeanCalculator.Reset();
     
    8288    #endregion
    8389
    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();
    8794      OnlineTheilsUStatisticCalculator calculator = new OnlineTheilsUStatisticCalculator();
    8895
    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);
    9999        if (calculator.ErrorState != OnlineCalculatorError.None) break;
    100100      }
    101101
    102       // check if both enumerators are at the end to make sure both enumerations have the same length
     102      // check if all enumerators are at the end to make sure both enumerations have the same length
    103103      if (calculator.ErrorState == OnlineCalculatorError.None &&
    104           (estimatedValuesEnumerator.MoveNext() || originalValuesEnumerator.MoveNext())) {
    105         throw new ArgumentException("Number of elements in first and second enumeration doesn'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.");
    106106      } else {
    107107        errorState = calculator.ErrorState;
  • branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineWeightedDirectionalSymmetryCalculator.cs

    r6807 r7085  
    2222using System;
    2323using System.Collections.Generic;
     24using HeuristicLab.Common;
    2425
    2526
    2627namespace HeuristicLab.Problems.DataAnalysis {
    27   public class OnlineWeightedDirectionalSymmetryCalculator : IOnlineCalculator {
    28     private double prevEstimated;
    29     private double prevOriginal;
     28  public class OnlineWeightedDirectionalSymmetryCalculator : IOnlineTimeSeriesCalculator {
    3029    private int n;
    3130    private double correctSum;
     
    5251    }
    5352
    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) {
    5655        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
    57       } else if (n == 0) {
    58         prevOriginal = original;
    59         prevEstimated = estimated;
    60         n++;
    6156      } 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;
    6579        } else {
    66           incorrectSum += err;
     80          errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
    6781        }
    68         n++;
    69         errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded);        // n >= 1
    70         prevOriginal = original;
    71         prevEstimated = estimated;
    7282      }
    7383    }
     
    7787      correctSum = 0;
    7888      incorrectSum = 0;
    79       prevOriginal = double.NaN;
    80       prevEstimated = double.NaN;
    8189      errorState = OnlineCalculatorError.InsufficientElementsAdded;
    8290    }
    8391
    8492
    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();
    9498
    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;
    101103      }
    102104
    103       // check if both enumerators are at the end to make sure both enumerations have the same length
    104       if (dsCalculator.ErrorState == OnlineCalculatorError.None &&
    105           (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
    106         throw new ArgumentException("Number of elements in first and second enumeration doesn'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.");
    107109      } else {
    108         errorState = dsCalculator.ErrorState;
    109         return dsCalculator.WeightedDirectionalSymmetry;
     110        errorState = calculator.ErrorState;
     111        return calculator.WeightedDirectionalSymmetry;
    110112      }
    111113    }
Note: See TracChangeset for help on using the changeset viewer.