Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Evaluators/SymbolicTimeSeriesPrognosisMahalanobisEvaluator.cs @ 5242

Last change on this file since 5242 was 4555, checked in by gkronber, 14 years ago

Improved time series evaluators. #1142

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