[5722] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5722] | 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 {
|
---|
[5894] | 34 | return targetMeanCalculator.Mean - Beta * originalMeanAndVarianceCalculator.Mean;
|
---|
[5722] | 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /// <summary>
|
---|
| 39 | /// Multiplicative factor
|
---|
| 40 | /// </summary>
|
---|
| 41 | public double Beta {
|
---|
| 42 | get {
|
---|
[5894] | 43 | if (originalMeanAndVarianceCalculator.PopulationVariance.IsAlmost(0.0))
|
---|
[5722] | 44 | return 1;
|
---|
| 45 | else
|
---|
[5942] | 46 | return originalTargetCovarianceCalculator.Covariance / originalMeanAndVarianceCalculator.PopulationVariance;
|
---|
[5722] | 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[5942] | 50 | public OnlineCalculatorError ErrorState {
|
---|
[5894] | 51 | get {
|
---|
| 52 | return targetMeanCalculator.MeanErrorState | originalMeanAndVarianceCalculator.MeanErrorState |
|
---|
[5942] | 53 | originalMeanAndVarianceCalculator.PopulationVarianceErrorState | originalTargetCovarianceCalculator.ErrorState;
|
---|
[5894] | 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[5722] | 57 | private OnlineMeanAndVarianceCalculator targetMeanCalculator;
|
---|
| 58 | private OnlineMeanAndVarianceCalculator originalMeanAndVarianceCalculator;
|
---|
[5942] | 59 | private OnlineCovarianceCalculator originalTargetCovarianceCalculator;
|
---|
[5722] | 60 |
|
---|
| 61 | public OnlineLinearScalingParameterCalculator() {
|
---|
| 62 | targetMeanCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
| 63 | originalMeanAndVarianceCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
[5942] | 64 | originalTargetCovarianceCalculator = new OnlineCovarianceCalculator();
|
---|
[5722] | 65 | Reset();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public void Reset() {
|
---|
| 69 | targetMeanCalculator.Reset();
|
---|
| 70 | originalMeanAndVarianceCalculator.Reset();
|
---|
[5942] | 71 | originalTargetCovarianceCalculator.Reset();
|
---|
[5722] | 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);
|
---|
[5942] | 83 | originalTargetCovarianceCalculator.Add(original, target);
|
---|
[5722] | 84 |
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /// <summary>
|
---|
| 88 | /// Calculates alpha and beta parameters to linearly scale elements of original to the scale and location of target
|
---|
| 89 | /// original[i] * beta + alpha
|
---|
| 90 | /// </summary>
|
---|
| 91 | /// <param name="original">Values that should be scaled</param>
|
---|
| 92 | /// <param name="target">Target values to which the original values should be scaled</param>
|
---|
| 93 | /// <param name="alpha">Additive constant for the linear scaling</param>
|
---|
| 94 | /// <param name="beta">Multiplicative factor for the linear scaling</param>
|
---|
[5894] | 95 | /// <param name="errorState">Flag that indicates if errors occurred in the calculation of the linea scaling parameters.</param>
|
---|
[5942] | 96 | public static void Calculate(IEnumerable<double> original, IEnumerable<double> target, out double alpha, out double beta, out OnlineCalculatorError errorState) {
|
---|
[5722] | 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);
|
---|
[5945] | 106 | if (calculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
[5722] | 107 | }
|
---|
| 108 |
|
---|
| 109 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
[5945] | 110 | if (calculator.ErrorState == OnlineCalculatorError.None &&
|
---|
| 111 | (originalEnumerator.MoveNext() || targetEnumerator.MoveNext())) {
|
---|
[5722] | 112 | throw new ArgumentException("Number of elements in original and target enumeration do not match.");
|
---|
| 113 | } else {
|
---|
[5894] | 114 | errorState = calculator.ErrorState;
|
---|
[5722] | 115 | alpha = calculator.Alpha;
|
---|
| 116 | beta = calculator.Beta;
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|