Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/12/10 18:46:59 (14 years ago)
Author:
gkronber
Message:

Moved code for calculation of covariance from the scaled MSE evaluator into a separate online evaluator. #1081

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionScaledMeanSquaredErrorEvaluator.cs

    r4001 r4027  
    114114      IEnumerator<double> originalEnumerator = original.GetEnumerator();
    115115      IEnumerator<double> estimatedEnumerator = estimated.GetEnumerator();
    116 
     116      OnlineMeanAndVarianceCalculator yVarianceCalculator = new OnlineMeanAndVarianceCalculator();
     117      OnlineMeanAndVarianceCalculator tMeanCalculator = new OnlineMeanAndVarianceCalculator();
     118      OnlineCovarianceEvaluator ytCovarianceEvaluator = new OnlineCovarianceEvaluator();
    117119      int cnt = 0;
    118       double tMean = 0;
    119       double yMean = 0;
    120       double Cn = 0;
    121       double M2 = 0;
    122120
    123121      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
     
    125123        double t = originalEnumerator.Current;
    126124        if (IsValidValue(t) && IsValidValue(y)) {
     125          tMeanCalculator.Add(t);
     126          yVarianceCalculator.Add(y);
     127          ytCovarianceEvaluator.Add(y, t);
     128
    127129          cnt++;
    128           // online calculation of tMean
    129           tMean = tMean + (t - tMean) / cnt;
    130           double delta = y - yMean; // delta = (y - yMean(n-1))
    131           yMean = yMean + delta / cnt;
    132          
    133           // online calculation of variance
    134           M2 = M2 + delta * (y - yMean); // M2(n) = M2(n-1) + (y - yMean(n-1)) (y - yMean(n))
    135 
    136           // online calculation of covariance
    137           Cn = Cn + delta * (t - tMean); // C(n) = C(n-1) + (y - yMean(n-1)) (t - tMean(n))
    138130        }
    139131      }
     
    145137        beta = 1;
    146138      } else {
    147         // yVariance = M2 / cnt;
    148         // ytCovariance = Cn / cnt;
    149 
    150         if (M2.IsAlmost(0.0))
     139        if (yVarianceCalculator.Variance.IsAlmost(0.0))
    151140          beta = 1;
    152141        else
    153           beta = Cn / M2; // omit division by cnt
     142          beta = ytCovarianceEvaluator.Covariance / yVarianceCalculator.Variance;
    154143
    155         alpha = tMean - beta * yMean;
     144        alpha = tMeanCalculator.Mean - beta * yVarianceCalculator.Mean;
    156145      }
    157146    }
Note: See TracChangeset for help on using the changeset viewer.