Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineCovarianceCalculator.cs @ 14427

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

#2672: Updated cloning mechanism to conform to the HL standard.

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