[6802] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16057] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6802] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 30 | [StorableClass]
|
---|
[8458] | 31 | public abstract class TimeSeriesPrognosisSolutionBase : RegressionSolutionBase, ITimeSeriesPrognosisSolution {
|
---|
[8468] | 32 | #region result names
|
---|
| 33 | protected const string TrainingDirectionalSymmetryResultName = "Average directional symmetry (training)";
|
---|
| 34 | protected const string TestDirectionalSymmetryResultName = "Average directional symmetry (test)";
|
---|
| 35 | protected const string TrainingWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (training)";
|
---|
| 36 | protected const string TestWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (test)";
|
---|
| 37 | protected const string TrainingTheilsUStatisticAR1ResultName = "Theil's U2 (AR1) (training)";
|
---|
| 38 | protected const string TestTheilsUStatisticLastResultName = "Theil's U2 (AR1) (test)";
|
---|
| 39 | protected const string TrainingTheilsUStatisticMeanResultName = "Theil's U2 (mean) (training)";
|
---|
| 40 | protected const string TestTheilsUStatisticMeanResultName = "Theil's U2 (mean) (test)";
|
---|
[8750] | 41 | protected const string TimeSeriesPrognosisResultName = "Prognosis Results";
|
---|
[8468] | 42 | #endregion
|
---|
| 43 |
|
---|
| 44 | #region result descriptions
|
---|
| 45 | protected const string TrainingDirectionalSymmetryResultDescription = "The average directional symmetry of the forecasts of the model on the training partition";
|
---|
| 46 | protected const string TestDirectionalSymmetryResultDescription = "The average directional symmetry of the forecasts of the model on the test partition";
|
---|
| 47 | protected const string TrainingWeightedDirectionalSymmetryResultDescription = "The average weighted directional symmetry of the forecasts of the model on the training partition";
|
---|
| 48 | protected const string TestWeightedDirectionalSymmetryResultDescription = "The average weighted directional symmetry of the forecasts of the model on the test partition";
|
---|
| 49 | protected const string TrainingTheilsUStatisticAR1ResultDescription = "The Theil's U statistic (reference: AR1 model) of the forecasts of the model on the training partition";
|
---|
| 50 | protected const string TestTheilsUStatisticAR1ResultDescription = "The Theil's U statistic (reference: AR1 model) of the forecasts of the model on the test partition";
|
---|
| 51 | protected const string TrainingTheilsUStatisticMeanResultDescription = "The Theil's U statistic (reference: mean model) of the forecasts of the model on the training partition";
|
---|
| 52 | protected const string TestTheilsUStatisticMeanResultDescription = "The Theil's U statistic (reference: mean value) of the forecasts of the model on the test partition";
|
---|
[8750] | 53 | protected const string TimeSeriesPrognosisResultDescription = "The calculated results of predictions in the future.";
|
---|
[8468] | 54 | #endregion
|
---|
| 55 |
|
---|
[6802] | 56 | public new ITimeSeriesPrognosisModel Model {
|
---|
| 57 | get { return (ITimeSeriesPrognosisModel)base.Model; }
|
---|
| 58 | protected set { base.Model = value; }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public new ITimeSeriesPrognosisProblemData ProblemData {
|
---|
| 62 | get { return (ITimeSeriesPrognosisProblemData)base.ProblemData; }
|
---|
| 63 | set { base.ProblemData = value; }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[14422] | 66 | public abstract IEnumerable<double> PrognosedTestValues { get; }
|
---|
[8010] | 67 | public abstract IEnumerable<IEnumerable<double>> GetPrognosedValues(IEnumerable<int> rows, IEnumerable<int> horizon);
|
---|
[6802] | 68 |
|
---|
| 69 | #region Results
|
---|
[7989] | 70 | public double TrainingDirectionalSymmetry {
|
---|
| 71 | get { return ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value; }
|
---|
| 72 | private set { ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value = value; }
|
---|
[6802] | 73 | }
|
---|
[7989] | 74 | public double TestDirectionalSymmetry {
|
---|
| 75 | get { return ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value; }
|
---|
| 76 | private set { ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value = value; }
|
---|
[6802] | 77 | }
|
---|
[7989] | 78 | public double TrainingWeightedDirectionalSymmetry {
|
---|
| 79 | get { return ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value; }
|
---|
| 80 | private set { ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value = value; }
|
---|
[6802] | 81 | }
|
---|
[7989] | 82 | public double TestWeightedDirectionalSymmetry {
|
---|
| 83 | get { return ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value; }
|
---|
| 84 | private set { ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value = value; }
|
---|
[6802] | 85 | }
|
---|
[8468] | 86 | public double TrainingTheilsUStatisticAR1 {
|
---|
| 87 | get { return ((DoubleValue)this[TrainingTheilsUStatisticAR1ResultName].Value).Value; }
|
---|
| 88 | private set { ((DoubleValue)this[TrainingTheilsUStatisticAR1ResultName].Value).Value = value; }
|
---|
[6802] | 89 | }
|
---|
[8468] | 90 | public double TestTheilsUStatisticAR1 {
|
---|
[7989] | 91 | get { return ((DoubleValue)this[TestTheilsUStatisticLastResultName].Value).Value; }
|
---|
| 92 | private set { ((DoubleValue)this[TestTheilsUStatisticLastResultName].Value).Value = value; }
|
---|
[6802] | 93 | }
|
---|
[7989] | 94 | public double TrainingTheilsUStatisticMean {
|
---|
| 95 | get { return ((DoubleValue)this[TrainingTheilsUStatisticMeanResultName].Value).Value; }
|
---|
| 96 | private set { ((DoubleValue)this[TrainingTheilsUStatisticMeanResultName].Value).Value = value; }
|
---|
[7160] | 97 | }
|
---|
[7989] | 98 | public double TestTheilsUStatisticMean {
|
---|
| 99 | get { return ((DoubleValue)this[TestTheilsUStatisticMeanResultName].Value).Value; }
|
---|
| 100 | private set { ((DoubleValue)this[TestTheilsUStatisticMeanResultName].Value).Value = value; }
|
---|
[7160] | 101 | }
|
---|
[8468] | 102 |
|
---|
[8750] | 103 | public TimeSeriesPrognosisResults TimeSeriesPrognosisResults {
|
---|
[8468] | 104 | get {
|
---|
[8750] | 105 | if (!ContainsKey(TimeSeriesPrognosisResultName)) return null;
|
---|
| 106 | return (TimeSeriesPrognosisResults)this[TimeSeriesPrognosisResultName];
|
---|
[8468] | 107 | }
|
---|
[8750] | 108 | set {
|
---|
| 109 | if (ContainsKey(TimeSeriesPrognosisResultName)) Remove(TimeSeriesPrognosisResultName);
|
---|
| 110 | Add(new Result(TimeSeriesPrognosisResultName, TimeSeriesPrognosisResultDescription, value));
|
---|
[8468] | 111 | }
|
---|
| 112 | }
|
---|
[8750] | 113 | #endregion
|
---|
[8468] | 114 |
|
---|
| 115 |
|
---|
[8458] | 116 | public override IEnumerable<double> EstimatedValues {
|
---|
| 117 | get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
|
---|
| 118 | }
|
---|
| 119 | public override IEnumerable<double> EstimatedTrainingValues {
|
---|
| 120 | get { return GetEstimatedValues(ProblemData.TrainingIndices); }
|
---|
| 121 | }
|
---|
| 122 | public override IEnumerable<double> EstimatedTestValues {
|
---|
| 123 | get { return GetEstimatedValues(ProblemData.TestIndices); }
|
---|
| 124 | }
|
---|
| 125 | public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
| 126 | return Model.GetEstimatedValues(ProblemData.Dataset, rows);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[6802] | 129 | [StorableConstructor]
|
---|
| 130 | protected TimeSeriesPrognosisSolutionBase(bool deserializing) : base(deserializing) { }
|
---|
[8468] | 131 | protected TimeSeriesPrognosisSolutionBase(TimeSeriesPrognosisSolutionBase original, Cloner cloner) : base(original, cloner) { }
|
---|
[6802] | 132 | protected TimeSeriesPrognosisSolutionBase(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData)
|
---|
| 133 | : base(model, problemData) {
|
---|
[8468] | 134 | Add(new Result(TrainingDirectionalSymmetryResultName, TrainingDirectionalSymmetryResultDescription, new DoubleValue()));
|
---|
| 135 | Add(new Result(TestDirectionalSymmetryResultName, TestDirectionalSymmetryResultDescription, new DoubleValue()));
|
---|
| 136 | Add(new Result(TrainingWeightedDirectionalSymmetryResultName, TrainingWeightedDirectionalSymmetryResultDescription, new DoubleValue()));
|
---|
| 137 | Add(new Result(TestWeightedDirectionalSymmetryResultName, TestWeightedDirectionalSymmetryResultDescription, new DoubleValue()));
|
---|
| 138 | Add(new Result(TrainingTheilsUStatisticAR1ResultName, TrainingTheilsUStatisticAR1ResultDescription, new DoubleValue()));
|
---|
| 139 | Add(new Result(TestTheilsUStatisticLastResultName, TestTheilsUStatisticAR1ResultDescription, new DoubleValue()));
|
---|
| 140 | Add(new Result(TrainingTheilsUStatisticMeanResultName, TrainingTheilsUStatisticMeanResultDescription, new DoubleValue()));
|
---|
| 141 | Add(new Result(TestTheilsUStatisticMeanResultName, TestTheilsUStatisticMeanResultDescription, new DoubleValue()));
|
---|
[6802] | 142 | }
|
---|
| 143 |
|
---|
[8458] | 144 | protected override void RecalculateResults() {
|
---|
| 145 | base.RecalculateResults();
|
---|
| 146 | CalculateTimeSeriesResults();
|
---|
[8468] | 147 | CalculateTimeSeriesResults(ProblemData.TrainingHorizon, ProblemData.TestHorizon);
|
---|
[8458] | 148 | }
|
---|
[6802] | 149 |
|
---|
[8742] | 150 | protected void CalculateTimeSeriesResults() {
|
---|
[8010] | 151 | OnlineCalculatorError errorState;
|
---|
[11031] | 152 | double trainingMean = ProblemData.TrainingIndices.Any() ? ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).Average() : double.NaN;
|
---|
[14422] | 153 | var meanModel = new ConstantModel(trainingMean, ProblemData.TargetVariable);
|
---|
[6802] | 154 |
|
---|
[8010] | 155 | double alpha, beta;
|
---|
[8430] | 156 | IEnumerable<double> trainingStartValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices.Select(r => r - 1).Where(r => r > 0)).ToList();
|
---|
| 157 | OnlineLinearScalingParameterCalculator.Calculate(ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices.Where(x => x > 0)), trainingStartValues, out alpha, out beta, out errorState);
|
---|
[8468] | 158 | var AR1model = new TimeSeriesPrognosisAutoRegressiveModel(ProblemData.TargetVariable, new double[] { beta }, alpha);
|
---|
[7183] | 159 |
|
---|
| 160 |
|
---|
[8010] | 161 | #region Calculate training quality measures
|
---|
[11031] | 162 | if (ProblemData.TrainingIndices.Any()) {
|
---|
| 163 | IEnumerable<double> trainingTargetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
|
---|
| 164 | IEnumerable<double> trainingEstimatedValues = EstimatedTrainingValues.ToList();
|
---|
| 165 | IEnumerable<double> trainingMeanModelPredictions = meanModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TrainingIndices).ToList();
|
---|
| 166 | IEnumerable<double> trainingAR1ModelPredictions = AR1model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TrainingIndices).ToList();
|
---|
[7989] | 167 |
|
---|
[11031] | 168 | TrainingDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingEstimatedValues, out errorState);
|
---|
| 169 | TrainingDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TrainingDirectionalSymmetry : 0.0;
|
---|
| 170 | TrainingWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingEstimatedValues, out errorState);
|
---|
| 171 | TrainingWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TrainingWeightedDirectionalSymmetry : 0.0;
|
---|
| 172 | TrainingTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingAR1ModelPredictions, trainingEstimatedValues, out errorState);
|
---|
| 173 | TrainingTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticAR1 : double.PositiveInfinity;
|
---|
| 174 | TrainingTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingMeanModelPredictions, trainingEstimatedValues, out errorState);
|
---|
| 175 | TrainingTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticMean : double.PositiveInfinity;
|
---|
| 176 | }
|
---|
[8010] | 177 | #endregion
|
---|
[7989] | 178 |
|
---|
[8468] | 179 | #region Calculate test quality measures
|
---|
[11031] | 180 | if (ProblemData.TestIndices.Any()) {
|
---|
| 181 | IEnumerable<double> testTargetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices).ToList();
|
---|
| 182 | IEnumerable<double> testEstimatedValues = EstimatedTestValues.ToList();
|
---|
| 183 | IEnumerable<double> testMeanModelPredictions = meanModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndices).ToList();
|
---|
| 184 | IEnumerable<double> testAR1ModelPredictions = AR1model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndices).ToList();
|
---|
[7989] | 185 |
|
---|
[11031] | 186 | TestDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(testTargetValues.First(), testTargetValues, testEstimatedValues, out errorState);
|
---|
| 187 | TestDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TestDirectionalSymmetry : 0.0;
|
---|
| 188 | TestWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(testTargetValues.First(), testTargetValues, testEstimatedValues, out errorState);
|
---|
| 189 | TestWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TestWeightedDirectionalSymmetry : 0.0;
|
---|
| 190 | TestTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(testTargetValues.First(), testTargetValues, testAR1ModelPredictions, testEstimatedValues, out errorState);
|
---|
| 191 | TestTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticAR1 : double.PositiveInfinity;
|
---|
| 192 | TestTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(testTargetValues.First(), testTargetValues, testMeanModelPredictions, testEstimatedValues, out errorState);
|
---|
| 193 | TestTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticMean : double.PositiveInfinity;
|
---|
| 194 | }
|
---|
[8430] | 195 | #endregion
|
---|
[6802] | 196 | }
|
---|
[8468] | 197 |
|
---|
[8742] | 198 | protected void CalculateTimeSeriesResults(int trainingHorizon, int testHorizon) {
|
---|
[8750] | 199 | TimeSeriesPrognosisResults = new TimeSeriesPrognosisResults(trainingHorizon, testHorizon, this);
|
---|
[8468] | 200 | }
|
---|
[6802] | 201 | }
|
---|
| 202 | }
|
---|