Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 4.0 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 OnlineCovarianceCalculator : DeepCloneable, IOnlineCalculator {
28
29    private double xMean, yMean, Cn;
30    private int n;
31    public double Covariance {
32      get {
33        return n > 0 ? Cn / n : 0.0;
34      }
35    }
36
37    public OnlineCovarianceCalculator() {
38      Reset();
39    }
40
41    protected OnlineCovarianceCalculator(OnlineCovarianceCalculator other, Cloner cloner) {
42      Cn = other.Cn;
43      xMean = other.xMean;
44      yMean = other.yMean;
45      n = other.n;
46      errorState = other.errorState;
47    }
48
49    #region IOnlineCalculator Members
50    private OnlineCalculatorError errorState;
51    public OnlineCalculatorError ErrorState {
52      get { return errorState; }
53    }
54    public double Value {
55      get { return Covariance; }
56    }
57    public void Reset() {
58      n = 0;
59      Cn = 0.0;
60      xMean = 0.0;
61      yMean = 0.0;
62      errorState = OnlineCalculatorError.InsufficientElementsAdded;
63    }
64
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) {
67        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
68      } else {
69        n++;
70        errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded);        // n >= 1
71
72        // online calculation of tMean
73        xMean = xMean + (x - xMean) / n;
74        double delta = y - yMean; // delta = (y - yMean(n-1))
75        yMean = yMean + delta / n;
76
77        // online calculation of covariance
78        Cn = Cn + delta * (x - xMean); // C(n) = C(n-1) + (y - yMean(n-1)) (t - tMean(n))       
79      }
80    }
81    #endregion
82
83    public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) {
84      IEnumerator<double> firstEnumerator = first.GetEnumerator();
85      IEnumerator<double> secondEnumerator = second.GetEnumerator();
86      OnlineCovarianceCalculator covarianceCalculator = new OnlineCovarianceCalculator();
87
88      // always move forward both enumerators (do not use short-circuit evaluation!)
89      while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
90        double x = secondEnumerator.Current;
91        double y = firstEnumerator.Current;
92        covarianceCalculator.Add(x, y);
93        if (covarianceCalculator.ErrorState != OnlineCalculatorError.None) break;
94      }
95
96      // check if both enumerators are at the end to make sure both enumerations have the same length
97      if (covarianceCalculator.ErrorState == OnlineCalculatorError.None &&
98          (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
99        throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
100      } else {
101        errorState = covarianceCalculator.ErrorState;
102        return covarianceCalculator.Covariance;
103      }
104    }
105
106    public override IDeepCloneable Clone(Cloner cloner) {
107      return new OnlineCovarianceCalculator(this, cloner);
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.