Index: /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/HeuristicLab.Problems.DataAnalysis-3.4.csproj
===================================================================
--- /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/HeuristicLab.Problems.DataAnalysis-3.4.csproj (revision 5942)
+++ /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/HeuristicLab.Problems.DataAnalysis-3.4.csproj (revision 5943)
@@ -119,5 +119,5 @@
-
+
@@ -130,5 +130,5 @@
-
+
@@ -147,10 +147,10 @@
-
-
-
-
-
-
+
+
+
+
+
+
Index: /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineAccuracyCalculator.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineAccuracyCalculator.cs (revision 5943)
+++ /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineAccuracyCalculator.cs (revision 5943)
@@ -0,0 +1,91 @@
+#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2011 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 .
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+using HeuristicLab.Common;
+
+namespace HeuristicLab.Problems.DataAnalysis {
+ public class OnlineAccuracyCalculator : IOnlineCalculator {
+
+ private int correctlyClassified;
+ private int n;
+ public double Accuracy {
+ get {
+ return correctlyClassified / (double)n;
+ }
+ }
+
+ public OnlineAccuracyCalculator() {
+ Reset();
+ }
+
+ #region IOnlineCalculator Members
+ private OnlineCalculatorError errorState;
+ public OnlineCalculatorError ErrorState {
+ get { return errorState; }
+ }
+ public double Value {
+ get { return Accuracy; }
+ }
+ public void Reset() {
+ n = 0;
+ correctlyClassified = 0;
+ errorState = OnlineCalculatorError.InsufficientElementsAdded;
+ }
+
+ public void Add(double original, double estimated) {
+ // ignore cases where original is NaN completly
+ if (!double.IsNaN(original)) {
+ // increment number of observed samples
+ n++;
+ if (original.IsAlmost(estimated)) {
+ // original = estimated = +Inf counts as correctly classified
+ // original = estimated = -Inf counts as correctly classified
+ correctlyClassified++;
+ }
+ errorState = OnlineCalculatorError.None; // number of (non-NaN) samples >= 1
+ }
+ }
+ #endregion
+
+ public static double Calculate(IEnumerable first, IEnumerable second, out OnlineCalculatorError errorState) {
+ IEnumerator firstEnumerator = first.GetEnumerator();
+ IEnumerator secondEnumerator = second.GetEnumerator();
+ OnlineAccuracyCalculator accuracyCalculator = new OnlineAccuracyCalculator();
+
+ // always move forward both enumerators (do not use short-circuit evaluation!)
+ while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
+ double estimated = secondEnumerator.Current;
+ double original = firstEnumerator.Current;
+ accuracyCalculator.Add(original, estimated);
+ }
+
+ // check if both enumerators are at the end to make sure both enumerations have the same length
+ if (secondEnumerator.MoveNext() || firstEnumerator.MoveNext()) {
+ throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
+ } else {
+ errorState = accuracyCalculator.ErrorState;
+ return accuracyCalculator.Accuracy;
+ }
+ }
+ }
+}
Index: /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineCovarianceCalcualtor.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineCovarianceCalcualtor.cs (revision 5943)
+++ /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineCovarianceCalcualtor.cs (revision 5943)
@@ -0,0 +1,95 @@
+#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2011 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 .
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+
+namespace HeuristicLab.Problems.DataAnalysis {
+ public class OnlineCovarianceCalculator : IOnlineCalculator {
+
+ private double originalMean, estimatedMean, Cn;
+ private int n;
+ public double Covariance {
+ get {
+ return n > 0 ? Cn / n : 0.0;
+ }
+ }
+
+ public OnlineCovarianceCalculator() {
+ Reset();
+ }
+
+ #region IOnlineCalculator Members
+ private OnlineCalculatorError errorState;
+ public OnlineCalculatorError ErrorState {
+ get { return errorState; }
+ }
+ public double Value {
+ get { return Covariance; }
+ }
+ public void Reset() {
+ n = 0;
+ Cn = 0.0;
+ originalMean = 0.0;
+ estimatedMean = 0.0;
+ errorState = OnlineCalculatorError.InsufficientElementsAdded;
+ }
+
+ public void Add(double original, double estimated) {
+ if (double.IsNaN(estimated) || double.IsInfinity(estimated) || double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
+ errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
+ } else {
+ n++;
+ errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
+
+ // online calculation of tMean
+ originalMean = originalMean + (original - originalMean) / n;
+ double delta = estimated - estimatedMean; // delta = (y - yMean(n-1))
+ estimatedMean = estimatedMean + delta / n;
+
+ // online calculation of covariance
+ Cn = Cn + delta * (original - originalMean); // C(n) = C(n-1) + (y - yMean(n-1)) (t - tMean(n))
+ }
+ }
+ #endregion
+
+ public static double Calculate(IEnumerable first, IEnumerable second, out OnlineCalculatorError errorState) {
+ IEnumerator firstEnumerator = first.GetEnumerator();
+ IEnumerator secondEnumerator = second.GetEnumerator();
+ OnlineCovarianceCalculator covarianceCalculator = new OnlineCovarianceCalculator();
+
+ // always move forward both enumerators (do not use short-circuit evaluation!)
+ while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
+ double estimated = secondEnumerator.Current;
+ double original = firstEnumerator.Current;
+ covarianceCalculator.Add(original, estimated);
+ }
+
+ // check if both enumerators are at the end to make sure both enumerations have the same length
+ if (secondEnumerator.MoveNext() || firstEnumerator.MoveNext()) {
+ throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
+ } else {
+ errorState = covarianceCalculator.ErrorState;
+ return covarianceCalculator.Covariance;
+ }
+ }
+ }
+}
Index: /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineLinearScalingParameterCalculator.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineLinearScalingParameterCalculator.cs (revision 5943)
+++ /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineLinearScalingParameterCalculator.cs (revision 5943)
@@ -0,0 +1,121 @@
+#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2011 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 .
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+using HeuristicLab.Common;
+
+namespace HeuristicLab.Problems.DataAnalysis {
+ public class OnlineLinearScalingParameterCalculator {
+
+ ///
+ /// Additive constant
+ ///
+ public double Alpha {
+ get {
+ return targetMeanCalculator.Mean - Beta * originalMeanAndVarianceCalculator.Mean;
+ }
+ }
+
+ ///
+ /// Multiplicative factor
+ ///
+ public double Beta {
+ get {
+ if (originalMeanAndVarianceCalculator.PopulationVariance.IsAlmost(0.0))
+ return 1;
+ else
+ return originalTargetCovarianceCalculator.Covariance / originalMeanAndVarianceCalculator.PopulationVariance;
+ }
+ }
+
+ public OnlineCalculatorError ErrorState {
+ get {
+ return targetMeanCalculator.MeanErrorState | originalMeanAndVarianceCalculator.MeanErrorState |
+ originalMeanAndVarianceCalculator.PopulationVarianceErrorState | originalTargetCovarianceCalculator.ErrorState;
+ }
+ }
+
+ private int cnt;
+ private OnlineMeanAndVarianceCalculator targetMeanCalculator;
+ private OnlineMeanAndVarianceCalculator originalMeanAndVarianceCalculator;
+ private OnlineCovarianceCalculator originalTargetCovarianceCalculator;
+
+ public OnlineLinearScalingParameterCalculator() {
+ targetMeanCalculator = new OnlineMeanAndVarianceCalculator();
+ originalMeanAndVarianceCalculator = new OnlineMeanAndVarianceCalculator();
+ originalTargetCovarianceCalculator = new OnlineCovarianceCalculator();
+ Reset();
+ }
+
+ public void Reset() {
+ cnt = 0;
+ targetMeanCalculator.Reset();
+ originalMeanAndVarianceCalculator.Reset();
+ originalTargetCovarianceCalculator.Reset();
+ }
+
+ ///
+ /// Calculates linear scaling parameters in one pass.
+ /// The formulas to calculate the scaling parameters were taken from Scaled Symblic Regression by Maarten Keijzer.
+ /// http://www.springerlink.com/content/x035121165125175/
+ ///
+ public void Add(double original, double target) {
+ // validity of values is checked in mean calculator and covariance calculator
+ targetMeanCalculator.Add(target);
+ originalMeanAndVarianceCalculator.Add(original);
+ originalTargetCovarianceCalculator.Add(original, target);
+
+ cnt++;
+ }
+
+ ///
+ /// Calculates alpha and beta parameters to linearly scale elements of original to the scale and location of target
+ /// original[i] * beta + alpha
+ ///
+ /// Values that should be scaled
+ /// Target values to which the original values should be scaled
+ /// Additive constant for the linear scaling
+ /// Multiplicative factor for the linear scaling
+ /// Flag that indicates if errors occurred in the calculation of the linea scaling parameters.
+ public static void Calculate(IEnumerable original, IEnumerable target, out double alpha, out double beta, out OnlineCalculatorError errorState) {
+ OnlineLinearScalingParameterCalculator calculator = new OnlineLinearScalingParameterCalculator();
+ IEnumerator originalEnumerator = original.GetEnumerator();
+ IEnumerator targetEnumerator = target.GetEnumerator();
+
+ // always move forward both enumerators (do not use short-circuit evaluation!)
+ while (originalEnumerator.MoveNext() & targetEnumerator.MoveNext()) {
+ double originalElement = originalEnumerator.Current;
+ double targetElement = targetEnumerator.Current;
+ calculator.Add(originalElement, targetElement);
+ }
+
+ // check if both enumerators are at the end to make sure both enumerations have the same length
+ if (originalEnumerator.MoveNext() || targetEnumerator.MoveNext()) {
+ throw new ArgumentException("Number of elements in original and target enumeration do not match.");
+ } else {
+ errorState = calculator.ErrorState;
+ alpha = calculator.Alpha;
+ beta = calculator.Beta;
+ }
+ }
+ }
+}
Index: /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanAbsolutePercentageErrorCalculator.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanAbsolutePercentageErrorCalculator.cs (revision 5943)
+++ /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanAbsolutePercentageErrorCalculator.cs (revision 5943)
@@ -0,0 +1,90 @@
+#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2011 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 .
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+using HeuristicLab.Common;
+
+namespace HeuristicLab.Problems.DataAnalysis {
+ public class OnlineMeanAbsolutePercentageErrorCalculator : IOnlineCalculator {
+
+ private double sre;
+ private int n;
+ public double MeanAbsolutePercentageError {
+ get {
+ return n > 0 ? sre / n : 0.0;
+ }
+ }
+
+ public OnlineMeanAbsolutePercentageErrorCalculator() {
+ Reset();
+ }
+
+ #region IOnlineCalculator Members
+ private OnlineCalculatorError errorState;
+ public OnlineCalculatorError ErrorState {
+ get { return errorState; }
+ }
+ public double Value {
+ get { return MeanAbsolutePercentageError; }
+ }
+ public void Reset() {
+ n = 0;
+ sre = 0.0;
+ errorState = OnlineCalculatorError.InsufficientElementsAdded;
+ }
+
+ public void Add(double original, double estimated) {
+ if (double.IsNaN(estimated) || double.IsInfinity(estimated) ||
+ double.IsNaN(original) || double.IsInfinity(original) ||
+ original.IsAlmost(0.0)) {
+ errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
+ } else {
+ sre += Math.Abs((estimated - original) / original);
+ n++;
+ errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
+ }
+ }
+
+ #endregion
+
+ public static double Calculate(IEnumerable first, IEnumerable second, out OnlineCalculatorError errorState) {
+ IEnumerator firstEnumerator = first.GetEnumerator();
+ IEnumerator secondEnumerator = second.GetEnumerator();
+ OnlineMeanAbsolutePercentageErrorCalculator calculator = new OnlineMeanAbsolutePercentageErrorCalculator();
+
+ // always move forward both enumerators (do not use short-circuit evaluation!)
+ while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
+ double estimated = secondEnumerator.Current;
+ double original = firstEnumerator.Current;
+ calculator.Add(original, estimated);
+ }
+
+ // check if both enumerators are at the end to make sure both enumerations have the same length
+ if (secondEnumerator.MoveNext() || firstEnumerator.MoveNext()) {
+ throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
+ } else {
+ errorState = calculator.ErrorState;
+ return calculator.MeanAbsolutePercentageError;
+ }
+ }
+ }
+}
Index: /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanAndVarianceCalculator.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanAndVarianceCalculator.cs (revision 5943)
+++ /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanAndVarianceCalculator.cs (revision 5943)
@@ -0,0 +1,109 @@
+#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2011 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 .
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+
+namespace HeuristicLab.Problems.DataAnalysis {
+ public class OnlineMeanAndVarianceCalculator {
+
+ private double m_oldM, m_newM, m_oldS, m_newS;
+ private int n;
+
+ private OnlineCalculatorError varianceErrorState;
+ public OnlineCalculatorError VarianceErrorState {
+ get { return varianceErrorState; }
+ }
+
+ public double Variance {
+ get {
+ return (n > 1) ? m_newS / (n - 1) : 0.0;
+ }
+ }
+
+ private OnlineCalculatorError errorState;
+ public OnlineCalculatorError PopulationVarianceErrorState {
+ get { return errorState; }
+ }
+ public double PopulationVariance {
+ get {
+ return (n > 0) ? m_newS / n : 0.0;
+ }
+ }
+
+ public OnlineCalculatorError MeanErrorState {
+ get { return errorState; }
+ }
+ public double Mean {
+ get {
+ return (n > 0) ? m_newM : 0.0;
+ }
+ }
+
+ public int Count {
+ get { return n; }
+ }
+
+ public OnlineMeanAndVarianceCalculator() {
+ Reset();
+ }
+
+ public void Reset() {
+ n = 0;
+ errorState = OnlineCalculatorError.InsufficientElementsAdded;
+ varianceErrorState = OnlineCalculatorError.InsufficientElementsAdded;
+ }
+
+ public void Add(double x) {
+ if (double.IsNaN(x) || double.IsInfinity(x) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
+ errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
+ varianceErrorState = errorState | OnlineCalculatorError.InvalidValueAdded;
+ } else {
+ n++;
+ // See Knuth TAOCP vol 2, 3rd edition, page 232
+ if (n == 1) {
+ m_oldM = m_newM = x;
+ m_oldS = 0.0;
+ errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
+ } else {
+ varianceErrorState = varianceErrorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 2
+ m_newM = m_oldM + (x - m_oldM) / n;
+ m_newS = m_oldS + (x - m_oldM) * (x - m_newM);
+
+ // set up for next iteration
+ m_oldM = m_newM;
+ m_oldS = m_newS;
+ }
+ }
+ }
+
+ public static void Calculate(IEnumerable x, out double mean, out double variance, out OnlineCalculatorError meanErrorState, out OnlineCalculatorError varianceErrorState) {
+ OnlineMeanAndVarianceCalculator meanAndVarianceCalculator = new OnlineMeanAndVarianceCalculator();
+ foreach (double xi in x) {
+ meanAndVarianceCalculator.Add(xi);
+ }
+ mean = meanAndVarianceCalculator.Mean;
+ variance = meanAndVarianceCalculator.Variance;
+ meanErrorState = meanAndVarianceCalculator.MeanErrorState;
+ varianceErrorState = meanAndVarianceCalculator.VarianceErrorState;
+ }
+ }
+}
Index: /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanSquaredErrorCalculator.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanSquaredErrorCalculator.cs (revision 5943)
+++ /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanSquaredErrorCalculator.cs (revision 5943)
@@ -0,0 +1,88 @@
+#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2011 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 .
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+
+namespace HeuristicLab.Problems.DataAnalysis {
+ public class OnlineMeanSquaredErrorCalculator : IOnlineCalculator {
+
+ private double sse;
+ private int n;
+ public double MeanSquaredError {
+ get {
+ return n > 0 ? sse / n : 0.0;
+ }
+ }
+
+ public OnlineMeanSquaredErrorCalculator() {
+ Reset();
+ }
+
+ #region IOnlineCalculator Members
+ private OnlineCalculatorError errorState;
+ public OnlineCalculatorError ErrorState {
+ get { return errorState; }
+ }
+ public double Value {
+ get { return MeanSquaredError; }
+ }
+ public void Reset() {
+ n = 0;
+ sse = 0.0;
+ errorState = OnlineCalculatorError.InsufficientElementsAdded;
+ }
+
+ public void Add(double original, double estimated) {
+ if (double.IsNaN(estimated) || double.IsInfinity(estimated) ||
+ double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
+ errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
+ } else {
+ double error = estimated - original;
+ sse += error * error;
+ n++;
+ errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
+ }
+ }
+ #endregion
+
+ public static double Calculate(IEnumerable first, IEnumerable second, out OnlineCalculatorError errorState) {
+ IEnumerator firstEnumerator = first.GetEnumerator();
+ IEnumerator secondEnumerator = second.GetEnumerator();
+ OnlineMeanSquaredErrorCalculator mseCalculator = new OnlineMeanSquaredErrorCalculator();
+
+ // always move forward both enumerators (do not use short-circuit evaluation!)
+ while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
+ double estimated = secondEnumerator.Current;
+ double original = firstEnumerator.Current;
+ mseCalculator.Add(original, estimated);
+ }
+
+ // check if both enumerators are at the end to make sure both enumerations have the same length
+ if (secondEnumerator.MoveNext() || firstEnumerator.MoveNext()) {
+ throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
+ } else {
+ errorState = mseCalculator.ErrorState;
+ return mseCalculator.MeanSquaredError;
+ }
+ }
+ }
+}
Index: /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineNormalizedMeanSquaredErrorCalculator.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineNormalizedMeanSquaredErrorCalculator.cs (revision 5943)
+++ /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineNormalizedMeanSquaredErrorCalculator.cs (revision 5943)
@@ -0,0 +1,86 @@
+#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2011 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 .
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+
+namespace HeuristicLab.Problems.DataAnalysis {
+ public class OnlineNormalizedMeanSquaredErrorCalculator : IOnlineCalculator {
+ private OnlineMeanAndVarianceCalculator meanSquaredErrorCalculator;
+ private OnlineMeanAndVarianceCalculator originalVarianceCalculator;
+
+ public double NormalizedMeanSquaredError {
+ get {
+ double var = originalVarianceCalculator.Variance;
+ double m = meanSquaredErrorCalculator.Mean;
+ return var > 0 ? m / var : 0.0;
+ }
+ }
+
+ public OnlineNormalizedMeanSquaredErrorCalculator() {
+ meanSquaredErrorCalculator = new OnlineMeanAndVarianceCalculator();
+ originalVarianceCalculator = new OnlineMeanAndVarianceCalculator();
+ Reset();
+ }
+
+ #region IOnlineCalculator Members
+ public OnlineCalculatorError ErrorState {
+ get { return meanSquaredErrorCalculator.MeanErrorState | originalVarianceCalculator.VarianceErrorState; }
+ }
+ public double Value {
+ get { return NormalizedMeanSquaredError; }
+ }
+
+ public void Reset() {
+ meanSquaredErrorCalculator.Reset();
+ originalVarianceCalculator.Reset();
+ }
+
+ public void Add(double original, double estimated) {
+ // no need to check for validity of values explicitly as it is checked in the meanAndVariance calculator anyway
+ double error = estimated - original;
+ meanSquaredErrorCalculator.Add(error * error);
+ originalVarianceCalculator.Add(original);
+ }
+ #endregion
+
+ public static double Calculate(IEnumerable first, IEnumerable second, out OnlineCalculatorError errorState) {
+ IEnumerator firstEnumerator = first.GetEnumerator();
+ IEnumerator secondEnumerator = second.GetEnumerator();
+ OnlineNormalizedMeanSquaredErrorCalculator normalizedMSECalculator = new OnlineNormalizedMeanSquaredErrorCalculator();
+
+ // always move forward both enumerators (do not use short-circuit evaluation!)
+ while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
+ double estimated = secondEnumerator.Current;
+ double original = firstEnumerator.Current;
+ normalizedMSECalculator.Add(original, estimated);
+ }
+
+ // check if both enumerators are at the end to make sure both enumerations have the same length
+ if (secondEnumerator.MoveNext() || firstEnumerator.MoveNext()) {
+ throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
+ } else {
+ errorState = normalizedMSECalculator.ErrorState;
+ return normalizedMSECalculator.NormalizedMeanSquaredError;
+ }
+ }
+ }
+}
Index: /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlinePearsonsRSquaredCalculator.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlinePearsonsRSquaredCalculator.cs (revision 5943)
+++ /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlinePearsonsRSquaredCalculator.cs (revision 5943)
@@ -0,0 +1,90 @@
+#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2011 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 .
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+using HeuristicLab.Common;
+
+namespace HeuristicLab.Problems.DataAnalysis {
+ public class OnlinePearsonsRSquaredCalculator : IOnlineCalculator {
+ private OnlineCovarianceCalculator covCalculator = new OnlineCovarianceCalculator();
+ private OnlineMeanAndVarianceCalculator sxCalculator = new OnlineMeanAndVarianceCalculator();
+ private OnlineMeanAndVarianceCalculator syCalculator = new OnlineMeanAndVarianceCalculator();
+
+ public double RSquared {
+ get {
+ double xVar = sxCalculator.PopulationVariance;
+ double yVar = syCalculator.PopulationVariance;
+ if (xVar.IsAlmost(0.0) || yVar.IsAlmost(0.0)) {
+ return 0.0;
+ } else {
+ double r = covCalculator.Covariance / (Math.Sqrt(xVar) * Math.Sqrt(yVar));
+ return r * r;
+ }
+ }
+ }
+
+ public OnlinePearsonsRSquaredCalculator() { }
+
+ #region IOnlineCalculator Members
+ public OnlineCalculatorError ErrorState {
+ get { return covCalculator.ErrorState | sxCalculator.PopulationVarianceErrorState | syCalculator.PopulationVarianceErrorState; }
+ }
+ public double Value {
+ get { return RSquared; }
+ }
+ public void Reset() {
+ covCalculator.Reset();
+ sxCalculator.Reset();
+ syCalculator.Reset();
+ }
+
+ public void Add(double x, double y) {
+ // no need to check validity of values explicitly here as it is checked in all three evaluators
+ covCalculator.Add(x, y);
+ sxCalculator.Add(x);
+ syCalculator.Add(y);
+ }
+
+ #endregion
+
+ public static double Calculate(IEnumerable first, IEnumerable second, out OnlineCalculatorError errorState) {
+ IEnumerator firstEnumerator = first.GetEnumerator();
+ IEnumerator secondEnumerator = second.GetEnumerator();
+ OnlinePearsonsRSquaredCalculator rSquaredCalculator = new OnlinePearsonsRSquaredCalculator();
+
+ // always move forward both enumerators (do not use short-circuit evaluation!)
+ while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
+ double estimated = secondEnumerator.Current;
+ double original = firstEnumerator.Current;
+ rSquaredCalculator.Add(original, estimated);
+ }
+
+ // check if both enumerators are at the end to make sure both enumerations have the same length
+ if (secondEnumerator.MoveNext() || firstEnumerator.MoveNext()) {
+ throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
+ } else {
+ errorState = rSquaredCalculator.ErrorState;
+ return rSquaredCalculator.RSquared;
+ }
+ }
+ }
+}