Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16001 for branches


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

Location:
branches/2904_CalculateImpacts/3.4
Files:
13 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}
  • branches/2904_CalculateImpacts/3.4/Interfaces/IOnlineCalculator.cs

    r15831 r16001  
    2222
    2323using System;
    24 using System.Collections.Generic;
    25 
    2624namespace HeuristicLab.Problems.DataAnalysis {
    2725  [Flags]
     
    4543    void Reset();
    4644    void Add(double original, double estimated);
    47     double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState);
    4845  }
    4946}
  • branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineAccuracyCalculator.cs

    r15831 r16001  
    2929    private int correctlyClassified;
    3030    private int n;
    31     public double Accuracy
    32     {
    33       get
    34       {
     31    public double Accuracy {
     32      get {
    3533        return correctlyClassified / (double)n;
    3634      }
     
    5553    #region IOnlineCalculator Members
    5654    private OnlineCalculatorError errorState;
    57     public OnlineCalculatorError ErrorState
    58     {
     55    public OnlineCalculatorError ErrorState {
    5956      get { return errorState; }
    6057    }
    61     public double Value
    62     {
     58    public double Value {
    6359      get { return Accuracy; }
    6460    }
     
    107103    }
    108104
    109     public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    110       return Calculate(originalValues, estimatedValues, out errorState);
    111     }
    112105  }
    113106}
  • branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineBoundedMeanSquaredErrorCalculator.cs

    r15831 r16001  
    2929    private double errorSum;
    3030    private int n;
    31     public double BoundedMeanSquaredError
    32     {
    33       get
    34       {
     31    public double BoundedMeanSquaredError {
     32      get {
    3533        return n > 0 ? errorSum / n : 0.0;
    3634      }
     
    6159    #region IOnlineCalculator Members
    6260    private OnlineCalculatorError errorState;
    63     public OnlineCalculatorError ErrorState
    64     {
     61    public OnlineCalculatorError ErrorState {
    6562      get { return errorState; }
    6663    }
    67     public double Value
    68     {
     64    public double Value {
    6965      get { return BoundedMeanSquaredError; }
    7066    }
     
    113109      }
    114110    }
    115 
    116     public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    117       throw new NotImplementedException();
    118     }
    119111  }
    120112}
  • branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineCovarianceCalculator.cs

    r15831 r16001  
    2929    private double xMean, yMean, Cn;
    3030    private int n;
    31     public double Covariance
    32     {
    33       get
    34       {
     31    public double Covariance {
     32      get {
    3533        return n > 0 ? Cn / n : 0.0;
    3634      }
     
    5654    #region IOnlineCalculator Members
    5755    private OnlineCalculatorError errorState;
    58     public OnlineCalculatorError ErrorState
    59     {
     56    public OnlineCalculatorError ErrorState {
    6057      get { return errorState; }
    6158    }
    62     public double Value
    63     {
     59    public double Value {
    6460      get { return Covariance; }
    6561    }
     
    112108      }
    113109    }
    114 
    115     public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    116       return Calculate(originalValues, estimatedValues, out errorState);
    117     }
    118110  }
    119111}
  • branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMaxAbsoluteErrorCalculator.cs

    r15831 r16001  
    2929    private double mae;
    3030    private int n;
    31     public double MaxAbsoluteError
    32     {
    33       get
    34       {
     31    public double MaxAbsoluteError {
     32      get {
    3533        return n > 0 ? mae : 0.0;
    3634      }
     
    5351    #region IOnlineCalculator Members
    5452    private OnlineCalculatorError errorState;
    55     public OnlineCalculatorError ErrorState
    56     {
     53    public OnlineCalculatorError ErrorState {
    5754      get { return errorState; }
    5855    }
    59     public double Value
    60     {
     56    public double Value {
    6157      get { return MaxAbsoluteError; }
    6258    }
     
    10399      }
    104100    }
    105 
    106     public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    107       return Calculate(originalValues, estimatedValues, out errorState);
    108     }
    109101  }
    110102}
  • branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMeanAbsoluteErrorCalculator.cs

    r15831 r16001  
    2929    private double sae;
    3030    private int n;
    31     public double MeanAbsoluteError
    32     {
    33       get
    34       {
     31    public double MeanAbsoluteError {
     32      get {
    3533        return n > 0 ? sae / n : 0.0;
    3634      }
     
    5351    #region IOnlineCalculator Members
    5452    private OnlineCalculatorError errorState;
    55     public OnlineCalculatorError ErrorState
    56     {
     53    public OnlineCalculatorError ErrorState {
    5754      get { return errorState; }
    5855    }
    59     public double Value
    60     {
     56    public double Value {
    6157      get { return MeanAbsoluteError; }
    6258    }
     
    10298      }
    10399    }
    104     public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    105       return Calculate(originalValues, estimatedValues, out errorState);
    106     }
    107100  }
    108101}
  • branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMeanAbsolutePercentageErrorCalculator.cs

    r15831 r16001  
    2929    private double sre;
    3030    private int n;
    31     public double MeanAbsolutePercentageError
    32     {
    33       get
    34       {
     31    public double MeanAbsolutePercentageError {
     32      get {
    3533        return n > 0 ? sre / n : 0.0;
    3634      }
     
    5452    #region IOnlineCalculator Members
    5553    private OnlineCalculatorError errorState;
    56     public OnlineCalculatorError ErrorState
    57     {
     54    public OnlineCalculatorError ErrorState {
    5855      get { return errorState; }
    5956    }
    60     public double Value
    61     {
     57    public double Value {
    6258      get { return MeanAbsolutePercentageError; }
    6359    }
     
    104100      }
    105101    }
    106 
    107     public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    108       return Calculate(originalValues, estimatedValues, out errorState);
    109     }
    110102  }
    111103}
  • branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMeanErrorCalculator.cs

    r15831 r16001  
    2828
    2929    private readonly OnlineMeanAndVarianceCalculator meanAndVarianceCalculator;
    30     public double MeanError
    31     {
     30    public double MeanError {
    3231      get { return meanAndVarianceCalculator.Mean; }
    3332    }
     
    4746
    4847    #region IOnlineCalculator Members
    49     public OnlineCalculatorError ErrorState
    50     {
     48    public OnlineCalculatorError ErrorState {
    5149      get { return meanAndVarianceCalculator.MeanErrorState; }
    5250    }
    53     public double Value
    54     {
     51    public double Value {
    5552      get { return MeanError; }
    5653    }
     
    8683      }
    8784    }
    88 
    89     public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    90       return Calculate(originalValues, estimatedValues, out errorState);
    91     }
    9285  }
    9386}
  • branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMeanSquaredErrorCalculator.cs

    r15831 r16001  
    2929    private double sse;
    3030    private int n;
    31     public double MeanSquaredError
    32     {
    33       get
    34       {
     31    public double MeanSquaredError {
     32      get {
    3533        return n > 0 ? sse / n : 0.0;
    3634      }
     
    5351    #region IOnlineCalculator Members
    5452    private OnlineCalculatorError errorState;
    55     public OnlineCalculatorError ErrorState
    56     {
     53    public OnlineCalculatorError ErrorState {
    5754      get { return errorState; }
    5855    }
    59     public double Value
    60     {
     56    public double Value {
    6157      get { return MeanSquaredError; }
    6258    }
     
    10298      }
    10399    }
    104 
    105     public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    106       return Calculate(originalValues, estimatedValues, out errorState);
    107     }
    108100  }
    109101}
  • branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineNormalizedMeanSquaredErrorCalculator.cs

    r15831 r16001  
    2929    private OnlineMeanAndVarianceCalculator originalVarianceCalculator;
    3030
    31     public double NormalizedMeanSquaredError
    32     {
    33       get
    34       {
     31    public double NormalizedMeanSquaredError {
     32      get {
    3533        double var = originalVarianceCalculator.PopulationVariance;
    3634        double m = meanSquaredErrorCalculator.Mean;
     
    5553
    5654    #region IOnlineCalculator Members
    57     public OnlineCalculatorError ErrorState
    58     {
     55    public OnlineCalculatorError ErrorState {
    5956      get { return meanSquaredErrorCalculator.MeanErrorState | originalVarianceCalculator.PopulationVarianceErrorState; }
    6057    }
    61     public double Value
    62     {
     58    public double Value {
    6359      get { return NormalizedMeanSquaredError; }
    6460    }
     
    107103    }
    108104
    109     public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    110       return Calculate(originalValues, estimatedValues, out errorState);
    111     }
     105
    112106  }
    113107}
  • branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlinePearsonsRCalculator.cs

    r15831 r16001  
    3030    private OnlineMeanAndVarianceCalculator syCalculator = new OnlineMeanAndVarianceCalculator();
    3131
    32     public double R
    33     {
    34       get
    35       {
     32    public double R {
     33      get {
    3634        double xVar = sxCalculator.PopulationVariance;
    3735        double yVar = syCalculator.PopulationVariance;
     
    6058
    6159    #region IOnlineCalculator Members
    62     public OnlineCalculatorError ErrorState
    63     {
     60    public OnlineCalculatorError ErrorState {
    6461      get { return covCalculator.ErrorState | sxCalculator.PopulationVarianceErrorState | syCalculator.PopulationVarianceErrorState; }
    6562    }
    66     public double Value
    67     {
     63    public double Value {
    6864      get { return R; }
    6965    }
     
    105101      }
    106102    }
    107 
    108     public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    109       return Calculate(originalValues, estimatedValues, out errorState);
    110     }
    111103  }
    112104}
  • branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlinePearsonsRSquaredCalculator.cs

    r15831 r16001  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using HeuristicLab.Common;
    2425
    2526namespace HeuristicLab.Problems.DataAnalysis {
     27  [Obsolete("Use OnlinePearsonsRCalculator directly")]
    2628  public class OnlinePearsonsRSquaredCalculator : DeepCloneable, IOnlineCalculator {
    2729    private readonly OnlinePearsonsRCalculator rCalculator = new OnlinePearsonsRCalculator();
    2830
    29     public double RSquared
    30     {
    31       get
    32       {
     31    public double RSquared {
     32      get {
    3333        if (rCalculator.ErrorState != OnlineCalculatorError.None) return 0.0;
    3434        else return rCalculator.R * rCalculator.R;
     
    4747
    4848    #region IOnlineCalculator Members
    49     public OnlineCalculatorError ErrorState
    50     {
     49    public OnlineCalculatorError ErrorState {
    5150      get { return rCalculator.ErrorState; }
    5251    }
    53     public double Value
    54     {
     52    public double Value {
    5553      get { return RSquared; }
    5654    }
     
    7068    }
    7169
    72     public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    73       return Calculate(originalValues, estimatedValues, out errorState);
    74     }
     70
    7571  }
    7672}
Note: See TracChangeset for help on using the changeset viewer.