[4555] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 |
|
---|
| 25 | namespace HeuristicLab.Problems.DataAnalysis.Evaluators {
|
---|
| 26 | /// <summary>
|
---|
| 27 | /// Calculates linear scaling parameters in one pass.
|
---|
| 28 | /// The formulas to calculate the scaling parameters were taken from Scaled Symblic Regression by Maarten Keijzer.
|
---|
| 29 | /// http://www.springerlink.com/content/x035121165125175/
|
---|
| 30 | /// </summary>
|
---|
| 31 | public class OnlineLinearScalingCalculator {
|
---|
| 32 | private int n;
|
---|
| 33 | private OnlineMeanAndVarianceCalculator yVarianceCalculator;
|
---|
| 34 | private OnlineMeanAndVarianceCalculator tMeanCalculator;
|
---|
| 35 | private OnlineCovarianceEvaluator ytCovarianceEvaluator;
|
---|
| 36 |
|
---|
| 37 | public double Alpha {
|
---|
| 38 | get {
|
---|
| 39 | if (n < 2) {
|
---|
| 40 | return 0.0;
|
---|
| 41 | } else {
|
---|
| 42 | return tMeanCalculator.Mean - Beta * yVarianceCalculator.Mean;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public double Beta {
|
---|
| 48 | get {
|
---|
| 49 | if (n < 2 || yVarianceCalculator.PopulationVariance.IsAlmost(0.0)) {
|
---|
| 50 | return 1;
|
---|
| 51 | } else {
|
---|
| 52 | return ytCovarianceEvaluator.Covariance / yVarianceCalculator.PopulationVariance;
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public OnlineLinearScalingCalculator() {
|
---|
| 58 | Reset();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public void Reset() {
|
---|
| 62 | n = 0;
|
---|
| 63 | yVarianceCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
| 64 | tMeanCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
| 65 | ytCovarianceEvaluator = new OnlineCovarianceEvaluator();
|
---|
| 66 |
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public void Add(double target, double x) {
|
---|
| 70 | if (double.IsNaN(target) || double.IsInfinity(target) || double.IsNaN(x) || double.IsInfinity(x))
|
---|
| 71 | throw new ArgumentException("Linear scaling is not defined for series containing NaN or infinity elements.");
|
---|
| 72 | else {
|
---|
| 73 | tMeanCalculator.Add(target);
|
---|
| 74 | yVarianceCalculator.Add(x);
|
---|
| 75 | ytCovarianceEvaluator.Add(x, target);
|
---|
| 76 |
|
---|
| 77 | n++;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|