Changeset 16001 for branches/2904_CalculateImpacts/3.4/Implementation
- Timestamp:
- 07/24/18 10:43:25 (6 years ago)
- File:
-
- 1 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 }
Note: See TracChangeset
for help on using the changeset viewer.