Changeset 15831
- Timestamp:
- 03/08/18 10:44:51 (7 years ago)
- Location:
- branches/2904_CalculateImpacts/3.4
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2904_CalculateImpacts/3.4/Implementation/Regression/RegressionSolutionVariableImpactsCalculator.cs
r15816 r15831 23 23 24 24 using System; 25 using System.Collections; 25 26 using System.Collections.Generic; 26 27 using System.Linq; … … 96 97 } 97 98 98 p rivate static void PrepareData(DataPartitionEnum partition,99 public static IEnumerable<Tuple<string, double>> CalculateImpacts( 99 100 IRegressionSolution solution, 100 out IEnumerable<int> rows, 101 DataPartitionEnum data = DataPartitionEnum.Training, 102 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median, 103 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best, 104 Func<double, string, bool> progressCallback = null) { 105 return CalculateImpacts(solution.Model, solution.ProblemData, solution.EstimatedValues, data, replacementMethod, factorReplacementMethod, progressCallback); 106 } 107 108 public static IEnumerable<Tuple<string, double>> CalculateImpacts( 109 IRegressionModel model, 110 IRegressionProblemData problemData, 111 IEnumerable<double> estimatedValues, 112 DataPartitionEnum data = DataPartitionEnum.Training, 113 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median, 114 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(); } 119 IEnumerable<int> rows; 120 121 switch (data) { 122 case DataPartitionEnum.All: 123 rows = problemData.AllIndices; 124 break; 125 case DataPartitionEnum.Test: 126 rows = problemData.TestIndices; 127 break; 128 case DataPartitionEnum.Training: 129 rows = problemData.TrainingIndices; 130 break; 131 default: 132 throw new NotSupportedException("DataPartition not supported"); 133 } 134 135 return CalculateImpacts(model, problemData, estimatedValues, rows, calculator, replacementMethod, factorReplacementMethod, progressCallback); 136 } 137 138 public static IEnumerable<Tuple<string, double>> CalculateImpacts( 139 IRegressionModel model, 140 IRegressionProblemData problemData, 141 IEnumerable<double> estimatedValues, 142 IEnumerable<int> rows, 143 IOnlineCalculator calculator, 144 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median, 145 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best, 146 Func<double, string, bool> progressCallback = null) { 147 148 IEnumerable<double> targetValues; 149 double originalValue = -1; 150 151 PrepareData(rows, problemData, estimatedValues, out targetValues, out originalValue, calculator); 152 153 var impacts = new Dictionary<string, double>(); 154 var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(model.VariablesUsedForPrediction)); 155 var allowedInputVariables = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).ToList(); 156 157 int curIdx = 0; 158 int count = allowedInputVariables 159 .Where(v => problemData.Dataset.VariableHasType<double>(v) || problemData.Dataset.VariableHasType<string>(v)) 160 .Count(); 161 162 foreach (var inputVariable in allowedInputVariables) { 163 //Report the current progress in percent. If the callback returns true, it means the execution shall be stopped 164 if (progressCallback != null) { 165 curIdx++; 166 if (progressCallback((double)curIdx / count, string.Format("Calculating impact for variable {0} ({1} of {2})", inputVariable, curIdx, count))) { return null; } 167 } 168 impacts[inputVariable] = CalculateImpact(inputVariable, model, problemData.Dataset, rows, targetValues, originalValue, calculator, replacementMethod, factorReplacementMethod); 169 } 170 171 return impacts.OrderByDescending(i => i.Value).Select(i => Tuple.Create(i.Key, i.Value)); 172 } 173 174 public static double CalculateImpact(string variableName, 175 IRegressionSolution solution, 176 IEnumerable<int> rows, 177 IEnumerable<double> targetValues, 178 double originalValue, 179 IOnlineCalculator calculator, 180 DataPartitionEnum data = DataPartitionEnum.Training, 181 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median, 182 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best) { 183 return CalculateImpact(variableName, solution.Model, solution.ProblemData.Dataset, rows, targetValues, originalValue, calculator, replacementMethod, factorReplacementMethod); 184 } 185 186 public static double CalculateImpact(string variableName, 187 IRegressionModel model, 188 IDataset dataset, 189 IEnumerable<int> rows, 190 IEnumerable<double> targetValues, 191 double originalValue, 192 IOnlineCalculator calculator, 193 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median, 194 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best) { 195 196 double impact = 0; 197 var modifiableDataset = ((Dataset)dataset).ToModifiable(); 198 199 // calculate impacts for double variables 200 if (dataset.VariableHasType<double>(variableName)) { 201 impact = CalculateImpactForDouble(variableName, model, modifiableDataset, rows, targetValues, originalValue, replacementMethod, calculator); 202 } else if (dataset.VariableHasType<string>(variableName)) { 203 impact = CalculateImpactForString(variableName, model, dataset, modifiableDataset, rows, targetValues, originalValue, factorReplacementMethod, calculator); 204 } else { 205 throw new NotSupportedException("Variable not supported"); 206 } 207 return impact; 208 } 209 210 private static void PrepareData(IEnumerable<int> rows, 211 IRegressionProblemData problemData, 212 IEnumerable<double> estimatedValues, 101 213 out IEnumerable<double> targetValues, 102 out double originalR2) { 214 out double originalValue, 215 IOnlineCalculator calculator) { 103 216 OnlineCalculatorError error; 104 217 105 switch (partition) { 106 case DataPartitionEnum.All: 107 rows = solution.ProblemData.AllIndices; 108 targetValues = solution.ProblemData.TargetVariableValues.ToList(); 109 originalR2 = OnlinePearsonsRCalculator.Calculate(solution.ProblemData.TargetVariableValues, solution.EstimatedValues, out error); 110 if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during R² calculation."); 111 originalR2 = originalR2 * originalR2; 112 break; 113 case DataPartitionEnum.Training: 114 rows = solution.ProblemData.TrainingIndices; 115 targetValues = solution.ProblemData.TargetVariableTrainingValues.ToList(); 116 originalR2 = solution.TrainingRSquared; 117 break; 118 case DataPartitionEnum.Test: 119 rows = solution.ProblemData.TestIndices; 120 targetValues = solution.ProblemData.TargetVariableTestValues.ToList(); 121 originalR2 = solution.TestRSquared; 122 break; 123 default: throw new ArgumentException(string.Format("DataPartition {0} cannot be handled.", partition)); 124 } 218 var targetVariableValueList = problemData.TargetVariableValues.ToList(); 219 targetValues = rows.Select(v => targetVariableValueList.ElementAt(v)); 220 var estimatedValuesPartition = rows.Select(v => estimatedValues.ElementAt(v)); 221 originalValue = calculator.CalculateValue(targetValues, estimatedValuesPartition, out error); 222 223 if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation."); 125 224 } 126 225 127 226 private static double CalculateImpactForDouble(string variableName, 128 IRegression Solution solution,227 IRegressionModel model, 129 228 ModifiableDataset modifiableDataset, 130 229 IEnumerable<int> rows, 131 230 IEnumerable<double> targetValues, 132 double originalR2, 133 ReplacementMethodEnum replacementMethod) { 231 double originalValue, 232 ReplacementMethodEnum replacementMethod, 233 IOnlineCalculator calculator) { 134 234 OnlineCalculatorError error; 135 var newEstimates = EvaluateModelWithReplacedVariable( solution.Model, variableName, modifiableDataset, rows, replacementMethod);136 var new R2 = OnlinePearsonsRCalculator.Calculate(targetValues, newEstimates, out error);137 if (error != OnlineCalculatorError.None) { throw new InvalidOperationException("Error during R²calculation with replaced inputs."); }138 return original R2 - (newR2 * newR2);235 var newEstimates = EvaluateModelWithReplacedVariable(model, variableName, modifiableDataset, rows, replacementMethod); 236 var newValue = calculator.CalculateValue(targetValues, newEstimates, out error); 237 if (error != OnlineCalculatorError.None) { throw new InvalidOperationException("Error during calculation with replaced inputs."); } 238 return originalValue - newValue; 139 239 } 140 240 141 241 private static double CalculateImpactForString(string variableName, 142 IRegressionSolution solution, 242 IRegressionModel model, 243 IDataset problemData, 143 244 ModifiableDataset modifiableDataset, 144 245 IEnumerable<int> rows, 145 246 IEnumerable<double> targetValues, 146 double originalR2, 147 FactorReplacementMethodEnum factorReplacementMethod) { 247 double originalValue, 248 FactorReplacementMethodEnum factorReplacementMethod, 249 IOnlineCalculator calculator) { 148 250 149 251 OnlineCalculatorError error; … … 151 253 // try replacing with all possible values and find the best replacement value 152 254 var smallestImpact = double.PositiveInfinity; 153 foreach (var repl in solution.ProblemData.Dataset.GetStringValues(variableName, rows).Distinct()) { 154 var newEstimates = EvaluateModelWithReplacedVariable(solution.Model, variableName, modifiableDataset, rows, Enumerable.Repeat(repl, solution.ProblemData.Dataset.Rows)); 155 var newR2 = OnlinePearsonsRCalculator.Calculate(targetValues, newEstimates, out error); 156 if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during R² calculation with replaced inputs."); 157 158 var curImpact = originalR2 - (newR2 * newR2); 255 foreach (var repl in problemData.GetStringValues(variableName, rows).Distinct()) { 256 var originalValues = modifiableDataset.GetReadOnlyStringValues(variableName).ToList(); 257 var newEstimates = EvaluateModelWithReplacedVariable(originalValues, model, variableName, modifiableDataset, rows, Enumerable.Repeat(repl, problemData.Rows).ToList()); 258 var newValue = calculator.CalculateValue(targetValues, newEstimates, out error); 259 if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation with replaced inputs."); 260 261 var curImpact = originalValue - newValue; 159 262 if (curImpact < smallestImpact) smallestImpact = curImpact; 160 263 } … … 163 266 // for replacement methods shuffle and mode 164 267 // calculate impacts for factor variables 165 var newEstimates = EvaluateModelWithReplacedVariable(solution.Model, variableName, modifiableDataset, rows, factorReplacementMethod); 166 var newR2 = OnlinePearsonsRCalculator.Calculate(targetValues, newEstimates, out error); 167 if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during R² calculation with replaced inputs."); 168 169 return originalR2 - (newR2 * newR2); 170 } 171 } 172 public static double CalculateImpact(string variableName, 173 IRegressionSolution solution, 174 IEnumerable<int> rows, 175 IEnumerable<double> targetValues, 176 double originalR2, 177 DataPartitionEnum data = DataPartitionEnum.Training, 178 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median, 179 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best) { 180 181 double impact = 0; 182 var modifiableDataset = ((Dataset)solution.ProblemData.Dataset).ToModifiable(); 183 184 // calculate impacts for double variables 185 if (solution.ProblemData.Dataset.VariableHasType<double>(variableName)) { 186 impact = CalculateImpactForDouble(variableName, solution, modifiableDataset, rows, targetValues, originalR2, replacementMethod); 187 } else if (solution.ProblemData.Dataset.VariableHasType<string>(variableName)) { 188 impact = CalculateImpactForString(variableName, solution, modifiableDataset, rows, targetValues, originalR2, factorReplacementMethod); 189 } else { 190 throw new NotSupportedException("Variable not supported"); 191 } 192 return impact; 193 } 194 195 public static IEnumerable<Tuple<string, double>> CalculateImpacts( 196 IRegressionSolution solution, 197 DataPartitionEnum data = DataPartitionEnum.Training, 198 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median, 199 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best, 200 Func<double, string, bool> progressCallback = null) { 201 202 IEnumerable<int> rows; 203 IEnumerable<double> targetValues; 204 double originalR2 = -1; 205 206 PrepareData(data, solution, out rows, out targetValues, out originalR2); 207 208 var impacts = new Dictionary<string, double>(); 209 var inputvariables = new HashSet<string>(solution.ProblemData.AllowedInputVariables.Union(solution.Model.VariablesUsedForPrediction)); 210 var allowedInputVariables = solution.ProblemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).ToList(); 211 212 int curIdx = 0; 213 int count = allowedInputVariables.Where(solution.ProblemData.Dataset.VariableHasType<double>).Count(); 214 // calculate impacts for double variables 215 foreach (var inputVariable in allowedInputVariables) { 216 //Report the current progress in percent. If the callback returns true, it means the execution shall be stopped 217 if (progressCallback != null) { 218 curIdx++; 219 if (progressCallback((double)curIdx / count, string.Format("Calculating impact for variable {0} ({1} of {2})", inputVariable, curIdx, count))) { return null; } 220 } 221 impacts[inputVariable] = CalculateImpact(inputVariable, solution, rows, targetValues, originalR2, data, replacementMethod, factorReplacementMethod); 222 } 223 224 return impacts.OrderByDescending(i => i.Value).Select(i => Tuple.Create(i.Key, i.Value)); 268 var newEstimates = EvaluateModelWithReplacedVariable(model, variableName, modifiableDataset, rows, factorReplacementMethod); 269 var newValue = calculator.CalculateValue(targetValues, newEstimates, out error); 270 if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation with replaced inputs."); 271 272 return originalValue - newValue; 273 } 225 274 } 226 275 … … 269 318 } 270 319 271 return EvaluateModelWithReplacedVariable( model, variable, dataset, rows, replacementValues);320 return EvaluateModelWithReplacedVariable(originalValues, model, variable, dataset, rows, replacementValues); 272 321 } 273 322 … … 305 354 } 306 355 307 return EvaluateModelWithReplacedVariable(model, variable, dataset, rows, replacementValues); 308 } 309 310 private static IEnumerable<double> EvaluateModelWithReplacedVariable(IRegressionModel model, string variable, 311 ModifiableDataset dataset, IEnumerable<int> rows, IEnumerable<double> replacementValues) { 312 var originalValues = dataset.GetReadOnlyDoubleValues(variable).ToList(); 313 dataset.ReplaceVariable(variable, replacementValues.ToList()); 356 return EvaluateModelWithReplacedVariable(originalValues, model, variable, dataset, rows, replacementValues); 357 } 358 359 private static IEnumerable<double> EvaluateModelWithReplacedVariable(IList originalValues, IRegressionModel model, string variable, 360 ModifiableDataset dataset, IEnumerable<int> rows, IList replacementValues) { 361 dataset.ReplaceVariable(variable, replacementValues); 314 362 //mkommend: ToList is used on purpose to avoid lazy evaluation that could result in wrong estimates due to variable replacements 315 363 var estimates = model.GetEstimatedValues(dataset, rows).ToList(); … … 318 366 return estimates; 319 367 } 320 private static IEnumerable<double> EvaluateModelWithReplacedVariable(IRegressionModel model, string variable,321 ModifiableDataset dataset, IEnumerable<int> rows, IEnumerable<string> replacementValues) {322 var originalValues = dataset.GetReadOnlyStringValues(variable).ToList();323 dataset.ReplaceVariable(variable, replacementValues.ToList());324 //mkommend: ToList is used on purpose to avoid lazy evaluation that could result in wrong estimates due to variable replacements325 var estimates = model.GetEstimatedValues(dataset, rows).ToList();326 dataset.ReplaceVariable(variable, originalValues);327 328 return estimates;329 }330 368 } 331 369 } -
branches/2904_CalculateImpacts/3.4/Interfaces/IOnlineCalculator.cs
r15583 r15831 22 22 23 23 using System; 24 using System.Collections.Generic; 25 24 26 namespace HeuristicLab.Problems.DataAnalysis { 25 27 [Flags] … … 43 45 void Reset(); 44 46 void Add(double original, double estimated); 47 double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState); 45 48 } 46 49 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineAccuracyCalculator.cs
r15583 r15831 29 29 private int correctlyClassified; 30 30 private int n; 31 public double Accuracy { 32 get { 31 public double Accuracy 32 { 33 get 34 { 33 35 return correctlyClassified / (double)n; 34 36 } … … 53 55 #region IOnlineCalculator Members 54 56 private OnlineCalculatorError errorState; 55 public OnlineCalculatorError ErrorState { 57 public OnlineCalculatorError ErrorState 58 { 56 59 get { return errorState; } 57 60 } 58 public double Value { 61 public double Value 62 { 59 63 get { return Accuracy; } 60 64 } … … 103 107 } 104 108 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 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineBoundedMeanSquaredErrorCalculator.cs
r15583 r15831 29 29 private double errorSum; 30 30 private int n; 31 public double BoundedMeanSquaredError { 32 get { 31 public double BoundedMeanSquaredError 32 { 33 get 34 { 33 35 return n > 0 ? errorSum / n : 0.0; 34 36 } … … 59 61 #region IOnlineCalculator Members 60 62 private OnlineCalculatorError errorState; 61 public OnlineCalculatorError ErrorState { 63 public OnlineCalculatorError ErrorState 64 { 62 65 get { return errorState; } 63 66 } 64 public double Value { 67 public double Value 68 { 65 69 get { return BoundedMeanSquaredError; } 66 70 } … … 109 113 } 110 114 } 115 116 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 117 throw new NotImplementedException(); 118 } 111 119 } 112 120 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineCovarianceCalculator.cs
r15583 r15831 29 29 private double xMean, yMean, Cn; 30 30 private int n; 31 public double Covariance { 32 get { 31 public double Covariance 32 { 33 get 34 { 33 35 return n > 0 ? Cn / n : 0.0; 34 36 } … … 54 56 #region IOnlineCalculator Members 55 57 private OnlineCalculatorError errorState; 56 public OnlineCalculatorError ErrorState { 58 public OnlineCalculatorError ErrorState 59 { 57 60 get { return errorState; } 58 61 } 59 public double Value { 62 public double Value 63 { 60 64 get { return Covariance; } 61 65 } … … 108 112 } 109 113 } 114 115 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 116 return Calculate(originalValues, estimatedValues, out errorState); 117 } 110 118 } 111 119 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMaxAbsoluteErrorCalculator.cs
r15583 r15831 29 29 private double mae; 30 30 private int n; 31 public double MaxAbsoluteError { 32 get { 31 public double MaxAbsoluteError 32 { 33 get 34 { 33 35 return n > 0 ? mae : 0.0; 34 36 } … … 51 53 #region IOnlineCalculator Members 52 54 private OnlineCalculatorError errorState; 53 public OnlineCalculatorError ErrorState { 55 public OnlineCalculatorError ErrorState 56 { 54 57 get { return errorState; } 55 58 } 56 public double Value { 59 public double Value 60 { 57 61 get { return MaxAbsoluteError; } 58 62 } … … 99 103 } 100 104 } 105 106 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 107 return Calculate(originalValues, estimatedValues, out errorState); 108 } 101 109 } 102 110 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMeanAbsoluteErrorCalculator.cs
r15583 r15831 29 29 private double sae; 30 30 private int n; 31 public double MeanAbsoluteError { 32 get { 31 public double MeanAbsoluteError 32 { 33 get 34 { 33 35 return n > 0 ? sae / n : 0.0; 34 36 } … … 51 53 #region IOnlineCalculator Members 52 54 private OnlineCalculatorError errorState; 53 public OnlineCalculatorError ErrorState { 55 public OnlineCalculatorError ErrorState 56 { 54 57 get { return errorState; } 55 58 } 56 public double Value { 59 public double Value 60 { 57 61 get { return MeanAbsoluteError; } 58 62 } … … 98 102 } 99 103 } 104 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 105 return Calculate(originalValues, estimatedValues, out errorState); 106 } 100 107 } 101 108 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMeanAbsolutePercentageErrorCalculator.cs
r15583 r15831 29 29 private double sre; 30 30 private int n; 31 public double MeanAbsolutePercentageError { 32 get { 31 public double MeanAbsolutePercentageError 32 { 33 get 34 { 33 35 return n > 0 ? sre / n : 0.0; 34 36 } … … 52 54 #region IOnlineCalculator Members 53 55 private OnlineCalculatorError errorState; 54 public OnlineCalculatorError ErrorState { 56 public OnlineCalculatorError ErrorState 57 { 55 58 get { return errorState; } 56 59 } 57 public double Value { 60 public double Value 61 { 58 62 get { return MeanAbsolutePercentageError; } 59 63 } … … 100 104 } 101 105 } 106 107 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 108 return Calculate(originalValues, estimatedValues, out errorState); 109 } 102 110 } 103 111 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMeanErrorCalculator.cs
r15583 r15831 28 28 29 29 private readonly OnlineMeanAndVarianceCalculator meanAndVarianceCalculator; 30 public double MeanError { 30 public double MeanError 31 { 31 32 get { return meanAndVarianceCalculator.Mean; } 32 33 } … … 46 47 47 48 #region IOnlineCalculator Members 48 public OnlineCalculatorError ErrorState { 49 public OnlineCalculatorError ErrorState 50 { 49 51 get { return meanAndVarianceCalculator.MeanErrorState; } 50 52 } 51 public double Value { 53 public double Value 54 { 52 55 get { return MeanError; } 53 56 } … … 83 86 } 84 87 } 88 89 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 90 return Calculate(originalValues, estimatedValues, out errorState); 91 } 85 92 } 86 93 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineMeanSquaredErrorCalculator.cs
r15583 r15831 29 29 private double sse; 30 30 private int n; 31 public double MeanSquaredError { 32 get { 31 public double MeanSquaredError 32 { 33 get 34 { 33 35 return n > 0 ? sse / n : 0.0; 34 36 } … … 51 53 #region IOnlineCalculator Members 52 54 private OnlineCalculatorError errorState; 53 public OnlineCalculatorError ErrorState { 55 public OnlineCalculatorError ErrorState 56 { 54 57 get { return errorState; } 55 58 } 56 public double Value { 59 public double Value 60 { 57 61 get { return MeanSquaredError; } 58 62 } … … 98 102 } 99 103 } 104 105 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 106 return Calculate(originalValues, estimatedValues, out errorState); 107 } 100 108 } 101 109 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlineNormalizedMeanSquaredErrorCalculator.cs
r15583 r15831 29 29 private OnlineMeanAndVarianceCalculator originalVarianceCalculator; 30 30 31 public double NormalizedMeanSquaredError { 32 get { 31 public double NormalizedMeanSquaredError 32 { 33 get 34 { 33 35 double var = originalVarianceCalculator.PopulationVariance; 34 36 double m = meanSquaredErrorCalculator.Mean; … … 53 55 54 56 #region IOnlineCalculator Members 55 public OnlineCalculatorError ErrorState { 57 public OnlineCalculatorError ErrorState 58 { 56 59 get { return meanSquaredErrorCalculator.MeanErrorState | originalVarianceCalculator.PopulationVarianceErrorState; } 57 60 } 58 public double Value { 61 public double Value 62 { 59 63 get { return NormalizedMeanSquaredError; } 60 64 } … … 103 107 } 104 108 105 109 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 110 return Calculate(originalValues, estimatedValues, out errorState); 111 } 106 112 } 107 113 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlinePearsonsRCalculator.cs
r15583 r15831 30 30 private OnlineMeanAndVarianceCalculator syCalculator = new OnlineMeanAndVarianceCalculator(); 31 31 32 public double R { 33 get { 32 public double R 33 { 34 get 35 { 34 36 double xVar = sxCalculator.PopulationVariance; 35 37 double yVar = syCalculator.PopulationVariance; … … 58 60 59 61 #region IOnlineCalculator Members 60 public OnlineCalculatorError ErrorState { 62 public OnlineCalculatorError ErrorState 63 { 61 64 get { return covCalculator.ErrorState | sxCalculator.PopulationVarianceErrorState | syCalculator.PopulationVarianceErrorState; } 62 65 } 63 public double Value { 66 public double Value 67 { 64 68 get { return R; } 65 69 } … … 101 105 } 102 106 } 107 108 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 109 return Calculate(originalValues, estimatedValues, out errorState); 110 } 103 111 } 104 112 } -
branches/2904_CalculateImpacts/3.4/OnlineCalculators/OnlinePearsonsRSquaredCalculator.cs
r15583 r15831 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using HeuristicLab.Common; 25 24 26 25 namespace HeuristicLab.Problems.DataAnalysis { 27 [Obsolete("Use OnlinePearsonsRCalculator directly")]28 26 public class OnlinePearsonsRSquaredCalculator : DeepCloneable, IOnlineCalculator { 29 27 private readonly OnlinePearsonsRCalculator rCalculator = new OnlinePearsonsRCalculator(); 30 28 31 public double RSquared { 32 get { 29 public double RSquared 30 { 31 get 32 { 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 { 49 public OnlineCalculatorError ErrorState 50 { 50 51 get { return rCalculator.ErrorState; } 51 52 } 52 public double Value { 53 public double Value 54 { 53 55 get { return RSquared; } 54 56 } … … 68 70 } 69 71 70 72 public double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 73 return Calculate(originalValues, estimatedValues, out errorState); 74 } 71 75 } 72 76 }
Note: See TracChangeset
for help on using the changeset viewer.