Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/15/12 13:17:07 (13 years ago)
Author:
mkommend
Message:

#1081: Corrected time series solution results and implemented new models.

Location:
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis
Files:
4 added
2 edited

Legend:

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

    r7989 r8010  
    2929  /// </summary>
    3030  [StorableClass]
    31   public abstract class TimeSeriesPrognosisSolution : TimeSeriesPrognosisSolutionBase {
    32 
     31  public class TimeSeriesPrognosisSolution : TimeSeriesPrognosisSolutionBase {
    3332    [StorableConstructor]
    3433    protected TimeSeriesPrognosisSolution(bool deserializing) : base(deserializing) { }
    3534    protected TimeSeriesPrognosisSolution(TimeSeriesPrognosisSolution original, Cloner cloner) : base(original, cloner) { }
    36     protected TimeSeriesPrognosisSolution(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData) : base(model, problemData) { }
     35    protected internal TimeSeriesPrognosisSolution(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData) : base(model, problemData) { }
    3736
    3837    protected override void RecalculateResults() {
     
    4039    }
    4140
    42     public override IEnumerable<IEnumerable<double>> GetPrognosedValues(IEnumerable<int> rows, int horizon) {
    43       return Model.GetPrognosedValues(ProblemData.Dataset, rows, horizon);
     41    public override IEnumerable<IEnumerable<double>> GetPrognosedValues(IEnumerable<int> rows, IEnumerable<int> horizons) {
     42      return Model.GetPrognosedValues(ProblemData.Dataset, rows, horizons);
    4443    }
    4544  }
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolutionBase.cs

    r7989 r8010  
    7474    }
    7575
    76     public abstract IEnumerable<IEnumerable<double>> GetPrognosedValues(IEnumerable<int> rows, int horizon);
     76    public abstract IEnumerable<IEnumerable<double>> GetPrognosedValues(IEnumerable<int> rows, IEnumerable<int> horizon);
    7777
    7878    #region Results
     
    192192
    193193    protected void CalculateResults() {
    194       string targetVariable = ProblemData.TargetVariable;
    195 
    196       var trainingMseCalculators = new OnlineMeanSquaredErrorCalculator();
    197       var testMseCalculators = new OnlineMeanSquaredErrorCalculator();
    198       var trainingMaeCalculators = new OnlineMeanAbsoluteErrorCalculator();
    199       var testMaeCalculators = new OnlineMeanAbsoluteErrorCalculator();
    200       var trainingRSquaredCalculators = new OnlinePearsonsRSquaredCalculator();
    201       var testRSquaredCalculators = new OnlinePearsonsRSquaredCalculator();
    202       var trainingRelErrorCalculators = new OnlineMeanAbsolutePercentageErrorCalculator();
    203       var testRelErrorCalculators = new OnlineMeanAbsolutePercentageErrorCalculator();
    204       var trainingNmseCalculators = new OnlineNormalizedMeanSquaredErrorCalculator();
    205       var testNmseCalculators = new OnlineNormalizedMeanSquaredErrorCalculator();
    206 
    207       var trainingDsCalculators = new OnlineDirectionalSymmetryCalculator();
    208       var testDsCalculators = new OnlineDirectionalSymmetryCalculator();
    209       var trainingWdsCalculators = new OnlineWeightedDirectionalSymmetryCalculator();
    210       var testWdsCalculators = new OnlineWeightedDirectionalSymmetryCalculator();
    211       var trainingTheilsULastCalculators = new OnlineTheilsUStatisticCalculator();
    212       var testTheilsULastCalculators = new OnlineTheilsUStatisticCalculator();
    213       var trainingTheilsUMeanCalculators = new OnlineTheilsUStatisticCalculator();
    214       var testTheilsUMeanCalculators = new OnlineTheilsUStatisticCalculator();
    215       var trainingTheilsUMovingAverageCalculators = new OnlineTheilsUStatisticCalculator();
    216       var testTheilsUMovingAverageCalculators = new OnlineTheilsUStatisticCalculator();
    217 
    218       double mean = ProblemData.Dataset.GetDoubleValues(targetVariable, ProblemData.TrainingIndizes).Average();
    219 
    220       foreach (var row in ProblemData.TrainingIndizes) {
    221         if (row + horizon < ProblemData.Dataset.Rows) {
    222           var actualContinuation = ProblemData.Dataset.GetDoubleValues(targetVariable, Enumerable.Range(row, horizon)).ToList();
    223           var prognosedContinuation = GetPrognosedValues(new List<int> { row }, horizon).First().ToList();
    224 
    225           int maWindow = 10 * horizon;
    226           var movingAverageContinuation = from h in Enumerable.Range(0, horizon)
    227                                           select (from r in Enumerable.Range(row + h - maWindow, maWindow - h)
    228                                                   where r > 0
    229                                                   select ProblemData.Dataset.GetDoubleValue(targetVariable, r)
    230                                                  ).Average();
    231 
    232           double startValue = ProblemData.Dataset.GetDoubleValue(targetVariable, row - 1);
    233 
    234           trainingDsCalculators.Add(startValue, actualContinuation, prognosedContinuation);
    235           trainingWdsCalculators.Add(startValue, actualContinuation, prognosedContinuation);
    236           trainingTheilsULastCalculators.Add(startValue, actualContinuation, prognosedContinuation);
    237           trainingTheilsUMeanCalculators.Add(startValue, actualContinuation.Select(x => mean), actualContinuation, prognosedContinuation);
    238           trainingTheilsUMovingAverageCalculators.Add(startValue, movingAverageContinuation, actualContinuation, prognosedContinuation);
    239 
    240           var actualContinuationEnumerator = actualContinuation.GetEnumerator();
    241           var prognosedContinuationEnumerator = prognosedContinuation.GetEnumerator();
    242 
    243           while (actualContinuationEnumerator.MoveNext() & prognosedContinuationEnumerator.MoveNext()) {
    244             trainingMseCalculators.Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
    245             trainingMaeCalculators.Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
    246             trainingRelErrorCalculators.Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
    247             trainingRSquaredCalculators.Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
    248             trainingNmseCalculators.Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
    249           }
    250           if (actualContinuationEnumerator.MoveNext() | prognosedContinuationEnumerator.MoveNext())
    251             throw new ArgumentException("Different number of elements in Actual continuation and prognosed continuation.");
    252         }
    253       }
    254 
    255       mean = ProblemData.Dataset.GetDoubleValues(targetVariable, ProblemData.TestIndizes).Average();
    256       foreach (var row in ProblemData.TestIndizes) {
    257         if (row + horizon < ProblemData.Dataset.Rows) {
    258           var actualContinuation = ProblemData.Dataset.GetDoubleValues(targetVariable, Enumerable.Range(row, horizon)).ToList();
    259           var prognosedContinuation = GetPrognosedValues(new List<int> { row }, horizon).First().ToList();
    260 
    261           int maWindow = 10 * horizon;
    262           var movingAverageContinuation = from h in Enumerable.Range(0, horizon)
    263                                           select (from r in Enumerable.Range(row + h - maWindow, maWindow - h)
    264                                                   where r > 0
    265                                                   select ProblemData.Dataset.GetDoubleValue(targetVariable, r)
    266                                                  ).Average();
    267 
    268           double startValue = ProblemData.Dataset.GetDoubleValue(targetVariable, row - 1);
    269           testDsCalculators.Add(startValue, actualContinuation, prognosedContinuation);
    270           testWdsCalculators.Add(startValue, actualContinuation, prognosedContinuation);
    271           testTheilsULastCalculators.Add(startValue, actualContinuation, prognosedContinuation);
    272           testTheilsUMeanCalculators.Add(startValue, actualContinuation.Select(x => mean), actualContinuation, prognosedContinuation);
    273           testTheilsUMovingAverageCalculators.Add(startValue, movingAverageContinuation, actualContinuation, prognosedContinuation);
    274 
    275           var actualContinuationEnumerator = actualContinuation.GetEnumerator();
    276           var prognosedContinuationEnumerator = prognosedContinuation.GetEnumerator();
    277           while (actualContinuationEnumerator.MoveNext() & prognosedContinuationEnumerator.MoveNext()) {
    278             testMseCalculators.Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
    279             testMaeCalculators.Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
    280             testRelErrorCalculators.Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
    281             testRSquaredCalculators.Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
    282             testNmseCalculators.Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
    283           }
    284           if (actualContinuationEnumerator.MoveNext() | prognosedContinuationEnumerator.MoveNext())
    285             throw new ArgumentException("Different number of elements in Actual continuation and prognosed continuation.");
    286         }
    287       }
    288 
    289       TrainingMeanSquaredError = trainingMseCalculators.ErrorState == OnlineCalculatorError.None ? trainingMseCalculators.Value : double.PositiveInfinity;
    290       TestMeanSquaredError = testMseCalculators.ErrorState == OnlineCalculatorError.None ? testMseCalculators.Value : double.PositiveInfinity;
    291       TrainingMeanAbsoluteError = trainingMaeCalculators.ErrorState == OnlineCalculatorError.None ? trainingMaeCalculators.Value : double.PositiveInfinity;
    292       TestMeanAbsoluteError = testMaeCalculators.ErrorState == OnlineCalculatorError.None ? testMaeCalculators.Value : double.PositiveInfinity;
    293       TrainingRelativeError = trainingRelErrorCalculators.ErrorState == OnlineCalculatorError.None ? trainingRelErrorCalculators.Value : double.PositiveInfinity;
    294       TestRelativeError = testRelErrorCalculators.ErrorState == OnlineCalculatorError.None ? testRelErrorCalculators.Value : double.PositiveInfinity;
    295       TrainingRSquared = trainingRSquaredCalculators.ErrorState == OnlineCalculatorError.None ? trainingRSquaredCalculators.Value : 0.0;
    296       TestRSquared = testRSquaredCalculators.ErrorState == OnlineCalculatorError.None ? testRSquaredCalculators.Value : 0.0;
    297       TrainingNormalizedMeanSquaredError = trainingNmseCalculators.ErrorState == OnlineCalculatorError.None ? trainingNmseCalculators.Value : double.PositiveInfinity;
    298       TestNormalizedMeanSquaredError = testNmseCalculators.ErrorState == OnlineCalculatorError.None ? testNmseCalculators.Value : double.PositiveInfinity;
    299 
    300       TrainingDirectionalSymmetry = trainingDsCalculators.ErrorState == OnlineCalculatorError.None ? trainingDsCalculators.Value : 0.0;
    301       TestDirectionalSymmetry = testDsCalculators.ErrorState == OnlineCalculatorError.None ? testDsCalculators.Value : 0.0;
    302       TrainingWeightedDirectionalSymmetry = trainingWdsCalculators.ErrorState == OnlineCalculatorError.None ? trainingWdsCalculators.Value : double.PositiveInfinity;
    303       TestWeightedDirectionalSymmetry = testWdsCalculators.ErrorState == OnlineCalculatorError.None ? testWdsCalculators.Value : double.PositiveInfinity;
    304       TrainingTheilsUStatisticLast = trainingTheilsULastCalculators.ErrorState == OnlineCalculatorError.None ? trainingTheilsULastCalculators.Value : double.PositiveInfinity;
    305       TestTheilsUStatisticLast = testTheilsULastCalculators.ErrorState == OnlineCalculatorError.None ? testTheilsULastCalculators.Value : double.PositiveInfinity;
    306       TrainingTheilsUStatisticMean = trainingTheilsUMeanCalculators.ErrorState == OnlineCalculatorError.None ? trainingTheilsUMeanCalculators.Value : double.PositiveInfinity;
    307       TestTheilsUStatisticMean = testTheilsUMeanCalculators.ErrorState == OnlineCalculatorError.None ? testTheilsUMeanCalculators.Value : double.PositiveInfinity;
    308       TrainingTheilsUStatisticMovingAverage = trainingTheilsUMovingAverageCalculators.ErrorState == OnlineCalculatorError.None ? trainingTheilsUMovingAverageCalculators.Value : double.PositiveInfinity;
    309       TestTheilsUStatisticMovingAverage = testTheilsUMovingAverageCalculators.ErrorState == OnlineCalculatorError.None ? testTheilsUMovingAverageCalculators.Value : double.PositiveInfinity;
     194      OnlineCalculatorError errorState;
     195      //mean model
     196      double trainingMean = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).Average();
     197      var meanModel = new TimeSeriesPrognosisConstantModel(trainingMean);
     198
     199      //AR1 model
     200      double alpha, beta;
     201      IEnumerable<double> trainingStartValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes.Select(r => r - 1).Where(r => r > 0)).ToList();
     202      OnlineLinearScalingParameterCalculator.Calculate(ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes.Where(x => x > 0)), trainingStartValues, out alpha, out beta, out errorState);
     203      var AR1model = new TimeSeriesPrognosisAutoRegressiveModel(alpha, beta, ProblemData.TargetVariable);
     204
     205      //MA model
     206      int movingAverageWindowSize = 10 + horizon;
     207      var MovingAverageModel = new TimeSeriesPrognosisMovingAverageModel(movingAverageWindowSize, ProblemData.TargetVariable);
     208
     209      #region Calculate training quality measures
     210      var trainingHorizions = ProblemData.TrainingIndizes.Select(r => Math.Min(horizon, ProblemData.TrainingPartition.End - r)).ToList();
     211      IEnumerable<IEnumerable<double>> trainingTargetValues = ProblemData.TrainingIndizes.Zip(trainingHorizions, Enumerable.Range).Select(r => ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, r)).ToList();
     212      IEnumerable<IEnumerable<double>> trainingEstimatedValues = Model.GetPrognosedValues(ProblemData.Dataset, ProblemData.TrainingIndizes, 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
     225      IEnumerable<IEnumerable<double>> trainingMeanModelPredictions = meanModel.GetPrognosedValues(ProblemData.Dataset, ProblemData.TrainingIndizes, trainingHorizions);
     226      IEnumerable<IEnumerable<double>> trainingAR1ModelPredictions = AR1model.GetPrognosedValues(ProblemData.Dataset, ProblemData.TrainingIndizes, trainingHorizions);
     227      IEnumerable<IEnumerable<double>> trainingMovingAverageModelPredictions = MovingAverageModel.GetPrognosedValues(ProblemData.Dataset, ProblemData.TrainingIndizes, trainingHorizions);
     228
     229      TrainingDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingEstimatedValues, out errorState);
     230      TrainingDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TrainingDirectionalSymmetry : 0.0;
     231      TrainingWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingEstimatedValues, out errorState);
     232      TrainingWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TrainingWeightedDirectionalSymmetry : 0.0;
     233      TrainingTheilsUStatisticLast = OnlineTheilsUStatisticCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingAR1ModelPredictions, trainingEstimatedValues, out errorState);
     234      TrainingTheilsUStatisticLast = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticLast : double.PositiveInfinity; ;
     235      TrainingTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingMeanModelPredictions, trainingEstimatedValues, out errorState);
     236      TrainingTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticMean : double.PositiveInfinity; ;
     237      TrainingTheilsUStatisticMovingAverage = OnlineTheilsUStatisticCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingMovingAverageModelPredictions, trainingEstimatedValues, out errorState);
     238      TrainingTheilsUStatisticMovingAverage = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticMovingAverage : double.PositiveInfinity; ; ;
     239      #endregion
     240
     241      #region  Calculate test quality measures
     242      var testHorizions = ProblemData.TestIndizes.Select(r => Math.Min(horizon, ProblemData.TestPartition.End - r)).ToList();
     243      IEnumerable<IEnumerable<double>> testTargetValues = ProblemData.TestIndizes.Zip(testHorizions, Enumerable.Range).Select(r => ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, r)).ToList();
     244      IEnumerable<IEnumerable<double>> testEstimatedValues = Model.GetPrognosedValues(ProblemData.Dataset, ProblemData.TestIndizes, 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
     257      IEnumerable<double> testStartValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndizes.Select(r => r - 1).Where(r => r > 0)).ToList();
     258      IEnumerable<IEnumerable<double>> testMeanModelPredictions = meanModel.GetPrognosedValues(ProblemData.Dataset, ProblemData.TestIndizes, testHorizions);
     259      IEnumerable<IEnumerable<double>> testAR1ModelPredictions = AR1model.GetPrognosedValues(ProblemData.Dataset, ProblemData.TestIndizes, testHorizions);
     260      IEnumerable<IEnumerable<double>> testMovingAverageModelPredictions = MovingAverageModel.GetPrognosedValues(ProblemData.Dataset, ProblemData.TestIndizes, testHorizions);
     261
     262      TestDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(testStartValues, testTargetValues, testEstimatedValues, out errorState);
     263      TestDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TestDirectionalSymmetry : 0.0;
     264      TestWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(testStartValues, testTargetValues, testEstimatedValues, out errorState);
     265      TestWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TestWeightedDirectionalSymmetry : 0.0;
     266      TestTheilsUStatisticLast = OnlineTheilsUStatisticCalculator.Calculate(testStartValues, testTargetValues, testAR1ModelPredictions, testEstimatedValues, out errorState);
     267      TestTheilsUStatisticLast = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticLast : double.PositiveInfinity; ;
     268      TestTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(testStartValues, testTargetValues, testMeanModelPredictions, testEstimatedValues, out errorState);
     269      TestTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticMean : double.PositiveInfinity; ;
     270      TestTheilsUStatisticMovingAverage = OnlineTheilsUStatisticCalculator.Calculate(testStartValues, testTargetValues, testMovingAverageModelPredictions, testEstimatedValues, out errorState);
     271      TestTheilsUStatisticMovingAverage = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticMovingAverage : double.PositiveInfinity; ; ;
     272      #endregion     
    310273    }
    311274  }
Note: See TracChangeset for help on using the changeset viewer.