Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/24/18 10:43:25 (6 years ago)
Author:
fholzing
Message:

#2904: Reverted the additional CalculateValue-Method in IOnlineCalculator and created a static property/method in RegressionSolutionVariableImpactsCalculator

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2904_CalculateImpacts/3.4/Implementation/Regression/RegressionSolutionVariableImpactsCalculator.cs

    r15831 r16001  
    5454    }
    5555
     56    //The PerasonsR²-Calculator is the default, but can be overwritten to any other IOnlineCalculator
     57    //Just remember to reset it after you're done
     58    private static IOnlineCalculator calculator = new OnlinePearsonsRSquaredCalculator();
     59    public IOnlineCalculator Calculator
     60    {
     61      get { return calculator; }
     62      set { calculator = value; }
     63    }
     64
    5665    private const string ReplacementParameterName = "Replacement Method";
    5766    private const string DataPartitionParameterName = "DataPartition";
     
    113122      ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median,
    114123      FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best,
    115       Func<double, string, bool> progressCallback = null,
    116       IOnlineCalculator calculator = null) {
    117       //PearsonsRSquared is the default calculator
    118       if (calculator == null) { calculator = new OnlinePearsonsRSquaredCalculator(); }
     124      Func<double, string, bool> progressCallback = null) {
    119125      IEnumerable<int> rows;
    120126
     
    133139      }
    134140
    135       return CalculateImpacts(model, problemData, estimatedValues, rows, calculator, replacementMethod, factorReplacementMethod, progressCallback);
     141      return CalculateImpacts(model, problemData, estimatedValues, rows, replacementMethod, factorReplacementMethod, progressCallback);
    136142    }
    137143
     
    141147     IEnumerable<double> estimatedValues,
    142148     IEnumerable<int> rows,
    143      IOnlineCalculator calculator,
    144149     ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median,
    145150     FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best,
     
    149154      double originalValue = -1;
    150155
    151       PrepareData(rows, problemData, estimatedValues, out targetValues, out originalValue, calculator);
     156      PrepareData(rows, problemData, estimatedValues, out targetValues, out originalValue);
    152157
    153158      var impacts = new Dictionary<string, double>();
     
    166171          if (progressCallback((double)curIdx / count, string.Format("Calculating impact for variable {0} ({1} of {2})", inputVariable, curIdx, count))) { return null; }
    167172        }
    168         impacts[inputVariable] = CalculateImpact(inputVariable, model, problemData.Dataset, rows, targetValues, originalValue, calculator, replacementMethod, factorReplacementMethod);
     173        impacts[inputVariable] = CalculateImpact(inputVariable, model, problemData.Dataset, rows, targetValues, originalValue, replacementMethod, factorReplacementMethod);
    169174      }
    170175
     
    177182      IEnumerable<double> targetValues,
    178183      double originalValue,
    179       IOnlineCalculator calculator,
    180184      DataPartitionEnum data = DataPartitionEnum.Training,
    181185      ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median,
    182186      FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best) {
    183       return CalculateImpact(variableName, solution.Model, solution.ProblemData.Dataset, rows, targetValues, originalValue, calculator, replacementMethod, factorReplacementMethod);
     187      return CalculateImpact(variableName, solution.Model, solution.ProblemData.Dataset, rows, targetValues, originalValue, replacementMethod, factorReplacementMethod);
    184188    }
    185189
     
    190194      IEnumerable<double> targetValues,
    191195      double originalValue,
    192       IOnlineCalculator calculator,
    193196      ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median,
    194197      FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best) {
     
    199202      // calculate impacts for double variables
    200203      if (dataset.VariableHasType<double>(variableName)) {
    201         impact = CalculateImpactForDouble(variableName, model, modifiableDataset, rows, targetValues, originalValue, replacementMethod, calculator);
     204        impact = CalculateImpactForDouble(variableName, model, modifiableDataset, rows, targetValues, originalValue, replacementMethod);
    202205      } else if (dataset.VariableHasType<string>(variableName)) {
    203         impact = CalculateImpactForString(variableName, model, dataset, modifiableDataset, rows, targetValues, originalValue, factorReplacementMethod, calculator);
     206        impact = CalculateImpactForString(variableName, model, dataset, modifiableDataset, rows, targetValues, originalValue, factorReplacementMethod);
    204207      } else {
    205208        throw new NotSupportedException("Variable not supported");
     
    212215      IEnumerable<double> estimatedValues,
    213216      out IEnumerable<double> targetValues,
    214       out double originalValue,
    215       IOnlineCalculator calculator) {
     217      out double originalValue) {
    216218      OnlineCalculatorError error;
    217219
     
    219221      targetValues = rows.Select(v => targetVariableValueList.ElementAt(v));
    220222      var estimatedValuesPartition = rows.Select(v => estimatedValues.ElementAt(v));
    221       originalValue = calculator.CalculateValue(targetValues, estimatedValuesPartition, out error);
     223      originalValue = CalculateValue(targetValues, estimatedValuesPartition, out error);
    222224
    223225      if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation.");
     
    230232      IEnumerable<double> targetValues,
    231233      double originalValue,
    232       ReplacementMethodEnum replacementMethod,
    233       IOnlineCalculator calculator) {
     234      ReplacementMethodEnum replacementMethod) {
    234235      OnlineCalculatorError error;
    235236      var newEstimates = EvaluateModelWithReplacedVariable(model, variableName, modifiableDataset, rows, replacementMethod);
    236       var newValue = calculator.CalculateValue(targetValues, newEstimates, out error);
     237      var newValue = CalculateValue(targetValues, newEstimates, out error);
    237238      if (error != OnlineCalculatorError.None) { throw new InvalidOperationException("Error during calculation with replaced inputs."); }
    238239      return originalValue - newValue;
     
    246247      IEnumerable<double> targetValues,
    247248      double originalValue,
    248       FactorReplacementMethodEnum factorReplacementMethod,
    249       IOnlineCalculator calculator) {
     249      FactorReplacementMethodEnum factorReplacementMethod) {
    250250
    251251      OnlineCalculatorError error;
     
    256256          var originalValues = modifiableDataset.GetReadOnlyStringValues(variableName).ToList();
    257257          var newEstimates = EvaluateModelWithReplacedVariable(originalValues, model, variableName, modifiableDataset, rows, Enumerable.Repeat(repl, problemData.Rows).ToList());
    258           var newValue = calculator.CalculateValue(targetValues, newEstimates, out error);
     258          var newValue = CalculateValue(targetValues, newEstimates, out error);
    259259          if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation with replaced inputs.");
    260260
     
    267267        // calculate impacts for factor variables
    268268        var newEstimates = EvaluateModelWithReplacedVariable(model, variableName, modifiableDataset, rows, factorReplacementMethod);
    269         var newValue = calculator.CalculateValue(targetValues, newEstimates, out error);
     269        var newValue = CalculateValue(targetValues, newEstimates, out error);
    270270        if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation with replaced inputs.");
    271271
     
    366366      return estimates;
    367367    }
     368
     369    private static double CalculateValue(IEnumerable<double> targets, IEnumerable<double> estimates, out OnlineCalculatorError error) {
     370      calculator.Reset();
     371      if (targets.Count() != estimates.Count()) {
     372        throw new ArgumentException(string.Format("Targets and Estimates must be of equal length ({0},{1})", targets.Count(), estimates.Count()));
     373      }
     374      foreach (var entry in targets.Zip(estimates, (target, estimate) => new { target, estimate })) {
     375        calculator.Add(entry.target, entry.estimate);
     376      }
     377      error = calculator.ErrorState;
     378      return calculator.Value;
     379    }
    368380  }
    369381}
Note: See TracChangeset for help on using the changeset viewer.