Free cookie consent management tool by TermsFeed Policy Generator

source: branches/crossvalidation-2434/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlinePearsonsRCalculator.cs @ 14728

Last change on this file since 14728 was 14029, checked in by gkronber, 8 years ago

#2434: merged trunk changes r12934:14026 from trunk to branch

File size: 3.6 KB
RevLine 
[3996]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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 {
[12492]27  public class OnlinePearsonsRCalculator : IOnlineCalculator {
[5942]28    private OnlineCovarianceCalculator covCalculator = new OnlineCovarianceCalculator();
29    private OnlineMeanAndVarianceCalculator sxCalculator = new OnlineMeanAndVarianceCalculator();
30    private OnlineMeanAndVarianceCalculator syCalculator = new OnlineMeanAndVarianceCalculator();
[3996]31
[12492]32    public double R {
[3996]33      get {
[5942]34        double xVar = sxCalculator.PopulationVariance;
35        double yVar = syCalculator.PopulationVariance;
[4126]36        if (xVar.IsAlmost(0.0) || yVar.IsAlmost(0.0)) {
37          return 0.0;
38        } else {
[14029]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;
[4126]43        }
[3996]44      }
45    }
46
[12492]47    public OnlinePearsonsRCalculator() { }
[3996]48
[5942]49    #region IOnlineCalculator Members
50    public OnlineCalculatorError ErrorState {
51      get { return covCalculator.ErrorState | sxCalculator.PopulationVarianceErrorState | syCalculator.PopulationVarianceErrorState; }
[5894]52    }
[4022]53    public double Value {
[12492]54      get { return R; }
[4022]55    }
[3996]56    public void Reset() {
[5942]57      covCalculator.Reset();
58      sxCalculator.Reset();
59      syCalculator.Reset();
[3996]60    }
61
[4122]62    public void Add(double x, double y) {
[5746]63      // no need to check validity of values explicitly here as it is checked in all three evaluators
[5942]64      covCalculator.Add(x, y);
65      sxCalculator.Add(x);
66      syCalculator.Add(y);
[3996]67    }
68
69    #endregion
70
[5942]71    public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) {
[5500]72      IEnumerator<double> firstEnumerator = first.GetEnumerator();
73      IEnumerator<double> secondEnumerator = second.GetEnumerator();
[12492]74      var calculator = new OnlinePearsonsRCalculator();
[5500]75
[5564]76      // always move forward both enumerators (do not use short-circuit evaluation!)
77      while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
[6961]78        double original = firstEnumerator.Current;
[5500]79        double estimated = secondEnumerator.Current;
[12492]80        calculator.Add(original, estimated);
81        if (calculator.ErrorState != OnlineCalculatorError.None) break;
[5500]82      }
83
[5564]84      // check if both enumerators are at the end to make sure both enumerations have the same length
[12492]85      if (calculator.ErrorState == OnlineCalculatorError.None &&
[5945]86           (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
[5500]87        throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
88      } else {
[12492]89        errorState = calculator.ErrorState;
90        return calculator.R;
[5500]91      }
92    }
[3996]93  }
94}
Note: See TracBrowser for help on using the repository browser.