Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Modeling/3.2/VariableEvaluationImpactCalculator.cs @ 2324

Last change on this file since 2324 was 2324, checked in by mkommend, 15 years ago

moved DoubleExtension to HeuristicLab.Common (ticket #733)

File size: 5.7 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 System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Common;
28using HeuristicLab.Data;
29using HeuristicLab.DataAnalysis;
30using System.Linq;
31
32namespace HeuristicLab.Modeling {
33  public class VariableEvaluationImpactCalculator : OperatorBase {
34
35    public VariableEvaluationImpactCalculator()
36      : base() {
37      AddVariableInfo(new VariableInfo("Predictor", "The predictor used to evaluate the model", typeof(IPredictor), VariableKind.In));
38      AddVariableInfo(new VariableInfo("Dataset", "Dataset", typeof(Dataset), VariableKind.In));
39      AddVariableInfo(new VariableInfo("TargetVariable", "TargetVariable", typeof(IntData), VariableKind.In));
40      AddVariableInfo(new VariableInfo("InputVariableNames", "Names of used variables in the model (optional)", typeof(ItemList<StringData>), VariableKind.In));
41      AddVariableInfo(new VariableInfo("SamplesStart", "TrainingSamplesStart", typeof(IntData), VariableKind.In));
42      AddVariableInfo(new VariableInfo("SamplesEnd", "TrainingSamplesEnd", typeof(IntData), VariableKind.In));
43      AddVariableInfo(new VariableInfo("VariableEvaluationImpacts", "VariableEvaluationImpacts", typeof(ItemList), VariableKind.New));
44    }
45
46    public override string Description {
47      get { return @"Calculates the impact of all allowed input variables on the model outputs using evaluator supplied as suboperator."; }
48    }
49
50    public override IOperation Apply(IScope scope) {
51      IPredictor predictor = GetVariableValue<IPredictor>("Predictor", scope, true);
52      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
53      int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
54      string targetVariableName = dataset.GetVariableName(targetVariable);
55      ItemList<StringData> inputVariableNames = GetVariableValue<ItemList<StringData>>("InputVariableNames", scope, true, false);
56      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
57      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
58
59      Dictionary<string, double> evaluationImpacts;
60      if (inputVariableNames == null)
61        evaluationImpacts = Calculate(dataset, predictor, targetVariableName, start, end);
62      else
63        evaluationImpacts = Calculate(dataset, predictor, targetVariableName, inputVariableNames.Select(iv => iv.Data), start, end);
64
65      ItemList variableImpacts = new ItemList();
66      foreach (KeyValuePair<string, double> p in evaluationImpacts) {
67        if (p.Key != targetVariableName) {
68          ItemList row = new ItemList();
69          row.Add(new StringData(p.Key));
70          row.Add(new DoubleData(p.Value));
71          variableImpacts.Add(row);
72        }
73      }
74
75      scope.AddVariable(new Variable(scope.TranslateName("VariableEvaluationImpacts"), variableImpacts));
76      return null;
77
78    }
79    public static Dictionary<string, double> Calculate(Dataset dataset, IPredictor predictor, string targetVariableName, int start, int end) {
80      return Calculate(dataset, predictor, targetVariableName, null, start, end);
81    }
82
83    public static Dictionary<string, double> Calculate(Dataset dataset, IPredictor predictor, string targetVariableName, IEnumerable<string> inputVariableNames, int start, int end) {
84      Dictionary<string, double> evaluationImpacts = new Dictionary<string, double>();
85      Dataset dirtyDataset = (Dataset)dataset.Clone();
86      double[] referenceValues = predictor.Predict(dataset, start, end);
87
88      double mean;
89      IEnumerable<double> oldValues;
90      double[] newValues;
91      IEnumerable<string> variables;
92      if (inputVariableNames != null)
93        variables = inputVariableNames;
94      else
95        variables = dataset.VariableNames;
96
97      foreach (string variableName in variables) {
98        if (variableName != targetVariableName) {
99          mean = dataset.GetMean(variableName, start, end);
100          oldValues = dirtyDataset.ReplaceVariableValues(variableName, Enumerable.Repeat(mean, end - start), start, end);
101          newValues = predictor.Predict(dirtyDataset, start, end);
102          evaluationImpacts[variableName] = CalculateMSE(referenceValues, newValues);
103          dirtyDataset.ReplaceVariableValues(variableName, oldValues, start, end);
104        }
105      }
106
107      double impactsSum = evaluationImpacts.Values.Sum();
108      if (impactsSum.IsAlmost(0.0)) impactsSum = 1.0;
109      foreach (KeyValuePair<string, double> p in evaluationImpacts.ToList())
110        evaluationImpacts[p.Key] = p.Value / impactsSum;
111
112      return evaluationImpacts;
113    }
114
115    private static double CalculateMSE(double[] referenceValues, double[] newValues) {
116      try {
117        return SimpleMSEEvaluator.Calculate(MatrixCreator<double>.CreateMatrix(referenceValues, newValues));
118      }
119      catch (ArgumentException) {
120        return double.PositiveInfinity;
121      }
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.