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