Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Modeling/3.2/SimpleR2Evaluator.cs @ 1913

Last change on this file since 1913 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
RevLine 
[1814]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.DataAnalysis;
8
[1869]9namespace HeuristicLab.Modeling {
[1888]10  public class SimpleR2Evaluator : SimpleEvaluatorBase {
[1814]11
[1888]12    public override string OutputVariableName {
13      get {
14        return "R2";
15      }
[1814]16    }
17
[1888]18    public override double Evaluate(double[,] values) {
19      return Calculate(values);
20    }
[1814]21
[1888]22    public static double Calculate(double[,] values) {
[1814]23      double targetMean = 0;
24      double sse = 0;
25      double cnt = 0;
[1888]26      for (int i = 0; i < values.GetLength(0); i++) {
27        double estimated = values[i, 0];
28        double target = values[i, 1];
[1814]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;
[1888]40      for (int i = 0; i < values.GetLength(0); i++) {
41        double target = values[i, 1];
[1814]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
[1888]52      return quality;
[1814]53    }
54  }
55}
Note: See TracBrowser for help on using the repository browser.