Index: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionScaledMeanSquaredErrorEvaluator.cs
===================================================================
--- trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionScaledMeanSquaredErrorEvaluator.cs	(revision 3999)
+++ trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionScaledMeanSquaredErrorEvaluator.cs	(revision 4001)
@@ -116,8 +116,8 @@
 
       int cnt = 0;
-      double tSum = 0;
-      double ySum = 0;
-      double yySum = 0;
-      double ytSum = 0;
+      double tMean = 0;
+      double yMean = 0;
+      double Cn = 0;
+      double M2 = 0;
 
       while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
@@ -126,8 +126,14 @@
         if (IsValidValue(t) && IsValidValue(y)) {
           cnt++;
-          tSum += t;
-          ySum += y;
-          yySum += y * y;
-          ytSum += t * y;
+          // online calculation of tMean
+          tMean = tMean + (t - tMean) / cnt;
+          double delta = y - yMean; // delta = (y - yMean(n-1))
+          yMean = yMean + delta / cnt;
+          
+          // online calculation of variance 
+          M2 = M2 + delta * (y - yMean); // M2(n) = M2(n-1) + (y - yMean(n-1)) (y - yMean(n))
+
+          // online calculation of covariance
+          Cn = Cn + delta * (t - tMean); // C(n) = C(n-1) + (y - yMean(n-1)) (t - tMean(n)) 
         }
       }
@@ -139,14 +145,11 @@
         beta = 1;
       } else {
-        double tMean = tSum / cnt;
-        double yMean = ySum / cnt;
-        //division by cnt is omited because the variance and covariance are divided afterwards.
-        double yVariance = yySum - 2 * yMean * ySum + cnt * yMean * yMean;
-        double ytCovariance = ytSum - tMean * ySum - yMean * tSum + cnt * yMean * tMean;
+        // yVariance = M2 / cnt;
+        // ytCovariance = Cn / cnt;
 
-        if (yVariance.IsAlmost(0.0))
+        if (M2.IsAlmost(0.0))
           beta = 1;
         else
-          beta = ytCovariance / yVariance;
+          beta = Cn / M2; // omit division by cnt
 
         alpha = tMean - beta * yMean;
Index: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/LinearScalingTest.cs
===================================================================
--- trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/LinearScalingTest.cs	(revision 4001)
+++ trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/LinearScalingTest.cs	(revision 4001)
@@ -0,0 +1,91 @@
+#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System.IO;
+using System;
+using HeuristicLab.Random;
+using HeuristicLab.Common;
+using System.Collections.Generic;
+using System.Diagnostics;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+using HeuristicLab.Problems.DataAnalysis.Symbolic;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System.Linq;
+using System.Globalization;
+using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
+namespace HeuristicLab.Problems.DataAnalysis.Tests {
+
+  [TestClass()]
+  public class LinearScalingTest {
+    [TestMethod()]
+    public void CalculateScalingParametersTest() {
+      var testData = new double[,] {
+     {5,1,1,1,2,1,3,1,1,2},
+     {5,4,4,5,7,10,3,2,1,2},
+     {3,1,1,1,2,2,3,1,1,2},
+     {6,8,8,1,3,4,3,7,1,2},
+     {4,1,1,3,2,1,3,1,1,2},
+     {8,10,10,8,7,10,9,7,1,4},            
+     {1,1,1,1,2,10,3,1,1,2},              
+     {2,1,2,1,2,1,3,1,1,2},                 
+     {2,1,1,1,2,1,1,1,5,2},                 
+     {4,2,1,1,2,1,2,1,1,2},                   
+     {1,1,1,1,1,1,3,1,1,2},    
+     {2,1,1,1,2,1,2,1,1,2},                   
+     {5,3,3,3,2,3,4,4,1,4},                          
+     {8,7,5,10,7,9,5,5,4,4},          
+     {7,4,6,4,6,1,4,3,1,4},                          
+     {4,1,1,1,2,1,2,1,1,2},     
+     {4,1,1,1,2,1,3,1,1,2},      
+     {10,7,7,6,4,10,4,1,2,4},  
+     {6,1,1,1,2,1,3,1,1,2},     
+     {7,3,2,10,5,10,5,4,4,4},   
+     {10,5,5,3,6,7,7,10,1,4} 
+      };
+
+      double alpha, beta;
+      int n = testData.GetLength(0);
+      {
+        IEnumerable<double> x = from rows in Enumerable.Range(0, n)
+                                select testData[rows, 0];
+        IEnumerable<double> y = from rows in Enumerable.Range(0, n)
+                                select testData[rows, 1];
+        SymbolicRegressionScaledMeanSquaredErrorEvaluator.CalculateScalingParameters(x, y, out beta, out alpha);
+
+        Assert.AreEqual(alpha, 2.757281, 1.0E-6);
+        Assert.AreEqual(beta, 0.720267, 1.0E-6);
+
+        IEnumerable<double> scaledY = from value in y select value * beta + alpha;
+        Assert.AreEqual(x.Average(), scaledY.Average(), 1.0E-6);
+      }
+      {
+        IEnumerable<double> x = from rows in Enumerable.Range(0, n)
+                                select testData[rows, 2] * 1.0E3;
+        IEnumerable<double> y = from rows in Enumerable.Range(0, n)
+                                select testData[rows, 8] * 1.0E-3;
+        SymbolicRegressionScaledMeanSquaredErrorEvaluator.CalculateScalingParameters(x, y, out beta, out alpha);
+
+        IEnumerable<double> scaledY = from value in y select value * beta + alpha;
+        Assert.AreEqual(x.Average(), scaledY.Average(), 1.0E-6);
+      }
+    }
+  }
+}
