Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlinePearsonsRCalculator.cs @ 15831

Last change on this file since 15831 was 15831, checked in by fholzing, 6 years ago

#2904: Refactored RegressionSolutionVariableImpactsCalculator. We don't dependent on the solution anymore. The impact can be calculated for a single variable. The calculator can be chosen.

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