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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 |
|
---|
26 | namespace 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 | protected OnlineCovarianceCalculator(OnlineCovarianceCalculator other, Cloner cloner = null) {
|
---|
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 | // IDeepCloneable interface members
|
---|
107 | public object Clone() {
|
---|
108 | return new OnlineCovarianceCalculator(this);
|
---|
109 | }
|
---|
110 |
|
---|
111 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
112 | var clone = cloner.GetClone(this);
|
---|
113 | if (clone == null) {
|
---|
114 | clone = new OnlineCovarianceCalculator(this);
|
---|
115 | cloner.RegisterClonedObject(this, clone);
|
---|
116 | }
|
---|
117 | return clone;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|