Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/TimeSeries/AutoregressiveModeling.cs @ 15583

Last change on this file since 15583 was 15583, checked in by swagner, 6 years ago

#2640: Updated year of copyrights in license headers

File size: 6.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Linq;
24using System.Threading;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.DataAnalysis;
33using HeuristicLab.Problems.DataAnalysis.Symbolic;
34using HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis;
35
36namespace HeuristicLab.Algorithms.DataAnalysis.TimeSeries {
37  [Item("Autoregressive Modeling (AR)", "Timeseries modeling algorithm that creates AR-N models.")]
38  [Creatable(CreatableAttribute.Categories.DataAnalysis, Priority = 130)]
39  [StorableClass]
40  public class AutoregressiveModeling : FixedDataAnalysisAlgorithm<ITimeSeriesPrognosisProblem> {
41    private const string TimeOffesetParameterName = "Maximum Time Offset";
42
43    public IFixedValueParameter<IntValue> TimeOffsetParameter {
44      get { return (IFixedValueParameter<IntValue>)Parameters[TimeOffesetParameterName]; }
45    }
46
47
48    public int TimeOffset {
49      get { return TimeOffsetParameter.Value.Value; }
50      set { TimeOffsetParameter.Value.Value = value; }
51    }
52
53    [StorableConstructor]
54    protected AutoregressiveModeling(bool deserializing) : base(deserializing) { }
55    protected AutoregressiveModeling(AutoregressiveModeling original, Cloner cloner) : base(original, cloner) { }
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new AutoregressiveModeling(this, cloner);
58    }
59
60    public AutoregressiveModeling()
61      : base() {
62      Parameters.Add(new FixedValueParameter<IntValue>(TimeOffesetParameterName, "The maximum time offset for the model ranging from 1 to infinity.", new IntValue(1)));
63      Problem = new TimeSeriesPrognosisProblem();
64    }
65
66    protected override void Run(CancellationToken cancellationToken) {
67      double rmsError, cvRmsError;
68      var solution = CreateAutoRegressiveSolution(Problem.ProblemData, TimeOffset, out rmsError, out cvRmsError);
69      Results.Add(new Result("Autoregressive solution", "The autoregressive time series prognosis solution.", solution));
70      Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the autoregressive time series prognosis solution on the training set.", new DoubleValue(rmsError)));
71      Results.Add(new Result("Estimated root mean square error (cross-validation)", "The estimated root of the mean of squared errors of the autoregressive time series prognosis solution via cross validation.", new DoubleValue(cvRmsError)));
72    }
73
74    /// <summary>
75    /// Calculates an AR(p) model. For further information see http://en.wikipedia.org/wiki/Autoregressive_model
76    /// </summary>
77    /// <param name="problemData">The problem data which should be used for training</param>
78    /// <param name="timeOffset">The parameter p of the AR(p) specifying the maximum time offset [1,infinity] </param>
79    /// <returns>The times series autoregressive solution </returns>
80    public static ITimeSeriesPrognosisSolution CreateAutoRegressiveSolution(ITimeSeriesPrognosisProblemData problemData, int timeOffset) {
81      double rmsError, cvRmsError;
82      return CreateAutoRegressiveSolution(problemData, timeOffset, out rmsError, out cvRmsError);
83    }
84
85    private static ITimeSeriesPrognosisSolution CreateAutoRegressiveSolution(ITimeSeriesPrognosisProblemData problemData, int timeOffset, out double rmsError, out double cvRmsError) {
86      string targetVariable = problemData.TargetVariable;
87
88      double[,] inputMatrix = new double[problemData.TrainingPartition.Size, timeOffset + 1];
89      var targetValues = problemData.Dataset.GetDoubleValues(targetVariable).ToList();
90      for (int i = 0, row = problemData.TrainingPartition.Start; i < problemData.TrainingPartition.Size; i++, row++) {
91        for (int col = 0; col < timeOffset; col++) {
92          inputMatrix[i, col] = targetValues[row - col - 1];
93        }
94      }
95      // set target values in last column
96      for (int i = 0; i < inputMatrix.GetLength(0); i++)
97        inputMatrix[i, timeOffset] = targetValues[i + problemData.TrainingPartition.Start];
98
99      if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
100        throw new NotSupportedException("Linear regression does not support NaN or infinity values in the input dataset.");
101
102
103      alglib.linearmodel lm = new alglib.linearmodel();
104      alglib.lrreport ar = new alglib.lrreport();
105      int nRows = inputMatrix.GetLength(0);
106      int nFeatures = inputMatrix.GetLength(1) - 1;
107      double[] coefficients = new double[nFeatures + 1]; // last coefficient is for the constant
108
109      int retVal = 1;
110      alglib.lrbuild(inputMatrix, nRows, nFeatures, out retVal, out lm, out ar);
111      if (retVal != 1) throw new ArgumentException("Error in calculation of linear regression solution");
112      rmsError = ar.rmserror;
113      cvRmsError = ar.cvrmserror;
114
115      alglib.lrunpack(lm, out coefficients, out nFeatures);
116
117      var tree = LinearModelToTreeConverter.CreateTree(
118        variableNames: Enumerable.Repeat(problemData.TargetVariable, nFeatures).ToArray(),
119        lags: Enumerable.Range(0, timeOffset).Select(i => (i + 1) * -1).ToArray(),
120        coefficients: coefficients.Take(nFeatures).ToArray(),
121        @const: coefficients[nFeatures]
122        );
123
124      var interpreter = new SymbolicTimeSeriesPrognosisExpressionTreeInterpreter(problemData.TargetVariable);
125      var model = new SymbolicTimeSeriesPrognosisModel(problemData.TargetVariable, tree, interpreter);
126      var solution = model.CreateTimeSeriesPrognosisSolution((ITimeSeriesPrognosisProblemData)problemData.Clone());
127      return solution;
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.