Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged changes from trunk to data analysis exploration branch and added fractional distance metric evaluator. #1142

File size: 5.3 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    [StorableConstructor]
42    protected PartialDerivativeEvaluator(bool deserializing) : base(deserializing) { }
43    protected PartialDerivativeEvaluator(PartialDerivativeEvaluator original, Cloner cloner)
44      : base(original, cloner) {
45    }
46    public PartialDerivativeEvaluator()
47      : base() {
48    }
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new PartialDerivativeEvaluator(this, cloner);
51    }
52    public override double Evaluate(SymbolicExpressionTree tree, ISymbolicExpressionTreeInterpreter interpreter, MultiVariateDataAnalysisProblemData problemData, IEnumerable<string> targetVariables, IEnumerable<int> rows, DoubleArray lowerEstimationBound, DoubleArray upperEstimationBound) {
53
54      Dataset dataset = problemData.Dataset;
55      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows);
56
57      var sortedNames = targetVariables.OrderBy(x => x);
58      var pairs = from v1 in sortedNames
59                  from v2 in sortedNames.Skip(1)
60                  where v1.CompareTo(v2) < 0
61                  select new { x = v1, y = v2 };
62
63      double meanErrorSum = 0;
64      foreach (var pair in pairs) {
65        double errorSum = 0;
66        string variableX = pair.x;
67        string variableY = pair.y;
68
69        var dFdX = new SymbolicExpressionTree(PartialSymbolicDifferential.Apply((SymbolicExpressionTreeNode)tree.Root.Clone(), variableX, variableY));
70        IEnumerable<double> estimatedDfDx = interpreter.GetSymbolicExpressionTreeValues(dFdX, dataset, rows).ToList();
71
72        var dFdY = new SymbolicExpressionTree(PartialSymbolicDifferential.Apply((SymbolicExpressionTreeNode)tree.Root.Clone(), variableY, variableX));
73        IEnumerable<double> estimatedDfDy = interpreter.GetSymbolicExpressionTreeValues(dFdY, dataset, rows).ToList();
74
75        List<int> rowsList = rows.ToList();
76        int n = rowsList.Count;
77        int x = dataset.GetVariableIndex(variableX);
78        int y = dataset.GetVariableIndex(variableY);
79
80        var estimatedDfDxEnumerator = estimatedDfDx.GetEnumerator();
81        var estimatedDfDyEnumerator = estimatedDfDy.GetEnumerator();
82        var rowsEnumerator = rows.GetEnumerator();
83
84        // skip 1
85        estimatedDfDxEnumerator.MoveNext();
86        estimatedDfDyEnumerator.MoveNext();
87        rowsEnumerator.MoveNext();
88
89        for (int i = 1; i < n - 1; i++) {
90          // evaluate next
91          estimatedDfDxEnumerator.MoveNext();
92          estimatedDfDyEnumerator.MoveNext();
93          rowsEnumerator.MoveNext();
94
95          double dFdXValue = estimatedDfDxEnumerator.Current;
96          double dFdYValue = estimatedDfDyEnumerator.Current;
97          double dXdY = GetLocalDifferential(dataset, rowsEnumerator.Current, x, y);
98
99          if ((dFdXValue.IsAlmost(0.0) && dFdYValue.IsAlmost(0.0))) {
100            errorSum += Math.Log(1 + Math.Abs(dXdY));
101          } else if (dFdXValue.IsAlmost(0.0) ||
102            double.IsInfinity(dFdXValue) || double.IsNaN(dFdXValue) ||
103            double.IsInfinity(dFdYValue) || double.IsNaN(dFdYValue)) {
104            errorSum += 1000000;
105          } else {
106            double error = dXdY - dFdYValue / dFdXValue;
107            errorSum += Math.Log(1 + Math.Abs(error));
108            // errorSum += error * error;
109          }
110        }
111
112        meanErrorSum += errorSum / n;
113      }
114      meanErrorSum /= pairs.Count();
115
116      return meanErrorSum;
117    }
118
119    private double GetLocalDifferential(Dataset dataset, int i, int varX, int varY) {
120      return
121        (dataset[i + 1, varX] - dataset[i - 1, varX]) /
122        (dataset[i + 1, varY] - dataset[i - 1, varY]);
123    }
124
125  }
126}
Note: See TracBrowser for help on using the repository browser.