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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.DataAnalysis;
|
---|
30 | using System.Linq;
|
---|
31 |
|
---|
32 | namespace 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(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 | 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(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 | 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 | if (dataset.CountMissingValues(variableName, start, end) < (end - start) && dataset.GetRange(variableName, start, end) > 0.0) {
|
---|
100 | mean = dataset.GetMean(variableName, start, end);
|
---|
101 | oldValues = dirtyDataset.ReplaceVariableValues(variableName, Enumerable.Repeat(mean, end - start), start, end);
|
---|
102 | newValues = predictor.Predict(dirtyDataset, start, end);
|
---|
103 | evaluationImpacts[variableName] = 1 - CalculateVAF(referenceValues, newValues);
|
---|
104 | dirtyDataset.ReplaceVariableValues(variableName, oldValues, start, end);
|
---|
105 | } else {
|
---|
106 | evaluationImpacts[variableName] = 0.0;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | return evaluationImpacts;
|
---|
112 | }
|
---|
113 |
|
---|
114 | private static double CalculateVAF(double[] referenceValues, double[] newValues) {
|
---|
115 | try {
|
---|
116 | return SimpleVarianceAccountedForEvaluator.Calculate(Matrix<double>.Create(referenceValues, newValues));
|
---|
117 | }
|
---|
118 | catch (ArgumentException) {
|
---|
119 | return double.PositiveInfinity;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|