Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineEvaluators/OnlineLinearScalingParameterCalculator.cs @ 5809

Last change on this file since 5809 was 5809, checked in by mkommend, 13 years ago

#1418: Reintegrated branch into trunk.

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      // validity of values is checked in mean calculator and covariance calculator
81      targetMeanCalculator.Add(target);
82      originalMeanAndVarianceCalculator.Add(original);
83      originalTargetCovarianceEvaluator.Add(original, target);
84
85      cnt++;
86    }
87
88    /// <summary>
89    /// Calculates alpha and beta parameters to linearly scale elements of original to the scale and location of target
90    /// original[i] * beta + alpha
91    /// </summary>
92    /// <param name="original">Values that should be scaled</param>
93    /// <param name="target">Target values to which the original values should be scaled</param>
94    /// <param name="alpha">Additive constant for the linear scaling</param>
95    /// <param name="beta">Multiplicative factor for the linear scaling</param>
96    public static void Calculate(IEnumerable<double> original, IEnumerable<double> target, out double alpha, out double beta) {
97      OnlineLinearScalingParameterCalculator calculator = new OnlineLinearScalingParameterCalculator();
98      IEnumerator<double> originalEnumerator = original.GetEnumerator();
99      IEnumerator<double> targetEnumerator = target.GetEnumerator();
100
101      // always move forward both enumerators (do not use short-circuit evaluation!)
102      while (originalEnumerator.MoveNext() & targetEnumerator.MoveNext()) {
103        double originalElement = originalEnumerator.Current;
104        double targetElement = targetEnumerator.Current;
105        // don't consider very large or very small values for scaling
106        // careful: this also excludes infinity and NaN values
107        if (originalElement > -1.0E07 && originalElement < 1.0E07) {
108          calculator.Add(originalElement, targetElement);
109        }
110      }
111
112      // check if both enumerators are at the end to make sure both enumerations have the same length
113      if (originalEnumerator.MoveNext() || targetEnumerator.MoveNext()) {
114        throw new ArgumentException("Number of elements in original and target enumeration do not match.");
115      } else {
116        alpha = calculator.Alpha;
117        beta = calculator.Beta;
118      }
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.