Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Solution.cs @ 10635

Last change on this file since 10635 was 10020, checked in by gkronber, 11 years ago

#1508: merged r9804:9805,r9808:9809,r9811:9812,r9822,r9824:9825,r9897,r9928,r9938:9941,r9964:9965,r9989,r9991:9992,r9995,r9997,r10004:10015 from trunk into stable branch.

File size: 5.6 KB
RevLine 
[6123]1#region License Information
2/* HeuristicLab
[9743]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6123]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
[9743]29namespace HeuristicLab.Problems.DataAnalysis.Trading {
[6123]30  /// <summary>
31  /// Abstract base class for trading data analysis solutions
32  /// </summary>
33  [StorableClass]
[9745]34  public abstract class Solution : DataAnalysisSolution, ISolution {
[6123]35    private const string TrainingSharpeRatioResultName = "Sharpe ratio (training)";
36    private const string TestSharpeRatioResultName = "Sharpe ratio (test)";
[9176]37    private const string TrainingProfitResultName = "Profit (training)";
38    private const string TestProfitResultName = "Profit (test)";
[6123]39
[9745]40    public new IModel Model {
41      get { return (IModel)base.Model; }
[6123]42      protected set { base.Model = value; }
43    }
44
[9745]45    public new IProblemData ProblemData {
46      get { return (IProblemData)base.ProblemData; }
[6123]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    }
[9176]59    public double TrainingProfit {
60      get { return ((DoubleValue)this[TrainingProfitResultName].Value).Value; }
61      private set { ((DoubleValue)this[TrainingProfitResultName].Value).Value = value; }
62    }
[6123]63
[9176]64    public double TestProfit {
65      get { return ((DoubleValue)this[TestProfitResultName].Value).Value; }
66      private set { ((DoubleValue)this[TestProfitResultName].Value).Value = value; }
67    }
68
[6123]69    [StorableConstructor]
[9745]70    protected Solution(bool deserializing) : base(deserializing) { }
71    protected Solution(Solution original, Cloner cloner)
[6123]72      : base(original, cloner) {
73    }
[9745]74    public Solution(IModel model, IProblemData problemData)
[6123]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()));
[9176]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()));
[6123]80    }
81
[6937]82    protected override void RecalculateResults() {
[9744]83      CalculateTradingResults();
84    }
85
86    protected void CalculateTradingResults() {
[6123]87      double[] trainingSignals = TrainingSignals.ToArray(); // cache values
[10020]88      IEnumerable<double> trainingReturns = ProblemData.Dataset.GetDoubleValues(ProblemData.PriceChangeVariable, ProblemData.TrainingIndices);
[6123]89      double[] testSignals = TestSignals.ToArray(); // cache values
[10020]90      IEnumerable<double> testReturns = ProblemData.Dataset.GetDoubleValues(ProblemData.PriceChangeVariable, ProblemData.TestIndices);
[9176]91
[6123]92      OnlineCalculatorError errorState;
93      double trainingSharpeRatio = OnlineSharpeRatioCalculator.Calculate(trainingReturns, trainingSignals, ProblemData.TransactionCosts, out errorState);
94      TrainingSharpeRatio = errorState == OnlineCalculatorError.None ? trainingSharpeRatio : double.NaN;
95      double testSharpeRatio = OnlineSharpeRatioCalculator.Calculate(testReturns, testSignals, ProblemData.TransactionCosts, out errorState);
96      TestSharpeRatio = errorState == OnlineCalculatorError.None ? testSharpeRatio : double.NaN;
97
[9176]98      double trainingProfit = OnlineProfitCalculator.Calculate(trainingReturns, trainingSignals, ProblemData.TransactionCosts, out errorState);
99      TrainingProfit = errorState == OnlineCalculatorError.None ? trainingProfit : double.NaN;
100      double testProfit = OnlineProfitCalculator.Calculate(testReturns, testSignals, ProblemData.TransactionCosts, out errorState);
101      TestProfit = errorState == OnlineCalculatorError.None ? testProfit : double.NaN;
102
[6123]103    }
104
105    public virtual IEnumerable<double> Signals {
[10020]106      get { return GetSignals(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
[6123]107    }
108    public virtual IEnumerable<double> TrainingSignals {
[10020]109      get { return GetSignals(ProblemData.TrainingIndices); }
[6123]110    }
111    public virtual IEnumerable<double> TestSignals {
[10020]112      get { return GetSignals(ProblemData.TestIndices); }
[6123]113    }
114    public virtual IEnumerable<double> GetSignals(IEnumerable<int> rows) {
115      return Model.GetSignals(ProblemData.Dataset, rows);
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.