Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Modeling/3.3/SimpleR2Evaluator.cs @ 2173

Last change on this file since 2173 was 1888, checked in by gkronber, 15 years ago

Added simple evaluators for mean absolute percentage error, mean absolute percentage of range error and, variance accounted for. #635 (Plugin HeuristicLab.Modeling as a common basis for all data-based modeling algorithms)

File size: 1.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.DataAnalysis;
8
9namespace HeuristicLab.Modeling {
10  public class SimpleR2Evaluator : SimpleEvaluatorBase {
11
12    public override string OutputVariableName {
13      get {
14        return "R2";
15      }
16    }
17
18    public override double Evaluate(double[,] values) {
19      return Calculate(values);
20    }
21
22    public static double Calculate(double[,] values) {
23      double targetMean = 0;
24      double sse = 0;
25      double cnt = 0;
26      for (int i = 0; i < values.GetLength(0); i++) {
27        double estimated = values[i, 0];
28        double target = values[i, 1];
29        if (!double.IsNaN(estimated) && !double.IsInfinity(estimated) &&
30            !double.IsNaN(target) && !double.IsInfinity(target)) {
31          targetMean += target;
32          double error = estimated - target;
33          sse += error * error;
34          cnt++;
35        }
36      }
37      targetMean /= cnt;
38
39      double targetDeviationTotalSumOfSquares = 0;
40      for (int i = 0; i < values.GetLength(0); i++) {
41        double target = values[i, 1];
42        if (!double.IsNaN(target) && !double.IsInfinity(target)) {
43          target = target - targetMean;
44          target = target * target;
45          targetDeviationTotalSumOfSquares += target;
46        }
47      }
48      double quality = 1 - sse / targetDeviationTotalSumOfSquares;
49      if (quality > 1)
50        throw new InvalidProgramException();
51
52      return quality;
53    }
54  }
55}
Note: See TracBrowser for help on using the repository browser.