Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/Models/TimeSeriesPrognosisAutoRegressiveModel.cs @ 16640

Last change on this file since 16640 was 16640, checked in by gkronber, 5 years ago

#2971: merged r16565:16631 from trunk/HeuristicLab.Problems.DataAnalysis to branch/HeuristicLab.Problems.DataAnalysis (resolving all conflicts)

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HEAL.Attic;
28using HEAL.Attic;
29
30namespace HeuristicLab.Problems.DataAnalysis {
31  [StorableType("9C44E097-50F8-4EC1-BE1B-6A0246EC020E")]
32  [Item("Autoregressive TimeSeries Model", "A linear autoregressive time series model used to predict future values.")]
33  public class TimeSeriesPrognosisAutoRegressiveModel : RegressionModel, ITimeSeriesPrognosisModel {
34    public override IEnumerable<string> VariablesUsedForPrediction {
35      get { return new[] { TargetVariable }; }
36    }
37
38    [Storable]
39    public double[] Phi { get; private set; }
40    [Storable]
41    public double Constant { get; private set; }
42
43    public int TimeOffset { get { return Phi.Length; } }
44
45    [StorableConstructor]
46    protected TimeSeriesPrognosisAutoRegressiveModel(StorableConstructorFlag _) : base(_) { }
47    protected TimeSeriesPrognosisAutoRegressiveModel(TimeSeriesPrognosisAutoRegressiveModel original, Cloner cloner)
48      : base(original, cloner) {
49      this.Phi = (double[])original.Phi.Clone();
50      this.Constant = original.Constant;
51    }
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new TimeSeriesPrognosisAutoRegressiveModel(this, cloner);
54    }
55    public TimeSeriesPrognosisAutoRegressiveModel(string targetVariable, double[] phi, double constant)
56      : base(targetVariable, "AR(1) Model") {
57      Phi = (double[])phi.Clone();
58      Constant = constant;
59    }
60
61    public IEnumerable<IEnumerable<double>> GetPrognosedValues(IDataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons) {
62      var rowsEnumerator = rows.GetEnumerator();
63      var horizonsEnumerator = horizons.GetEnumerator();
64      var targetValues = dataset.GetReadOnlyDoubleValues(TargetVariable);
65      // produce a n-step forecast for all rows
66      while (rowsEnumerator.MoveNext() & horizonsEnumerator.MoveNext()) {
67        int row = rowsEnumerator.Current;
68        int horizon = horizonsEnumerator.Current;
69        if (row - TimeOffset < 0) {
70          yield return Enumerable.Repeat(double.NaN, horizon);
71          continue;
72        }
73
74        double[] prognosis = new double[horizon];
75        for (int h = 0; h < horizon; h++) {
76          double estimatedValue = 0.0;
77          for (int i = 1; i <= TimeOffset; i++) {
78            int offset = h - i;
79            if (offset >= 0) estimatedValue += prognosis[offset] * Phi[i - 1];
80            else estimatedValue += targetValues[row + offset] * Phi[i - 1];
81
82          }
83          estimatedValue += Constant;
84          prognosis[h] = estimatedValue;
85        }
86
87        yield return prognosis;
88      }
89
90      if (rowsEnumerator.MoveNext() || horizonsEnumerator.MoveNext())
91        throw new ArgumentException("Number of elements in rows and horizon enumerations doesn't match.");
92    }
93
94    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
95      var targetVariables = dataset.GetReadOnlyDoubleValues(TargetVariable);
96      foreach (int row in rows) {
97        double estimatedValue = 0.0;
98        if (row - TimeOffset < 0) {
99          yield return double.NaN;
100          continue;
101        }
102
103        for (int i = 1; i <= TimeOffset; i++) {
104          estimatedValue += targetVariables[row - i] * Phi[i - 1];
105        }
106        estimatedValue += Constant;
107        yield return estimatedValue;
108      }
109    }
110
111    public ITimeSeriesPrognosisSolution CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData) {
112      return new TimeSeriesPrognosisSolution(this, new TimeSeriesPrognosisProblemData(problemData));
113    }
114    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
115      throw new NotSupportedException();
116    }
117
118  }
119}
Note: See TracBrowser for help on using the repository browser.