Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/OnlineEvaluators/OnlineLinearScalingParameterCalculator.cs @ 5722

Last change on this file since 5722 was 5722, checked in by gkronber, 13 years ago

#1418 fixed evaluator call from validation analyzers, fixed bugs in interactive simplifier view and added apply linear scaling flag to analyzers.

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25
26namespace HeuristicLab.Problems.DataAnalysis {
27  public class OnlineLinearScalingParameterCalculator {
28
29    /// <summary>
30    /// Additive constant
31    /// </summary>
32    public double Alpha {
33      get {
34        if (cnt < 2)
35          return 0;
36        else
37          return targetMeanCalculator.Mean - Beta * originalMeanAndVarianceCalculator.Mean;
38      }
39    }
40
41    /// <summary>
42    /// Multiplicative factor
43    /// </summary>
44    public double Beta {
45      get {
46        if (cnt < 2)
47          return 1;
48        else if (originalMeanAndVarianceCalculator.PopulationVariance.IsAlmost(0.0))
49          return 1;
50        else
51          return originalTargetCovarianceEvaluator.Covariance / originalMeanAndVarianceCalculator.PopulationVariance;
52      }
53    }
54
55    private int cnt;
56    private OnlineMeanAndVarianceCalculator targetMeanCalculator;
57    private OnlineMeanAndVarianceCalculator originalMeanAndVarianceCalculator;
58    private OnlineCovarianceEvaluator originalTargetCovarianceEvaluator;
59
60    public OnlineLinearScalingParameterCalculator() {
61      targetMeanCalculator = new OnlineMeanAndVarianceCalculator();
62      originalMeanAndVarianceCalculator = new OnlineMeanAndVarianceCalculator();
63      originalTargetCovarianceEvaluator = new OnlineCovarianceEvaluator();
64      Reset();
65    }
66
67    public void Reset() {
68      cnt = 0;
69      targetMeanCalculator.Reset();
70      originalMeanAndVarianceCalculator.Reset();
71      originalTargetCovarianceEvaluator.Reset();
72    }
73
74    /// <summary>
75    /// Calculates linear scaling parameters in one pass.
76    /// The formulas to calculate the scaling parameters were taken from Scaled Symblic Regression by Maarten Keijzer.
77    /// http://www.springerlink.com/content/x035121165125175/
78    /// </summary>
79    public void Add(double original, double target) {
80      if (IsValidValue(original) && IsValidValue(target)) {
81        targetMeanCalculator.Add(target);
82        originalMeanAndVarianceCalculator.Add(original);
83        originalTargetCovarianceEvaluator.Add(original, target);
84
85        cnt++;
86      }
87    }
88
89    private static bool IsValidValue(double d) {
90      return !double.IsInfinity(d) && !double.IsNaN(d) && d > -1.0E07 && d < 1.0E07;  // don't consider very large or very small values for scaling
91    }
92
93
94    /// <summary>
95    /// Calculates alpha and beta parameters to linearly scale elements of original to the scale and location of target
96    /// original[i] * beta + alpha
97    /// </summary>
98    /// <param name="original">Values that should be scaled</param>
99    /// <param name="target">Target values to which the original values should be scaled</param>
100    /// <param name="alpha">Additive constant for the linear scaling</param>
101    /// <param name="beta">Multiplicative factor for the linear scaling</param>
102    public static void Calculate(IEnumerable<double> original, IEnumerable<double> target, out double alpha, out double beta) {
103      OnlineLinearScalingParameterCalculator calculator = new OnlineLinearScalingParameterCalculator();
104      IEnumerator<double> originalEnumerator = original.GetEnumerator();
105      IEnumerator<double> targetEnumerator = target.GetEnumerator();
106
107      // always move forward both enumerators (do not use short-circuit evaluation!)
108      while (originalEnumerator.MoveNext() & targetEnumerator.MoveNext()) {
109        double originalElement = originalEnumerator.Current;
110        double targetElement = targetEnumerator.Current;
111        calculator.Add(originalElement, targetElement);
112      }
113
114      // check if both enumerators are at the end to make sure both enumerations have the same length
115      if (originalEnumerator.MoveNext() || targetEnumerator.MoveNext()) {
116        throw new ArgumentException("Number of elements in original and target enumeration do not match.");
117      } else {
118        alpha = calculator.Alpha;
119        beta = calculator.Beta;
120      }
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.