Index: /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Evaluators/OnlineMeanAndVarianceCalculator.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Evaluators/OnlineMeanAndVarianceCalculator.cs	(revision 4121)
+++ /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Evaluators/OnlineMeanAndVarianceCalculator.cs	(revision 4122)
@@ -31,4 +31,10 @@
       get {
         return (n > 1) ? m_newS / (n - 1) : 0.0;
+      }
+    }
+
+    public double PopulationVariance {
+      get {
+        return (n > 0) ? m_newS / n : 0.0;
       }
     }
Index: /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Evaluators/OnlinePearsonsRSquaredEvaluator.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Evaluators/OnlinePearsonsRSquaredEvaluator.cs	(revision 4121)
+++ /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Evaluators/OnlinePearsonsRSquaredEvaluator.cs	(revision 4122)
@@ -25,28 +25,12 @@
 namespace HeuristicLab.Problems.DataAnalysis.Evaluators {
   public class OnlinePearsonsRSquaredEvaluator : IOnlineEvaluator {
-
-    private double sum_sq_x;
-    private double sum_sq_y;
-    private double sum_coproduct;
-    private double mean_x;
-    private double mean_y;
-    private int n;
+    private OnlineCovarianceEvaluator covEvaluator = new OnlineCovarianceEvaluator();
+    private OnlineMeanAndVarianceCalculator sxEvaluator = new OnlineMeanAndVarianceCalculator();
+    private OnlineMeanAndVarianceCalculator syEvaluator = new OnlineMeanAndVarianceCalculator();
 
     public double RSquared {
       get {
-        if (n < 1)
-          throw new InvalidOperationException("No elements");
-        else {
-          double pop_sd_x = Math.Sqrt(sum_sq_x / n);
-          double pop_sd_y = Math.Sqrt(sum_sq_y / n);
-          double cov_x_y = sum_coproduct / n;
-
-          if (pop_sd_x.IsAlmost(0.0) || pop_sd_y.IsAlmost(0.0))
-            return 0.0;
-          else {
-            double r = cov_x_y / (pop_sd_x * pop_sd_y);
-            return r * r;
-          }
-        }
+        double r = covEvaluator.Covariance / (Math.Sqrt(sxEvaluator.PopulationVariance) * Math.Sqrt(syEvaluator.PopulationVariance));
+        return r * r;
       }
     }
@@ -59,32 +43,16 @@
     }
     public void Reset() {
-      sum_sq_x = 0.0;
-      sum_sq_y = 0.0;
-      sum_coproduct = 0.0;
-      mean_x = 0.0;
-      mean_y = 0.0;
-      n = 0;
+      covEvaluator.Reset();
+      sxEvaluator.Reset();
+      syEvaluator.Reset();
     }
 
-    public void Add(double original, double estimated) {
-      // stable and iterative calculation of R²
-      if (IsInvalidValue(original) || IsInvalidValue(estimated)) {
+    public void Add(double x, double y) {
+      if (IsInvalidValue(x) || IsInvalidValue(y)) {
         throw new ArgumentException("R² is not defined for variables with NaN or infinity values.");
       }
-      if (n == 0) {
-        mean_x = original;
-        mean_y = estimated;
-        n = 1;
-      } else {
-        double sweep = (n - 1.0) / n;
-        double delta_x = original - mean_x;
-        double delta_y = estimated - mean_y;
-        sum_sq_x += delta_x * delta_x * sweep;
-        sum_sq_y += delta_y * delta_y * sweep;
-        sum_coproduct += delta_x * delta_y * sweep;
-        mean_x += delta_x / n;
-        mean_y += delta_y / n;
-        n++;
-      }
+      covEvaluator.Add(x, y);
+      sxEvaluator.Add(x);
+      syEvaluator.Add(y);
     }
 
Index: /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/HeuristicLab.Problems.DataAnalysis.Tests-3.3.csproj
===================================================================
--- /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/HeuristicLab.Problems.DataAnalysis.Tests-3.3.csproj	(revision 4121)
+++ /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/HeuristicLab.Problems.DataAnalysis.Tests-3.3.csproj	(revision 4122)
@@ -77,4 +77,5 @@
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="StatisticCalculatorsTest.cs" />
     <Compile Include="LinearScalingTest.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
@@ -104,4 +105,8 @@
       <Project>{125D3006-67F5-48CB-913E-73C0548F17FA}</Project>
       <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3</Name>
+    </ProjectReference>
+    <ProjectReference Include="..\..\..\HeuristicLab.ExtLibs\HeuristicLab.ALGLIB\2.5.0\ALGLIB-2.5.0\ALGLIB-2.5.0.csproj">
+      <Project>{29E4B033-1FEF-4FE1-AE17-0A9319D7C54E}</Project>
+      <Name>ALGLIB-2.5.0</Name>
     </ProjectReference>
     <ProjectReference Include="..\..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj">
Index: /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/StatisticCalculatorsTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/StatisticCalculatorsTest.cs	(revision 4122)
+++ /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/StatisticCalculatorsTest.cs	(revision 4122)
@@ -0,0 +1,125 @@
+#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;
+using HeuristicLab.Problems.DataAnalysis.Evaluators;
+namespace HeuristicLab.Problems.DataAnalysis.Tests {
+
+  [TestClass()]
+  public class StatisticCalculatorsTest {
+    private double[,] 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} 
+      };
+
+    [TestMethod()]
+    public void CalculateMeanAndVarianceTest() {
+      System.Random random = new System.Random(31415);
+
+      int n = testData.GetLength(0);
+      int cols = testData.GetLength(1);
+      {
+        for (int col = 0; col < cols; col++) {
+          double scale = random.NextDouble() * 1E7;
+          IEnumerable<double> x = from rows in Enumerable.Range(0, n)
+                                  select testData[rows, col] * scale;
+          double[] xs = x.ToArray();
+          double mean_alglib, variance_alglib;
+          mean_alglib = variance_alglib = 0.0;
+          double tmp = 0;
+
+          alglib.descriptivestatistics.calculatemoments(ref xs, n, ref mean_alglib, ref variance_alglib, ref tmp, ref tmp);
+
+          var calculator = new OnlineMeanAndVarianceCalculator();
+          for (int i = 0; i < n; i++) {
+            calculator.Add(xs[i]);
+          }
+          double mean = calculator.Mean;
+          double variance = calculator.Variance;
+
+          Assert.AreEqual(mean_alglib, mean, 1E-6 * scale);
+          Assert.AreEqual(variance_alglib, variance, 1E-6 * scale);
+        }
+      }
+    }
+
+    [TestMethod()]
+    public void CalculatePearsonsRSquaredTest() {
+      System.Random random = new System.Random(31415);
+      int n = testData.GetLength(0);
+      int cols = testData.GetLength(1);
+      for (int c1 = 0; c1 < cols; c1++) {
+        for (int c2 = c1 + 1; c2 < cols; c2++) {
+          {
+            double c1Scale = random.NextDouble() * 1E7;
+            double c2Scale = random.NextDouble() * 1E7;
+            IEnumerable<double> x = from rows in Enumerable.Range(0, n)
+                                    select testData[rows, c1] * c1Scale;
+            IEnumerable<double> y = from rows in Enumerable.Range(0, n)
+                                    select testData[rows, c2] * c2Scale;
+            double[] xs = x.ToArray();
+            double[] ys = y.ToArray();
+            double r2_alglib = alglib.correlation.pearsoncorrelation(ref xs, ref ys, n);
+            r2_alglib *= r2_alglib;
+
+            var r2Calculator = new OnlinePearsonsRSquaredEvaluator();
+            for (int i = 0; i < n; i++) {
+              r2Calculator.Add(xs[i], ys[i]);
+            }
+            double r2 = r2Calculator.RSquared;
+
+            Assert.AreEqual(r2_alglib, r2, 1E-6 * Math.Max(c1Scale, c2Scale));
+          }
+        }
+      }
+    }
+  }
+}
