Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineCovarianceCalculator.cs @ 12015

Last change on this file since 12015 was 11594, checked in by jkarder, 10 years ago

#2116: merged r10041-r11593 from trunk into branch

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