Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/09/11 12:56:21 (13 years ago)
Author:
gkronber
Message:

#1081 worked on multi-variate time series prognosis

Location:
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolution.cs

    r7100 r7160  
    4949    public override IEnumerable<IEnumerable<double>> PrognosedTrainingValues {
    5050      get {
    51         return GetPrognosedValues(Enumerable.Range(ProblemData.TrainingPartition.Start, 1),
     51        return GetPrognosedValues(ProblemData.TrainingIndizes.Take(1),
    5252          ProblemData.TrainingPartition.End - ProblemData.TrainingPartition.Start)
    5353          .First();
     
    5656    public override IEnumerable<IEnumerable<double>> PrognosedTestValues {
    5757      get {
    58         return GetPrognosedValues(Enumerable.Range(ProblemData.TestPartition.Start, 1),
     58        return GetPrognosedValues(ProblemData.TestIndizes.Take(1),
    5959          ProblemData.TestPartition.End - ProblemData.TestPartition.Start)
    6060          .First();
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolutionBase.cs

    r7154 r7160  
    4545    private const string TrainingWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (training)";
    4646    private const string TestWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (test)";
    47     private const string TrainingTheilsUStatisticResultName = "Average Theil's U (training)";
    48     private const string TestTheilsUStatisticResultName = "Average Theil's U (test)";
     47    private const string TrainingTheilsUStatisticLastResultName = "Average Theil's U (last) (training)";
     48    private const string TestTheilsUStatisticLastResultName = "Average Theil's U (last) (test)";
     49    private const string TrainingTheilsUStatisticMeanResultName = "Average Theil's U (mean) (training)";
     50    private const string TestTheilsUStatisticMeanResultName = "Average Theil's U (mean) (test)";
     51    private const string TrainingTheilsUStatisticMAResultName = "Average Theil's U (moving average) (training)";
     52    private const string TestTheilsUStatisticMAResultName = "Average Theil's U (moving average) (test)";
    4953
    5054    public new ITimeSeriesPrognosisModel Model {
     
    5660      get { return (ITimeSeriesPrognosisProblemData)base.ProblemData; }
    5761      set { base.ProblemData = value; }
     62    }
     63
     64    [Storable]
     65    private int horizon;
     66    public int Horizon {
     67      get { return horizon; }
     68      set {
     69        if (horizon != value) {
     70          horizon = value;
     71          RecalculateResults();
     72        }
     73      }
    5874    }
    5975
     
    119135      private set { this[TestWeightedDirectionalSymmetryResultName].Value = new DoubleArray(value); }
    120136    }
    121     public double[] TrainingTheilsUStatistic {
    122       get { return ((DoubleArray)this[TrainingTheilsUStatisticResultName].Value).ToArray(); }
    123       private set { this[TrainingTheilsUStatisticResultName].Value = new DoubleArray(value); }
    124     }
    125     public double[] TestTheilsUStatistic {
    126       get { return ((DoubleArray)this[TestTheilsUStatisticResultName].Value).ToArray(); }
    127       private set { this[TestTheilsUStatisticResultName].Value = new DoubleArray(value); }
     137    public double[] TrainingTheilsUStatisticLast {
     138      get { return ((DoubleArray)this[TrainingTheilsUStatisticLastResultName].Value).ToArray(); }
     139      private set { this[TrainingTheilsUStatisticLastResultName].Value = new DoubleArray(value); }
     140    }
     141    public double[] TestTheilsUStatisticLast {
     142      get { return ((DoubleArray)this[TestTheilsUStatisticLastResultName].Value).ToArray(); }
     143      private set { this[TestTheilsUStatisticLastResultName].Value = new DoubleArray(value); }
     144    }
     145    public double[] TrainingTheilsUStatisticMean {
     146      get { return ((DoubleArray)this[TrainingTheilsUStatisticMeanResultName].Value).ToArray(); }
     147      private set { this[TrainingTheilsUStatisticMeanResultName].Value = new DoubleArray(value); }
     148    }
     149    public double[] TestTheilsUStatisticMean {
     150      get { return ((DoubleArray)this[TestTheilsUStatisticMeanResultName].Value).ToArray(); }
     151      private set { this[TestTheilsUStatisticMeanResultName].Value = new DoubleArray(value); }
     152    }
     153    public double[] TrainingTheilsUStatisticMovingAverage {
     154      get { return ((DoubleArray)this[TrainingTheilsUStatisticMAResultName].Value).ToArray(); }
     155      private set { this[TrainingTheilsUStatisticMAResultName].Value = new DoubleArray(value); }
     156    }
     157    public double[] TestTheilsUStatisticMovingAverage {
     158      get { return ((DoubleArray)this[TestTheilsUStatisticMAResultName].Value).ToArray(); }
     159      private set { this[TestTheilsUStatisticMAResultName].Value = new DoubleArray(value); }
    128160    }
    129161    #endregion
     
    133165    protected TimeSeriesPrognosisSolutionBase(TimeSeriesPrognosisSolutionBase original, Cloner cloner)
    134166      : base(original, cloner) {
     167      this.horizon = original.horizon;
    135168    }
    136169    protected TimeSeriesPrognosisSolutionBase(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData)
     
    150183      Add(new Result(TrainingWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the training partition", new DoubleArray()));
    151184      Add(new Result(TestWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the test partition", new DoubleArray()));
    152       Add(new Result(TrainingTheilsUStatisticResultName, "The average Theil's U statistic of the forecasts of the model on the training partition", new DoubleArray()));
    153       Add(new Result(TestTheilsUStatisticResultName, "The average Theil's U statistic of the forecasts of the model on the test partition", new DoubleArray()));
     185      Add(new Result(TrainingTheilsUStatisticLastResultName, "The average Theil's U statistic (reference: previous value) of the forecasts of the model on the training partition", new DoubleArray()));
     186      Add(new Result(TestTheilsUStatisticLastResultName, "The average Theil's U statistic (reference: previous value) of the forecasts of the model on the test partition", new DoubleArray()));
     187      Add(new Result(TrainingTheilsUStatisticMeanResultName, "The average Theil's U statistic (reference: mean value) of the forecasts of the model on the training partition", new DoubleArray()));
     188      Add(new Result(TestTheilsUStatisticMeanResultName, "The average Theil's U statistic (reference: mean value) of the forecasts of the model on the test partition", new DoubleArray()));
     189      Add(new Result(TrainingTheilsUStatisticMAResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the training partition", new DoubleArray()));
     190      Add(new Result(TestTheilsUStatisticMAResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the test partition", new DoubleArray()));
     191      horizon = 1;
    154192    }
    155193
    156194    [StorableHook(HookType.AfterDeserialization)]
    157195    private void AfterDeserialization() {
    158 
     196      if (horizon == 0) horizon = 1;
    159197    }
    160198
     
    207245      var trainingWdsCalculators = new OnlineWeightedDirectionalSymmetryCalculator[targetVariables.Length];
    208246      var testWdsCalculators = new OnlineWeightedDirectionalSymmetryCalculator[targetVariables.Length];
    209       var trainingTheilsUCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
    210       var testTheilsUCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
     247      var trainingTheilsULastCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
     248      var testTheilsULastCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
     249      var trainingTheilsUMeanCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
     250      var testTheilsUMeanCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
     251      var trainingTheilsUMovingAverageCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
     252      var testTheilsUMovingAverageCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
    211253      for (int i = 0; i < targetVariables.Length; i++) {
    212254        trainingDsCalculators[i] = new OnlineDirectionalSymmetryCalculator();
     
    214256        trainingWdsCalculators[i] = new OnlineWeightedDirectionalSymmetryCalculator();
    215257        testWdsCalculators[i] = new OnlineWeightedDirectionalSymmetryCalculator();
    216         trainingTheilsUCalculators[i] = new OnlineTheilsUStatisticCalculator();
    217         testTheilsUCalculators[i] = new OnlineTheilsUStatisticCalculator();
     258        trainingTheilsULastCalculators[i] = new OnlineTheilsUStatisticCalculator();
     259        testTheilsULastCalculators[i] = new OnlineTheilsUStatisticCalculator();
     260        trainingTheilsUMeanCalculators[i] = new OnlineTheilsUStatisticCalculator();
     261        testTheilsUMeanCalculators[i] = new OnlineTheilsUStatisticCalculator();
     262        trainingTheilsUMovingAverageCalculators[i] = new OnlineTheilsUStatisticCalculator();
     263        testTheilsUMovingAverageCalculators[i] = new OnlineTheilsUStatisticCalculator();
    218264      }
    219265
    220       var allPrognosedTrainingValues = PrognosedTrainingValues.SelectMany(x => x).ToArray();
    221       var allPrognosedTestValues = PrognosedTestValues.SelectMany(x => x).ToArray();
    222       for (int t = 0; t < targetVariables.Length; t++) {
    223         var actualTrainingValues = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes);
    224         double startTrainingValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], ProblemData.TrainingIndizes.First() - 1);
    225         var prognosedTrainingValues = allPrognosedTrainingValues.Skip(t).TakeEvery(targetVariables.Length);
    226         trainingDsCalculators[t].Add(startTrainingValue, actualTrainingValues, prognosedTrainingValues);
    227         trainingWdsCalculators[t].Add(startTrainingValue, actualTrainingValues, prognosedTrainingValues);
    228         trainingTheilsUCalculators[t].Add(startTrainingValue, actualTrainingValues, prognosedTrainingValues);
    229 
    230         var actualTestValues = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TestIndizes);
    231         double startTestValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], ProblemData.TestIndizes.First() - 1);
    232         var prognosedTestValues = allPrognosedTestValues.Skip(t).TakeEvery(targetVariables.Length);
    233         testDsCalculators[t].Add(startTestValue, actualTestValues, prognosedTestValues);
    234         testWdsCalculators[t].Add(startTestValue, actualTestValues, prognosedTestValues);
    235         testTheilsUCalculators[t].Add(startTestValue, actualTestValues, prognosedTestValues);
     266      var allPrognosedTrainingValues = GetPrognosedValues(ProblemData.TrainingIndizes, horizon).GetEnumerator();
     267      foreach (var row in ProblemData.TrainingIndizes) {
     268        if (row + horizon < ProblemData.Dataset.Rows) {
     269          allPrognosedTrainingValues.MoveNext();
     270          var prognosedTrainingValues = allPrognosedTrainingValues.Current.SelectMany(x => x).ToArray();
     271          for (int t = 0; t < targetVariables.Length; t++) {
     272            var actualContinuation = ProblemData.Dataset.GetDoubleValues(targetVariables[t],
     273                                                                         Enumerable.Range(row, horizon));
     274            int maWindow = 10 * horizon;
     275            var movingAverageContinuation = from h in Enumerable.Range(0, horizon)
     276                                            select (from r in Enumerable.Range(row + h - maWindow, maWindow - h)
     277                                                    where r > 0
     278                                                    select ProblemData.Dataset.GetDoubleValue(targetVariables[t], r)
     279                                                   ).Average();
     280            var mean = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes).Median();
     281            double startValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], row - 1);
     282            var prognosedContinuation = prognosedTrainingValues.Skip(t).TakeEvery(targetVariables.Length);
     283            trainingDsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
     284            trainingWdsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
     285            trainingTheilsULastCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
     286            trainingTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean), actualContinuation, prognosedContinuation);
     287            trainingTheilsUMovingAverageCalculators[t].Add(startValue, movingAverageContinuation, actualContinuation, prognosedContinuation);
     288          }
     289        }
    236290      }
     291      var allPrognosedTestValues = GetPrognosedValues(ProblemData.TestIndizes, horizon).GetEnumerator();
     292      foreach (var row in ProblemData.TestIndizes) {
     293        if (row + horizon < ProblemData.Dataset.Rows) {
     294          allPrognosedTestValues.MoveNext();
     295          var prognosedTestValues = allPrognosedTestValues.Current.SelectMany(x => x).ToArray();
     296          for (int t = 0; t < targetVariables.Length; t++) {
     297            var actualContinuation = ProblemData.Dataset.GetDoubleValues(targetVariables[t],
     298                                                                         Enumerable.Range(row, horizon));
     299            int maWindow = 10 * horizon;
     300            var movingAverageContinuation = from h in Enumerable.Range(0, horizon)
     301                                            select (from r in Enumerable.Range(row + h - maWindow, maWindow - h)
     302                                                    where r > 0
     303                                                    select ProblemData.Dataset.GetDoubleValue(targetVariables[t], r)
     304                                                   ).Average();
     305            // mean must be calculated on the training set!
     306            var mean = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes).Median();
     307            double startValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], row - 1);
     308            var prognosedContinuation = prognosedTestValues.Skip(t).TakeEvery(targetVariables.Length);
     309            testDsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
     310            testWdsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
     311            testTheilsULastCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
     312            testTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean), actualContinuation, prognosedContinuation);
     313            testTheilsUMovingAverageCalculators[t].Add(startValue, movingAverageContinuation, actualContinuation, prognosedContinuation);
     314          }
     315        }
     316      }
    237317
    238318      TrainingDirectionalSymmetry = trainingDsCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : 0.0)
     
    244324      TestWeightedDirectionalSymmetry = testWdsCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
    245325        .ToArray();
    246       TrainingTheilsUStatistic = trainingDsCalculators
    247         .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
    248         .ToArray();
    249       TestTheilsUStatistic = testTheilsUCalculators
     326      TrainingTheilsUStatisticLast = trainingTheilsULastCalculators
     327        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
     328        .ToArray();
     329      TestTheilsUStatisticLast = testTheilsULastCalculators
     330        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
     331        .ToArray();
     332      TrainingTheilsUStatisticMean = trainingTheilsUMeanCalculators
     333        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
     334        .ToArray();
     335      TestTheilsUStatisticMean = testTheilsUMeanCalculators
     336        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
     337        .ToArray();
     338      TrainingTheilsUStatisticMovingAverage = trainingTheilsUMovingAverageCalculators
     339        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
     340        .ToArray();
     341      TestTheilsUStatisticMovingAverage = testTheilsUMovingAverageCalculators
    250342        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
    251343        .ToArray();
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/TimeSeriesPrognosis/ITimeSeriesPrognosisSolution.cs

    r7100 r7160  
    2323namespace HeuristicLab.Problems.DataAnalysis {
    2424  public interface ITimeSeriesPrognosisSolution : IDataAnalysisSolution {
     25    int Horizon { get; set; }
    2526    new ITimeSeriesPrognosisModel Model { get; }
    2627    new ITimeSeriesPrognosisProblemData ProblemData { get; set; }
     
    4041    double[] TrainingNormalizedMeanSquaredError { get; }
    4142    double[] TestNormalizedMeanSquaredError { get; }
    42     double[] TrainingTheilsUStatistic { get; }
    43     double[] TestTheilsUStatistic { get; }
     43    double[] TrainingTheilsUStatisticLast { get; }
     44    double[] TestTheilsUStatisticLast { get; }
     45    double[] TrainingTheilsUStatisticMean { get; }
     46    double[] TestTheilsUStatisticMean { get; }
    4447    double[] TrainingDirectionalSymmetry { get; }
    4548    double[] TestDirectionalSymmetry { get; }
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineTheilsUStatisticCalculator.cs

    r7099 r7160  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526
     
    5253
    5354    public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) {
     55      Add(startValue, actualContinuation.Select(x => startValue), actualContinuation, predictedContinuation);
     56    }
     57
     58    public void Add(double startValue, IEnumerable<double> referenceContinuation, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) {
    5459      if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
    5560        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
     
    5762        var actualEnumerator = actualContinuation.GetEnumerator();
    5863        var predictedEnumerator = predictedContinuation.GetEnumerator();
    59         while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & ErrorState != OnlineCalculatorError.InvalidValueAdded) {
     64        var referenceEnumerator = referenceContinuation.GetEnumerator();
     65        while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & referenceEnumerator.MoveNext()
     66          & ErrorState != OnlineCalculatorError.InvalidValueAdded) {
    6067          double actual = actualEnumerator.Current;
    6168          double predicted = predictedEnumerator.Current;
    62           if (double.IsNaN(actual) || double.IsNaN(predicted)) {
     69          double reference = referenceEnumerator.Current;
     70          if (double.IsNaN(actual) || double.IsNaN(predicted) || double.IsNaN(reference)) {
    6371            errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
    6472          } else {
     
    6775            squaredErrorMeanCalculator.Add(errorPredictedChange * errorPredictedChange);
    6876
    69             double errorNoChange = (actual - startValue);
    70             unbiasedEstimatorMeanCalculator.Add(errorNoChange * errorNoChange);
     77            double errorReference = (reference - startValue) - (actual - startValue);
     78            unbiasedEstimatorMeanCalculator.Add(errorReference * errorReference);
    7179          }
    7280        }
    7381        // check if both enumerators are at the end to make sure both enumerations have the same length
    74         if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext()) {
     82        if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext() || referenceEnumerator.MoveNext()) {
    7583          errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
    7684        } else {
     
    7987      }
    8088    }
     89
    8190
    8291    public void Reset() {
Note: See TracChangeset for help on using the changeset viewer.