Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2960 was 2619, checked in by gkronber, 15 years ago

Implemented #834 (IPredictor.Predict() should return an IEnumerable<double> instead of an double[]).

File size: 5.9 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(StringData), 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(ModelingResult.VariableEvaluationImpact.ToString(), "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      string targetVariableName = GetVariableValue<StringData>("TargetVariable", scope, true).Data;
54      int targetVariable = dataset.GetVariableIndex(targetVariableName);
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(ModelingResult.VariableEvaluationImpact.ToString()), 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      IPredictor dirtyPredictor = (IPredictor)predictor.Clone();
87      double[] referenceValues = predictor.Predict(dataset, start, end).ToArray();
88
89      double mean;
90      IEnumerable<double> oldValues;
91      double[] newValues;
92      IEnumerable<string> variables;
93      if (inputVariableNames != null)
94        variables = inputVariableNames;
95      else
96        variables = dataset.VariableNames;
97
98      foreach (string variableName in variables) {
99        if (variableName != targetVariableName) {
100          if (dataset.CountMissingValues(variableName, start, end) < (end - start) && dataset.GetRange(variableName, start, end) > 0.0) {
101            mean = dataset.GetMean(variableName, start, end);
102            oldValues = dirtyDataset.ReplaceVariableValues(variableName, Enumerable.Repeat(mean, end - start), start, end);
103            newValues = dirtyPredictor.Predict(dirtyDataset, start, end).ToArray();
104            evaluationImpacts[variableName] = 1 - CalculateVAF(referenceValues, newValues);
105            dirtyDataset.ReplaceVariableValues(variableName, oldValues, start, end);
106          } else {
107            evaluationImpacts[variableName] = 0.0;
108          }
109        }
110      }
111
112      return evaluationImpacts;
113    }
114
115    private static double CalculateVAF(double[] referenceValues, double[] newValues) {
116      try {
117        return SimpleVarianceAccountedForEvaluator.Calculate(Matrix<double>.Create(referenceValues, newValues));
118      }
119      catch (ArgumentException) {
120        return double.PositiveInfinity;
121      }
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.