Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/29/11 20:05:38 (12 years ago)
Author:
gkronber
Message:

#1081 worked on multi-variate time series prognosis

File:
1 edited

Legend:

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

    r7099 r7100  
    2020#endregion
    2121
     22using System.Collections.Concurrent;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    5758    }
    5859
    59     public abstract IEnumerable<double> PrognosedValues { get; }
    60     public abstract IEnumerable<double> PrognosedTrainingValues { get; }
    61     public abstract IEnumerable<double> PrognosedTestValues { get; }
    62     public abstract IEnumerable<double> GetPrognosedValues(IEnumerable<int> rows);
     60    public abstract IEnumerable<IEnumerable<double>> PrognosedTrainingValues { get; }
     61    public abstract IEnumerable<IEnumerable<double>> PrognosedTestValues { get; }
     62    public abstract IEnumerable<IEnumerable<IEnumerable<double>>> GetPrognosedValues(IEnumerable<int> rows, int horizon);
    6363
    6464    #region Results
    65     public double TrainingMeanSquaredError {
    66       get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
    67       private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
    68     }
    69     public double TestMeanSquaredError {
    70       get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
    71       private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
    72     }
    73     public double TrainingMeanAbsoluteError {
    74       get { return ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value; }
    75       private set { ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value = value; }
    76     }
    77     public double TestMeanAbsoluteError {
    78       get { return ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value; }
    79       private set { ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value = value; }
    80     }
    81     public double TrainingRSquared {
    82       get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
    83       private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
    84     }
    85     public double TestRSquared {
    86       get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
    87       private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
    88     }
    89     public double TrainingRelativeError {
    90       get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
    91       private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
    92     }
    93     public double TestRelativeError {
    94       get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
    95       private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
    96     }
    97     public double TrainingNormalizedMeanSquaredError {
    98       get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }
    99       private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }
    100     }
    101     public double TestNormalizedMeanSquaredError {
    102       get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }
    103       private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }
    104     }
    105     public double TrainingDirectionalSymmetry {
    106       get { return ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value; }
    107       private set { ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value = value; }
    108     }
    109     public double TestDirectionalSymmetry {
    110       get { return ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value; }
    111       private set { ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value = value; }
    112     }
    113     public double TrainingWeightedDirectionalSymmetry {
    114       get { return ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value; }
    115       private set { ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value = value; }
    116     }
    117     public double TestWeightedDirectionalSymmetry {
    118       get { return ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value; }
    119       private set { ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value = value; }
    120     }
    121     public double TrainingTheilsUStatistic {
    122       get { return ((DoubleValue)this[TrainingTheilsUStatisticResultName].Value).Value; }
    123       private set { ((DoubleValue)this[TrainingTheilsUStatisticResultName].Value).Value = value; }
    124     }
    125     public double TestTheilsUStatistic {
    126       get { return ((DoubleValue)this[TestTheilsUStatisticResultName].Value).Value; }
    127       private set { ((DoubleValue)this[TestTheilsUStatisticResultName].Value).Value = value; }
     65    public double[] TrainingMeanSquaredError {
     66      get { return ((DoubleArray)this[TrainingMeanSquaredErrorResultName].Value).ToArray(); }
     67      private set { this[TrainingMeanSquaredErrorResultName].Value = new DoubleArray(value); }
     68    }
     69    public double[] TestMeanSquaredError {
     70      get { return ((DoubleArray)this[TestMeanSquaredErrorResultName].Value).ToArray(); }
     71      private set { this[TestMeanSquaredErrorResultName].Value = new DoubleArray(value); }
     72    }
     73    public double[] TrainingMeanAbsoluteError {
     74      get { return ((DoubleArray)this[TrainingMeanAbsoluteErrorResultName].Value).ToArray(); }
     75      private set { this[TrainingMeanAbsoluteErrorResultName].Value = new DoubleArray(value); }
     76    }
     77    public double[] TestMeanAbsoluteError {
     78      get { return ((DoubleArray)this[TestMeanAbsoluteErrorResultName].Value).ToArray(); }
     79      private set { this[TestMeanAbsoluteErrorResultName].Value = new DoubleArray(value); }
     80    }
     81    public double[] TrainingRSquared {
     82      get { return ((DoubleArray)this[TrainingSquaredCorrelationResultName].Value).ToArray(); }
     83      private set { this[TrainingSquaredCorrelationResultName].Value = new DoubleArray(value); }
     84    }
     85    public double[] TestRSquared {
     86      get { return ((DoubleArray)this[TestSquaredCorrelationResultName].Value).ToArray(); }
     87      private set { this[TestSquaredCorrelationResultName].Value = new DoubleArray(value); }
     88    }
     89    public double[] TrainingRelativeError {
     90      get { return ((DoubleArray)this[TrainingRelativeErrorResultName].Value).ToArray(); }
     91      private set { this[TrainingRelativeErrorResultName].Value = new DoubleArray(value); }
     92    }
     93    public double[] TestRelativeError {
     94      get { return ((DoubleArray)this[TestRelativeErrorResultName].Value).ToArray(); }
     95      private set { this[TestRelativeErrorResultName].Value = new DoubleArray(value); }
     96    }
     97    public double[] TrainingNormalizedMeanSquaredError {
     98      get { return ((DoubleArray)this[TrainingNormalizedMeanSquaredErrorResultName].Value).ToArray(); }
     99      private set { this[TrainingNormalizedMeanSquaredErrorResultName].Value = new DoubleArray(value); }
     100    }
     101    public double[] TestNormalizedMeanSquaredError {
     102      get { return ((DoubleArray)this[TestNormalizedMeanSquaredErrorResultName].Value).ToArray(); }
     103      private set { this[TestNormalizedMeanSquaredErrorResultName].Value = new DoubleArray(value); }
     104    }
     105    public double[] TrainingDirectionalSymmetry {
     106      get { return ((DoubleArray)this[TrainingDirectionalSymmetryResultName].Value).ToArray(); }
     107      private set { this[TrainingDirectionalSymmetryResultName].Value = new DoubleArray(value); }
     108    }
     109    public double[] TestDirectionalSymmetry {
     110      get { return ((DoubleArray)this[TestDirectionalSymmetryResultName].Value).ToArray(); }
     111      private set { this[TestDirectionalSymmetryResultName].Value = new DoubleArray(value); }
     112    }
     113    public double[] TrainingWeightedDirectionalSymmetry {
     114      get { return ((DoubleArray)this[TrainingWeightedDirectionalSymmetryResultName].Value).ToArray(); }
     115      private set { this[TrainingWeightedDirectionalSymmetryResultName].Value = new DoubleArray(value); }
     116    }
     117    public double[] TestWeightedDirectionalSymmetry {
     118      get { return ((DoubleArray)this[TestWeightedDirectionalSymmetryResultName].Value).ToArray(); }
     119      private set { this[TestWeightedDirectionalSymmetryResultName].Value = new DoubleArray(value); }
     120    }
     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); }
    128128    }
    129129    #endregion
     
    136136    protected TimeSeriesPrognosisSolutionBase(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData)
    137137      : base(model, problemData) {
    138       Add(new Result(TrainingMeanSquaredErrorResultName, "Mean of squared errors of the model on the training partition", new DoubleValue()));
    139       Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new DoubleValue()));
    140       Add(new Result(TrainingMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the training partition", new DoubleValue()));
    141       Add(new Result(TestMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the test partition", new DoubleValue()));
    142       Add(new Result(TrainingSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new DoubleValue()));
    143       Add(new Result(TestSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleValue()));
    144       Add(new Result(TrainingRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the training partition", new PercentValue()));
    145       Add(new Result(TestRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the test partition", new PercentValue()));
    146       Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the training partition", new DoubleValue()));
    147       Add(new Result(TestNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the test partition", new DoubleValue()));
    148       Add(new Result(TrainingDirectionalSymmetryResultName, "The average directional symmetry of the forecasts of the model on the training partition", new PercentValue()));
    149       Add(new Result(TestDirectionalSymmetryResultName, "The average directional symmetry of the forecasts of the model on the test partition", new PercentValue()));
    150       Add(new Result(TrainingWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the training partition", new DoubleValue()));
    151       Add(new Result(TestWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the test partition", new DoubleValue()));
    152       Add(new Result(TrainingTheilsUStatisticResultName, "The average Theil's U statistic of the forecasts of the model on the training partition", new DoubleValue()));
    153       Add(new Result(TestTheilsUStatisticResultName, "The average Theil's U statistic of the forecasts of the model on the test partition", new DoubleValue()));
     138      Add(new Result(TrainingMeanSquaredErrorResultName, "Mean of squared errors of the model on the training partition", new DoubleArray()));
     139      Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new DoubleArray()));
     140      Add(new Result(TrainingMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the training partition", new DoubleArray()));
     141      Add(new Result(TestMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the test partition", new DoubleArray()));
     142      Add(new Result(TrainingSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new DoubleArray()));
     143      Add(new Result(TestSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleArray()));
     144      Add(new Result(TrainingRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the training partition", new DoubleArray()));
     145      Add(new Result(TestRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the test partition", new DoubleArray()));
     146      Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the training partition", new DoubleArray()));
     147      Add(new Result(TestNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the test partition", new DoubleArray()));
     148      Add(new Result(TrainingDirectionalSymmetryResultName, "The average directional symmetry of the forecasts of the model on the training partition", new DoubleArray()));
     149      Add(new Result(TestDirectionalSymmetryResultName, "The average directional symmetry of the forecasts of the model on the test partition", new DoubleArray()));
     150      Add(new Result(TrainingWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the training partition", new DoubleArray()));
     151      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()));
    154154    }
    155155
     
    160160
    161161    protected void CalculateResults() {
     162      OnlineCalculatorError errorState;
     163      /*
    162164      double[] estimatedTrainingValues = PrognosedTrainingValues.ToArray(); // cache values
    163165      double[] originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToArray();
     
    165167      double[] originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndizes).ToArray();
    166168
    167       OnlineCalculatorError errorState;
    168169      double trainingMse = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
    169170      TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMse : double.NaN;
     
    190191      double testNmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
    191192      TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNmse : double.NaN;
    192 
    193       var startTrainingValues = originalTrainingValues;
    194       // each continuation is only one element long
    195       var actualContinuationsTraining = from x in originalTrainingValues.Skip(1)
    196                                         select Enumerable.Repeat(x, 1);
    197       // each forecast is only one elemnt long
    198       // disregards the first estimated value (we could include this again by extending the list of original values by one step to the left
    199       // this is the easier way
    200       var predictedContinuationsTraining = from x in estimatedTrainingValues.Skip(1)
    201                                            select Enumerable.Repeat(x, 1);
    202 
    203       var startTestValues = originalTestValues;
    204       var actualContinuationsTest = from x in originalTestValues.Skip(1)
    205                                     select Enumerable.Repeat(x, 1);
    206       var predictedContinuationsTest = from x in estimatedTestValues.Skip(1)
    207                                        select Enumerable.Repeat(x, 1);
    208 
    209       double trainingDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(startTrainingValues, actualContinuationsTraining, predictedContinuationsTraining, out errorState);
    210       TrainingDirectionalSymmetry = errorState == OnlineCalculatorError.None ? trainingDirectionalSymmetry : double.NaN;
    211       double testDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(startTestValues, actualContinuationsTest, predictedContinuationsTest, out errorState);
    212       TestDirectionalSymmetry = errorState == OnlineCalculatorError.None ? testDirectionalSymmetry : double.NaN;
    213 
    214       double trainingWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(startTrainingValues, actualContinuationsTraining, predictedContinuationsTraining, out errorState);
    215       TrainingWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? trainingWeightedDirectionalSymmetry : double.NaN;
    216       double testWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(startTestValues, actualContinuationsTest, predictedContinuationsTest, out errorState);
    217       TestWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? testWeightedDirectionalSymmetry : double.NaN;
    218 
    219       double trainingTheilsU = OnlineTheilsUStatisticCalculator.Calculate(startTrainingValues, actualContinuationsTraining, predictedContinuationsTraining, out errorState);
    220       TrainingTheilsUStatistic = errorState == OnlineCalculatorError.None ? trainingTheilsU : double.NaN;
    221       double testTheilsU = OnlineTheilsUStatisticCalculator.Calculate(startTestValues, actualContinuationsTest, predictedContinuationsTest, out errorState);
    222       TestTheilsUStatistic = errorState == OnlineCalculatorError.None ? testTheilsU : double.NaN;
     193              */
     194
     195      double[] trainingDs = new double[ProblemData.TargetVariables.Count()];
     196      double[] testDs = new double[ProblemData.TargetVariables.Count()];
     197
     198      double[] trainingWds = new double[ProblemData.TargetVariables.Count()];
     199      double[] testWds = new double[ProblemData.TargetVariables.Count()];
     200
     201      double[] trainingTheilsU = new double[ProblemData.TargetVariables.Count()];
     202      double[] testTheilsU = new double[ProblemData.TargetVariables.Count()];
     203      int i = 0;
     204      var predictedContinuationTraining = PrognosedTrainingValues.ToArray();
     205      var predictedContinuationTest = PrognosedTestValues.ToArray();
     206
     207      foreach (var targetVariable in ProblemData.TargetVariables) {
     208        var actualTrainingValues = ProblemData.Dataset.GetDoubleValues(targetVariable, ProblemData.TrainingIndizes).ToArray();
     209        var startTrainingValues = actualTrainingValues.Take(1);
     210        // only one continuation (but the full training set)
     211        var actualContinuationTraining = actualTrainingValues.Skip(1);
     212
     213        var actualTestValues = ProblemData.Dataset.GetDoubleValues(targetVariable, ProblemData.TestIndizes).ToArray();
     214        var startTestValues = actualTestValues.Take(1);
     215        // only one continuation (but the full training set)
     216        var actualContinuationTest = actualTestValues.Skip(1);
     217
     218
     219        trainingDs[i] = OnlineDirectionalSymmetryCalculator.Calculate(startTrainingValues,
     220          Enumerable.Repeat(actualContinuationTraining, 1),
     221          Enumerable.Repeat(predictedContinuationTraining[i], 1), out errorState);
     222        if (errorState != OnlineCalculatorError.None) trainingDs[i] = double.NaN;
     223
     224        testDs[i] = OnlineDirectionalSymmetryCalculator.Calculate(startTestValues,
     225          Enumerable.Repeat(actualContinuationTest, 1),
     226          Enumerable.Repeat(predictedContinuationTest[i], 1), out errorState);
     227        if (errorState != OnlineCalculatorError.None) testDs[i] = double.NaN;
     228
     229        trainingWds[i] =
     230          OnlineWeightedDirectionalSymmetryCalculator.Calculate(startTrainingValues,
     231          Enumerable.Repeat(actualContinuationTraining, 1),
     232          Enumerable.Repeat(predictedContinuationTraining[i], 1), out errorState);
     233        if (errorState != OnlineCalculatorError.None) trainingWds[i] = double.NaN;
     234
     235        testWds[i] = OnlineWeightedDirectionalSymmetryCalculator.Calculate(startTestValues,
     236          Enumerable.Repeat(actualContinuationTest, 1),
     237          Enumerable.Repeat(predictedContinuationTest[i], 1), out errorState);
     238        if (errorState != OnlineCalculatorError.None) testWds[i] = double.NaN;
     239
     240        trainingTheilsU[i] = OnlineTheilsUStatisticCalculator.Calculate(startTrainingValues,
     241          Enumerable.Repeat(actualContinuationTraining, 1),
     242          Enumerable.Repeat(predictedContinuationTraining[i], 1), out errorState);
     243        if (errorState != OnlineCalculatorError.None) trainingTheilsU[i] = double.NaN;
     244
     245        testTheilsU[i] = OnlineTheilsUStatisticCalculator.Calculate(startTestValues,
     246          Enumerable.Repeat(actualContinuationTest, 1),
     247          Enumerable.Repeat(predictedContinuationTest[i], 1), out errorState);
     248        if (errorState != OnlineCalculatorError.None) testTheilsU[i] = double.NaN;
     249        i++;
     250      }
     251
     252      TrainingDirectionalSymmetry = trainingDs;
     253      TestDirectionalSymmetry = testDs;
     254      TrainingWeightedDirectionalSymmetry = trainingWds;
     255      TestWeightedDirectionalSymmetry = testWds;
     256      TrainingTheilsUStatistic = trainingTheilsU;
     257      TestTheilsUStatistic = testTheilsU;
    223258    }
    224259  }
Note: See TracChangeset for help on using the changeset viewer.