[5722] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 |
|
---|
| 26 | namespace 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) {
|
---|
[5759] | 80 | // validity of values is checked in mean calculator and covariance calculator
|
---|
[5746] | 81 | targetMeanCalculator.Add(target);
|
---|
| 82 | originalMeanAndVarianceCalculator.Add(original);
|
---|
| 83 | originalTargetCovarianceEvaluator.Add(original, target);
|
---|
[5722] | 84 |
|
---|
[5746] | 85 | cnt++;
|
---|
[5722] | 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;
|
---|
[5818] | 105 | calculator.Add(originalElement, targetElement);
|
---|
[5722] | 106 | }
|
---|
| 107 |
|
---|
| 108 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
| 109 | if (originalEnumerator.MoveNext() || targetEnumerator.MoveNext()) {
|
---|
| 110 | throw new ArgumentException("Number of elements in original and target enumeration do not match.");
|
---|
| 111 | } else {
|
---|
| 112 | alpha = calculator.Alpha;
|
---|
| 113 | beta = calculator.Beta;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | }
|
---|