source: branches/HeuristicLab.Problems.DataAnalysis.Trading/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Solution.cs @ 9745

Last change on this file since 9745 was 9745, checked in by gkronber, 7 years ago

#1508 refactoring: removed smurf-naming.

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis.Trading {
30  /// <summary>
31  /// Abstract base class for trading data analysis solutions
32  /// </summary>
33  [StorableClass]
34  public abstract class Solution : DataAnalysisSolution, ISolution {
35    private const string TrainingSharpeRatioResultName = "Sharpe ratio (training)";
36    private const string TestSharpeRatioResultName = "Sharpe ratio (test)";
37    private const string TrainingProfitResultName = "Profit (training)";
38    private const string TestProfitResultName = "Profit (test)";
39
40    public new IModel Model {
41      get { return (IModel)base.Model; }
42      protected set { base.Model = value; }
43    }
44
45    public new IProblemData ProblemData {
46      get { return (IProblemData)base.ProblemData; }
47      protected set { base.ProblemData = value; }
48    }
49
50    public double TrainingSharpeRatio {
51      get { return ((DoubleValue)this[TrainingSharpeRatioResultName].Value).Value; }
52      private set { ((DoubleValue)this[TrainingSharpeRatioResultName].Value).Value = value; }
53    }
54
55    public double TestSharpeRatio {
56      get { return ((DoubleValue)this[TestSharpeRatioResultName].Value).Value; }
57      private set { ((DoubleValue)this[TestSharpeRatioResultName].Value).Value = value; }
58    }
59    public double TrainingProfit {
60      get { return ((DoubleValue)this[TrainingProfitResultName].Value).Value; }
61      private set { ((DoubleValue)this[TrainingProfitResultName].Value).Value = value; }
62    }
63
64    public double TestProfit {
65      get { return ((DoubleValue)this[TestProfitResultName].Value).Value; }
66      private set { ((DoubleValue)this[TestProfitResultName].Value).Value = value; }
67    }
68
69    [StorableConstructor]
70    protected Solution(bool deserializing) : base(deserializing) { }
71    protected Solution(Solution original, Cloner cloner)
72      : base(original, cloner) {
73    }
74    public Solution(IModel model, IProblemData problemData)
75      : base(model, problemData) {
76      Add(new Result(TrainingSharpeRatioResultName, "Share ratio of the signals of the model on the training partition", new DoubleValue()));
77      Add(new Result(TestSharpeRatioResultName, "Sharpe ratio of the signals of the model on the test partition", new DoubleValue()));
78      Add(new Result(TrainingProfitResultName, "Profit of the model on the training partition", new DoubleValue()));
79      Add(new Result(TestProfitResultName, "Profit of the model on the test partition", new DoubleValue()));
80    }
81
82
83    protected override void OnModelChanged() {
84      base.OnModelChanged();
85      RecalculateResults();
86    }
87
88    protected override void OnProblemDataChanged() {
89      base.OnProblemDataChanged();
90      RecalculateResults();
91    }
92
93    protected override void RecalculateResults() {
94      CalculateTradingResults();
95    }
96
97    protected void CalculateTradingResults() {
98      double[] trainingSignals = TrainingSignals.ToArray(); // cache values
99      IEnumerable<double> trainingReturns = ProblemData.Dataset.GetDoubleValues(ProblemData.PriceVariable, ProblemData.TrainingIndices);
100      double[] testSignals = TestSignals.ToArray(); // cache values
101      IEnumerable<double> testReturns = ProblemData.Dataset.GetDoubleValues(ProblemData.PriceVariable, ProblemData.TestIndices);
102
103      OnlineCalculatorError errorState;
104      double trainingSharpeRatio = OnlineSharpeRatioCalculator.Calculate(trainingReturns, trainingSignals, ProblemData.TransactionCosts, out errorState);
105      TrainingSharpeRatio = errorState == OnlineCalculatorError.None ? trainingSharpeRatio : double.NaN;
106      double testSharpeRatio = OnlineSharpeRatioCalculator.Calculate(testReturns, testSignals, ProblemData.TransactionCosts, out errorState);
107      TestSharpeRatio = errorState == OnlineCalculatorError.None ? testSharpeRatio : double.NaN;
108
109      double trainingProfit = OnlineProfitCalculator.Calculate(trainingReturns, trainingSignals, ProblemData.TransactionCosts, out errorState);
110      TrainingProfit = errorState == OnlineCalculatorError.None ? trainingProfit : double.NaN;
111      double testProfit = OnlineProfitCalculator.Calculate(testReturns, testSignals, ProblemData.TransactionCosts, out errorState);
112      TestProfit = errorState == OnlineCalculatorError.None ? testProfit : double.NaN;
113
114    }
115
116    public virtual IEnumerable<double> Signals {
117      get {
118        return GetSignals(Enumerable.Range(0, ProblemData.Dataset.Rows));
119      }
120    }
121
122    public virtual IEnumerable<double> TrainingSignals {
123      get {
124        return GetSignals(ProblemData.TrainingIndices);
125      }
126    }
127
128    public virtual IEnumerable<double> TestSignals {
129      get {
130        return GetSignals(ProblemData.TestIndices);
131      }
132    }
133
134    public virtual IEnumerable<double> GetSignals(IEnumerable<int> rows) {
135      return Model.GetSignals(ProblemData.Dataset, rows);
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.