Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/22/15 20:04:01 (9 years ago)
Author:
gkronber
Message:

#2392: implemented PearsonsRCalculator to fix incorrect correlation values in the correlation matrix.

Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/DependencyCalculator/PearsonsRDependenceCalculator.cs

    r12012 r12492  
    3333
    3434    public double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    35       return Math.Sqrt(OnlinePearsonsRSquaredCalculator.Calculate(originalValues, estimatedValues, out errorState));
     35      return OnlinePearsonsRCalculator.Calculate(originalValues, estimatedValues, out errorState);
    3636    }
    3737  }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlinePearsonsRCalculator.cs

    r12491 r12492  
    2525
    2626namespace HeuristicLab.Problems.DataAnalysis {
    27   public class OnlinePearsonsRSquaredCalculator : IOnlineCalculator {
     27  public class OnlinePearsonsRCalculator : IOnlineCalculator {
    2828    private OnlineCovarianceCalculator covCalculator = new OnlineCovarianceCalculator();
    2929    private OnlineMeanAndVarianceCalculator sxCalculator = new OnlineMeanAndVarianceCalculator();
    3030    private OnlineMeanAndVarianceCalculator syCalculator = new OnlineMeanAndVarianceCalculator();
    3131
    32     public double RSquared {
     32    public double R {
    3333      get {
    3434        double xVar = sxCalculator.PopulationVariance;
     
    3737          return 0.0;
    3838        } else {
    39           double r = covCalculator.Covariance / (Math.Sqrt(xVar) * Math.Sqrt(yVar));
    40           return r * r;
     39          return covCalculator.Covariance / (Math.Sqrt(xVar) * Math.Sqrt(yVar));
    4140        }
    4241      }
    4342    }
    4443
    45     public OnlinePearsonsRSquaredCalculator() { }
     44    public OnlinePearsonsRCalculator() { }
    4645
    4746    #region IOnlineCalculator Members
     
    5049    }
    5150    public double Value {
    52       get { return RSquared; }
     51      get { return R; }
    5352    }
    5453    public void Reset() {
     
    7069      IEnumerator<double> firstEnumerator = first.GetEnumerator();
    7170      IEnumerator<double> secondEnumerator = second.GetEnumerator();
    72       OnlinePearsonsRSquaredCalculator rSquaredCalculator = new OnlinePearsonsRSquaredCalculator();
     71      var calculator = new OnlinePearsonsRCalculator();
    7372
    7473      // always move forward both enumerators (do not use short-circuit evaluation!)
     
    7675        double original = firstEnumerator.Current;
    7776        double estimated = secondEnumerator.Current;
    78         rSquaredCalculator.Add(original, estimated);
    79         if (rSquaredCalculator.ErrorState != OnlineCalculatorError.None) break;
     77        calculator.Add(original, estimated);
     78        if (calculator.ErrorState != OnlineCalculatorError.None) break;
    8079      }
    8180
    8281      // check if both enumerators are at the end to make sure both enumerations have the same length
    83       if (rSquaredCalculator.ErrorState == OnlineCalculatorError.None &&
     82      if (calculator.ErrorState == OnlineCalculatorError.None &&
    8483           (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
    8584        throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
    8685      } else {
    87         errorState = rSquaredCalculator.ErrorState;
    88         return rSquaredCalculator.RSquared;
     86        errorState = calculator.ErrorState;
     87        return calculator.R;
    8988      }
    9089    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlinePearsonsRSquaredCalculator.cs

    r12012 r12492  
    2525
    2626namespace HeuristicLab.Problems.DataAnalysis {
     27  [Obsolete("Use OnlinePearsonsRCalculator directly")]
    2728  public class OnlinePearsonsRSquaredCalculator : IOnlineCalculator {
    28     private OnlineCovarianceCalculator covCalculator = new OnlineCovarianceCalculator();
    29     private OnlineMeanAndVarianceCalculator sxCalculator = new OnlineMeanAndVarianceCalculator();
    30     private OnlineMeanAndVarianceCalculator syCalculator = new OnlineMeanAndVarianceCalculator();
     29    private readonly OnlinePearsonsRCalculator rCalculator = new OnlinePearsonsRCalculator();
    3130
    3231    public double RSquared {
    3332      get {
    34         double xVar = sxCalculator.PopulationVariance;
    35         double yVar = syCalculator.PopulationVariance;
    36         if (xVar.IsAlmost(0.0) || yVar.IsAlmost(0.0)) {
    37           return 0.0;
    38         } else {
    39           double r = covCalculator.Covariance / (Math.Sqrt(xVar) * Math.Sqrt(yVar));
    40           return r * r;
    41         }
     33        if (rCalculator.ErrorState != OnlineCalculatorError.None) return 0.0;
     34        else return rCalculator.R * rCalculator.R;
    4235      }
    4336    }
     
    4740    #region IOnlineCalculator Members
    4841    public OnlineCalculatorError ErrorState {
    49       get { return covCalculator.ErrorState | sxCalculator.PopulationVarianceErrorState | syCalculator.PopulationVarianceErrorState; }
     42      get { return rCalculator.ErrorState; }
    5043    }
    5144    public double Value {
     
    5346    }
    5447    public void Reset() {
    55       covCalculator.Reset();
    56       sxCalculator.Reset();
    57       syCalculator.Reset();
     48      rCalculator.Reset();
    5849    }
    5950
    6051    public void Add(double x, double y) {
    61       // no need to check validity of values explicitly here as it is checked in all three evaluators
    62       covCalculator.Add(x, y);
    63       sxCalculator.Add(x);
    64       syCalculator.Add(y);
     52      rCalculator.Add(x, y);
    6553    }
    6654
     
    6856
    6957    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();
    72       OnlinePearsonsRSquaredCalculator rSquaredCalculator = new OnlinePearsonsRSquaredCalculator();
    73 
    74       // always move forward both enumerators (do not use short-circuit evaluation!)
    75       while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
    76         double original = firstEnumerator.Current;
    77         double estimated = secondEnumerator.Current;
    78         rSquaredCalculator.Add(original, estimated);
    79         if (rSquaredCalculator.ErrorState != OnlineCalculatorError.None) break;
    80       }
    81 
    82       // check if both enumerators are at the end to make sure both enumerations have the same length
    83       if (rSquaredCalculator.ErrorState == OnlineCalculatorError.None &&
    84            (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
    85         throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
    86       } else {
    87         errorState = rSquaredCalculator.ErrorState;
    88         return rSquaredCalculator.RSquared;
    89       }
     58      var r = OnlinePearsonsRCalculator.Calculate(first, second, out errorState);
     59      return r * r;
    9060    }
    9161  }
Note: See TracChangeset for help on using the changeset viewer.