Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1081: Derived time series classes from regression classes to avoid code duplication.

File size: 3.8 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 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    [Storable]
40    public int TimeOffset { get; private set; }
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      this.TimeOffset = original.TimeOffset;
50    }
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new TimeSeriesPrognosisAutoRegressiveModel(this, cloner);
53    }
54    public TimeSeriesPrognosisAutoRegressiveModel(double alpha, double beta, string targetVariable)
55      : base() {
56      //Alpha = alpha;
57      //Beta = beta;
58      TargetVariable = targetVariable;
59      TimeOffset = 1;
60    }
61
62    public IEnumerable<IEnumerable<double>> GetPrognosedValues(Dataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons) {
63      var rowsEnumerator = rows.GetEnumerator();
64      var horizonsEnumerator = horizons.GetEnumerator();
65      var targetVariables = dataset.GetReadOnlyDoubleValues(TargetVariable);
66      // produce a n-step forecast for all rows
67      while (rowsEnumerator.MoveNext() & horizonsEnumerator.MoveNext()) {
68        int row = rowsEnumerator.Current;
69        int horizon = horizonsEnumerator.Current;
70        double[] prognosis = new double[horizon];
71        for (int i = 0; i < horizon; i++)
72          prognosis[i] = targetVariables[row - TimeOffset];
73        yield return prognosis;
74      }
75
76      if (rowsEnumerator.MoveNext() || horizonsEnumerator.MoveNext())
77        throw new ArgumentException("Number of elements in rows and horizon enumerations doesn't match.");
78    }
79
80    public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
81      return GetPrognosedValues(dataset, rows, rows.Select(r => 1)).SelectMany(e => e);
82    }
83
84    public ITimeSeriesPrognosisSolution CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData) {
85      return new TimeSeriesPrognosisSolution(this, problemData);
86    }
87    public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
88      throw new NotSupportedException();
89    }
90
91  }
92}
Note: See TracBrowser for help on using the repository browser.