Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Evaluators/SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.cs @ 4475

Last change on this file since 4475 was 4475, checked in by gkronber, 13 years ago

Fixed bugs in time series prognosis classes #1142.

File size: 9.1 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;
40
41namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Evaluators {
42  [Item("SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator", "")]
43  [StorableClass]
44  public class SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator : SymbolicTimeSeriesPrognosisEvaluator {
45
46    public SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator()
47      : base() {
48    }
49
50    public override double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, ISymbolicTimeSeriesExpressionInterpreter interpreter, IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) {
51      return Calculate(tree, problemData, interpreter, rows, predictionHorizon, lowerEstimationLimit, upperEstimationLimit);
52    }
53
54    public static double Calculate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, ISymbolicTimeSeriesExpressionInterpreter interpreter, IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) {
55      double[] alpha, beta;
56      double quality;
57
58      // calculate scaling parameters based on one-step-predictions
59      IEnumerable<string> selectedTargetVariables = from item in problemData.TargetVariables
60                                                    where problemData.TargetVariables.ItemChecked(item)
61                                                    select item.Value;
62      int dimension = selectedTargetVariables.Count();
63
64      IEnumerable<double[]> oneStepPredictions =
65        interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, selectedTargetVariables, rows, 1);
66      IEnumerable<double[]> originalValues = from row in rows
67                                             select (from targetVariable in selectedTargetVariables
68                                                     select problemData.Dataset[targetVariable, row]).ToArray();
69      alpha = new double[dimension];
70      beta = new double[dimension];
71
72      CalculateScalingParameters(originalValues, oneStepPredictions, ref beta, ref alpha);
73
74      // calculate the quality for the full horizon
75      quality = CalculateWithScaling(tree, problemData, interpreter,
76        rows, predictionHorizon,
77        lowerEstimationLimit, upperEstimationLimit,
78        beta, alpha);
79      return quality;
80    }
81
82    public static double CalculateWithScaling(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData,
83      ISymbolicTimeSeriesExpressionInterpreter interpreter,
84      IEnumerable<int> rows, int predictionHorizon,
85      DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit,
86      double[] beta, double[] alpha) {
87      IEnumerable<string> selectedTargetVariables = from targetVariable in problemData.TargetVariables
88                                                    where problemData.TargetVariables.ItemChecked(targetVariable)
89                                                    select targetVariable.Value;
90
91      IEnumerable<double[]> estimatedValues =
92        interpreter.GetScaledSymbolicExpressionTreeValues(tree, problemData.Dataset, selectedTargetVariables,
93        rows, predictionHorizon, beta, alpha);
94
95      IEnumerable<double[]> originalValues = from row in rows
96                                             from step in Enumerable.Range(0, predictionHorizon)
97                                             select (from targetVariable in selectedTargetVariables
98                                                     select problemData.Dataset[targetVariable, row + step]).ToArray();
99
100      var evaluator = new OnlineMultiVariateEvaluator<OnlineNormalizedMeanSquaredErrorEvaluator>();
101
102      var estimatedValuesEnumerator = estimatedValues.GetEnumerator();
103      var originalValuesEnumerator = originalValues.GetEnumerator();
104      while (originalValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) {
105        double[] original = originalValuesEnumerator.Current;
106        double[] estimated = estimatedValuesEnumerator.Current;
107        for (int i = 0; i < estimated.Length; i++) {
108          if (double.IsNaN(estimated[i])) estimated[i] = upperEstimationLimit[i];
109          else estimated[i] = Math.Min(upperEstimationLimit[i], Math.Max(lowerEstimationLimit[i], estimated[i]));
110        }
111        evaluator.Add(original, estimated);
112      }
113
114      double quality = evaluator.Value;
115      return quality;
116    }
117
118    public static void CalculateScalingParameters(IEnumerable<double[]> originalValues, IEnumerable<double[]> estimatedValues, ref double[] beta, ref double[] alpha) {
119      List<OnlineMeanAndVarianceCalculator> estimatedVarianceEvaluators = new List<OnlineMeanAndVarianceCalculator>();
120      List<OnlineCovarianceEvaluator> covarianceEvaluators = new List<OnlineCovarianceEvaluator>();
121      List<OnlineMeanAndVarianceCalculator> originalMeanCalculators = new List<OnlineMeanAndVarianceCalculator>();
122      int[] cnt = null;
123
124      var estimatedEnumerator = estimatedValues.GetEnumerator();
125      var originalEnumerator = originalValues.GetEnumerator();
126      while (estimatedEnumerator.MoveNext() & originalEnumerator.MoveNext()) {
127        double[] original = originalEnumerator.Current;
128        double[] estimated = estimatedEnumerator.Current;
129        int dimension = original.Length;
130        // initialize
131        if (cnt == null) {
132          cnt = new int[dimension];
133          for (int i = 0; i < dimension; i++) {
134            estimatedVarianceEvaluators.Add(new OnlineMeanAndVarianceCalculator());
135            covarianceEvaluators.Add(new OnlineCovarianceEvaluator());
136            originalMeanCalculators.Add(new OnlineMeanAndVarianceCalculator());
137          }
138        } else if (cnt.Length == dimension) {
139          for (int component = 0; component < dimension; component++) {
140            if (IsValidValue(original[component]) && IsValidValue(estimated[component])) {
141              cnt[component]++;
142              estimatedVarianceEvaluators[component].Add(estimated[component]);
143              covarianceEvaluators[component].Add(original[component], estimated[component]);
144              originalMeanCalculators[component].Add(original[component]);
145            }
146          }
147        } else {
148          throw new ArgumentException("Dimension of input array doesn't match");
149        }
150      }
151
152      if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())
153        throw new InvalidOperationException("Number of elements in estimated and original series doesn't match.");
154      if (cnt == null) throw new ArgumentException("No elements in estimated and original.");
155
156      for (int component = 0; component < cnt.Length; component++) {
157        if (cnt[component] < 2) {
158          alpha[component] = 0;
159          beta[component] = 1;
160        } else {
161          if (estimatedVarianceEvaluators[component].PopulationVariance.IsAlmost(0.0))
162            beta[component] = 1;
163          else
164            beta[component] = covarianceEvaluators[component].Covariance / estimatedVarianceEvaluators[component].PopulationVariance;
165
166          alpha[component] = originalMeanCalculators[component].Mean - beta[component] * estimatedVarianceEvaluators[component].Mean;
167        }
168      }
169    }
170
171    private static bool IsValidValue(double d) {
172      return !double.IsInfinity(d) && !double.IsNaN(d) && d > -1.0E07 && d < 1.0E07;  // don't consider very large or very small values for scaling
173    }
174  }
175}
Note: See TracBrowser for help on using the repository browser.