Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/05/11 20:17:59 (13 years ago)
Author:
gkronber
Message:

#1081 worked on multi-variate time series prognosis

Location:
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4
Files:
5 edited

Legend:

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

    r7120 r7129  
    117117    <Compile Include="Plugin.cs" />
    118118    <Compile Include="SingleObjective\SymbolicTimeSeriesPrognosisSingleObjectiveEvaluator.cs" />
    119     <Compile Include="SingleObjective\SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator.cs" />
    120     <Compile Include="SingleObjective\SymbolicTimeSeriesPrognosisSingleObjectivePearsonRSquaredEvaluator.cs" />
     119    <Compile Include="SingleObjective\SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator.cs">
     120      <SubType>Code</SubType>
     121    </Compile>
    121122    <Compile Include="SingleObjective\SymbolicTimeSeriesPrognosisSingleObjectiveProblem.cs" />
    122123    <Compile Include="SingleObjective\SymbolicTimeSeriesPrognosisSingleObjectiveTrainingBestSolutionAnalyzer.cs" />
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/Interfaces/ISymbolicTimeSeriesPrognosisModel.cs

    r7120 r7129  
    2222using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2323namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis {
    24   public interface ISymbolicTimeSeriesPrognosisModel : ITimeSeriesPrognosisModel {
    25     ISymbolicExpressionTree SymbolicExpressionTree { get; }
     24  public interface ISymbolicTimeSeriesPrognosisModel : ITimeSeriesPrognosisModel, ISymbolicDataAnalysisModel {
    2625    ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter Interpreter { get; }
    2726  }
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator.cs

    r7120 r7129  
    6161
    6262    public static double Calculate(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows, int horizon) {
    63       var allPredictedContinuations = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, problemData.TargetVariables.ToArray(), rows, horizon);
    64       var meanCalculator = new OnlineMeanAndVarianceCalculator();
    65       var allPredictedContinuationsEnumerator = allPredictedContinuations.GetEnumerator();
    66       foreach (var targetVariable in problemData.TargetVariables) {
    67         if (!allPredictedContinuationsEnumerator.MoveNext()) throw new InvalidOperationException();
    68         var actualContinuations = from r in rows
    69                                   select problemData.Dataset.GetDoubleValues(targetVariable, Enumerable.Range(r, horizon));
    70         var actualContinuationsEnumerator = actualContinuations.GetEnumerator();
    71         var predictedContinuationsEnumerator = allPredictedContinuationsEnumerator.Current.GetEnumerator();
    72         while (actualContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) {
    73           OnlineCalculatorError errorState;
    74           meanCalculator.Add(OnlineMeanSquaredErrorCalculator.Calculate(predictedContinuationsEnumerator.Current.LimitToRange(lowerEstimationLimit, upperEstimationLimit),
    75                                                                         actualContinuationsEnumerator.Current, out errorState));
    76           if (errorState != OnlineCalculatorError.None) return double.NaN;
     63      double[] alpha;
     64      double[] beta;
     65      DetermineScalingFactors(solution, problemData, interpreter, rows, out alpha, out beta);
     66      var scaledSolution = Scale(solution, alpha, beta);
     67
     68      var meanSquaredErrorCalculators = Enumerable.Range(0, problemData.TargetVariables.Count())
     69        .Select(i => new OnlineMeanSquaredErrorCalculator()).ToArray();
     70
     71      var allPredictedContinuations = interpreter.GetSymbolicExpressionTreeValues(scaledSolution, problemData.Dataset,
     72                                                                                  problemData.TargetVariables.ToArray(),
     73                                                                                  rows, horizon);
     74      var allContinuationsEnumerator = allPredictedContinuations.GetEnumerator();
     75      var rowsEnumerator = rows.GetEnumerator();
     76      while (allContinuationsEnumerator.MoveNext() & rowsEnumerator.MoveNext()) {
     77        var componentEnumerator = allContinuationsEnumerator.Current.GetEnumerator();
     78        var mseCalculatorsEnumerator = meanSquaredErrorCalculators.AsEnumerable().GetEnumerator();
     79        var targetVarEnumerator = problemData.TargetVariables.GetEnumerator();
     80        while (componentEnumerator.MoveNext() & mseCalculatorsEnumerator.MoveNext() & targetVarEnumerator.MoveNext()) {
     81          var stepEnumerator = componentEnumerator.Current.GetEnumerator();
     82          var actualContinuationEnumerator = problemData.Dataset.GetDoubleValues(targetVarEnumerator.Current,
     83                                                                       Enumerable.Range(rowsEnumerator.Current, horizon)).GetEnumerator();
     84          while (stepEnumerator.MoveNext() & actualContinuationEnumerator.MoveNext()) {
     85            double e = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, stepEnumerator.Current));
     86            mseCalculatorsEnumerator.Current.Add(actualContinuationEnumerator.Current, e);
     87            if (mseCalculatorsEnumerator.Current.ErrorState == OnlineCalculatorError.InvalidValueAdded)
     88              return double.NaN;
     89          }
    7790        }
    7891      }
    79       return meanCalculator.Mean;
     92      var meanCalculator = new OnlineMeanAndVarianceCalculator();
     93      foreach (var calc in meanSquaredErrorCalculators) {
     94        if (calc.ErrorState != OnlineCalculatorError.None) return double.NaN;
     95        meanCalculator.Add(calc.MeanSquaredError);
     96      }
     97      //int i = 0;
     98      //foreach (var targetVariable in problemData.TargetVariables) {
     99      //  var predictedContinuations = allPredictedContinuations.Select(v => v.ElementAt(i));
     100      //  for (int h = 0; h < horizon; h++) {
     101      //    OnlineCalculatorError errorState;
     102      //    meanCalculator.Add(OnlineMeanSquaredErrorCalculator.Calculate(predictedContinuations
     103      //                                                                    .Select(x => x.ElementAt(h))
     104      //                                                                    .LimitToRange(lowerEstimationLimit,
     105      //                                                                                  upperEstimationLimit),
     106      //                                                                  actualContinuations.Select(x => x.ElementAt(h)),
     107      //                                                                  out errorState));
     108      //    if (errorState != OnlineCalculatorError.None) return double.NaN;
     109      //  }
     110      //}
     111      return meanCalculator.MeanErrorState == OnlineCalculatorError.None ? meanCalculator.Mean : double.NaN;
     112    }
     113
     114    private static ISymbolicExpressionTree Scale(ISymbolicExpressionTree solution, double[] alpha, double[] beta) {
     115      var clone = (ISymbolicExpressionTree)solution.Clone();
     116      int n = alpha.Length;
     117      for (int i = 0; i < n; i++) {
     118        var parent = clone.Root.GetSubtree(0);
     119        var rpb = clone.Root.GetSubtree(0).GetSubtree(i);
     120        var scaledRpb = MakeSum(
     121          MakeProduct(rpb,
     122            MakeConstant(beta[i], clone.Root.Grammar), clone.Root.Grammar),
     123            MakeConstant(alpha[i], clone.Root.Grammar), clone.Root.Grammar);
     124        parent.RemoveSubtree(i);
     125        parent.InsertSubtree(i, scaledRpb);
     126      }
     127      return clone;
     128    }
     129
     130    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b, ISymbolicExpressionTreeGrammar grammar) {
     131      var sum = grammar.Symbols.Where(s => s is Addition).First().CreateTreeNode();
     132      sum.AddSubtree(a);
     133      sum.AddSubtree(b);
     134      return sum;
     135    }
     136
     137    private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b, ISymbolicExpressionTreeGrammar grammar) {
     138      var prod = grammar.Symbols.Where(s => s is Multiplication).First().CreateTreeNode();
     139      prod.AddSubtree(a);
     140      prod.AddSubtree(b);
     141      return prod;
     142    }
     143
     144    private static ISymbolicExpressionTreeNode MakeConstant(double c, ISymbolicExpressionTreeGrammar grammar) {
     145      var node = (ConstantTreeNode)grammar.Symbols.Where(s => s is Constant).First().CreateTreeNode();
     146      node.Value = c;
     147      return node;
     148    }
     149
     150    private static void DetermineScalingFactors(ISymbolicExpressionTree solution, ITimeSeriesPrognosisProblemData problemData, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, IEnumerable<int> rows, out double[] alpha, out double[] beta) {
     151      string[] targetVariables = problemData.TargetVariables.ToArray();
     152      int nComponents = targetVariables.Length;
     153      alpha = new double[nComponents];
     154      beta = new double[nComponents];
     155      var oneStepPredictions = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset,
     156                                                                     targetVariables, rows,
     157                                                                     1);
     158      for (int i = 0; i < nComponents; i++) {
     159        var target = problemData.Dataset.GetDoubleValues(targetVariables[i], rows);
     160        var prediction = oneStepPredictions.Select(v => v.ElementAt(i).First());
     161        OnlineCalculatorError errorState;
     162        OnlineLinearScalingParameterCalculator.Calculate(prediction, target, out alpha[i], out beta[i], out errorState);
     163        if (errorState != OnlineCalculatorError.None) {
     164          alpha[i] = 0;
     165          beta[i] = 1;
     166        }
     167      }
    80168    }
    81169
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveProblem.cs

    r7120 r7129  
    5252
    5353    public SymbolicTimeSeriesPrognosisSingleObjectiveProblem()
    54       : base(new TimeSeriesPrognosisProblemData(), new SymbolicTimeSeriesPrognosisSingleObjectivePearsonRSquaredEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
     54      : base(new TimeSeriesPrognosisProblemData(), new SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
    5555      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
    5656
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SymbolicTimeSeriesPrognosisModel.cs

    r7120 r7129  
    5353    }
    5454
     55    ISymbolicDataAnalysisExpressionTreeInterpreter ISymbolicDataAnalysisModel.Interpreter {
     56      get { return (ISymbolicDataAnalysisExpressionTreeInterpreter)interpreter; }
     57    }
     58
    5559    #endregion
    5660
     
    102106        double beta;
    103107        OnlineCalculatorError errorState;
    104         OnlineLinearScalingParameterCalculator.Calculate(estimatedValues[i].Select(x => x.First()), targetValues,
     108        OnlineLinearScalingParameterCalculator.Calculate(estimatedValues.Select(x => x.ElementAt(i).First()), targetValues,
    105109          out alpha, out beta, out errorState);
    106         if (errorState != OnlineCalculatorError.None) return;
     110        if (errorState != OnlineCalculatorError.None) continue;
    107111
    108112        ConstantTreeNode alphaTreeNode = null;
Note: See TracChangeset for help on using the changeset viewer.