Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15912 was 15831, checked in by fholzing, 7 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
RevLine 
[3996]1#region License Information
2/* HeuristicLab
[15583]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3996]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;
[5500]23using System.Collections.Generic;
[3996]24using HeuristicLab.Common;
25
[5490]26namespace HeuristicLab.Problems.DataAnalysis {
[14376]27  public class OnlinePearsonsRCalculator : DeepCloneable, IOnlineCalculator {
[5942]28    private OnlineCovarianceCalculator covCalculator = new OnlineCovarianceCalculator();
29    private OnlineMeanAndVarianceCalculator sxCalculator = new OnlineMeanAndVarianceCalculator();
30    private OnlineMeanAndVarianceCalculator syCalculator = new OnlineMeanAndVarianceCalculator();
[3996]31
[15831]32    public double R
33    {
34      get
35      {
[5942]36        double xVar = sxCalculator.PopulationVariance;
37        double yVar = syCalculator.PopulationVariance;
[4126]38        if (xVar.IsAlmost(0.0) || yVar.IsAlmost(0.0)) {
39          return 0.0;
40        } else {
[13038]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;
[4126]45        }
[3996]46      }
47    }
48
[12492]49    public OnlinePearsonsRCalculator() { }
[3996]50
[14465]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);
[14293]56    }
[14465]57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new OnlinePearsonsRCalculator(this, cloner);
59    }
[14293]60
[5942]61    #region IOnlineCalculator Members
[15831]62    public OnlineCalculatorError ErrorState
63    {
[5942]64      get { return covCalculator.ErrorState | sxCalculator.PopulationVarianceErrorState | syCalculator.PopulationVarianceErrorState; }
[5894]65    }
[15831]66    public double Value
67    {
[12492]68      get { return R; }
[4022]69    }
[3996]70    public void Reset() {
[5942]71      covCalculator.Reset();
72      sxCalculator.Reset();
73      syCalculator.Reset();
[3996]74    }
75
[4122]76    public void Add(double x, double y) {
[5746]77      // no need to check validity of values explicitly here as it is checked in all three evaluators
[5942]78      covCalculator.Add(x, y);
79      sxCalculator.Add(x);
80      syCalculator.Add(y);
[3996]81    }
82
83    #endregion
84
[5942]85    public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) {
[5500]86      IEnumerator<double> firstEnumerator = first.GetEnumerator();
87      IEnumerator<double> secondEnumerator = second.GetEnumerator();
[12492]88      var calculator = new OnlinePearsonsRCalculator();
[5500]89
[5564]90      // always move forward both enumerators (do not use short-circuit evaluation!)
91      while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
[6961]92        double original = firstEnumerator.Current;
[5500]93        double estimated = secondEnumerator.Current;
[12492]94        calculator.Add(original, estimated);
95        if (calculator.ErrorState != OnlineCalculatorError.None) break;
[5500]96      }
97
[5564]98      // check if both enumerators are at the end to make sure both enumerations have the same length
[12492]99      if (calculator.ErrorState == OnlineCalculatorError.None &&
[5945]100           (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
[5500]101        throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
102      } else {
[12492]103        errorState = calculator.ErrorState;
104        return calculator.R;
[5500]105      }
106    }
[15831]107
108    public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
109      return Calculate(originalValues, estimatedValues, out errorState);
110    }
[3996]111  }
112}
Note: See TracBrowser for help on using the repository browser.