Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Modeling/3.2/SimpleMSEEvaluator.cs @ 3580

Last change on this file since 3580 was 2357, checked in by gkronber, 15 years ago

Fixed #740 (SimpleEvaluators in HL.GP.StructId and HL.SVM are not compatible with evaluators in HL.Modeling).

File size: 1.3 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 SimpleMSEEvaluator : SimpleEvaluatorBase {
11
12    public override string OutputVariableName {
13      get {
14        return "MSE";
15      }
16    }
17    public override double Evaluate(double[,] values) {
18      try {
19        return Calculate(values);
20      }
21      catch (ArgumentException) {
22        return double.PositiveInfinity;
23      }
24    }
25
26    public static double Calculate(double[,] values) {
27      double sse = 0;
28      double cnt = 0;
29      for (int i = 0; i < values.GetLength(0); i++) {
30        double estimated = values[i, ESTIMATION_INDEX];
31        double target = values[i, ORIGINAL_INDEX];
32        if (!double.IsNaN(estimated) && !double.IsInfinity(estimated) &&
33            !double.IsNaN(target) && !double.IsInfinity(target)) {
34          double error = estimated - target;
35          sse += error * error;
36          cnt++;
37        }
38      }
39      if (cnt > 0) {
40        double mse = sse / cnt;
41        return mse;
42      } else {
43        throw new ArgumentException("Mean squared errors is not defined for input vectors of NaN or Inf");
44      }
45    }
46  }
47}
Note: See TracBrowser for help on using the repository browser.