Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 686 was 686, checked in by gkronber, 16 years ago

fixed #322

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