source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Evaluators/OnlinePearsonsRSquaredEvaluator.cs @ 4022

Last change on this file since 4022 was 4022, checked in by gkronber, 11 years ago

Worked on symbolic regression classes to prepare for time series prognosis plugin. #1081

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System.Linq;
25using System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30
31namespace HeuristicLab.Problems.DataAnalysis.Evaluators {
32  public class OnlinePearsonsRSquaredEvaluator : IOnlineEvaluator {
33
34    private double sum_sq_x;
35    private double sum_sq_y;
36    private double sum_coproduct;
37    private double mean_x;
38    private double mean_y;
39    private int n;
40
41    public double RSquared {
42      get {
43        if (n < 1)
44          throw new InvalidOperationException("No elements");
45        else {
46          double pop_sd_x = Math.Sqrt(sum_sq_x / n);
47          double pop_sd_y = Math.Sqrt(sum_sq_y / n);
48          double cov_x_y = sum_coproduct / n;
49
50          if (pop_sd_x.IsAlmost(0.0) || pop_sd_y.IsAlmost(0.0))
51            return 0.0;
52          else {
53            double r = cov_x_y / (pop_sd_x * pop_sd_y);
54            return r * r;
55          }
56        }
57      }
58    }
59
60    public OnlinePearsonsRSquaredEvaluator() { }
61
62    #region IOnlineEvaluator Members
63    public double Value {
64      get { return RSquared; }
65    }
66    public void Reset() {
67      sum_sq_x = 0.0;
68      sum_sq_y = 0.0;
69      sum_coproduct = 0.0;
70      mean_x = 0.0;
71      mean_y = 0.0;
72      n = 0;
73    }
74
75    public void Add(double original, double estimated) {
76      // stable and iterative calculation of R²
77      if (IsInvalidValue(original) || IsInvalidValue(estimated)) {
78        throw new ArgumentException("R² is not defined for variables with NaN or infinity values.");
79      }
80      if (n == 0) {
81        mean_x = original;
82        mean_y = estimated;
83        n = 1;
84      } else {
85        double sweep = (n - 1.0) / n;
86        double delta_x = original - mean_x;
87        double delta_y = estimated - mean_y;
88        sum_sq_x += delta_x * delta_x * sweep;
89        sum_sq_y += delta_y * delta_y * sweep;
90        sum_coproduct += delta_x * delta_y * sweep;
91        mean_x += delta_x / n;
92        mean_y += delta_y / n;
93        n++;
94      }
95    }
96
97    #endregion
98
99    private bool IsInvalidValue(double x) {
100      return double.IsNaN(x) || double.IsInfinity(x);
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.