Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/Models/TimeSeriesPrognosisAutoRegressiveModel.cs @ 17736

Last change on this file since 17736 was 12509, checked in by mkommend, 9 years ago

#2276: Reintegrated branch for dataset refactoring.

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