Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression/3.3/Symbolic/Evaluators/PartialDerivativeEvaluator.cs @ 4197

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

Added new files for data analysis feature exploration. #1142

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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 HeuristicLab.Core;
23using HeuristicLab.Data;
24using System;
25using HeuristicLab.Common;
26using System.Linq;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis.Symbolic;
29using System.Collections.Generic;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
32using HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression.Symbolic.Interfaces;
33using HeuristicLab.Parameters;
34
35
36namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression.Symbolic.Evaluators {
37  [Item("PartialDerivativeEvaluator", "Evaluator for implict equation modelling")]
38  [StorableClass]
39  public class PartialDerivativeEvaluator : SingleObjectiveSymbolicVectorRegressionEvaluator {
40
41    public PartialDerivativeEvaluator(bool deserializing) : base(deserializing) { }
42    public PartialDerivativeEvaluator()
43      : base() {
44    }
45
46    public override double Evaluate(SymbolicExpressionTree tree, ISymbolicExpressionTreeInterpreter interpreter, MultiVariateDataAnalysisProblemData problemData, IEnumerable<string> targetVariables, IEnumerable<int> rows, DoubleArray lowerEstimationBound, DoubleArray upperEstimationBound) {
47
48      Dataset dataset = problemData.Dataset;
49      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows);
50
51      var sortedNames = targetVariables.OrderBy(x => x);
52      var pairs = from v1 in sortedNames
53                  from v2 in sortedNames.Skip(1)
54                  where v1.CompareTo(v2) < 0
55                  select new { x = v1, y = v2 };
56
57      double meanErrorSum = 0;
58      foreach (var pair in pairs) {
59        double errorSum = 0;
60        string variableX = pair.x;
61        string variableY = pair.y;
62
63        var dFdX = new SymbolicExpressionTree(PartialSymbolicDifferential.Apply((SymbolicExpressionTreeNode)tree.Root.Clone(), variableX, variableY));
64        IEnumerable<double> estimatedDfDx = interpreter.GetSymbolicExpressionTreeValues(dFdX, dataset, rows).ToList();
65
66        var dFdY = new SymbolicExpressionTree(PartialSymbolicDifferential.Apply((SymbolicExpressionTreeNode)tree.Root.Clone(), variableY, variableX));
67        IEnumerable<double> estimatedDfDy = interpreter.GetSymbolicExpressionTreeValues(dFdY, dataset, rows).ToList();
68
69        List<int> rowsList = rows.ToList();
70        int n = rowsList.Count;
71        int x = dataset.GetVariableIndex(variableX);
72        int y = dataset.GetVariableIndex(variableY);
73
74        var estimatedDfDxEnumerator = estimatedDfDx.GetEnumerator();
75        var estimatedDfDyEnumerator = estimatedDfDy.GetEnumerator();
76        var rowsEnumerator = rows.GetEnumerator();
77
78        // skip 1
79        estimatedDfDxEnumerator.MoveNext();
80        estimatedDfDyEnumerator.MoveNext();
81        rowsEnumerator.MoveNext();
82
83        for (int i = 1; i < n - 1; i++) {
84          // evaluate next
85          estimatedDfDxEnumerator.MoveNext();
86          estimatedDfDyEnumerator.MoveNext();
87          rowsEnumerator.MoveNext();
88
89          double dFdXValue = estimatedDfDxEnumerator.Current;
90          double dFdYValue = estimatedDfDyEnumerator.Current;
91          double dXdY = GetLocalDifferential(dataset, rowsEnumerator.Current, x, y);
92
93          if ((dFdXValue.IsAlmost(0.0) && dFdYValue.IsAlmost(0.0))) {
94            errorSum += Math.Log(1 + Math.Abs(dXdY));
95          } else if (dFdXValue.IsAlmost(0.0) ||
96            double.IsInfinity(dFdXValue) || double.IsNaN(dFdXValue) ||
97            double.IsInfinity(dFdYValue) || double.IsNaN(dFdYValue)) {
98            errorSum += 1000000;
99          } else {
100            double error = dXdY - dFdYValue / dFdXValue;
101            errorSum += Math.Log(1 + Math.Abs(error));
102            // errorSum += error * error;
103          }
104        }
105
106        meanErrorSum += errorSum / n;
107      }
108      meanErrorSum /= pairs.Count();
109
110      return meanErrorSum;
111    }
112
113    private double GetLocalDifferential(Dataset dataset, int i, int varX, int varY) {
114      return
115        (dataset[i + 1, varX] - dataset[i - 1, varX]) /
116        (dataset[i + 1, varY] - dataset[i - 1, varY]);
117    }
118
119  }
120}
Note: See TracBrowser for help on using the repository browser.