Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1873 was 1869, checked in by gkronber, 15 years ago

Moved simple evaluators from plugin SVM to plugin Modeling. #635

File size: 1.9 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 : OperatorBase {
11
12    public SimpleR2Evaluator()
13      : base() {
14      AddVariableInfo(new VariableInfo("Values", "Target vs predicted values", typeof(DoubleMatrixData), VariableKind.In));
15      AddVariableInfo(new VariableInfo("R2", "Coefficient of determination", typeof(DoubleData), VariableKind.New | VariableKind.Out));
16    }
17
18    public override IOperation Apply(IScope scope) {
19      DoubleMatrixData values = GetVariableValue<DoubleMatrixData>("Values", scope, true);
20
21      double targetMean = 0;
22      double sse = 0;
23      double cnt = 0;
24      for (int i = 0; i < values.Data.GetLength(0); i++) {
25        double estimated = values.Data[i, 0];
26        double target = values.Data[i, 1];
27        if (!double.IsNaN(estimated) && !double.IsInfinity(estimated) &&
28            !double.IsNaN(target) && !double.IsInfinity(target)) {
29          targetMean += target;
30          double error = estimated - target;
31          sse += error * error;
32          cnt++;
33        }
34      }
35      targetMean /= cnt;
36
37      double targetDeviationTotalSumOfSquares = 0;
38      for (int i = 0; i < values.Data.GetLength(0); i++) {
39        double target = values.Data[i, 1];
40        if (!double.IsNaN(target) && !double.IsInfinity(target)) {
41          target = target - targetMean;
42          target = target * target;
43          targetDeviationTotalSumOfSquares += target;
44        }
45      }
46      double quality = 1 - sse / targetDeviationTotalSumOfSquares;
47      if (quality > 1)
48        throw new InvalidProgramException();
49
50      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("R2"), new DoubleData(quality)));
51      return null;
52    }
53  }
54}
Note: See TracBrowser for help on using the repository browser.