#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis {
[StorableClass]
[Item("Moveing Average TimeSeries Model", "A moving average time series model used to predict future values.")]
public class TimeSeriesPrognosisMovingAverageModel : NamedItem, ITimeSeriesPrognosisModel {
[Storable]
public string TargetVariable { get; private set; }
[Storable]
public int WindowSize { get; private set; }
[StorableConstructor]
protected TimeSeriesPrognosisMovingAverageModel(bool deserializing) : base(deserializing) { }
protected TimeSeriesPrognosisMovingAverageModel(TimeSeriesPrognosisMovingAverageModel original, Cloner cloner)
: base(original, cloner) {
this.TargetVariable = original.TargetVariable;
this.WindowSize = original.WindowSize;
}
public override IDeepCloneable Clone(Cloner cloner) {
return new TimeSeriesPrognosisMovingAverageModel(this, cloner);
}
public TimeSeriesPrognosisMovingAverageModel(int windowSize, string targetVariable)
: base() {
TargetVariable = targetVariable;
WindowSize = Math.Abs(windowSize);
}
public IEnumerable> GetPrognosedValues(Dataset dataset, IEnumerable rows, IEnumerable horizons) {
var rowsEnumerator = rows.GetEnumerator();
var horizonsEnumerator = horizons.GetEnumerator();
// produce a n-step forecast for all rows
while (rowsEnumerator.MoveNext() & horizonsEnumerator.MoveNext()) {
int row = rowsEnumerator.Current;
int horizon = horizonsEnumerator.Current;
int startIndex = row - WindowSize;
if (startIndex < 0) startIndex = 0;
int count = row - startIndex - 1;
List targetValues = dataset.GetDoubleValues(TargetVariable, Enumerable.Range(startIndex, count)).ToList();
int position = 0;
for (int i = 0; i < horizon; i++) {
double prognosis = targetValues.GetRange(position, count).Average();
targetValues.Add(prognosis);
if (count < WindowSize) count++;
else position++;
}
yield return targetValues.GetRange(targetValues.Count - horizon, horizon);
}
if (rowsEnumerator.MoveNext() || horizonsEnumerator.MoveNext())
throw new ArgumentException("Number of elements in rows and horizon enumerations doesn't match.");
}
public IEnumerable GetEstimatedValues(Dataset dataset, IEnumerable rows) {
return GetPrognosedValues(dataset, rows, rows.Select(r => 1)).SelectMany(e => e);
}
public IEnumerable GetEstimatedValues(Dataset dataset, IEnumerable rows, int x) {
var targetValues = dataset.GetReadOnlyDoubleValues(TargetVariable).ToList();
foreach (int row in rows) {
yield return targetValues.GetRange(row - WindowSize, WindowSize).Average();
}
}
public ITimeSeriesPrognosisSolution CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData) {
return new TimeSeriesPrognosisSolution(this, problemData);
}
public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
throw new NotSupportedException();
}
}
}