Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineLinearScalingParameterCalculator.cs @ 14294

Last change on this file since 14294 was 14294, checked in by bburlacu, 8 years ago

#2672: Add cloning constructors.

File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 : IDeepCloneable {
28
29    /// <summary>
30    /// Additive constant
31    /// </summary>
32    public double Alpha {
33      get {
34        return targetMeanCalculator.Mean - Beta * originalMeanAndVarianceCalculator.Mean;
35      }
36    }
37
38    /// <summary>
39    /// Multiplicative factor
40    /// </summary>
41    public double Beta {
42      get {
43        if (originalMeanAndVarianceCalculator.PopulationVariance.IsAlmost(0.0))
44          return 1;
45        else
46          return originalTargetCovarianceCalculator.Covariance / originalMeanAndVarianceCalculator.PopulationVariance;
47      }
48    }
49
50    public OnlineCalculatorError ErrorState {
51      get {
52        return targetMeanCalculator.MeanErrorState | originalMeanAndVarianceCalculator.MeanErrorState |
53          originalMeanAndVarianceCalculator.PopulationVarianceErrorState | originalTargetCovarianceCalculator.ErrorState;
54      }
55    }
56
57    private OnlineMeanAndVarianceCalculator targetMeanCalculator;
58    private OnlineMeanAndVarianceCalculator originalMeanAndVarianceCalculator;
59    private OnlineCovarianceCalculator originalTargetCovarianceCalculator;
60
61    public OnlineLinearScalingParameterCalculator() {
62      targetMeanCalculator = new OnlineMeanAndVarianceCalculator();
63      originalMeanAndVarianceCalculator = new OnlineMeanAndVarianceCalculator();
64      originalTargetCovarianceCalculator = new OnlineCovarianceCalculator();
65      Reset();
66    }
67
68    protected OnlineLinearScalingParameterCalculator(OnlineLinearScalingParameterCalculator other, Cloner cloner) {
69      targetMeanCalculator = (OnlineMeanAndVarianceCalculator)other.targetMeanCalculator.Clone(cloner);
70      originalMeanAndVarianceCalculator = (OnlineMeanAndVarianceCalculator)other.originalMeanAndVarianceCalculator.Clone(cloner);
71      originalTargetCovarianceCalculator = (OnlineCovarianceCalculator)other.originalTargetCovarianceCalculator.Clone(cloner);
72      // do not reset the calculators here
73    }
74
75
76    public void Reset() {
77      targetMeanCalculator.Reset();
78      originalMeanAndVarianceCalculator.Reset();
79      originalTargetCovarianceCalculator.Reset();
80    }
81
82    /// <summary>
83    /// Calculates linear scaling parameters in one pass.
84    /// The formulas to calculate the scaling parameters were taken from Scaled Symblic Regression by Maarten Keijzer.
85    /// http://www.springerlink.com/content/x035121165125175/
86    /// </summary>
87    public void Add(double original, double target) {
88      // validity of values is checked in mean calculator and covariance calculator
89      targetMeanCalculator.Add(target);
90      originalMeanAndVarianceCalculator.Add(original);
91      originalTargetCovarianceCalculator.Add(original, target);
92
93    }
94
95    /// <summary>
96    /// Calculates alpha and beta parameters to linearly scale elements of original to the scale and location of target
97    /// original[i] * beta + alpha
98    /// </summary>
99    /// <param name="original">Values that should be scaled</param>
100    /// <param name="target">Target values to which the original values should be scaled</param>
101    /// <param name="alpha">Additive constant for the linear scaling</param>
102    /// <param name="beta">Multiplicative factor for the linear scaling</param>
103    /// <param name="errorState">Flag that indicates if errors occurred in the calculation of the linea scaling parameters.</param>
104    public static void Calculate(IEnumerable<double> original, IEnumerable<double> target, out double alpha, out double beta, out OnlineCalculatorError errorState) {
105      OnlineLinearScalingParameterCalculator calculator = new OnlineLinearScalingParameterCalculator();
106      IEnumerator<double> originalEnumerator = original.GetEnumerator();
107      IEnumerator<double> targetEnumerator = target.GetEnumerator();
108
109      // always move forward both enumerators (do not use short-circuit evaluation!)
110      while (originalEnumerator.MoveNext() & targetEnumerator.MoveNext()) {
111        double originalElement = originalEnumerator.Current;
112        double targetElement = targetEnumerator.Current;
113        calculator.Add(originalElement, targetElement);
114        if (calculator.ErrorState != OnlineCalculatorError.None) break;
115      }
116
117      // check if both enumerators are at the end to make sure both enumerations have the same length
118      if (calculator.ErrorState == OnlineCalculatorError.None &&
119            (originalEnumerator.MoveNext() || targetEnumerator.MoveNext())) {
120        throw new ArgumentException("Number of elements in original and target enumeration do not match.");
121      } else {
122        errorState = calculator.ErrorState;
123        alpha = calculator.Alpha;
124        beta = calculator.Beta;
125      }
126    }
127
128    // IDeepCloneable interface members
129    public object Clone() {
130      var cloner = new Cloner();
131      return new OnlineLinearScalingParameterCalculator(this, cloner);
132    }
133
134    public IDeepCloneable Clone(Cloner cloner) {
135      var clone = cloner.GetClone(this);
136      if (clone == null) {
137        clone = new OnlineLinearScalingParameterCalculator(this, cloner);
138        cloner.RegisterClonedObject(this, clone);
139      }
140      return clone;
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.