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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 |
|
---|
31 | namespace 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 void Reset() {
|
---|
64 | sum_sq_x = 0.0;
|
---|
65 | sum_sq_y = 0.0;
|
---|
66 | sum_coproduct = 0.0;
|
---|
67 | mean_x = 0.0;
|
---|
68 | mean_y = 0.0;
|
---|
69 | n = 0;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public void Add(double original, double estimated) {
|
---|
73 | // stable and iterative calculation of R²
|
---|
74 | if (IsInvalidValue(original) || IsInvalidValue(estimated)) {
|
---|
75 | throw new ArgumentException("R² is not defined for variables with NaN or infinity values.");
|
---|
76 | }
|
---|
77 | if (n == 0) {
|
---|
78 | mean_x = original;
|
---|
79 | mean_y = estimated;
|
---|
80 | n = 1;
|
---|
81 | } else {
|
---|
82 | double sweep = (n - 1.0) / n;
|
---|
83 | double delta_x = original - mean_x;
|
---|
84 | double delta_y = estimated - mean_y;
|
---|
85 | sum_sq_x += delta_x * delta_x * sweep;
|
---|
86 | sum_sq_y += delta_y * delta_y * sweep;
|
---|
87 | sum_coproduct += delta_x * delta_y * sweep;
|
---|
88 | mean_x += delta_x / n;
|
---|
89 | mean_y += delta_y / n;
|
---|
90 | n++;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | #endregion
|
---|
95 |
|
---|
96 | private bool IsInvalidValue(double x) {
|
---|
97 | return double.IsNaN(x) || double.IsInfinity(x);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|