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 OnlinePearsonsRCalculator : IOnlineCalculator, IDeepCloneable {
|
---|
28 | private OnlineCovarianceCalculator covCalculator = new OnlineCovarianceCalculator();
|
---|
29 | private OnlineMeanAndVarianceCalculator sxCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
30 | private OnlineMeanAndVarianceCalculator syCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
31 |
|
---|
32 | public double R {
|
---|
33 | get {
|
---|
34 | double xVar = sxCalculator.PopulationVariance;
|
---|
35 | double yVar = syCalculator.PopulationVariance;
|
---|
36 | if (xVar.IsAlmost(0.0) || yVar.IsAlmost(0.0)) {
|
---|
37 | return 0.0;
|
---|
38 | } else {
|
---|
39 | var r = covCalculator.Covariance / (Math.Sqrt(xVar) * Math.Sqrt(yVar));
|
---|
40 | if (r < -1.0) r = -1.0;
|
---|
41 | else if (r > 1.0) r = 1.0;
|
---|
42 | return r;
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public OnlinePearsonsRCalculator() { }
|
---|
48 |
|
---|
49 | // private constructor used internally by the Clone() method
|
---|
50 | private OnlinePearsonsRCalculator(OnlineCovarianceCalculator covCalculator, OnlineMeanAndVarianceCalculator sxCalculator, OnlineMeanAndVarianceCalculator syCalculator) {
|
---|
51 | this.covCalculator = covCalculator;
|
---|
52 | this.sxCalculator = sxCalculator;
|
---|
53 | this.syCalculator = syCalculator;
|
---|
54 | }
|
---|
55 |
|
---|
56 | #region IOnlineCalculator Members
|
---|
57 | public OnlineCalculatorError ErrorState {
|
---|
58 | get { return covCalculator.ErrorState | sxCalculator.PopulationVarianceErrorState | syCalculator.PopulationVarianceErrorState; }
|
---|
59 | }
|
---|
60 | public double Value {
|
---|
61 | get { return R; }
|
---|
62 | }
|
---|
63 | public void Reset() {
|
---|
64 | covCalculator.Reset();
|
---|
65 | sxCalculator.Reset();
|
---|
66 | syCalculator.Reset();
|
---|
67 | }
|
---|
68 |
|
---|
69 | public void Add(double x, double y) {
|
---|
70 | // no need to check validity of values explicitly here as it is checked in all three evaluators
|
---|
71 | covCalculator.Add(x, y);
|
---|
72 | sxCalculator.Add(x);
|
---|
73 | syCalculator.Add(y);
|
---|
74 | }
|
---|
75 |
|
---|
76 | #endregion
|
---|
77 |
|
---|
78 | public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) {
|
---|
79 | IEnumerator<double> firstEnumerator = first.GetEnumerator();
|
---|
80 | IEnumerator<double> secondEnumerator = second.GetEnumerator();
|
---|
81 | var calculator = new OnlinePearsonsRCalculator();
|
---|
82 |
|
---|
83 | // always move forward both enumerators (do not use short-circuit evaluation!)
|
---|
84 | while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
|
---|
85 | double original = firstEnumerator.Current;
|
---|
86 | double estimated = secondEnumerator.Current;
|
---|
87 | calculator.Add(original, estimated);
|
---|
88 | if (calculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
89 | }
|
---|
90 |
|
---|
91 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
92 | if (calculator.ErrorState == OnlineCalculatorError.None &&
|
---|
93 | (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
|
---|
94 | throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
|
---|
95 | } else {
|
---|
96 | errorState = calculator.ErrorState;
|
---|
97 | return calculator.R;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | // IDeepCloneable members
|
---|
102 | public object Clone() {
|
---|
103 | var covCalculatorClone = (OnlineCovarianceCalculator)covCalculator.Clone();
|
---|
104 | var sxCalculatorClone = (OnlineMeanAndVarianceCalculator)sxCalculator.Clone();
|
---|
105 | var syCalculatorClone = (OnlineMeanAndVarianceCalculator)syCalculator.Clone();
|
---|
106 | return new OnlinePearsonsRCalculator(covCalculatorClone, sxCalculatorClone, syCalculatorClone);
|
---|
107 | }
|
---|
108 |
|
---|
109 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
110 | var clone = cloner.GetClone(this);
|
---|
111 | if (clone == null) {
|
---|
112 | var covCalculatorClone = (OnlineCovarianceCalculator)covCalculator.Clone(cloner);
|
---|
113 | var sxCalculatorClone = (OnlineMeanAndVarianceCalculator)sxCalculator.Clone(cloner);
|
---|
114 | var syCalculatorClone = (OnlineMeanAndVarianceCalculator)syCalculator.Clone(cloner);
|
---|
115 | clone = new OnlinePearsonsRCalculator(covCalculatorClone, sxCalculatorClone, syCalculatorClone);
|
---|
116 | cloner.RegisterClonedObject(this, clone);
|
---|
117 | }
|
---|
118 | return clone;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|