Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/Models/TimeSeriesPrognosisMovingAverageModel.cs @ 8430

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

#1081: Intermediate commit of trunk updates - interpreter changes must be redone.

File size: 3.6 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("Moveing Average TimeSeries Model", "A moving average time series model used to predict future values.")]
32  public class TimeSeriesPrognosisMovingAverageModel : NamedItem, ITimeSeriesPrognosisModel {
33    [Storable]
34    public string TargetVariable { get; private set; }
35    [Storable]
36    public int WindowSize { get; private set; }
37
38    [StorableConstructor]
39    protected TimeSeriesPrognosisMovingAverageModel(bool deserializing) : base(deserializing) { }
40    protected TimeSeriesPrognosisMovingAverageModel(TimeSeriesPrognosisMovingAverageModel original, Cloner cloner)
41      : base(original, cloner) {
42      this.TargetVariable = original.TargetVariable;
43      this.WindowSize = original.WindowSize;
44    }
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new TimeSeriesPrognosisMovingAverageModel(this, cloner);
47    }
48    public TimeSeriesPrognosisMovingAverageModel(int windowSize, string targetVariable)
49      : base() {
50      TargetVariable = targetVariable;
51      WindowSize = Math.Abs(windowSize);
52    }
53
54    public IEnumerable<IEnumerable<double>> GetPrognosedValues(Dataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons) {
55      var rowsEnumerator = rows.GetEnumerator();
56      var horizonsEnumerator = horizons.GetEnumerator();
57      // produce a n-step forecast for all rows
58      while (rowsEnumerator.MoveNext() & horizonsEnumerator.MoveNext()) {
59        int row = rowsEnumerator.Current;
60        int horizon = horizonsEnumerator.Current;
61        int startIndex = row - WindowSize;
62
63        if (startIndex < 0) startIndex = 0;
64        int count = row - startIndex - 1;
65
66        List<double> targetValues = dataset.GetDoubleValues(TargetVariable, Enumerable.Range(startIndex, count)).ToList();
67        int position = 0;
68        for (int i = 0; i < horizon; i++) {
69          double prognosis = targetValues.GetRange(position, count).Average();
70          targetValues.Add(prognosis);
71          if (count < WindowSize) count++;
72          else position++;
73        }
74        yield return targetValues.GetRange(targetValues.Count - horizon, horizon);
75      }
76
77      if (rowsEnumerator.MoveNext() || horizonsEnumerator.MoveNext())
78        throw new ArgumentException("Number of elements in rows and horizon enumerations doesn't match.");
79    }
80
81    public ITimeSeriesPrognosisSolution CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData) {
82      return new TimeSeriesPrognosisSolution(this, problemData);
83    }
84
85  }
86}
Note: See TracBrowser for help on using the repository browser.