1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Problems.DataAnalysis;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using HeuristicLab.Optimization;
|
---|
10 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
11 | using HeuristicLab.Problems.TradeRules.Evaluator;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Problems.TradeRules
|
---|
14 | {
|
---|
15 | [StorableClass]
|
---|
16 | public abstract class TradeRulesSolutionBase : DataAnalysisSolution, IRegressionSolution
|
---|
17 | {
|
---|
18 | private const string TrainingCashResultName = "Cash after the operation (training)";
|
---|
19 | private const string TestCashResultName = "Cash after the operation (test)";
|
---|
20 | private const string TrainingUpDaysResultName = "Number of days that stock market goes up (training)";
|
---|
21 | private const string TrainingDownDaysResultName = "Number of days that stock market goes down (training)";
|
---|
22 | private const string TrainingHeuristicResultName = "Percentage of right actions after the operation (training)";
|
---|
23 | private const string TestHeuristicResultName = "Percentage of right actions after the operation (test)";
|
---|
24 | private const string TrainingMeanSquaredErrorResultName = "Mean squared error (training)";
|
---|
25 | private const string TestMeanSquaredErrorResultName = "Mean squared error (test)";
|
---|
26 | private const string TrainingMeanAbsoluteErrorResultName = "Mean absolute error (training)";
|
---|
27 | private const string TestMeanAbsoluteErrorResultName = "Mean absolute error (test)";
|
---|
28 | private const string TrainingSquaredCorrelationResultName = "Pearson's R² (training)";
|
---|
29 | private const string TestSquaredCorrelationResultName = "Pearson's R² (test)";
|
---|
30 | private const string TrainingRelativeErrorResultName = "Average relative error (training)";
|
---|
31 | private const string TestRelativeErrorResultName = "Average relative error (test)";
|
---|
32 | private const string TrainingNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (training)";
|
---|
33 | private const string TestNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (test)";
|
---|
34 | private const string TrainingMeanErrorResultName = "Mean error (training)";
|
---|
35 | private const string TestMeanErrorResultName = "Mean error (test)";
|
---|
36 | private const string TestTradeDaysResultName = "Trade days";
|
---|
37 | private const string TestNumberTradesResultName = "Number of trades";
|
---|
38 | private const string TestTotalTradesResultName = "Total trades";
|
---|
39 |
|
---|
40 |
|
---|
41 | public new IRegressionModel Model
|
---|
42 | {
|
---|
43 | get { return (IRegressionModel)base.Model; }
|
---|
44 | protected set { base.Model = value; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public new IRegressionProblemData ProblemData
|
---|
48 | {
|
---|
49 | get { return (IRegressionProblemData)base.ProblemData; }
|
---|
50 | set { base.ProblemData = value; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public abstract IEnumerable<double> EstimatedValues { get; }
|
---|
54 | public abstract IEnumerable<double> EstimatedTrainingValues { get; }
|
---|
55 | public abstract IEnumerable<double> EstimatedTestValues { get; }
|
---|
56 | public abstract IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows);
|
---|
57 |
|
---|
58 |
|
---|
59 | #region Results
|
---|
60 | public double TrainingCash
|
---|
61 | {
|
---|
62 | get { return ((DoubleValue)this[TrainingCashResultName].Value).Value; }
|
---|
63 | private set { ((DoubleValue)this[TrainingCashResultName].Value).Value = value; }
|
---|
64 | }
|
---|
65 | public double TestCash
|
---|
66 | {
|
---|
67 | get { return ((DoubleValue)this[TestCashResultName].Value).Value; }
|
---|
68 | private set { ((DoubleValue)this[TestCashResultName].Value).Value = value; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public double TrainingHeuristic
|
---|
72 | {
|
---|
73 | get { return ((DoubleValue)this[TrainingHeuristicResultName].Value).Value; }
|
---|
74 | private set { ((DoubleValue)this[TrainingHeuristicResultName].Value).Value = value; }
|
---|
75 | }
|
---|
76 | public double TestHeuristic
|
---|
77 | {
|
---|
78 | get { return ((DoubleValue)this[TestHeuristicResultName].Value).Value; }
|
---|
79 | private set { ((DoubleValue)this[TestHeuristicResultName].Value).Value = value; }
|
---|
80 | }
|
---|
81 |
|
---|
82 | public double NumberUpDays
|
---|
83 | {
|
---|
84 | get { return ((DoubleValue)this[TrainingUpDaysResultName].Value).Value; }
|
---|
85 | private set { ((DoubleValue)this[TrainingUpDaysResultName].Value).Value = value; }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public double NumberDownDays
|
---|
89 | {
|
---|
90 | get { return ((DoubleValue)this[TrainingDownDaysResultName].Value).Value; }
|
---|
91 | private set { ((DoubleValue)this[TrainingDownDaysResultName].Value).Value = value; }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public double TradeDays
|
---|
95 | {
|
---|
96 | get { return ((DoubleValue)this[TestTradeDaysResultName].Value).Value; }
|
---|
97 | private set { ((DoubleValue)this[TestTradeDaysResultName].Value).Value = value; }
|
---|
98 | }
|
---|
99 | public double NumberTrades
|
---|
100 | {
|
---|
101 | get { return ((DoubleValue)this[TestNumberTradesResultName].Value).Value; }
|
---|
102 | private set { ((DoubleValue)this[TestNumberTradesResultName].Value).Value = value; }
|
---|
103 | }
|
---|
104 | public double TotalTrades
|
---|
105 | {
|
---|
106 | get { return ((DoubleValue)this[TestTotalTradesResultName].Value).Value; }
|
---|
107 | private set { ((DoubleValue)this[TestTotalTradesResultName].Value).Value = value; }
|
---|
108 | }
|
---|
109 | public double TrainingMeanSquaredError
|
---|
110 | {
|
---|
111 | get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
|
---|
112 | private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
|
---|
113 | }
|
---|
114 | public double TestMeanSquaredError
|
---|
115 | {
|
---|
116 | get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
|
---|
117 | private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
|
---|
118 | }
|
---|
119 | public double TrainingMeanAbsoluteError
|
---|
120 | {
|
---|
121 | get { return ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value; }
|
---|
122 | private set { ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value = value; }
|
---|
123 | }
|
---|
124 | public double TestMeanAbsoluteError
|
---|
125 | {
|
---|
126 | get { return ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value; }
|
---|
127 | private set { ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value = value; }
|
---|
128 | }
|
---|
129 | public double TrainingRSquared
|
---|
130 | {
|
---|
131 | get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
|
---|
132 | private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
|
---|
133 | }
|
---|
134 | public double TestRSquared
|
---|
135 | {
|
---|
136 | get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
|
---|
137 | private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
|
---|
138 | }
|
---|
139 | public double TrainingRelativeError
|
---|
140 | {
|
---|
141 | get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
|
---|
142 | private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
|
---|
143 | }
|
---|
144 | public double TestRelativeError
|
---|
145 | {
|
---|
146 | get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
|
---|
147 | private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
|
---|
148 | }
|
---|
149 | public double TrainingNormalizedMeanSquaredError
|
---|
150 | {
|
---|
151 | get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }
|
---|
152 | private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }
|
---|
153 | }
|
---|
154 | public double TestNormalizedMeanSquaredError
|
---|
155 | {
|
---|
156 | get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }
|
---|
157 | private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }
|
---|
158 | }
|
---|
159 | public double TrainingMeanError
|
---|
160 | {
|
---|
161 | get { return ((DoubleValue)this[TrainingMeanErrorResultName].Value).Value; }
|
---|
162 | private set { ((DoubleValue)this[TrainingMeanErrorResultName].Value).Value = value; }
|
---|
163 | }
|
---|
164 | public double TestMeanError
|
---|
165 | {
|
---|
166 | get { return ((DoubleValue)this[TestMeanErrorResultName].Value).Value; }
|
---|
167 | private set { ((DoubleValue)this[TestMeanErrorResultName].Value).Value = value; }
|
---|
168 | }
|
---|
169 | #endregion
|
---|
170 |
|
---|
171 |
|
---|
172 | [StorableConstructor]
|
---|
173 | protected TradeRulesSolutionBase(bool deserializing) : base(deserializing) { }
|
---|
174 | protected TradeRulesSolutionBase(TradeRulesSolutionBase original, Cloner cloner)
|
---|
175 | : base(original, cloner) {
|
---|
176 | }
|
---|
177 | protected TradeRulesSolutionBase(IRegressionModel model, IRegressionProblemData problemData)
|
---|
178 | : base(model, problemData) {
|
---|
179 | /*Add(new Result(TrainingCashResultName, "Cash obtained after training period in the stock market", new DoubleValue()));*/
|
---|
180 | Add(new Result(TestCashResultName, "Cash obtained after test period in the stock market", new DoubleValue()));
|
---|
181 | Add(new Result(TrainingHeuristicResultName, "Percentage of right actions after training period in the stock market", new DoubleValue()));
|
---|
182 | Add(new Result(TestHeuristicResultName, "Percentage of right actions after test period in the stock market", new DoubleValue()));
|
---|
183 | Add(new Result(TrainingUpDaysResultName, "Number of days actions go up in the stock market", new DoubleValue()));
|
---|
184 | Add(new Result(TrainingDownDaysResultName, "Number of days actions down up in the stock market", new DoubleValue()));
|
---|
185 |
|
---|
186 | Add(new Result(TestTradeDaysResultName, "Number of trading days", new DoubleValue()));
|
---|
187 | Add(new Result(TestNumberTradesResultName, "Number of trades", new DoubleValue()));
|
---|
188 | Add(new Result(TestTotalTradesResultName, "Total trades", new DoubleValue()));
|
---|
189 | }
|
---|
190 | protected void CalculateResults()
|
---|
191 | {
|
---|
192 | IEnumerable<double> estimatedValues = EstimatedValues;
|
---|
193 | IEnumerable<double> estimatedTrainingValues = EstimatedTrainingValues; // cache values
|
---|
194 | IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);
|
---|
195 | IEnumerable<double> estimatedTestValues = EstimatedTestValues; // cache values
|
---|
196 | IEnumerable<double> originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices);
|
---|
197 |
|
---|
198 | /*double trainingCTR = OnlineTradeRulesCalculator.Calculate(estimatedTrainingValues, ProblemData, ProblemData.TrainingIndices);
|
---|
199 | TrainingCash = trainingCTR;*/
|
---|
200 |
|
---|
201 | double trainingHTR = OnlineHeuristicTradeRulesCalculator.Calculate(estimatedTrainingValues, ProblemData, ProblemData.TrainingIndices);
|
---|
202 | TrainingHeuristic = trainingHTR;
|
---|
203 | double trainingUpDays = OnlineHeuristicTradeRulesCalculator.getNumberUpDays();
|
---|
204 | NumberUpDays = trainingUpDays;
|
---|
205 | double trainingDownDays = OnlineHeuristicTradeRulesCalculator.getNumberDownDays();
|
---|
206 | NumberDownDays = trainingDownDays;
|
---|
207 |
|
---|
208 | double testHTR = OnlineHeuristicTradeRulesCalculator.Calculate(estimatedTestValues, ProblemData, ProblemData.TestIndices);
|
---|
209 | TestHeuristic = testHTR;
|
---|
210 |
|
---|
211 | double testCTR = OnlineTradeRulesCalculator.Calculate(estimatedTestValues, ProblemData, ProblemData.TestIndices);
|
---|
212 | TestCash = testCTR;
|
---|
213 | double testTradeDays = OnlineTradeRulesCalculator.getTradeDays();
|
---|
214 | TradeDays = testTradeDays;
|
---|
215 | double testNumberTrades = OnlineTradeRulesCalculator.getNumberTrades();
|
---|
216 | NumberTrades = testNumberTrades;
|
---|
217 | double testTotalTradeDays = OnlineTradeRulesCalculator.getTotalTradesDays();
|
---|
218 | TotalTrades = testTotalTradeDays;
|
---|
219 |
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | }
|
---|
224 | }
|
---|