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