Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2645_ContextAlgorithms/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineCovarianceCalculator.cs

Last change on this file was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 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
54    #region IOnlineCalculator Members
55    private OnlineCalculatorError errorState;
56    public OnlineCalculatorError ErrorState {
57      get { return errorState; }
58    }
59    public double Value {
60      get { return Covariance; }
61    }
62    public void Reset() {
63      n = 0;
64      Cn = 0.0;
65      xMean = 0.0;
66      yMean = 0.0;
67      errorState = OnlineCalculatorError.InsufficientElementsAdded;
68    }
69
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) {
72        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
73      } else {
74        n++;
75        errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded);        // n >= 1
76
77        // online calculation of tMean
78        xMean = xMean + (x - xMean) / n;
79        double delta = y - yMean; // delta = (y - yMean(n-1))
80        yMean = yMean + delta / n;
81
82        // online calculation of covariance
83        Cn = Cn + delta * (x - xMean); // C(n) = C(n-1) + (y - yMean(n-1)) (t - tMean(n))       
84      }
85    }
86    #endregion
87
88    public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) {
89      IEnumerator<double> firstEnumerator = first.GetEnumerator();
90      IEnumerator<double> secondEnumerator = second.GetEnumerator();
91      OnlineCovarianceCalculator covarianceCalculator = new OnlineCovarianceCalculator();
92
93      // always move forward both enumerators (do not use short-circuit evaluation!)
94      while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
95        double x = secondEnumerator.Current;
96        double y = firstEnumerator.Current;
97        covarianceCalculator.Add(x, y);
98        if (covarianceCalculator.ErrorState != OnlineCalculatorError.None) break;
99      }
100
101      // check if both enumerators are at the end to make sure both enumerations have the same length
102      if (covarianceCalculator.ErrorState == OnlineCalculatorError.None &&
103          (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
104        throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
105      } else {
106        errorState = covarianceCalculator.ErrorState;
107        return covarianceCalculator.Covariance;
108      }
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.