Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Modeling/3.2/SimpleTheilInequalityCoefficientEvaluator.cs @ 2879

Last change on this file since 2879 was 2828, checked in by gkronber, 15 years ago

Prevented division by zero in relative error calculation for Theil's U. #879

File size: 3.1 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)) / y_(t-1), A_t = (y_t - y_(t-1)) / 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 estimatedValue = values[sample, ESTIMATION_INDEX];
61        double originalValue = values[sample, ORIGINAL_INDEX];
62        if (!double.IsNaN(originalValue) && !double.IsInfinity(originalValue) && prevValue != 0.0) {
63          double errorEstimatedChange = (estimatedValue - originalValue) / prevValue; // percentage error of predicted change
64          errorsSquaredSum += errorEstimatedChange * errorEstimatedChange;
65          double errorNoChange = (prevValue - originalValue) / prevValue; // percentage error of naive model y(t+1) = y(t)
66          originalSquaredSum += errorNoChange * errorNoChange;
67          nSamples++;
68        }
69      }
70      double quality = Math.Sqrt(errorsSquaredSum / nSamples) / Math.Sqrt(originalSquaredSum / nSamples);
71      if (double.IsNaN(quality) || double.IsInfinity(quality))
72        quality = double.MaxValue;
73      return quality;
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.