Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8458


Ignore:
Timestamp:
08/09/12 13:48:43 (12 years ago)
Author:
mkommend
Message:

#1081: Derived time series classes from regression classes to avoid code duplication.

Location:
branches/HeuristicLab.TimeSeries
Files:
22 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Views/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Views-3.4.csproj

    r7989 r8458  
    106106    <Reference Include="HeuristicLab.Persistence-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" />
    107107    <Reference Include="HeuristicLab.PluginInfrastructure-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" />
     108    <Reference Include="HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" />
    108109    <Reference Include="HeuristicLab.Visualization.ChartControlsExtensions-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" />
    109110    <Reference Include="System" />
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Views/3.4/InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView.cs

    r8430 r8458  
    5151    protected override void UpdateModel(ISymbolicExpressionTree tree) {
    5252      var model = new SymbolicTimeSeriesPrognosisModel(tree, Content.Model.Interpreter);
    53       SymbolicTimeSeriesPrognosisModel.Scale(model, Content.ProblemData, Content.ProblemData.TrainingIndices);
     53      SymbolicTimeSeriesPrognosisModel.Scale(model, Content.ProblemData);
    5454      Content.Model = model;
    5555    }
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis-3.4.csproj

    r7989 r8458  
    107107    <Reference Include="HeuristicLab.Persistence-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" />
    108108    <Reference Include="HeuristicLab.PluginInfrastructure-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" />
     109    <Reference Include="HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" />
    109110    <Reference Include="HeuristicLab.Problems.Instances-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" />
    110111    <Reference Include="System" />
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator.cs

    r8114 r8458  
    7676      } else if (applyLinearScaling) { //first create model to perform linear scaling and afterwards calculate fitness for the scaled model
    7777        var model = new SymbolicTimeSeriesPrognosisModel((ISymbolicExpressionTree)solution.Clone(), interpreter, lowerEstimationLimit, upperEstimationLimit);
    78         SymbolicTimeSeriesPrognosisModel.Scale(model, problemData, rows);
     78        SymbolicTimeSeriesPrognosisModel.Scale(model, problemData);
    7979        var scaledSolution = model.SymbolicExpressionTree;
    8080        estimatedValues = interpreter.GetSymbolicExpressionTreeValues(scaledSolution, problemData.Dataset, rows, horizions).SelectMany(x => x);
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveTrainingBestSolutionAnalyzer.cs

    r8430 r8458  
    7676    protected override ISymbolicTimeSeriesPrognosisSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) {
    7777      var model = new SymbolicTimeSeriesPrognosisModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue as ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    78       if (ApplyLinearScaling.Value)
    79         SymbolicTimeSeriesPrognosisModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
     78      if (ApplyLinearScaling.Value) SymbolicTimeSeriesPrognosisModel.Scale(model, ProblemDataParameter.ActualValue);
    8079      return new SymbolicTimeSeriesPrognosisSolution(model, (ITimeSeriesPrognosisProblemData)ProblemDataParameter.ActualValue.Clone());
    8180    }
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveValidationBestSolutionAnalyzer.cs

    r8430 r8458  
    6565    protected override ISymbolicTimeSeriesPrognosisSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) {
    6666      var model = new SymbolicTimeSeriesPrognosisModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue as ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    67       if (ApplyLinearScaling.Value)
    68         SymbolicTimeSeriesPrognosisModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
     67      if (ApplyLinearScaling.Value) SymbolicTimeSeriesPrognosisModel.Scale(model, ProblemDataParameter.ActualValue);
     68
    6969      return new SymbolicTimeSeriesPrognosisSolution(model, (ITimeSeriesPrognosisProblemData)ProblemDataParameter.ActualValue.Clone());
    7070    }
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SymbolicTimeSeriesPrognosisModel.cs

    r8114 r8458  
    2121
    2222using System.Collections.Generic;
    23 using System.Drawing;
    2423using System.Linq;
    2524using HeuristicLab.Common;
     
    2726using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2827using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    2929
    3030namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis {
     
    3434  [StorableClass]
    3535  [Item(Name = "Symbolic Time-Series Prognosis Model", Description = "Represents a symbolic time series prognosis model.")]
    36   public class SymbolicTimeSeriesPrognosisModel : NamedItem, ISymbolicTimeSeriesPrognosisModel {
    37     public override Image ItemImage {
    38       get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
     36  public class SymbolicTimeSeriesPrognosisModel : SymbolicRegressionModel, ISymbolicTimeSeriesPrognosisModel {
     37
     38    public new ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter Interpreter {
     39      get { return (ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter)base.Interpreter; }
    3940    }
    40     [Storable(DefaultValue = double.MinValue)]
    41     private double lowerEstimationLimit;
    42     [Storable(DefaultValue = double.MaxValue)]
    43     private double upperEstimationLimit;
    44 
    45     #region properties
    46 
    47     [Storable]
    48     private ISymbolicExpressionTree symbolicExpressionTree;
    49     public ISymbolicExpressionTree SymbolicExpressionTree {
    50       get { return symbolicExpressionTree; }
    51     }
    52 
    53     [Storable]
    54     private ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter;
    55     public ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter Interpreter {
    56       get { return interpreter; }
    57     }
    58 
    59     ISymbolicDataAnalysisExpressionTreeInterpreter ISymbolicDataAnalysisModel.Interpreter {
    60       get { return (ISymbolicDataAnalysisExpressionTreeInterpreter)interpreter; }
    61     }
    62 
    63     #endregion
    6441
    6542    [StorableConstructor]
    6643    protected SymbolicTimeSeriesPrognosisModel(bool deserializing) : base(deserializing) { }
    67     protected SymbolicTimeSeriesPrognosisModel(SymbolicTimeSeriesPrognosisModel original, Cloner cloner)
    68       : base(original, cloner) {
    69       this.symbolicExpressionTree = cloner.Clone(original.symbolicExpressionTree);
    70       this.interpreter = cloner.Clone(original.interpreter);
    71       this.lowerEstimationLimit = original.lowerEstimationLimit;
    72       this.upperEstimationLimit = original.upperEstimationLimit;
    73     }
    74     public SymbolicTimeSeriesPrognosisModel(ISymbolicExpressionTree tree, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double lowerLimit = double.MinValue, double upperLimit = double.MaxValue)
    75       : base() {
    76       this.name = ItemName;
    77       this.description = ItemDescription;
    78       this.symbolicExpressionTree = tree;
    79       this.interpreter = interpreter;
    80       this.lowerEstimationLimit = lowerLimit;
    81       this.upperEstimationLimit = upperLimit;
    82     }
    83 
     44    protected SymbolicTimeSeriesPrognosisModel(SymbolicTimeSeriesPrognosisModel original, Cloner cloner) : base(original, cloner) { }
    8445    public override IDeepCloneable Clone(Cloner cloner) {
    8546      return new SymbolicTimeSeriesPrognosisModel(this, cloner);
    8647    }
    8748
     49    public SymbolicTimeSeriesPrognosisModel(ISymbolicExpressionTree tree, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double lowerLimit = double.MinValue, double upperLimit = double.MaxValue) : base(tree, interpreter, lowerLimit, upperLimit) { }
     50
     51
     52
    8853    public IEnumerable<IEnumerable<double>> GetPrognosedValues(Dataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons) {
    8954      var estimatedValues = Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows, horizons);
    90       return estimatedValues.Select(predictionPerRow => predictionPerRow.LimitToRange(lowerEstimationLimit, upperEstimationLimit));
     55      return estimatedValues.Select(predictionPerRow => predictionPerRow.LimitToRange(LowerEstimationLimit, UpperEstimationLimit));
    9156    }
    9257
     
    9863    }
    9964
    100     public static void Scale(SymbolicTimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows) {
    101       var dataset = problemData.Dataset;
    102       var targetVariable = problemData.TargetVariable;
    103       var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
    104       var boundedEstimatedValues = estimatedValues.LimitToRange(model.lowerEstimationLimit, model.upperEstimationLimit);
    105       var targetValues = problemData.Dataset.GetDoubleValues(targetVariable, rows);
     65    //public static void Scale(SymbolicTimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows) {
     66    //  var dataset = problemData.Dataset;
     67    //  var targetVariable = problemData.TargetVariable;
     68    //  var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
     69    //  var boundedEstimatedValues = estimatedValues.LimitToRange(model.lowerEstimationLimit, model.upperEstimationLimit);
     70    //  var targetValues = problemData.Dataset.GetDoubleValues(targetVariable, rows);
    10671
    107       double alpha, beta;
    108       OnlineCalculatorError error;
    109       OnlineLinearScalingParameterCalculator.Calculate(boundedEstimatedValues, targetValues, out alpha, out beta, out error);
    110       if (error != OnlineCalculatorError.None) return;
     72    //  double alpha, beta;
     73    //  OnlineCalculatorError error;
     74    //  OnlineLinearScalingParameterCalculator.Calculate(boundedEstimatedValues, targetValues, out alpha, out beta, out error);
     75    //  if (error != OnlineCalculatorError.None) return;
    11176
    112       ConstantTreeNode alphaTreeNode = null;
    113       ConstantTreeNode betaTreeNode = null;
    114       // check if model has been scaled previously by analyzing the structure of the tree
    115       var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
    116       if (startNode.GetSubtree(0).Symbol is Addition) {
    117         var addNode = startNode.GetSubtree(0);
    118         if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
    119           alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
    120           var mulNode = addNode.GetSubtree(0);
    121           if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
    122             betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
    123           }
    124         }
    125       }
    126       // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
    127       if (alphaTreeNode != null && betaTreeNode != null) {
    128         betaTreeNode.Value *= beta;
    129         alphaTreeNode.Value *= beta;
    130         alphaTreeNode.Value += alpha;
    131       } else {
    132         var mainBranch = startNode.GetSubtree(0);
    133         startNode.RemoveSubtree(0);
    134         var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
    135         startNode.AddSubtree(scaledMainBranch);
    136       }
    137     }
     77    //  ConstantTreeNode alphaTreeNode = null;
     78    //  ConstantTreeNode betaTreeNode = null;
     79    //  // check if model has been scaled previously by analyzing the structure of the tree
     80    //  var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
     81    //  if (startNode.GetSubtree(0).Symbol is Addition) {
     82    //    var addNode = startNode.GetSubtree(0);
     83    //    if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
     84    //      alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
     85    //      var mulNode = addNode.GetSubtree(0);
     86    //      if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
     87    //        betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
     88    //      }
     89    //    }
     90    //  }
     91    //  // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
     92    //  if (alphaTreeNode != null && betaTreeNode != null) {
     93    //    betaTreeNode.Value *= beta;
     94    //    alphaTreeNode.Value *= beta;
     95    //    alphaTreeNode.Value += alpha;
     96    //  } else {
     97    //    var mainBranch = startNode.GetSubtree(0);
     98    //    startNode.RemoveSubtree(0);
     99    //    var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
     100    //    startNode.AddSubtree(scaledMainBranch);
     101    //  }
     102    //}
    138103
    139     private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
    140       if (alpha.IsAlmost(0.0)) {
    141         return treeNode;
    142       } else {
    143         var addition = new Addition();
    144         var node = addition.CreateTreeNode();
    145         var alphaConst = MakeConstant(alpha);
    146         node.AddSubtree(treeNode);
    147         node.AddSubtree(alphaConst);
    148         return node;
    149       }
    150     }
     104    //private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
     105    //  if (alpha.IsAlmost(0.0)) {
     106    //    return treeNode;
     107    //  } else {
     108    //    var addition = new Addition();
     109    //    var node = addition.CreateTreeNode();
     110    //    var alphaConst = MakeConstant(alpha);
     111    //    node.AddSubtree(treeNode);
     112    //    node.AddSubtree(alphaConst);
     113    //    return node;
     114    //  }
     115    //}
    151116
    152     private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
    153       if (beta.IsAlmost(1.0)) {
    154         return treeNode;
    155       } else {
    156         var multipliciation = new Multiplication();
    157         var node = multipliciation.CreateTreeNode();
    158         var betaConst = MakeConstant(beta);
    159         node.AddSubtree(treeNode);
    160         node.AddSubtree(betaConst);
    161         return node;
    162       }
    163     }
     117    //private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
     118    //  if (beta.IsAlmost(1.0)) {
     119    //    return treeNode;
     120    //  } else {
     121    //    var multipliciation = new Multiplication();
     122    //    var node = multipliciation.CreateTreeNode();
     123    //    var betaConst = MakeConstant(beta);
     124    //    node.AddSubtree(treeNode);
     125    //    node.AddSubtree(betaConst);
     126    //    return node;
     127    //  }
     128    //}
    164129
    165     private static ISymbolicExpressionTreeNode MakeConstant(double c) {
    166       var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
    167       node.Value = c;
    168       return node;
    169     }
     130    //private static ISymbolicExpressionTreeNode MakeConstant(double c) {
     131    //  var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
     132    //  node.Value = c;
     133    //  return node;
     134    //}
    170135  }
    171136}
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblem.cs

    r7842 r8458  
    6666    }
    6767
     68    protected DataAnalysisProblem(T problemData)
     69      : this() {
     70      ProblemData = problemData;
     71    }
     72
    6873    [StorableHook(HookType.AfterDeserialization)]
    6974    private void AfterDeserialization() {
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/ConstantRegressionModel.cs

    r7268 r8458  
    3030  public class ConstantRegressionModel : NamedItem, IRegressionModel {
    3131    [Storable]
    32     private double constant;
     32    protected double constant;
    3333    public double Constant {
    3434      get { return constant; }
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleSolution.cs

    r8430 r8458  
    155155    }
    156156
    157     protected override void RecalculateResults() {
    158       CalculateResults();
    159     }
    160 
    161157    #region Evaluation
    162158    public override IEnumerable<double> EstimatedTrainingValues {
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblem.cs

    r7842 r8458  
    3636    public override IDeepCloneable Clone(Cloner cloner) { return new RegressionProblem(this, cloner); }
    3737
    38     public RegressionProblem()
    39       : base() {
    40       ProblemData = new RegressionProblemData();
    41     }
     38    public RegressionProblem() : base(new RegressionProblemData()) { }
     39
    4240  }
    4341}
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolution.cs

    r8430 r8458  
    4747    }
    4848
    49     protected override void RecalculateResults() {
    50       CalculateResults();
    51     }
    52 
    5349    public override IEnumerable<double> EstimatedValues {
    5450      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolutionBase.cs

    r8430 r8458  
    164164    }
    165165
    166     protected void CalculateResults() {
     166    protected override void RecalculateResults() {
     167      CalculateResults();
     168    }
     169
     170    private void CalculateResults() {
    167171      IEnumerable<double> estimatedTrainingValues = EstimatedTrainingValues; // cache values
    168172      IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/Models/ConstantTimeSeriesPrognosisModel.cs

    r8430 r8458  
    2929  [StorableClass]
    3030  [Item("Constant TimeSeries Model", "A time series model that returns for all prediciton the same constant value.")]
    31   public class ConstantTimeSeriesPrognosisModel : NamedItem, ITimeSeriesPrognosisModel {
    32     [Storable]
    33     public double Constant { get; private set; }
    34 
     31  public class ConstantTimeSeriesPrognosisModel : ConstantRegressionModel, ITimeSeriesPrognosisModel {
    3532    [StorableConstructor]
    3633    protected ConstantTimeSeriesPrognosisModel(bool deserializing) : base(deserializing) { }
    37     protected ConstantTimeSeriesPrognosisModel(ConstantTimeSeriesPrognosisModel original, Cloner cloner)
    38       : base(original, cloner) {
    39       Constant = original.Constant;
    40     }
     34    protected ConstantTimeSeriesPrognosisModel(ConstantTimeSeriesPrognosisModel original, Cloner cloner) : base(original, cloner) { }
    4135    public override IDeepCloneable Clone(Cloner cloner) {
    4236      return new ConstantTimeSeriesPrognosisModel(this, cloner);
    4337    }
    4438
    45     public ConstantTimeSeriesPrognosisModel(double constant) {
    46       Constant = constant;
    47     }
     39    public ConstantTimeSeriesPrognosisModel(double constant) : base(constant) { }
    4840
    4941    public IEnumerable<IEnumerable<double>> GetPrognosedValues(Dataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons) {
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/Models/TimeSeriesPrognosisAutoRegressiveModel.cs

    r8430 r8458  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    6263      var rowsEnumerator = rows.GetEnumerator();
    6364      var horizonsEnumerator = horizons.GetEnumerator();
     65      var targetVariables = dataset.GetReadOnlyDoubleValues(TargetVariable);
    6466      // produce a n-step forecast for all rows
    6567      while (rowsEnumerator.MoveNext() & horizonsEnumerator.MoveNext()) {
     
    6769        int horizon = horizonsEnumerator.Current;
    6870        double[] prognosis = new double[horizon];
    69 
    70         prognosis[0] = dataset.GetDoubleValue(TargetVariable, row - TimeOffset);
    71         for (int i = 1; i < horizon; i++) {
    72           //prognosis[i] = Beta * prognosis[i - 1] + Alpha;
    73         }
     71        for (int i = 0; i < horizon; i++)
     72          prognosis[i] = targetVariables[row - TimeOffset];
    7473        yield return prognosis;
    7574      }
     
    7978    }
    8079
     80    public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
     81      return GetPrognosedValues(dataset, rows, rows.Select(r => 1)).SelectMany(e => e);
     82    }
     83
    8184    public ITimeSeriesPrognosisSolution CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData) {
    8285      return new TimeSeriesPrognosisSolution(this, problemData);
     86    }
     87    public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
     88      throw new NotSupportedException();
    8389    }
    8490
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/Models/TimeSeriesPrognosisMovingAverageModel.cs

    r8430 r8458  
    7979    }
    8080
     81    public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
     82      return GetPrognosedValues(dataset, rows, rows.Select(r => 1)).SelectMany(e => e);
     83    }
     84
    8185    public ITimeSeriesPrognosisSolution CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData) {
    8286      return new TimeSeriesPrognosisSolution(this, problemData);
     87    }
     88    public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
     89      throw new NotSupportedException();
    8390    }
    8491
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisProblem.cs

    r7843 r8458  
    3434    public override IDeepCloneable Clone(Cloner cloner) { return new TimeSeriesPrognosisProblem(this, cloner); }
    3535
    36     public TimeSeriesPrognosisProblem()
    37       : base() {
    38       ProblemData = new TimeSeriesPrognosisProblemData();
    39     }
    40 
     36    public TimeSeriesPrognosisProblem() : base(new TimeSeriesPrognosisProblemData()) { }
    4137  }
    4238}
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolution.cs

    r8010 r8458  
    3535    protected internal TimeSeriesPrognosisSolution(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData) : base(model, problemData) { }
    3636
    37     protected override void RecalculateResults() {
    38       CalculateResults();
    39     }
    40 
    4137    public override IEnumerable<IEnumerable<double>> GetPrognosedValues(IEnumerable<int> rows, IEnumerable<int> horizons) {
    4238      return Model.GetPrognosedValues(ProblemData.Dataset, rows, horizons);
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolutionBase.cs

    r8430 r8458  
    3030namespace HeuristicLab.Problems.DataAnalysis {
    3131  [StorableClass]
    32   public abstract class TimeSeriesPrognosisSolutionBase : DataAnalysisSolution, ITimeSeriesPrognosisSolution {
    33     private const string TrainingMeanSquaredErrorResultName = "Mean squared error (training)";
    34     private const string TestMeanSquaredErrorResultName = "Mean squared error (test)";
    35     private const string TrainingMeanAbsoluteErrorResultName = "Mean absolute error (training)";
    36     private const string TestMeanAbsoluteErrorResultName = "Mean absolute error (test)";
    37     private const string TrainingSquaredCorrelationResultName = "Pearson's R² (training)";
    38     private const string TestSquaredCorrelationResultName = "Pearson's R² (test)";
    39     private const string TrainingRelativeErrorResultName = "Average relative error (training)";
    40     private const string TestRelativeErrorResultName = "Average relative error (test)";
    41     private const string TrainingNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (training)";
    42     private const string TestNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (test)";
     32  public abstract class TimeSeriesPrognosisSolutionBase : RegressionSolutionBase, ITimeSeriesPrognosisSolution {
    4333    private const string TrainingDirectionalSymmetryResultName = "Average directional symmetry (training)";
    4434    private const string TestDirectionalSymmetryResultName = "Average directional symmetry (test)";
     
    7767
    7868    #region Results
    79     public double TrainingMeanSquaredError {
    80       get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
    81       private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
    82     }
    83     public double TestMeanSquaredError {
    84       get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
    85       private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
    86     }
    87     public double TrainingMeanAbsoluteError {
    88       get { return ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value; }
    89       private set { ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value = value; }
    90     }
    91     public double TestMeanAbsoluteError {
    92       get { return ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value; }
    93       private set { ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value = value; }
    94     }
    95     public double TrainingRSquared {
    96       get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
    97       private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
    98     }
    99     public double TestRSquared {
    100       get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
    101       private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
    102     }
    103     public double TrainingRelativeError {
    104       get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
    105       private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
    106     }
    107     public double TestRelativeError {
    108       get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
    109       private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
    110     }
    111     public double TrainingNormalizedMeanSquaredError {
    112       get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }
    113       private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }
    114     }
    115     public double TestNormalizedMeanSquaredError {
    116       get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }
    117       private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }
    118     }
    11969    public double TrainingDirectionalSymmetry {
    12070      get { return ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value; }
     
    158108    }
    159109    #endregion
     110
     111    public override IEnumerable<double> EstimatedValues {
     112      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
     113    }
     114    public override IEnumerable<double> EstimatedTrainingValues {
     115      get { return GetEstimatedValues(ProblemData.TrainingIndices); }
     116    }
     117    public override IEnumerable<double> EstimatedTestValues {
     118      get { return GetEstimatedValues(ProblemData.TestIndices); }
     119    }
     120    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
     121      return Model.GetEstimatedValues(ProblemData.Dataset, rows);
     122    }
    160123
    161124    [StorableConstructor]
     
    167130    protected TimeSeriesPrognosisSolutionBase(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData)
    168131      : base(model, problemData) {
    169       Add(new Result(TrainingMeanSquaredErrorResultName, "Mean of squared errors of the model on the training partition", new DoubleValue()));
    170       Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new DoubleValue()));
    171       Add(new Result(TrainingMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the training partition", new DoubleValue()));
    172       Add(new Result(TestMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the test partition", new DoubleValue()));
    173       Add(new Result(TrainingSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new DoubleValue()));
    174       Add(new Result(TestSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleValue()));
    175       Add(new Result(TrainingRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the training partition", new DoubleValue()));
    176       Add(new Result(TestRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the test partition", new DoubleValue()));
    177       Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the training partition", new DoubleValue()));
    178       Add(new Result(TestNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the test partition", new DoubleValue()));
    179132      Add(new Result(TrainingDirectionalSymmetryResultName, "The average directional symmetry of the forecasts of the model on the training partition", new DoubleValue()));
    180133      Add(new Result(TestDirectionalSymmetryResultName, "The average directional symmetry of the forecasts of the model on the test partition", new DoubleValue()));
     
    190143    }
    191144
    192 
    193     protected void CalculateResults() {
     145    protected override void RecalculateResults() {
     146      base.RecalculateResults();
     147      CalculateTimeSeriesResults();
     148    }
     149
     150    private void CalculateTimeSeriesResults() {
    194151      OnlineCalculatorError errorState;
    195152      //mean model
     
    211168      IEnumerable<IEnumerable<double>> trainingTargetValues = ProblemData.TrainingIndices.Zip(trainingHorizions, Enumerable.Range).Select(r => ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, r)).ToList();
    212169      IEnumerable<IEnumerable<double>> trainingEstimatedValues = Model.GetPrognosedValues(ProblemData.Dataset, ProblemData.TrainingIndices, trainingHorizions).ToList();
    213 
    214       TrainingMeanSquaredError = OnlineMeanSquaredErrorCalculator.Calculate(trainingTargetValues.SelectMany(x => x), trainingEstimatedValues.SelectMany(x => x), out errorState);
    215       TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? TrainingMeanSquaredError : double.PositiveInfinity;
    216       TrainingMeanAbsoluteError = OnlineMeanAbsoluteErrorCalculator.Calculate(trainingTargetValues.SelectMany(x => x), trainingEstimatedValues.SelectMany(x => x), out errorState);
    217       TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? TrainingMeanAbsoluteError : double.PositiveInfinity;
    218       TrainingRelativeError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(trainingTargetValues.SelectMany(x => x), trainingEstimatedValues.SelectMany(x => x), out errorState);
    219       TrainingRelativeError = errorState == OnlineCalculatorError.None ? TrainingRelativeError : double.PositiveInfinity;
    220       TrainingRSquared = OnlinePearsonsRSquaredCalculator.Calculate(trainingTargetValues.SelectMany(x => x), trainingEstimatedValues.SelectMany(x => x), out errorState);
    221       TrainingRSquared = errorState == OnlineCalculatorError.None ? TrainingRSquared : 0.0;
    222       TrainingNormalizedMeanSquaredError = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(trainingTargetValues.SelectMany(x => x), trainingEstimatedValues.SelectMany(x => x), out errorState);
    223       TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? TrainingNormalizedMeanSquaredError : double.PositiveInfinity;
    224 
    225170      IEnumerable<IEnumerable<double>> trainingMeanModelPredictions = meanModel.GetPrognosedValues(ProblemData.Dataset, ProblemData.TrainingIndices, trainingHorizions);
    226171      IEnumerable<IEnumerable<double>> trainingAR1ModelPredictions = AR1model.GetPrognosedValues(ProblemData.Dataset, ProblemData.TrainingIndices, trainingHorizions);
     
    243188      IEnumerable<IEnumerable<double>> testTargetValues = ProblemData.TestIndices.Zip(testHorizions, Enumerable.Range).Select(r => ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, r)).ToList();
    244189      IEnumerable<IEnumerable<double>> testEstimatedValues = Model.GetPrognosedValues(ProblemData.Dataset, ProblemData.TestIndices, testHorizions).ToList();
    245 
    246       TestMeanSquaredError = OnlineMeanSquaredErrorCalculator.Calculate(testTargetValues.SelectMany(x => x), testEstimatedValues.SelectMany(x => x), out errorState);
    247       TestMeanSquaredError = errorState == OnlineCalculatorError.None ? TestMeanSquaredError : double.PositiveInfinity;
    248       TestMeanAbsoluteError = OnlineMeanAbsoluteErrorCalculator.Calculate(testTargetValues.SelectMany(x => x), testEstimatedValues.SelectMany(x => x), out errorState);
    249       TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? TestMeanAbsoluteError : double.PositiveInfinity;
    250       TestRelativeError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(testTargetValues.SelectMany(x => x), testEstimatedValues.SelectMany(x => x), out errorState);
    251       TestRelativeError = errorState == OnlineCalculatorError.None ? TestRelativeError : double.PositiveInfinity;
    252       TestRSquared = OnlinePearsonsRSquaredCalculator.Calculate(testTargetValues.SelectMany(x => x), testEstimatedValues.SelectMany(x => x), out errorState);
    253       TestRSquared = errorState == OnlineCalculatorError.None ? TestRSquared : 0.0;
    254       TestNormalizedMeanSquaredError = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(testTargetValues.SelectMany(x => x), testEstimatedValues.SelectMany(x => x), out errorState);
    255       TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? TestNormalizedMeanSquaredError : double.PositiveInfinity;
    256 
    257190      IEnumerable<double> testStartValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices.Select(r => r - 1).Where(r => r > 0)).ToList();
    258191      IEnumerable<IEnumerable<double>> testMeanModelPredictions = meanModel.GetPrognosedValues(ProblemData.Dataset, ProblemData.TestIndices, testHorizions);
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/TimeSeriesPrognosis/ITimeSeriesPrognosisModel.cs

    r8010 r8458  
    2222using System.Collections.Generic;
    2323namespace HeuristicLab.Problems.DataAnalysis {
    24   public interface ITimeSeriesPrognosisModel : IDataAnalysisModel {
     24  public interface ITimeSeriesPrognosisModel : IRegressionModel {
    2525    IEnumerable<IEnumerable<double>> GetPrognosedValues(Dataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons);
    2626    ITimeSeriesPrognosisSolution CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData);
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/TimeSeriesPrognosis/ITimeSeriesPrognosisProblemData.cs

    r7989 r8458  
    2121
    2222namespace HeuristicLab.Problems.DataAnalysis {
    23   public interface ITimeSeriesPrognosisProblemData : IDataAnalysisProblemData {
    24     string TargetVariable { get; }
     23  public interface ITimeSeriesPrognosisProblemData : IRegressionProblemData {
    2524  }
    2625}
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/TimeSeriesPrognosis/ITimeSeriesPrognosisSolution.cs

    r8010 r8458  
    2222using System.Collections.Generic;
    2323namespace HeuristicLab.Problems.DataAnalysis {
    24   public interface ITimeSeriesPrognosisSolution : IDataAnalysisSolution {
     24  public interface ITimeSeriesPrognosisSolution : IRegressionSolution {
    2525    int Horizon { get; set; }
    2626    new ITimeSeriesPrognosisModel Model { get; }
     
    2929    IEnumerable<IEnumerable<double>> GetPrognosedValues(IEnumerable<int> rows, IEnumerable<int> horizon);
    3030
    31     double TrainingMeanSquaredError { get; }
    32     double TestMeanSquaredError { get; }
    33     double TrainingMeanAbsoluteError { get; }
    34     double TestMeanAbsoluteError { get; }
    35     double TrainingRSquared { get; }
    36     double TestRSquared { get; }
    37     double TrainingRelativeError { get; }
    38     double TestRelativeError { get; }
    39     double TrainingNormalizedMeanSquaredError { get; }
    40     double TestNormalizedMeanSquaredError { get; }
    4131    double TrainingTheilsUStatisticLast { get; }
    4232    double TestTheilsUStatisticLast { get; }
Note: See TracChangeset for help on using the changeset viewer.