#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 . */ #endregion using System; using HeuristicLab.Common; namespace HeuristicLab.Problems.DataAnalysis.Evaluators { /// /// 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 class OnlineLinearScalingCalculator { private int n; private OnlineMeanAndVarianceCalculator yVarianceCalculator; private OnlineMeanAndVarianceCalculator tMeanCalculator; private OnlineCovarianceEvaluator ytCovarianceEvaluator; public double Alpha { get { if (n < 2) { return 0.0; } else { return tMeanCalculator.Mean - Beta * yVarianceCalculator.Mean; } } } public double Beta { get { if (n < 2 || yVarianceCalculator.PopulationVariance.IsAlmost(0.0)) { return 1; } else { return ytCovarianceEvaluator.Covariance / yVarianceCalculator.PopulationVariance; } } } public OnlineLinearScalingCalculator() { Reset(); } public void Reset() { n = 0; yVarianceCalculator = new OnlineMeanAndVarianceCalculator(); tMeanCalculator = new OnlineMeanAndVarianceCalculator(); ytCovarianceEvaluator = new OnlineCovarianceEvaluator(); } public void Add(double target, double x) { if (double.IsNaN(target) || double.IsInfinity(target) || double.IsNaN(x) || double.IsInfinity(x)) throw new ArgumentException("Linear scaling is not defined for series containing NaN or infinity elements."); else { tMeanCalculator.Add(target); yVarianceCalculator.Add(x); ytCovarianceEvaluator.Add(x, target); n++; } } } }