Free cookie consent management tool by TermsFeed Policy Generator

Changeset 4001


Ignore:
Timestamp:
07/06/10 15:07:21 (14 years ago)
Author:
gkronber
Message:

Changed calculation of linear scaling parameters to a more numerically stable solution. #1074

Location:
trunk/sources
Files:
1 added
1 edited

Legend:

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

    r3996 r4001  
    116116
    117117      int cnt = 0;
    118       double tSum = 0;
    119       double ySum = 0;
    120       double yySum = 0;
    121       double ytSum = 0;
     118      double tMean = 0;
     119      double yMean = 0;
     120      double Cn = 0;
     121      double M2 = 0;
    122122
    123123      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
     
    126126        if (IsValidValue(t) && IsValidValue(y)) {
    127127          cnt++;
    128           tSum += t;
    129           ySum += y;
    130           yySum += y * y;
    131           ytSum += t * y;
     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))
    132138        }
    133139      }
     
    139145        beta = 1;
    140146      } else {
    141         double tMean = tSum / cnt;
    142         double yMean = ySum / cnt;
    143         //division by cnt is omited because the variance and covariance are divided afterwards.
    144         double yVariance = yySum - 2 * yMean * ySum + cnt * yMean * yMean;
    145         double ytCovariance = ytSum - tMean * ySum - yMean * tSum + cnt * yMean * tMean;
     147        // yVariance = M2 / cnt;
     148        // ytCovariance = Cn / cnt;
    146149
    147         if (yVariance.IsAlmost(0.0))
     150        if (M2.IsAlmost(0.0))
    148151          beta = 1;
    149152        else
    150           beta = ytCovariance / yVariance;
     153          beta = Cn / M2; // omit division by cnt
    151154
    152155        alpha = tMean - beta * yMean;
Note: See TracChangeset for help on using the changeset viewer.