Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/Models/TimeSeriesPrognosisAutoRegressiveModel.cs @ 8468

Last change on this file since 8468 was 8468, checked in by mkommend, 12 years ago

#1081: Adapated calculation of results for horizons larger than 1.

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