1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis.SupportVectorMachine;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis.Evaluators;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 | using HeuristicLab.Operators;
|
---|
34 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
|
---|
35 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
36 | using System.Collections.Generic;
|
---|
37 | using HeuristicLab.Problems.DataAnalysis.Regression;
|
---|
38 | using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Interfaces;
|
---|
39 | using HeuristicLab.Problems.DataAnalysis.MultiVariate.Evaluators;
|
---|
40 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
41 |
|
---|
42 | namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Evaluators {
|
---|
43 | [Item("SymbolicTimeSeriesPrognosisMahalanobisEvaluator", "")]
|
---|
44 | [StorableClass]
|
---|
45 | public class SymbolicTimeSeriesPrognosisMahalanobisEvaluator : SymbolicTimeSeriesPrognosisEvaluator {
|
---|
46 |
|
---|
47 | [StorableConstructor]
|
---|
48 | protected SymbolicTimeSeriesPrognosisMahalanobisEvaluator(bool deserializing) : base(deserializing) { }
|
---|
49 | protected SymbolicTimeSeriesPrognosisMahalanobisEvaluator(SymbolicTimeSeriesPrognosisMahalanobisEvaluator original, Cloner cloner)
|
---|
50 | : base(original, cloner) {
|
---|
51 | }
|
---|
52 | public SymbolicTimeSeriesPrognosisMahalanobisEvaluator()
|
---|
53 | : base() {
|
---|
54 | }
|
---|
55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
56 | return new SymbolicTimeSeriesPrognosisMahalanobisEvaluator(this, cloner);
|
---|
57 | }
|
---|
58 | public override double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, ISymbolicTimeSeriesExpressionInterpreter interpreter, IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) {
|
---|
59 | return Calculate(tree, problemData, interpreter, rows, predictionHorizon, lowerEstimationLimit, upperEstimationLimit);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public static double Calculate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData,
|
---|
63 | ISymbolicTimeSeriesExpressionInterpreter interpreter,
|
---|
64 | IEnumerable<int> rows, int predictionHorizon,
|
---|
65 | DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) {
|
---|
66 | double[] alpha, beta;
|
---|
67 | double quality;
|
---|
68 |
|
---|
69 | Dataset dataset = problemData.Dataset;
|
---|
70 | // calculate scaling parameters based on one-step-predictions
|
---|
71 | IEnumerable<string> selectedTargetVariables = (from item in problemData.TargetVariables
|
---|
72 | where problemData.TargetVariables.ItemChecked(item)
|
---|
73 | select item.Value).ToArray();
|
---|
74 | int dimension = selectedTargetVariables.Count();
|
---|
75 |
|
---|
76 | IEnumerable<int> selectedTargetVariableIndexes = (from targetVariable in selectedTargetVariables
|
---|
77 | select dataset.GetVariableIndex(targetVariable)).ToArray();
|
---|
78 | IEnumerable<IEnumerable<double>> oneStepPredictions =
|
---|
79 | interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, selectedTargetVariables, rows, 1)
|
---|
80 | .Cast<IEnumerable<double>>();
|
---|
81 | IEnumerable<IEnumerable<double>> originalValues = from row in rows
|
---|
82 | select (from targetVariableIndex in selectedTargetVariableIndexes
|
---|
83 | select dataset[row, targetVariableIndex]);
|
---|
84 | alpha = new double[dimension];
|
---|
85 | beta = new double[dimension];
|
---|
86 |
|
---|
87 | SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.CalculateScalingParameters(originalValues, oneStepPredictions, ref beta, ref alpha);
|
---|
88 |
|
---|
89 | // calculate the quality for the full horizon
|
---|
90 | quality = CalculateWithScaling(tree, problemData, interpreter,
|
---|
91 | rows, predictionHorizon,
|
---|
92 | lowerEstimationLimit, upperEstimationLimit,
|
---|
93 | beta, alpha);
|
---|
94 | return quality;
|
---|
95 |
|
---|
96 | }
|
---|
97 |
|
---|
98 | public static double CalculateWithScaling(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData,
|
---|
99 | ISymbolicTimeSeriesExpressionInterpreter interpreter,
|
---|
100 | IEnumerable<int> rows, int predictionHorizon,
|
---|
101 | DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit,
|
---|
102 | double[] beta, double[] alpha) {
|
---|
103 |
|
---|
104 | Dataset dataset = problemData.Dataset;
|
---|
105 |
|
---|
106 | IEnumerable<string> selectedTargetVariables = (from targetVariable in problemData.TargetVariables
|
---|
107 | where problemData.TargetVariables.ItemChecked(targetVariable)
|
---|
108 | select targetVariable.Value).ToArray();
|
---|
109 |
|
---|
110 | IEnumerable<int> selectedTargetVariableIndexes = (from targetVariable in selectedTargetVariables
|
---|
111 | select dataset.GetVariableIndex(targetVariable)).ToArray();
|
---|
112 | IEnumerable<double[]> estimatedValues =
|
---|
113 | interpreter.GetScaledSymbolicExpressionTreeValues(tree, dataset, selectedTargetVariables,
|
---|
114 | rows, predictionHorizon, beta, alpha);
|
---|
115 |
|
---|
116 | IEnumerable<IEnumerable<double>> originalValues = from row in rows
|
---|
117 | from step in Enumerable.Range(0, predictionHorizon)
|
---|
118 | select (from targetVariableIndex in selectedTargetVariableIndexes
|
---|
119 | select dataset[row + step, targetVariableIndex]);
|
---|
120 |
|
---|
121 | OnlineMeanMahalanobisDistanceEvaluator evaluator = new OnlineMeanMahalanobisDistanceEvaluator();
|
---|
122 | // for covariance calculation: array of variables
|
---|
123 | IEnumerable<double>[] targetValues = (from targetVariableIndex in selectedTargetVariableIndexes
|
---|
124 | select dataset.GetEnumeratedVariableValues(targetVariableIndex, rows))
|
---|
125 | .ToArray();
|
---|
126 | evaluator.InitializeCovarianceMatrixFromSamples(targetValues);
|
---|
127 |
|
---|
128 | var estimatedValuesEnumerator = estimatedValues.GetEnumerator();
|
---|
129 | var originalValuesEnumerator = originalValues.GetEnumerator();
|
---|
130 | while (originalValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) {
|
---|
131 | IEnumerable<double> currentOriginal = originalValuesEnumerator.Current;
|
---|
132 | double[] currentEstimated = estimatedValuesEnumerator.Current;
|
---|
133 | // limit estimated values to bounds
|
---|
134 | for (int i = 0; i < currentEstimated.Length; i++) {
|
---|
135 | if (double.IsNaN(currentEstimated[i])) currentEstimated[i] = upperEstimationLimit[i];
|
---|
136 | else currentEstimated[i] = Math.Min(upperEstimationLimit[i], Math.Max(lowerEstimationLimit[i], currentEstimated[i]));
|
---|
137 | }
|
---|
138 |
|
---|
139 | evaluator.Add(currentOriginal, currentEstimated);
|
---|
140 | }
|
---|
141 |
|
---|
142 | return evaluator.MeanGeneralizedSquaredInterpointDistance;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|