using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Problems.DataAnalysis; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Data; using HeuristicLab.Common; using HeuristicLab.Optimization; using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; using HeuristicLab.Problems.TradeRules.Evaluator; namespace HeuristicLab.Problems.TradeRules { [StorableClass] public abstract class TradeRulesSolutionBase : DataAnalysisSolution, IRegressionSolution { private const string TrainingCashResultName = "Cash after the operation (training)"; private const string TestCashResultName = "Cash after the operation (test)"; private const string TrainingUpDaysResultName = "Number of days that stock market goes up (training)"; private const string TrainingDownDaysResultName = "Number of days that stock market goes down (training)"; private const string TrainingHeuristicResultName = "Percentage of right actions after the operation (training)"; private const string TestHeuristicResultName = "Percentage of right actions after the operation (test)"; private const string TrainingMeanSquaredErrorResultName = "Mean squared error (training)"; private const string TestMeanSquaredErrorResultName = "Mean squared error (test)"; private const string TrainingMeanAbsoluteErrorResultName = "Mean absolute error (training)"; private const string TestMeanAbsoluteErrorResultName = "Mean absolute error (test)"; private const string TrainingSquaredCorrelationResultName = "Pearson's R² (training)"; private const string TestSquaredCorrelationResultName = "Pearson's R² (test)"; private const string TrainingRelativeErrorResultName = "Average relative error (training)"; private const string TestRelativeErrorResultName = "Average relative error (test)"; private const string TrainingNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (training)"; private const string TestNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (test)"; private const string TrainingMeanErrorResultName = "Mean error (training)"; private const string TestMeanErrorResultName = "Mean error (test)"; private const string TestTradeDaysResultName = "Trade days"; private const string TestNumberTradesResultName = "Number of trades"; private const string TestTotalTradesResultName = "Total trades"; public new IRegressionModel Model { get { return (IRegressionModel)base.Model; } protected set { base.Model = value; } } public new IRegressionProblemData ProblemData { get { return (IRegressionProblemData)base.ProblemData; } set { base.ProblemData = value; } } public abstract IEnumerable EstimatedValues { get; } public abstract IEnumerable EstimatedTrainingValues { get; } public abstract IEnumerable EstimatedTestValues { get; } public abstract IEnumerable GetEstimatedValues(IEnumerable rows); #region Results public double TrainingCash { get { return ((DoubleValue)this[TrainingCashResultName].Value).Value; } private set { ((DoubleValue)this[TrainingCashResultName].Value).Value = value; } } public double TestCash { get { return ((DoubleValue)this[TestCashResultName].Value).Value; } private set { ((DoubleValue)this[TestCashResultName].Value).Value = value; } } public double TrainingHeuristic { get { return ((DoubleValue)this[TrainingHeuristicResultName].Value).Value; } private set { ((DoubleValue)this[TrainingHeuristicResultName].Value).Value = value; } } public double TestHeuristic { get { return ((DoubleValue)this[TestHeuristicResultName].Value).Value; } private set { ((DoubleValue)this[TestHeuristicResultName].Value).Value = value; } } public double NumberUpDays { get { return ((DoubleValue)this[TrainingUpDaysResultName].Value).Value; } private set { ((DoubleValue)this[TrainingUpDaysResultName].Value).Value = value; } } public double NumberDownDays { get { return ((DoubleValue)this[TrainingDownDaysResultName].Value).Value; } private set { ((DoubleValue)this[TrainingDownDaysResultName].Value).Value = value; } } public double TradeDays { get { return ((DoubleValue)this[TestTradeDaysResultName].Value).Value; } private set { ((DoubleValue)this[TestTradeDaysResultName].Value).Value = value; } } public double NumberTrades { get { return ((DoubleValue)this[TestNumberTradesResultName].Value).Value; } private set { ((DoubleValue)this[TestNumberTradesResultName].Value).Value = value; } } public double TotalTrades { get { return ((DoubleValue)this[TestTotalTradesResultName].Value).Value; } private set { ((DoubleValue)this[TestTotalTradesResultName].Value).Value = value; } } public double TrainingMeanSquaredError { get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; } private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; } } public double TestMeanSquaredError { get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; } private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; } } public double TrainingMeanAbsoluteError { get { return ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value; } private set { ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value = value; } } public double TestMeanAbsoluteError { get { return ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value; } private set { ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value = value; } } public double TrainingRSquared { get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; } private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; } } public double TestRSquared { get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; } private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; } } public double TrainingRelativeError { get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; } private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; } } public double TestRelativeError { get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; } private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; } } public double TrainingNormalizedMeanSquaredError { get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; } private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; } } public double TestNormalizedMeanSquaredError { get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; } private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; } } public double TrainingMeanError { get { return ((DoubleValue)this[TrainingMeanErrorResultName].Value).Value; } private set { ((DoubleValue)this[TrainingMeanErrorResultName].Value).Value = value; } } public double TestMeanError { get { return ((DoubleValue)this[TestMeanErrorResultName].Value).Value; } private set { ((DoubleValue)this[TestMeanErrorResultName].Value).Value = value; } } #endregion [StorableConstructor] protected TradeRulesSolutionBase(bool deserializing) : base(deserializing) { } protected TradeRulesSolutionBase(TradeRulesSolutionBase original, Cloner cloner) : base(original, cloner) { } protected TradeRulesSolutionBase(IRegressionModel model, IRegressionProblemData problemData) : base(model, problemData) { /*Add(new Result(TrainingCashResultName, "Cash obtained after training period in the stock market", new DoubleValue()));*/ Add(new Result(TestCashResultName, "Cash obtained after test period in the stock market", new DoubleValue())); Add(new Result(TrainingHeuristicResultName, "Percentage of right actions after training period in the stock market", new DoubleValue())); Add(new Result(TestHeuristicResultName, "Percentage of right actions after test period in the stock market", new DoubleValue())); Add(new Result(TrainingUpDaysResultName, "Number of days actions go up in the stock market", new DoubleValue())); Add(new Result(TrainingDownDaysResultName, "Number of days actions down up in the stock market", new DoubleValue())); Add(new Result(TestTradeDaysResultName, "Number of trading days", new DoubleValue())); Add(new Result(TestNumberTradesResultName, "Number of trades", new DoubleValue())); Add(new Result(TestTotalTradesResultName, "Total trades", new DoubleValue())); } protected void CalculateResults() { IEnumerable estimatedValues = EstimatedValues; IEnumerable estimatedTrainingValues = EstimatedTrainingValues; // cache values IEnumerable originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices); IEnumerable estimatedTestValues = EstimatedTestValues; // cache values IEnumerable originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices); /*double trainingCTR = OnlineTradeRulesCalculator.Calculate(estimatedTrainingValues, ProblemData, ProblemData.TrainingIndices); TrainingCash = trainingCTR;*/ double trainingHTR = OnlineHeuristicTradeRulesCalculator.Calculate(estimatedTrainingValues, ProblemData, ProblemData.TrainingIndices); TrainingHeuristic = trainingHTR; double trainingUpDays = OnlineHeuristicTradeRulesCalculator.getNumberUpDays(); NumberUpDays = trainingUpDays; double trainingDownDays = OnlineHeuristicTradeRulesCalculator.getNumberDownDays(); NumberDownDays = trainingDownDays; double testHTR = OnlineHeuristicTradeRulesCalculator.Calculate(estimatedTestValues, ProblemData, ProblemData.TestIndices); TestHeuristic = testHTR; double testCTR = OnlineTradeRulesCalculator.Calculate(estimatedTestValues, ProblemData, ProblemData.TestIndices); TestCash = testCTR; double testTradeDays = OnlineTradeRulesCalculator.getTradeDays(); TradeDays = testTradeDays; double testNumberTrades = OnlineTradeRulesCalculator.getNumberTrades(); NumberTrades = testNumberTrades; double testTotalTradeDays = OnlineTradeRulesCalculator.getTotalTradesDays(); TotalTrades = testTotalTradeDays; } } }