[4027] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4027] | 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;
|
---|
[5507] | 23 | using System.Collections.Generic;
|
---|
[14801] | 24 | using HeuristicLab.Common;
|
---|
[4027] | 25 |
|
---|
[5491] | 26 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[14801] | 27 | public class OnlineCovarianceCalculator : DeepCloneable, IOnlineCalculator {
|
---|
[4027] | 28 |
|
---|
[6964] | 29 | private double xMean, yMean, Cn;
|
---|
[4027] | 30 | private int n;
|
---|
| 31 | public double Covariance {
|
---|
| 32 | get {
|
---|
[5742] | 33 | return n > 0 ? Cn / n : 0.0;
|
---|
[4027] | 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[5942] | 37 | public OnlineCovarianceCalculator() {
|
---|
[4027] | 38 | Reset();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[14801] | 41 | protected OnlineCovarianceCalculator(OnlineCovarianceCalculator original, Cloner cloner)
|
---|
| 42 | : base(original, cloner) {
|
---|
| 43 | Cn = original.Cn;
|
---|
| 44 | xMean = original.xMean;
|
---|
| 45 | yMean = original.yMean;
|
---|
| 46 | n = original.n;
|
---|
| 47 | errorState = original.errorState;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 51 | return new OnlineCovarianceCalculator(this, cloner);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[5942] | 54 | #region IOnlineCalculator Members
|
---|
| 55 | private OnlineCalculatorError errorState;
|
---|
| 56 | public OnlineCalculatorError ErrorState {
|
---|
[5894] | 57 | get { return errorState; }
|
---|
| 58 | }
|
---|
[4027] | 59 | public double Value {
|
---|
| 60 | get { return Covariance; }
|
---|
| 61 | }
|
---|
| 62 | public void Reset() {
|
---|
| 63 | n = 0;
|
---|
| 64 | Cn = 0.0;
|
---|
[6964] | 65 | xMean = 0.0;
|
---|
| 66 | yMean = 0.0;
|
---|
[5942] | 67 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
[4027] | 68 | }
|
---|
| 69 |
|
---|
[6964] | 70 | public void Add(double x, double y) {
|
---|
| 71 | if (double.IsNaN(y) || double.IsInfinity(y) || double.IsNaN(x) || double.IsInfinity(x) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
|
---|
[5942] | 72 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
[5904] | 73 | } else {
|
---|
[4027] | 74 | n++;
|
---|
[5942] | 75 | errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
|
---|
[5894] | 76 |
|
---|
[4027] | 77 | // online calculation of tMean
|
---|
[6964] | 78 | xMean = xMean + (x - xMean) / n;
|
---|
| 79 | double delta = y - yMean; // delta = (y - yMean(n-1))
|
---|
| 80 | yMean = yMean + delta / n;
|
---|
[4027] | 81 |
|
---|
| 82 | // online calculation of covariance
|
---|
[6964] | 83 | Cn = Cn + delta * (x - xMean); // C(n) = C(n-1) + (y - yMean(n-1)) (t - tMean(n))
|
---|
[4027] | 84 | }
|
---|
| 85 | }
|
---|
| 86 | #endregion
|
---|
[5507] | 87 |
|
---|
[5942] | 88 | public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) {
|
---|
[5507] | 89 | IEnumerator<double> firstEnumerator = first.GetEnumerator();
|
---|
| 90 | IEnumerator<double> secondEnumerator = second.GetEnumerator();
|
---|
[5942] | 91 | OnlineCovarianceCalculator covarianceCalculator = new OnlineCovarianceCalculator();
|
---|
[5507] | 92 |
|
---|
[5564] | 93 | // always move forward both enumerators (do not use short-circuit evaluation!)
|
---|
| 94 | while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
|
---|
[6964] | 95 | double x = secondEnumerator.Current;
|
---|
| 96 | double y = firstEnumerator.Current;
|
---|
| 97 | covarianceCalculator.Add(x, y);
|
---|
[5945] | 98 | if (covarianceCalculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
[5507] | 99 | }
|
---|
| 100 |
|
---|
[5564] | 101 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
[5945] | 102 | if (covarianceCalculator.ErrorState == OnlineCalculatorError.None &&
|
---|
| 103 | (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
|
---|
[5507] | 104 | throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
|
---|
| 105 | } else {
|
---|
[5942] | 106 | errorState = covarianceCalculator.ErrorState;
|
---|
| 107 | return covarianceCalculator.Covariance;
|
---|
[5507] | 108 | }
|
---|
| 109 | }
|
---|
[4027] | 110 | }
|
---|
| 111 | }
|
---|