Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/3.3/AveragePercentageChangeEvaluator.cs @ 3190

Last change on this file since 3190 was 2578, checked in by gkronber, 15 years ago

Implemented #824 (Refactor: ITreeEvaluator interface to provide a method that evaluates a tree on a range of samples.)

File size: 3.7 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.GP.Interfaces;
26using HeuristicLab.DataAnalysis;
27using System.Linq;
28
29namespace HeuristicLab.GP.StructureIdentification.TimeSeries {
30  public class AvergePercentageChangeEvaluator : GPEvaluatorBase {
31    public override string Description {
32      get {
33        return @"TASK";
34      }
35    }
36
37    public AvergePercentageChangeEvaluator()
38      : base() {
39      AddVariableInfo(new VariableInfo("Differential", "Wether to transform the target variable to percentage change first or not.", typeof(BoolData), VariableKind.In));
40      AddVariableInfo(new VariableInfo("APC", "The average percentage change of the model", typeof(DoubleData), VariableKind.New));
41    }
42
43    public override void Evaluate(IScope scope, IFunctionTree tree, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end) {
44      bool differential = GetVariableValue<BoolData>("Differential", scope, true).Data;
45      DoubleData apc = GetVariableValue<DoubleData>("APC", scope, false, false);
46      if (apc == null) {
47        apc = new DoubleData();
48        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("APC"), apc));
49      }
50
51      double percentageSum = 0;
52      double[] estimatedValues = evaluator.Evaluate(dataset, tree, Enumerable.Range(start, end - start)).ToArray();
53      double[] originalValues = dataset.GetVariableValues(targetVariable, start - 1, end);
54      for (int i = 0; i < estimatedValues.Length; i++) {
55        double prevOriginal;
56        double originalPercentageChange;
57        double estimatedPercentageChange;
58        if (differential) {
59          prevOriginal = originalValues[i];
60          originalPercentageChange = (originalValues[i + 1] - prevOriginal) / prevOriginal;
61          estimatedPercentageChange = (estimatedValues[i] - prevOriginal) / prevOriginal;
62
63        } else {
64          originalPercentageChange = originalValues[i + 1];
65          estimatedPercentageChange = estimatedValues[i];
66
67        }
68        if (!double.IsNaN(originalPercentageChange) && !double.IsInfinity(originalPercentageChange)) {
69          if ((estimatedPercentageChange > 0 && originalPercentageChange > 0) ||
70            (estimatedPercentageChange < 0 && originalPercentageChange < 0)) {
71            percentageSum += Math.Abs(originalPercentageChange);
72          } else if ((estimatedPercentageChange > 0 && originalPercentageChange < 0) ||
73            (estimatedPercentageChange < 0 && originalPercentageChange > 0)) {
74            percentageSum -= Math.Abs(originalPercentageChange);
75          }
76        }
77      }
78
79      percentageSum /= (end - start);
80      if (double.IsNaN(percentageSum) || double.IsInfinity(percentageSum)) {
81        percentageSum = double.MinValue;
82      }
83      apc.Data = percentageSum;
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.