[645] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
[695] | 25 | using HeuristicLab.DataAnalysis;
|
---|
[645] | 26 |
|
---|
[2349] | 27 | namespace HeuristicLab.Modeling {
|
---|
| 28 | public class SimpleTheilInequalityCoefficientEvaluator : SimpleEvaluatorBase {
|
---|
[645] | 29 | public override string Description {
|
---|
| 30 | get {
|
---|
[2349] | 31 | return @"Calculates the Theil inequality coefficient (Theil's U2 not U1!) of estimated values vs. real values of 'TargetVariable'.
|
---|
[695] | 32 |
|
---|
| 33 | U2 = Sqrt(1/N * Sum(P_t - A_t)^2 ) / Sqrt(1/N * Sum(A_t)^2 )
|
---|
| 34 |
|
---|
| 35 | where P_t is the predicted change of the target variable and A_t is the measured (original) change.
|
---|
[2827] | 36 | (P_t = (y'_t - y_(t-1)) / y_(t-1), A_t = (y_t - y_(t-1)) / y_(t-1)).
|
---|
[695] | 37 |
|
---|
| 38 | U2 is 0 for a perfect prediction and 1 for the naive model y'_t = y_(t-1). An U2 > 1 means the
|
---|
| 39 | model is worse than the naive model (=> model is useless).";
|
---|
[645] | 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[2349] | 43 | public override string OutputVariableName {
|
---|
| 44 | get {
|
---|
| 45 | return "TheilInequalityCoefficient";
|
---|
| 46 | }
|
---|
[645] | 47 | }
|
---|
| 48 |
|
---|
[2349] | 49 | public override double Evaluate(double[,] values) {
|
---|
| 50 | return Calculate(values);
|
---|
| 51 | }
|
---|
[645] | 52 |
|
---|
[2349] | 53 | public static double Calculate(double[,] values) {
|
---|
| 54 | int n = values.GetLength(0);
|
---|
[645] | 55 | double errorsSquaredSum = 0.0;
|
---|
| 56 | double originalSquaredSum = 0.0;
|
---|
[695] | 57 | int nSamples = 0;
|
---|
[2349] | 58 | for (int sample = 1; sample < n; sample++) {
|
---|
[2357] | 59 | double prevValue = values[sample - 1, ORIGINAL_INDEX];
|
---|
[2827] | 60 | double estimatedValue = values[sample, ESTIMATION_INDEX];
|
---|
| 61 | double originalValue = values[sample, ORIGINAL_INDEX];
|
---|
[2828] | 62 | if (!double.IsNaN(originalValue) && !double.IsInfinity(originalValue) && prevValue != 0.0) {
|
---|
[2827] | 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;
|
---|
[695] | 67 | nSamples++;
|
---|
[645] | 68 | }
|
---|
| 69 | }
|
---|
[692] | 70 | double quality = Math.Sqrt(errorsSquaredSum / nSamples) / Math.Sqrt(originalSquaredSum / nSamples);
|
---|
[712] | 71 | if (double.IsNaN(quality) || double.IsInfinity(quality))
|
---|
[645] | 72 | quality = double.MaxValue;
|
---|
[2349] | 73 | return quality;
|
---|
[645] | 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|