[4027] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 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 |
|
---|
| 22 | using System;
|
---|
[5507] | 23 | using System.Collections.Generic;
|
---|
[4027] | 24 |
|
---|
[5491] | 25 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[5942] | 26 | public class OnlineCovarianceCalculator : IOnlineCalculator {
|
---|
[4027] | 27 |
|
---|
| 28 | private double originalMean, estimatedMean, Cn;
|
---|
| 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;
|
---|
| 51 | originalMean = 0.0;
|
---|
| 52 | estimatedMean = 0.0;
|
---|
[5942] | 53 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
[4027] | 54 | }
|
---|
| 55 |
|
---|
| 56 | public void Add(double original, double estimated) {
|
---|
[5942] | 57 | if (double.IsNaN(estimated) || double.IsInfinity(estimated) || double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
|
---|
| 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
|
---|
| 64 | originalMean = originalMean + (original - originalMean) / n;
|
---|
| 65 | double delta = estimated - estimatedMean; // delta = (y - yMean(n-1))
|
---|
| 66 | estimatedMean = estimatedMean + delta / n;
|
---|
| 67 |
|
---|
| 68 | // online calculation of covariance
|
---|
| 69 | Cn = Cn + delta * (original - originalMean); // C(n) = C(n-1) + (y - yMean(n-1)) (t - tMean(n))
|
---|
| 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()) {
|
---|
[5507] | 81 | double estimated = secondEnumerator.Current;
|
---|
| 82 | double original = firstEnumerator.Current;
|
---|
[5942] | 83 | covarianceCalculator.Add(original, estimated);
|
---|
[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 | }
|
---|