1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Threading;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence;
|
---|
32 | using HeuristicLab.Problems.DataAnalysis;
|
---|
33 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
34 | using HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis;
|
---|
35 |
|
---|
36 | namespace 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 | [StorableType("ceaaeee5-9be2-40f4-a924-2d83ec8bf9d0")]
|
---|
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(StorableConstructorFlag 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 | }
|
---|