Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2672: Implement online calculator cloning.

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