[8010] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8010] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[8486] | 24 | using System.Linq;
|
---|
[8010] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace 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 {
|
---|
[13921] | 33 | public IEnumerable<string> VariablesUsedForPrediction {
|
---|
| 34 | get { return Enumerable.Empty<string>(); } // what to return here?
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[8010] | 37 | [Storable]
|
---|
[8430] | 38 | public double[] Phi { get; private set; }
|
---|
[8010] | 39 | [Storable]
|
---|
[8430] | 40 | public double Constant { get; private set; }
|
---|
[8010] | 41 | [Storable]
|
---|
| 42 | public string TargetVariable { get; private set; }
|
---|
| 43 |
|
---|
[8468] | 44 | public int TimeOffset { get { return Phi.Length; } }
|
---|
| 45 |
|
---|
[8010] | 46 | [StorableConstructor]
|
---|
| 47 | protected TimeSeriesPrognosisAutoRegressiveModel(bool deserializing) : base(deserializing) { }
|
---|
| 48 | protected TimeSeriesPrognosisAutoRegressiveModel(TimeSeriesPrognosisAutoRegressiveModel original, Cloner cloner)
|
---|
| 49 | : base(original, cloner) {
|
---|
[8430] | 50 | this.Phi = (double[])original.Phi.Clone();
|
---|
| 51 | this.Constant = original.Constant;
|
---|
[8010] | 52 | this.TargetVariable = original.TargetVariable;
|
---|
| 53 | }
|
---|
| 54 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 55 | return new TimeSeriesPrognosisAutoRegressiveModel(this, cloner);
|
---|
| 56 | }
|
---|
[8468] | 57 | public TimeSeriesPrognosisAutoRegressiveModel(string targetVariable, double[] phi, double constant)
|
---|
[8487] | 58 | : base("AR(1) Model") {
|
---|
[8468] | 59 | Phi = (double[])phi.Clone();
|
---|
| 60 | Constant = constant;
|
---|
[8010] | 61 | TargetVariable = targetVariable;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[12509] | 64 | public IEnumerable<IEnumerable<double>> GetPrognosedValues(IDataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons) {
|
---|
[8010] | 65 | var rowsEnumerator = rows.GetEnumerator();
|
---|
| 66 | var horizonsEnumerator = horizons.GetEnumerator();
|
---|
[8468] | 67 | var targetValues = dataset.GetReadOnlyDoubleValues(TargetVariable);
|
---|
[8010] | 68 | // produce a n-step forecast for all rows
|
---|
| 69 | while (rowsEnumerator.MoveNext() & horizonsEnumerator.MoveNext()) {
|
---|
| 70 | int row = rowsEnumerator.Current;
|
---|
| 71 | int horizon = horizonsEnumerator.Current;
|
---|
[8486] | 72 | if (row - TimeOffset < 0) {
|
---|
| 73 | yield return Enumerable.Repeat(double.NaN, horizon);
|
---|
| 74 | continue;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[8010] | 77 | double[] prognosis = new double[horizon];
|
---|
[8468] | 78 | for (int h = 0; h < horizon; h++) {
|
---|
| 79 | double estimatedValue = 0.0;
|
---|
[8486] | 80 | for (int i = 1; i <= TimeOffset; i++) {
|
---|
[8468] | 81 | int offset = h - i;
|
---|
| 82 | if (offset >= 0) estimatedValue += prognosis[offset] * Phi[i - 1];
|
---|
| 83 | else estimatedValue += targetValues[row + offset] * Phi[i - 1];
|
---|
| 84 |
|
---|
| 85 | }
|
---|
| 86 | estimatedValue += Constant;
|
---|
| 87 | prognosis[h] = estimatedValue;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[8010] | 90 | yield return prognosis;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | if (rowsEnumerator.MoveNext() || horizonsEnumerator.MoveNext())
|
---|
| 94 | throw new ArgumentException("Number of elements in rows and horizon enumerations doesn't match.");
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[12509] | 97 | public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
[8468] | 98 | var targetVariables = dataset.GetReadOnlyDoubleValues(TargetVariable);
|
---|
| 99 | foreach (int row in rows) {
|
---|
| 100 | double estimatedValue = 0.0;
|
---|
[8486] | 101 | if (row - TimeOffset < 0) {
|
---|
| 102 | yield return double.NaN;
|
---|
| 103 | continue;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[8468] | 106 | for (int i = 1; i <= TimeOffset; i++) {
|
---|
| 107 | estimatedValue += targetVariables[row - i] * Phi[i - 1];
|
---|
| 108 | }
|
---|
| 109 | estimatedValue += Constant;
|
---|
| 110 | yield return estimatedValue;
|
---|
| 111 | }
|
---|
[8458] | 112 | }
|
---|
| 113 |
|
---|
[8010] | 114 | public ITimeSeriesPrognosisSolution CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData) {
|
---|
[8857] | 115 | return new TimeSeriesPrognosisSolution(this, new TimeSeriesPrognosisProblemData(problemData));
|
---|
[8010] | 116 | }
|
---|
[8458] | 117 | public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
| 118 | throw new NotSupportedException();
|
---|
| 119 | }
|
---|
[8010] | 120 |
|
---|
| 121 | }
|
---|
| 122 | }
|
---|