Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/31/10 16:29:42 (14 years ago)
Author:
gkronber
Message:

Fixed #1116

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Evaluators/OnlinePearsonsRSquaredEvaluator.cs

    r4068 r4122  
    2525namespace HeuristicLab.Problems.DataAnalysis.Evaluators {
    2626  public class OnlinePearsonsRSquaredEvaluator : IOnlineEvaluator {
    27 
    28     private double sum_sq_x;
    29     private double sum_sq_y;
    30     private double sum_coproduct;
    31     private double mean_x;
    32     private double mean_y;
    33     private int n;
     27    private OnlineCovarianceEvaluator covEvaluator = new OnlineCovarianceEvaluator();
     28    private OnlineMeanAndVarianceCalculator sxEvaluator = new OnlineMeanAndVarianceCalculator();
     29    private OnlineMeanAndVarianceCalculator syEvaluator = new OnlineMeanAndVarianceCalculator();
    3430
    3531    public double RSquared {
    3632      get {
    37         if (n < 1)
    38           throw new InvalidOperationException("No elements");
    39         else {
    40           double pop_sd_x = Math.Sqrt(sum_sq_x / n);
    41           double pop_sd_y = Math.Sqrt(sum_sq_y / n);
    42           double cov_x_y = sum_coproduct / n;
    43 
    44           if (pop_sd_x.IsAlmost(0.0) || pop_sd_y.IsAlmost(0.0))
    45             return 0.0;
    46           else {
    47             double r = cov_x_y / (pop_sd_x * pop_sd_y);
    48             return r * r;
    49           }
    50         }
     33        double r = covEvaluator.Covariance / (Math.Sqrt(sxEvaluator.PopulationVariance) * Math.Sqrt(syEvaluator.PopulationVariance));
     34        return r * r;
    5135      }
    5236    }
     
    5943    }
    6044    public void Reset() {
    61       sum_sq_x = 0.0;
    62       sum_sq_y = 0.0;
    63       sum_coproduct = 0.0;
    64       mean_x = 0.0;
    65       mean_y = 0.0;
    66       n = 0;
     45      covEvaluator.Reset();
     46      sxEvaluator.Reset();
     47      syEvaluator.Reset();
    6748    }
    6849
    69     public void Add(double original, double estimated) {
    70       // stable and iterative calculation of R²
    71       if (IsInvalidValue(original) || IsInvalidValue(estimated)) {
     50    public void Add(double x, double y) {
     51      if (IsInvalidValue(x) || IsInvalidValue(y)) {
    7252        throw new ArgumentException("R² is not defined for variables with NaN or infinity values.");
    7353      }
    74       if (n == 0) {
    75         mean_x = original;
    76         mean_y = estimated;
    77         n = 1;
    78       } else {
    79         double sweep = (n - 1.0) / n;
    80         double delta_x = original - mean_x;
    81         double delta_y = estimated - mean_y;
    82         sum_sq_x += delta_x * delta_x * sweep;
    83         sum_sq_y += delta_y * delta_y * sweep;
    84         sum_coproduct += delta_x * delta_y * sweep;
    85         mean_x += delta_x / n;
    86         mean_y += delta_y / n;
    87         n++;
    88       }
     54      covEvaluator.Add(x, y);
     55      sxEvaluator.Add(x);
     56      syEvaluator.Add(y);
    8957    }
    9058
Note: See TracChangeset for help on using the changeset viewer.