Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9989 for trunk


Ignore:
Timestamp:
09/19/13 10:16:25 (11 years ago)
Author:
gkronber
Message:

#1508 worked on problem instance providers for trading problem

Location:
trunk/sources
Files:
7 edited
1 copied
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Trading/3.4/CsvProblemInstanceProvider.cs

    r9987 r9989  
    2626using System.Linq;
    2727using HeuristicLab.Common;
    28 using HeuristicLab.Problems.DataAnalysis;
     28using HeuristicLab.Problems.Instances;
     29using HeuristicLab.Problems.Instances.DataAnalysis;
    2930
    30 namespace HeuristicLab.Problems.Instances.DataAnalysis {
    31   public class RegressionCSVInstanceProvider : RegressionInstanceProvider {
     31namespace HeuristicLab.Problems.DataAnalysis.Trading {
     32  public class CsvProblemInstanceProvider : ProblemInstanceProvider<IProblemData> {
    3233    public override string Name {
    3334      get { return "CSV File"; }
     
    3536    public override string Description {
    3637      get {
    37         return "";
     38        return "Comma separated values file importer";
    3839      }
    3940    }
     
    4849      return new List<IDataDescriptor>();
    4950    }
    50     public override IRegressionProblemData LoadData(IDataDescriptor descriptor) {
     51    public override IProblemData LoadData(IDataDescriptor descriptor) {
    5152      throw new NotImplementedException();
    5253    }
     
    5556      get { return true; }
    5657    }
    57     public override IRegressionProblemData ImportData(string path) {
     58    public override IProblemData ImportData(string path) {
    5859      TableFileParser csvFileParser = new TableFileParser();
    5960      csvFileParser.Parse(path, csvFileParser.AreColumnNamesInFirstLine(path));
    6061
    6162      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
    62       string targetVar = dataset.DoubleVariables.Last();
     63      string targetVar = (from v in dataset.DoubleVariables
     64                          where dataset.GetReadOnlyDoubleValues(v).Min() <= 0
     65                          where dataset.GetReadOnlyDoubleValues(v).Max() >= 0
     66                          select v).LastOrDefault();
     67      if (targetVar == null) throw new ArgumentException("The target variable must contain changes (deltas) of the asset price over time.");
    6368
    64       // turn of input variables that are constant in the training partition
     69      // turn off input variables that are constant in the training partition
    6570      var allowedInputVars = new List<string>();
    6671      var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
     
    7580      }
    7681
    77       IRegressionProblemData regressionData = new RegressionProblemData(dataset, allowedInputVars, targetVar);
     82      IProblemData problemData = new ProblemData(dataset, allowedInputVars, targetVar);
    7883
    7984      var trainingPartEnd = trainingIndizes.Last();
    80       regressionData.TrainingPartition.Start = trainingIndizes.First();
    81       regressionData.TrainingPartition.End = trainingPartEnd;
    82       regressionData.TestPartition.Start = trainingPartEnd;
    83       regressionData.TestPartition.End = csvFileParser.Rows;
     85      problemData.TrainingPartition.Start = trainingIndizes.First();
     86      problemData.TrainingPartition.End = trainingPartEnd;
     87      problemData.TestPartition.Start = trainingPartEnd;
     88      problemData.TestPartition.End = csvFileParser.Rows;
    8489
    85       regressionData.Name = Path.GetFileName(path);
     90      problemData.Name = Path.GetFileName(path);
    8691
    87       return regressionData;
    88     }
    89 
    90     protected override IRegressionProblemData ImportData(string path, RegressionImportType type, TableFileParser csvFileParser) {
    91       List<IList> values = csvFileParser.Values;
    92       if (type.Shuffle) {
    93         values = Shuffle(values);
    94       }
    95       Dataset dataset = new Dataset(csvFileParser.VariableNames, values);
    96 
    97       // turn of input variables that are constant in the training partition
    98       var allowedInputVars = new List<string>();
    99       int trainingPartEnd = (csvFileParser.Rows * type.TrainingPercentage) / 100;
    100       trainingPartEnd = trainingPartEnd > 0 ? trainingPartEnd : 1;
    101       var trainingIndizes = Enumerable.Range(0, trainingPartEnd);
    102       if (trainingIndizes.Count() >= 2) {
    103         foreach (var variableName in dataset.DoubleVariables) {
    104           if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
    105             variableName != type.TargetVariable)
    106             allowedInputVars.Add(variableName);
    107         }
    108       } else {
    109         allowedInputVars.AddRange(dataset.DoubleVariables.Where(x => !x.Equals(type.TargetVariable)));
    110       }
    111 
    112       RegressionProblemData regressionData = new RegressionProblemData(dataset, allowedInputVars, type.TargetVariable);
    113 
    114       regressionData.TrainingPartition.Start = 0;
    115       regressionData.TrainingPartition.End = trainingPartEnd;
    116       regressionData.TestPartition.Start = trainingPartEnd;
    117       regressionData.TestPartition.End = csvFileParser.Rows;
    118 
    119       regressionData.Name = Path.GetFileName(path);
    120 
    121       return regressionData;
     92      return problemData;
    12293    }
    12394  }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Trading/3.4/EcbProblemInstanceProvider.cs

    r9987 r9989  
    2929
    3030namespace HeuristicLab.Problems.DataAnalysis.Trading {
    31 
    3231  public class EcbProblemInstanceProvider : ProblemInstanceProvider<IProblemData> {
    3332    private class EcbDataDescriptor : IDataDescriptor {
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Trading/3.4/HeuristicLab.Problems.DataAnalysis.Trading-3.4.csproj

    r9964 r9989  
    103103    <Compile Include="Calculators\OnlineProfitCalculator.cs" />
    104104    <Compile Include="Calculators\OnlineSharpeRatioCalculator.cs" />
     105    <Compile Include="CsvProblemInstanceProvider.cs" />
     106    <Compile Include="EcbProblemInstanceProvider.cs" />
    105107    <Compile Include="Interfaces\IModel.cs" />
    106108    <Compile Include="Interfaces\IProblem.cs" />
     
    109111    <Compile Include="Plugin.cs" />
    110112    <Compile Include="ProblemData.cs" />
    111     <Compile Include="ProblemInstanceProvider.cs" />
    112113    <Compile Include="Solution.cs" />
    113114    <Compile Include="Symbolic\Interfaces\IEvaluator.cs" />
     
    205206      <Name>HeuristicLab.Problems.DataAnalysis-3.4</Name>
    206207      <Private>False</Private>
     208    </ProjectReference>
     209    <ProjectReference Include="..\..\HeuristicLab.Problems.Instances.DataAnalysis\3.3\HeuristicLab.Problems.Instances.DataAnalysis-3.3.csproj">
     210      <Project>{94C7714E-29D4-4D6D-B213-2C18D627AB75}</Project>
     211      <Name>HeuristicLab.Problems.Instances.DataAnalysis-3.3</Name>
    207212    </ProjectReference>
    208213    <ProjectReference Include="..\..\HeuristicLab.Problems.Instances\3.3\HeuristicLab.Problems.Instances-3.3.csproj">
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Interfaces/IProblemData.cs

    r9745 r9989  
    2222namespace HeuristicLab.Problems.DataAnalysis.Trading {
    2323  public interface IProblemData : IDataAnalysisProblemData {
    24     string PriceVariable { get; }
     24    string PriceChangeVariable { get; }
    2525    double TransactionCosts { get; }
    2626  }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Trading/3.4/ProblemData.cs

    r9812 r9989  
    3333  [Item("TradingProblemData", "Represents an item containing all data defining a trading problem.")]
    3434  public sealed class ProblemData : DataAnalysisProblemData, IProblemData {
    35     private const string PriceVariableParameterName = "PriceVariable";
     35    private const string PriceChangeVariableParameterName = "PriceChangeVariable";
    3636    private const string TransactionCostsParameterName = "TransactionCosts";
    3737
     
    15891589
    15901590    static ProblemData() {
    1591       defaultDataset = new Dataset(new string[] { "AUD" }, audInUsdDiff);
     1591      defaultDataset = new Dataset(new string[] { "d(AUD/USD)/dt" }, audInUsdDiff);
    15921592      defaultDataset.Name = "AUD in USD";
    1593       defaultDataset.Description = "Price of Australian dollar in US dollar.";
    1594       defaultAllowedInputVariables = new List<string>() { "AUD" };
    1595       defaultPriceVariable = "AUD";
     1593      defaultDataset.Description = "Australian dollar in US dollar.";
     1594      defaultAllowedInputVariables = new List<string>() { "d(AUD/USD)/dt" };
     1595      defaultPriceVariable = "d(AUD/USD)/dt";
    15961596    }
    15971597    #endregion
    15981598
    1599     public IConstrainedValueParameter<StringValue> PriceVariableParameter {
    1600       get { return (IConstrainedValueParameter<StringValue>)Parameters[PriceVariableParameterName]; }
     1599    public IConstrainedValueParameter<StringValue> PriceChangeVariableParameter {
     1600      get { return (IConstrainedValueParameter<StringValue>)Parameters[PriceChangeVariableParameterName]; }
    16011601    }
    16021602    public IValueParameter<DoubleValue> TransactionCostsParameter {
    16031603      get { return (IValueParameter<DoubleValue>)Parameters[TransactionCostsParameterName]; }
    16041604    }
    1605     public string PriceVariable {
    1606       get { return PriceVariableParameter.Value.Value; }
     1605    public string PriceChangeVariable {
     1606      get { return PriceChangeVariableParameter.Value.Value; }
    16071607    }
    16081608    public double TransactionCosts {
     
    16301630      : base(dataset, allowedInputVariables) {
    16311631      var variables = InputVariables.Select(x => x.AsReadOnly()).ToList();
    1632       Parameters.Add(new ConstrainedValueParameter<StringValue>(PriceVariableParameterName, new ItemSet<StringValue>(variables), variables.First(x => x.Value == targetVariable)));
     1632      Parameters.Add(new ConstrainedValueParameter<StringValue>(PriceChangeVariableParameterName, new ItemSet<StringValue>(variables), variables.First(x => x.Value == targetVariable)));
    16331633      Parameters.Add(new FixedValueParameter<DoubleValue>(TransactionCostsParameterName, "The absolute cost of on buy/sell transaction (assumed to be constant and independent of transaction volume)", new DoubleValue(0.0002)));
     1634
     1635      if (dataset.GetReadOnlyDoubleValues(targetVariable).Min() >= 0) throw new ArgumentException("The target variable must contain changes (deltas) of the asset price over time.");
     1636
    16341637      RegisterParameterEvents();
    16351638    }
    16361639
    16371640    private void RegisterParameterEvents() {
    1638       PriceVariableParameter.ValueChanged += new EventHandler(PriceVariableParameter_ValueChanged);
     1641      PriceChangeVariableParameter.ValueChanged += new EventHandler(PriceVariableParameter_ValueChanged);
    16391642      TransactionCostsParameter.Value.ValueChanged += new EventHandler(TransactionCostsParameter_ValueChanged);
    16401643    }
     
    16441647    }
    16451648    private void PriceVariableParameter_ValueChanged(object sender, EventArgs e) {
     1649      if (Dataset.GetReadOnlyDoubleValues(PriceChangeVariable).Min() >= 0) throw new ArgumentException("The target variable must contain changes (deltas) of the asset price over time.");
    16461650      OnChanged();
    16471651    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Solution.cs

    r9928 r9989  
    8686    protected void CalculateTradingResults() {
    8787      double[] trainingSignals = TrainingSignals.ToArray(); // cache values
    88       IEnumerable<double> trainingReturns = ProblemData.Dataset.GetDoubleValues(ProblemData.PriceVariable, ProblemData.TrainingIndices);
     88      IEnumerable<double> trainingReturns = ProblemData.Dataset.GetDoubleValues(ProblemData.PriceChangeVariable, ProblemData.TrainingIndices);
    8989      double[] testSignals = TestSignals.ToArray(); // cache values
    90       IEnumerable<double> testReturns = ProblemData.Dataset.GetDoubleValues(ProblemData.PriceVariable, ProblemData.TestIndices);
     90      IEnumerable<double> testReturns = ProblemData.Dataset.GetDoubleValues(ProblemData.PriceChangeVariable, ProblemData.TestIndices);
    9191
    9292      OnlineCalculatorError errorState;
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Symbolic/SingleObjective/ProfitEvaluator.cs

    r9825 r9989  
    5959    public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, IProblemData problemData, IEnumerable<int> rows) {
    6060      IEnumerable<double> signals = GetSignals(interpreter, solution, problemData.Dataset, rows);
    61       IEnumerable<double> returns = problemData.Dataset.GetDoubleValues(problemData.PriceVariable, rows);
     61      IEnumerable<double> returns = problemData.Dataset.GetDoubleValues(problemData.PriceChangeVariable, rows);
    6262      OnlineCalculatorError errorState;
    6363      double profit = OnlineProfitCalculator.Calculate(returns, signals, problemData.TransactionCosts, out errorState);
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Symbolic/SingleObjective/SharpeRatioEvaluator.cs

    r9825 r9989  
    5959    public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, IProblemData problemData, IEnumerable<int> rows) {
    6060      IEnumerable<double> signals = GetSignals(interpreter, solution, problemData.Dataset, rows);
    61       IEnumerable<double> returns = problemData.Dataset.GetDoubleValues(problemData.PriceVariable, rows);
     61      IEnumerable<double> returns = problemData.Dataset.GetDoubleValues(problemData.PriceChangeVariable, rows);
    6262      OnlineCalculatorError errorState;
    6363      double sharpRatio = OnlineSharpeRatioCalculator.Calculate(returns, signals, problemData.TransactionCosts, out errorState);
  • trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/CSV/RegressionCSVInstanceProvider.cs

    r9608 r9989  
    6262      string targetVar = dataset.DoubleVariables.Last();
    6363
    64       // turn of input variables that are constant in the training partition
     64      // turn off input variables that are constant in the training partition
    6565      var allowedInputVars = new List<string>();
    6666      var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
Note: See TracChangeset for help on using the changeset viewer.