Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Modeling/3.2/VariableQualityImpactCalculator.cs @ 2582

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

fixed memory leak in VariableImpactCalculators by cloning the IPredictor (ticket #815)

File size: 6.1 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.Data;
28using HeuristicLab.DataAnalysis;
29using System.Linq;
30
31namespace HeuristicLab.Modeling {
32  public class VariableQualityImpactCalculator : OperatorBase {
33
34    public VariableQualityImpactCalculator()
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(StringData), 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(ModelingResult.VariableQualityImpact.ToString(), "VariableQualityImpacts", typeof(ItemList), VariableKind.New));
43    }
44
45    public override string Description {
46      get { return @"Calculates the impact of all allowed input variables on the quality of the model 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      string targetVariableName = GetVariableValue<StringData>("TargetVariable", scope, true).Data;
53      int targetVariable = dataset.GetVariableIndex(targetVariableName);
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> qualityImpacts;
59      if (inputVariableNames == null)
60        qualityImpacts = Calculate(dataset, predictor, targetVariableName, start, end);
61      else
62        qualityImpacts = Calculate(dataset, predictor, targetVariableName, inputVariableNames.Select(iv => iv.Data), start, end);
63
64      ItemList variableImpacts = new ItemList();
65      foreach (KeyValuePair<string, double> p in qualityImpacts) {
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(ModelingResult.VariableQualityImpact.ToString()), variableImpacts));
75      return null;
76    }
77
78    public static Dictionary<string, double> Calculate(Dataset dataset, IPredictor predictor, string targetVariableName, int start, int end) {
79      return Calculate(dataset, predictor, targetVariableName, null, start, end);
80    }
81
82    public static Dictionary<string, double> Calculate(Dataset dataset, IPredictor predictor, string targetVariableName, IEnumerable<string> inputVariableNames, int start, int end) {
83      Dictionary<string, double> evaluationImpacts = new Dictionary<string, double>();
84      Dataset dirtyDataset = (Dataset)dataset.Clone();
85      IPredictor dirtyPredictor = (IPredictor)predictor.Clone();
86
87      double[] predictedValues = predictor.Predict(dataset, start, end);
88      double[] targetValues = dataset.GetVariableValues(targetVariableName, start, end);
89
90      double oldMSE = CalculateMSE(targetValues, predictedValues);
91      double newMSE;
92
93      double mean;
94      IEnumerable<double> oldValues;
95      IEnumerable<string> variables;
96      if (inputVariableNames != null)
97        variables = inputVariableNames;
98      else
99        variables = dataset.VariableNames;
100
101      foreach (string variableName in variables) {
102        if (dataset.CountMissingValues(variableName, start, end) < (end - start) &&
103          dataset.GetRange(variableName, start, end) > 0.0 &&
104          variableName != targetVariableName) {
105          mean = dataset.GetMean(variableName, start, end);
106          oldValues = dirtyDataset.ReplaceVariableValues(variableName, Enumerable.Repeat(mean, end - start), start, end);
107          predictedValues = dirtyPredictor.Predict(dirtyDataset, start, end);
108          newMSE = CalculateMSE(predictedValues, targetValues);
109          evaluationImpacts[variableName] = newMSE / oldMSE;
110          dirtyDataset.ReplaceVariableValues(variableName, oldValues, start, end);
111        } else {
112          evaluationImpacts[variableName] = 1.0;
113        }
114      }
115
116      return evaluationImpacts;
117    }
118
119    private static double CalculateImpact(double referenceValue, double newValue) {
120      return newValue / referenceValue;
121    }
122
123    private static double CalculateMSE(double[] referenceValues, double[] newValues) {
124      try {
125        return SimpleMSEEvaluator.Calculate(Matrix<double>.Create(referenceValues, newValues));
126      }
127      catch (ArgumentException) {
128        return double.PositiveInfinity;
129      }
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.