Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/3.3/ProfitEvaluator.cs @ 3580

Last change on this file since 3580 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: 4.0 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 HeuristicLab.Core;
23using HeuristicLab.Data;
24using HeuristicLab.GP.Interfaces;
25using HeuristicLab.DataAnalysis;
26using System.Linq;
27
28namespace HeuristicLab.GP.StructureIdentification.TimeSeries {
29  public class ProfitEvaluator : GPEvaluatorBase {
30    public override string Description {
31      get {
32        return @"TASK";
33      }
34    }
35
36    public ProfitEvaluator()
37      : base() {
38      AddVariableInfo(new VariableInfo("ExchangeRate", "The index of the variable in the dataset for the exchange rate", typeof(IntData), VariableKind.In));
39      AddVariableInfo(new VariableInfo("Profit", "The estimated profit of the model", typeof(DoubleData), VariableKind.New));
40      AddVariableInfo(new VariableInfo("TransactionCost", "Cost of a trade in percent of the transaction volume (0..1)", typeof(DoubleData), VariableKind.In));
41    }
42
43    public override void Evaluate(IScope scope, IFunctionTree tree, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end) {
44      int exchangeRateVarIndex = GetVariableValue<IntData>("ExchangeRate", scope, true).Data;
45      double transactionCost = GetVariableValue<DoubleData>("TransactionCost", scope, true).Data;
46      DoubleData profit = GetVariableValue<DoubleData>("Profit", scope, false, false);
47      if (profit == null) {
48        profit = new DoubleData();
49        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Profit"), profit));
50      }
51
52      double cA = 1.0; // start with a capital of one entity of A
53      double cB = 0;
54      double exchangeRate = double.MaxValue;
55      double[] estimatedValues = evaluator.Evaluate(dataset, tree, Enumerable.Range(start, end - start)).ToArray();
56      double[] originalValues = dataset.GetVariableValues(targetVariable, start, end);
57      double[] exchangeRateValues = dataset.GetVariableValues(exchangeRateVarIndex, start, end);
58      for (int i = 0; i < estimatedValues.Length; i++) {
59        exchangeRate = exchangeRateValues[i];
60        double originalPercentageChange = originalValues[i];
61        double estimatedPercentageChange = estimatedValues[i];
62
63        if (!double.IsNaN(originalPercentageChange) && !double.IsInfinity(originalPercentageChange)) {
64          if (estimatedPercentageChange > 0) {
65            // prediction is the rate of B/A will increase (= get more B for one A) => exchange all B to A
66            cA += (cB / exchangeRate) * (1 - transactionCost);
67            cB = 0;
68          } else if (estimatedPercentageChange < 0) {
69            // prediction is the rate of B/A will drop (= get more A for one B) => exchange all A to B
70            cB += (cA * exchangeRate) * (1 - transactionCost);
71            cA = 0;
72          }
73        }
74      }
75
76      if (double.IsNaN(cA) || double.IsInfinity(cA) || double.IsInfinity(cB) || double.IsNaN(cB)) {
77        cA = 0;
78        cB = 0;
79      }
80      // at the end we must exchange all B back to A
81      // (because we want to buy the local beer not import one from B-country)
82      profit.Data = cA + ((cB / exchangeRate) * (1 - transactionCost));
83      profit.Data -= 1.0; // substract the start capital to determine actual profit
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.