Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Modeling/3.2/SimpleTheilInequalityCoefficientEvaluator.cs @ 3698

Last change on this file since 3698 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: 2.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
22using System;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.DataAnalysis;
26
27namespace HeuristicLab.Modeling {
28  public class SimpleTheilInequalityCoefficientEvaluator : SimpleEvaluatorBase {
29    public override string Description {
30      get {
31        return @"Calculates the Theil inequality coefficient (Theil's U2 not U1!) of estimated values vs. real values of 'TargetVariable'.
32
33U2 = Sqrt(1/N * Sum(P_t - A_t)^2 ) / Sqrt(1/N * Sum(A_t)^2 )
34
35where P_t is the predicted change of the target variable and A_t is the measured (original) change.
36(P_t = y'_t - y_(t-1), A_t = y_t - y_(t-1)).
37
38U2 is 0 for a perfect prediction and 1 for the naive model y'_t = y_(t-1). An U2 > 1 means the
39model is worse than the naive model (=> model is useless).";
40      }
41    }
42
43    public override string OutputVariableName {
44      get {
45        return "TheilInequalityCoefficient";
46      }
47    }
48
49    public override double Evaluate(double[,] values) {
50      return Calculate(values);
51    }
52
53    public static double Calculate(double[,] values) {
54      int n = values.GetLength(0);
55      double errorsSquaredSum = 0.0;
56      double originalSquaredSum = 0.0;
57      int nSamples = 0;
58      for (int sample = 1; sample < n; sample++) {
59        double prevValue = values[sample - 1, ORIGINAL_INDEX];
60        double estimatedChange = values[sample, ESTIMATION_INDEX] - prevValue;
61        double originalChange = values[sample, ORIGINAL_INDEX] - prevValue;
62        if (!double.IsNaN(originalChange) && !double.IsInfinity(originalChange)) {
63          double error = estimatedChange - originalChange;
64          errorsSquaredSum += error * error;
65          originalSquaredSum += originalChange * originalChange;
66          nSamples++;
67        }
68      }
69      double quality = Math.Sqrt(errorsSquaredSum / nSamples) / Math.Sqrt(originalSquaredSum / nSamples);
70      if (double.IsNaN(quality) || double.IsInfinity(quality))
71        quality = double.MaxValue;
72      return quality;
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.