- Timestamp:
- 07/24/18 10:43:25 (6 years ago)
- Location:
- branches/2904_CalculateImpacts/3.4
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2904_CalculateImpacts/3.4/Implementation/Regression/RegressionSolutionVariableImpactsCalculator.cs
r15831 r16001 54 54 } 55 55 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 56 65 private const string ReplacementParameterName = "Replacement Method"; 57 66 private const string DataPartitionParameterName = "DataPartition"; … … 113 122 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median, 114 123 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) { 119 125 IEnumerable<int> rows; 120 126 … … 133 139 } 134 140 135 return CalculateImpacts(model, problemData, estimatedValues, rows, calculator,replacementMethod, factorReplacementMethod, progressCallback);141 return CalculateImpacts(model, problemData, estimatedValues, rows, replacementMethod, factorReplacementMethod, progressCallback); 136 142 } 137 143 … … 141 147 IEnumerable<double> estimatedValues, 142 148 IEnumerable<int> rows, 143 IOnlineCalculator calculator,144 149 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median, 145 150 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best, … … 149 154 double originalValue = -1; 150 155 151 PrepareData(rows, problemData, estimatedValues, out targetValues, out originalValue , calculator);156 PrepareData(rows, problemData, estimatedValues, out targetValues, out originalValue); 152 157 153 158 var impacts = new Dictionary<string, double>(); … … 166 171 if (progressCallback((double)curIdx / count, string.Format("Calculating impact for variable {0} ({1} of {2})", inputVariable, curIdx, count))) { return null; } 167 172 } 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); 169 174 } 170 175 … … 177 182 IEnumerable<double> targetValues, 178 183 double originalValue, 179 IOnlineCalculator calculator,180 184 DataPartitionEnum data = DataPartitionEnum.Training, 181 185 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median, 182 186 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); 184 188 } 185 189 … … 190 194 IEnumerable<double> targetValues, 191 195 double originalValue, 192 IOnlineCalculator calculator,193 196 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median, 194 197 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best) { … … 199 202 // calculate impacts for double variables 200 203 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); 202 205 } 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); 204 207 } else { 205 208 throw new NotSupportedException("Variable not supported"); … … 212 215 IEnumerable<double> estimatedValues, 213 216 out IEnumerable<double> targetValues, 214 out double originalValue, 215 IOnlineCalculator calculator) { 217 out double originalValue) { 216 218 OnlineCalculatorError error; 217 219 … … 219 221 targetValues = rows.Select(v => targetVariableValueList.ElementAt(v)); 220 222 var estimatedValuesPartition = rows.Select(v => estimatedValues.ElementAt(v)); 221 originalValue = calculator.CalculateValue(targetValues, estimatedValuesPartition, out error);223 originalValue = CalculateValue(targetValues, estimatedValuesPartition, out error); 222 224 223 225 if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation."); … … 230 232 IEnumerable<double> targetValues, 231 233 double originalValue, 232 ReplacementMethodEnum replacementMethod, 233 IOnlineCalculator calculator) { 234 ReplacementMethodEnum replacementMethod) { 234 235 OnlineCalculatorError error; 235 236 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); 237 238 if (error != OnlineCalculatorError.None) { throw new InvalidOperationException("Error during calculation with replaced inputs."); } 238 239 return originalValue - newValue; … … 246 247 IEnumerable<double> targetValues, 247 248 double originalValue, 248 FactorReplacementMethodEnum factorReplacementMethod, 249 IOnlineCalculator calculator) { 249 FactorReplacementMethodEnum factorReplacementMethod) { 250 250 251 251 OnlineCalculatorError error; … … 256 256 var originalValues = modifiableDataset.GetReadOnlyStringValues(variableName).ToList(); 257 257 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); 259 259 if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation with replaced inputs."); 260 260 … … 267 267 // calculate impacts for factor variables 268 268 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); 270 270 if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation with replaced inputs."); 271 271 … … 366 366 return estimates; 367 367 } 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 } 368 380 } 369 381 } -
branches/2904_CalculateImpacts/3.4/Interfaces/IOnlineCalculator.cs
r15831 r16001 22 22 23 23 using System; 24 using System.Collections.Generic;25 26 24 namespace HeuristicLab.Problems.DataAnalysis { 27 25 [Flags] … … 45 43 void Reset(); 46 44 void Add(double original, double estimated); 47 double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState);48 45 } 49 46 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineAccuracyCalculator.cs
r15831 r16001 29 29 private int correctlyClassified; 30 30 private int n; 31 public double Accuracy 32 { 33 get 34 { 31 public double Accuracy { 32 get { 35 33 return correctlyClassified / (double)n; 36 34 } … … 55 53 #region IOnlineCalculator Members 56 54 private OnlineCalculatorError errorState; 57 public OnlineCalculatorError ErrorState 58 { 55 public OnlineCalculatorError ErrorState { 59 56 get { return errorState; } 60 57 } 61 public double Value 62 { 58 public double Value { 63 59 get { return Accuracy; } 64 60 } … … 107 103 } 108 104 109 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {110 return Calculate(originalValues, estimatedValues, out errorState);111 }112 105 } 113 106 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineBoundedMeanSquaredErrorCalculator.cs
r15831 r16001 29 29 private double errorSum; 30 30 private int n; 31 public double BoundedMeanSquaredError 32 { 33 get 34 { 31 public double BoundedMeanSquaredError { 32 get { 35 33 return n > 0 ? errorSum / n : 0.0; 36 34 } … … 61 59 #region IOnlineCalculator Members 62 60 private OnlineCalculatorError errorState; 63 public OnlineCalculatorError ErrorState 64 { 61 public OnlineCalculatorError ErrorState { 65 62 get { return errorState; } 66 63 } 67 public double Value 68 { 64 public double Value { 69 65 get { return BoundedMeanSquaredError; } 70 66 } … … 113 109 } 114 110 } 115 116 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {117 throw new NotImplementedException();118 }119 111 } 120 112 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineCovarianceCalculator.cs
r15831 r16001 29 29 private double xMean, yMean, Cn; 30 30 private int n; 31 public double Covariance 32 { 33 get 34 { 31 public double Covariance { 32 get { 35 33 return n > 0 ? Cn / n : 0.0; 36 34 } … … 56 54 #region IOnlineCalculator Members 57 55 private OnlineCalculatorError errorState; 58 public OnlineCalculatorError ErrorState 59 { 56 public OnlineCalculatorError ErrorState { 60 57 get { return errorState; } 61 58 } 62 public double Value 63 { 59 public double Value { 64 60 get { return Covariance; } 65 61 } … … 112 108 } 113 109 } 114 115 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {116 return Calculate(originalValues, estimatedValues, out errorState);117 }118 110 } 119 111 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMaxAbsoluteErrorCalculator.cs
r15831 r16001 29 29 private double mae; 30 30 private int n; 31 public double MaxAbsoluteError 32 { 33 get 34 { 31 public double MaxAbsoluteError { 32 get { 35 33 return n > 0 ? mae : 0.0; 36 34 } … … 53 51 #region IOnlineCalculator Members 54 52 private OnlineCalculatorError errorState; 55 public OnlineCalculatorError ErrorState 56 { 53 public OnlineCalculatorError ErrorState { 57 54 get { return errorState; } 58 55 } 59 public double Value 60 { 56 public double Value { 61 57 get { return MaxAbsoluteError; } 62 58 } … … 103 99 } 104 100 } 105 106 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {107 return Calculate(originalValues, estimatedValues, out errorState);108 }109 101 } 110 102 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMeanAbsoluteErrorCalculator.cs
r15831 r16001 29 29 private double sae; 30 30 private int n; 31 public double MeanAbsoluteError 32 { 33 get 34 { 31 public double MeanAbsoluteError { 32 get { 35 33 return n > 0 ? sae / n : 0.0; 36 34 } … … 53 51 #region IOnlineCalculator Members 54 52 private OnlineCalculatorError errorState; 55 public OnlineCalculatorError ErrorState 56 { 53 public OnlineCalculatorError ErrorState { 57 54 get { return errorState; } 58 55 } 59 public double Value 60 { 56 public double Value { 61 57 get { return MeanAbsoluteError; } 62 58 } … … 102 98 } 103 99 } 104 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {105 return Calculate(originalValues, estimatedValues, out errorState);106 }107 100 } 108 101 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMeanAbsolutePercentageErrorCalculator.cs
r15831 r16001 29 29 private double sre; 30 30 private int n; 31 public double MeanAbsolutePercentageError 32 { 33 get 34 { 31 public double MeanAbsolutePercentageError { 32 get { 35 33 return n > 0 ? sre / n : 0.0; 36 34 } … … 54 52 #region IOnlineCalculator Members 55 53 private OnlineCalculatorError errorState; 56 public OnlineCalculatorError ErrorState 57 { 54 public OnlineCalculatorError ErrorState { 58 55 get { return errorState; } 59 56 } 60 public double Value 61 { 57 public double Value { 62 58 get { return MeanAbsolutePercentageError; } 63 59 } … … 104 100 } 105 101 } 106 107 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {108 return Calculate(originalValues, estimatedValues, out errorState);109 }110 102 } 111 103 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMeanErrorCalculator.cs
r15831 r16001 28 28 29 29 private readonly OnlineMeanAndVarianceCalculator meanAndVarianceCalculator; 30 public double MeanError 31 { 30 public double MeanError { 32 31 get { return meanAndVarianceCalculator.Mean; } 33 32 } … … 47 46 48 47 #region IOnlineCalculator Members 49 public OnlineCalculatorError ErrorState 50 { 48 public OnlineCalculatorError ErrorState { 51 49 get { return meanAndVarianceCalculator.MeanErrorState; } 52 50 } 53 public double Value 54 { 51 public double Value { 55 52 get { return MeanError; } 56 53 } … … 86 83 } 87 84 } 88 89 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {90 return Calculate(originalValues, estimatedValues, out errorState);91 }92 85 } 93 86 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMeanSquaredErrorCalculator.cs
r15831 r16001 29 29 private double sse; 30 30 private int n; 31 public double MeanSquaredError 32 { 33 get 34 { 31 public double MeanSquaredError { 32 get { 35 33 return n > 0 ? sse / n : 0.0; 36 34 } … … 53 51 #region IOnlineCalculator Members 54 52 private OnlineCalculatorError errorState; 55 public OnlineCalculatorError ErrorState 56 { 53 public OnlineCalculatorError ErrorState { 57 54 get { return errorState; } 58 55 } 59 public double Value 60 { 56 public double Value { 61 57 get { return MeanSquaredError; } 62 58 } … … 102 98 } 103 99 } 104 105 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {106 return Calculate(originalValues, estimatedValues, out errorState);107 }108 100 } 109 101 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineNormalizedMeanSquaredErrorCalculator.cs
r15831 r16001 29 29 private OnlineMeanAndVarianceCalculator originalVarianceCalculator; 30 30 31 public double NormalizedMeanSquaredError 32 { 33 get 34 { 31 public double NormalizedMeanSquaredError { 32 get { 35 33 double var = originalVarianceCalculator.PopulationVariance; 36 34 double m = meanSquaredErrorCalculator.Mean; … … 55 53 56 54 #region IOnlineCalculator Members 57 public OnlineCalculatorError ErrorState 58 { 55 public OnlineCalculatorError ErrorState { 59 56 get { return meanSquaredErrorCalculator.MeanErrorState | originalVarianceCalculator.PopulationVarianceErrorState; } 60 57 } 61 public double Value 62 { 58 public double Value { 63 59 get { return NormalizedMeanSquaredError; } 64 60 } … … 107 103 } 108 104 109 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 110 return Calculate(originalValues, estimatedValues, out errorState); 111 } 105 112 106 } 113 107 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlinePearsonsRCalculator.cs
r15831 r16001 30 30 private OnlineMeanAndVarianceCalculator syCalculator = new OnlineMeanAndVarianceCalculator(); 31 31 32 public double R 33 { 34 get 35 { 32 public double R { 33 get { 36 34 double xVar = sxCalculator.PopulationVariance; 37 35 double yVar = syCalculator.PopulationVariance; … … 60 58 61 59 #region IOnlineCalculator Members 62 public OnlineCalculatorError ErrorState 63 { 60 public OnlineCalculatorError ErrorState { 64 61 get { return covCalculator.ErrorState | sxCalculator.PopulationVarianceErrorState | syCalculator.PopulationVarianceErrorState; } 65 62 } 66 public double Value 67 { 63 public double Value { 68 64 get { return R; } 69 65 } … … 105 101 } 106 102 } 107 108 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {109 return Calculate(originalValues, estimatedValues, out errorState);110 }111 103 } 112 104 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlinePearsonsRSquaredCalculator.cs
r15831 r16001 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using HeuristicLab.Common; 24 25 25 26 namespace HeuristicLab.Problems.DataAnalysis { 27 [Obsolete("Use OnlinePearsonsRCalculator directly")] 26 28 public class OnlinePearsonsRSquaredCalculator : DeepCloneable, IOnlineCalculator { 27 29 private readonly OnlinePearsonsRCalculator rCalculator = new OnlinePearsonsRCalculator(); 28 30 29 public double RSquared 30 { 31 get 32 { 31 public double RSquared { 32 get { 33 33 if (rCalculator.ErrorState != OnlineCalculatorError.None) return 0.0; 34 34 else return rCalculator.R * rCalculator.R; … … 47 47 48 48 #region IOnlineCalculator Members 49 public OnlineCalculatorError ErrorState 50 { 49 public OnlineCalculatorError ErrorState { 51 50 get { return rCalculator.ErrorState; } 52 51 } 53 public double Value 54 { 52 public double Value { 55 53 get { return RSquared; } 56 54 } … … 70 68 } 71 69 72 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 73 return Calculate(originalValues, estimatedValues, out errorState); 74 } 70 75 71 } 76 72 }
Note: See TracChangeset
for help on using the changeset viewer.