Changeset 2136
- Timestamp:
- 07/06/09 17:12:37 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 20 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.DataAnalysis/3.2/Statistics.cs
r1788 r2136 119 119 120 120 /// <summary> 121 /// calculates the sum of all values.122 /// </summary>123 /// <param name="values"></param>124 /// <returns></returns>125 public static double Sum(double[] values) {126 int n = values.Length;127 double sum = 0.0;128 for (int i = 0; i < n; i++) {129 if (double.IsNaN(values[i])) {130 throw new NotFiniteNumberException();131 } else {132 sum += values[i];133 }134 }135 return sum;136 }137 138 /// <summary>139 121 /// Calculates the mean of all values. 140 122 /// </summary> … … 158 140 /// <returns></returns> 159 141 public static double Mean(double[] values, int start, int end) { 160 if (values.Length == 0) throw new InvalidOperationException(); 142 if (values.Length == 0) throw new ArgumentException("Values is empty."); 143 if(end <=start) throw new ArgumentException("End is smaller or equal start"); 161 144 double sum = 0.0; 162 145 int n = 0; … … 167 150 } 168 151 } 169 if (n == 0) throw new InvalidOperationException(); 170 return sum / n; 152 if (n > 0) 153 return sum / n; 154 else throw new ArgumentException("Only NaN elements in values"); 171 155 } 172 156 … … 223 207 /// <returns></returns> 224 208 public static double Variance(double[] values, int start, int end) { 209 if (values.Length == 0) throw new ArgumentException("Values is empty."); 210 if (end <= start) throw new ArgumentException("End is smaller or equal start"); 225 211 if (end - start == 1) 226 212 return 0.0; … … 238 224 } 239 225 if (n < 2) { 240 throw new InvalidOperationException();226 throw new ArgumentException("Only one non-NaN element in values"); 241 227 } 242 228 return squaredErrorsSum / (n - 1); -
trunk/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3/ConditionalCoefficientOfDeterminationEvaluator.cs
r1902 r2136 36 36 } 37 37 } 38 38 39 39 public override string Description { 40 40 get { … … 45 45 46 46 public override double Evaluate(double[,] values) { 47 double quality = SimpleR2Evaluator.Calculate(values); 48 if (double.IsNaN(quality) || double.IsInfinity(quality)) 49 quality = double.MaxValue; 50 51 return quality; 47 try { return SimpleR2Evaluator.Calculate(values); } 48 catch (ArgumentException) { 49 return double.NegativeInfinity; 50 } 52 51 } 53 52 } -
trunk/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3/ConditionalMeanAbsolutePercentageErrorEvaluator.cs
r1902 r2136 45 45 46 46 public override double Evaluate(double[,] values) { 47 double quality = SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(values); 48 if (double.IsNaN(quality) || double.IsInfinity(quality)) 49 quality = double.MaxValue; 50 51 return quality; 47 try { return SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(values); } 48 catch (ArgumentException) { 49 return double.PositiveInfinity; 50 } 52 51 } 53 52 } -
trunk/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3/ConditionalMeanAbsolutePercentageOfRangeErrorEvaluator.cs
r1902 r2136 44 44 45 45 public override double Evaluate(double[,] values) { 46 double quality = SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.Calculate(values); 47 if (double.IsNaN(quality) || double.IsInfinity(quality)) 48 quality = double.MaxValue; 49 50 return quality; 46 try { 47 return SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.Calculate(values); 48 } 49 catch (ArgumentException) { 50 return double.PositiveInfinity; 51 } 51 52 } 52 53 } -
trunk/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3/ConditionalMeanSquaredErrorEvaluator.cs
r1902 r2136 44 44 45 45 public override double Evaluate(double[,] values) { 46 double quality = SimpleMSEEvaluator.Calculate(values); 47 48 if (double.IsNaN(quality) || double.IsInfinity(quality)) { 49 quality = double.MaxValue; 46 try { 47 return SimpleMSEEvaluator.Calculate(values); 50 48 } 51 52 return quality; 49 catch (ArgumentException) { 50 return double.PositiveInfinity; 51 } 53 52 } 54 53 } -
trunk/sources/HeuristicLab.GP.StructureIdentification.ConditionalEvaluation/3.3/ConditionalVarianceAccountedForEvaluator.cs
r1902 r2136 53 53 54 54 public override double Evaluate(double[,] values) { 55 double quality = SimpleVarianceAccountedForEvaluator.Calculate(values); 56 57 if (double.IsNaN(quality) || double.IsInfinity(quality)) { 58 quality = double.MaxValue; 55 try { return SimpleVarianceAccountedForEvaluator.Calculate(values); } 56 catch (ArgumentException) { 57 return double.NegativeInfinity; 59 58 } 60 return quality;61 59 } 62 60 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/CoefficientOfDeterminationEvaluator.cs
r1894 r2136 37 37 } 38 38 } 39 39 40 40 public override string Description { 41 41 get { … … 46 46 47 47 public override double Evaluate(double[,] values) { 48 49 double quality =SimpleR2Evaluator.Calculate(values);50 if (double.IsNaN(quality) || double.IsInfinity(quality))51 quality = double.MaxValue;52 53 return quality;48 try { 49 return SimpleR2Evaluator.Calculate(values); 50 } 51 catch (ArgumentException) { 52 return double.NegativeInfinity; 53 } 54 54 } 55 55 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanAbsolutePercentageErrorEvaluator.cs
r1894 r2136 46 46 47 47 public override double Evaluate(double[,] values) { 48 double quality = SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(values); 49 if (double.IsNaN(quality) || double.IsInfinity(quality)) 50 quality = double.MaxValue; 51 52 return quality; 48 try { 49 return SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(values); 50 } 51 catch (ArgumentException) { 52 return double.PositiveInfinity; 53 } 53 54 } 54 55 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanAbsolutePercentageOfRangeErrorEvaluator.cs
r1894 r2136 45 45 46 46 public override double Evaluate(double[,] values) { 47 double quality = SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.Calculate(values); 48 if (double.IsNaN(quality) || double.IsInfinity(quality)) 49 quality = double.MaxValue; 50 51 return quality; 47 try { return SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.Calculate(values); } 48 catch (ArgumentException) { 49 return double.PositiveInfinity; 50 } 52 51 } 53 52 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanSquaredErrorEvaluator.cs
r1894 r2136 45 45 46 46 public override double Evaluate(double[,] values) { 47 double quality = SimpleMSEEvaluator.Calculate(values); 48 49 if (double.IsNaN(quality) || double.IsInfinity(quality)) { 50 quality = double.MaxValue; 47 try { return SimpleMSEEvaluator.Calculate(values); } 48 catch (ArgumentException) { 49 return double.PositiveInfinity; 51 50 } 52 53 return quality;54 51 } 55 52 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VariableQualityImpactCalculator.cs
r2043 r2136 46 46 evaluator.PrepareForEvaluation(dataset, targetVariable, start, end, punishmentFactor, tree); 47 47 48 double[,] result = new double[end - start, 2];48 double[,] result = new double[end - start, 2]; 49 49 for (int i = start; i < end; i++) { 50 50 result[i - start, 0] = dataset.GetValue(i, targetVariable); 51 result[i - start, 1] = evaluator.Evaluate(i);51 result[i - start, 1] = evaluator.Evaluate(i); 52 52 } 53 53 54 return HeuristicLab.Modeling.SimpleMSEEvaluator.Calculate(result); 54 try { 55 return HeuristicLab.Modeling.SimpleMSEEvaluator.Calculate(result); 56 } 57 catch (ArgumentException) { 58 return double.PositiveInfinity; 59 } 55 60 } 56 61 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VarianceAccountedForEvaluator.cs
r1894 r2136 54 54 55 55 public override double Evaluate(double[,] values) { 56 double quality = SimpleVarianceAccountedForEvaluator.Calculate(values); 57 58 if (double.IsNaN(quality) || double.IsInfinity(quality)) { 59 quality = double.MaxValue; 56 try { return SimpleVarianceAccountedForEvaluator.Calculate(values); } 57 catch (ArgumentException) { 58 return double.NegativeInfinity; 60 59 } 61 return quality;62 60 } 63 61 } -
trunk/sources/HeuristicLab.Modeling/3.2/SimpleEvaluatorBase.cs
r1888 r2136 31 31 32 32 public abstract double Evaluate(double[,] values); 33 34 protected static bool IsAlmost(double x, double y) { 35 return Math.Abs(x - y) < 1.0E-12; 36 } 33 37 } 34 38 } -
trunk/sources/HeuristicLab.Modeling/3.2/SimpleMSEEvaluator.cs
r1888 r2136 16 16 } 17 17 public override double Evaluate(double[,] values) { 18 return Calculate(values); 18 try { 19 return Calculate(values); 20 } 21 catch (ArgumentException) { 22 return double.PositiveInfinity; 23 } 19 24 } 20 25 … … 32 37 } 33 38 } 34 35 double mse = sse / cnt; 36 return mse; 39 if (cnt > 0) { 40 double mse = sse / cnt; 41 return mse; 42 } else { 43 throw new ArgumentException("Mean squared errors is not defined for input vectors of NaN or Inf"); 44 } 37 45 } 38 46 } -
trunk/sources/HeuristicLab.Modeling/3.2/SimpleMeanAbsolutePercentageErrorEvaluator.cs
r1888 r2136 38 38 39 39 public override double Evaluate(double[,] values) { 40 return Calculate(values); 40 try { 41 return Calculate(values); 42 } 43 catch (ArgumentException) { 44 return double.PositiveInfinity; 45 } 41 46 } 42 47 … … 55 60 } 56 61 } 57 return errorsSum / n; 62 if (n > 0) { 63 return errorsSum / n; 64 } else throw new ArgumentException("Mean of absolute percentage error is not defined for input vectors of NaN or Inf"); 58 65 } 59 66 } -
trunk/sources/HeuristicLab.Modeling/3.2/SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.cs
r1888 r2136 37 37 38 38 public override double Evaluate(double[,] values) { 39 return Calculate(values); 39 try { 40 return Calculate(values); 41 } 42 catch (ArgumentException) { 43 return double.PositiveInfinity; 44 } 40 45 } 41 46 … … 47 52 for (int i = 0; i < originalValues.Length; i++) originalValues[i] = values[i, 1]; 48 53 double range = Statistics.Range(originalValues); 54 if (double.IsInfinity(range)) throw new ArgumentException("Range of elements in values is infinity"); 55 if (IsAlmost(range, 0.0)) throw new ArgumentException("Range of elements in values is zero"); 49 56 50 57 for (int i = 0; i < values.GetLength(0); i++) { … … 59 66 } 60 67 } 61 return errorsSum / n; 68 if (double.IsInfinity(range) || n == 0) { 69 throw new ArgumentException("Mean of absolute percentage of range error is not defined for input vectors of NaN or Inf"); 70 } else { 71 return errorsSum / n; 72 } 62 73 } 63 74 } -
trunk/sources/HeuristicLab.Modeling/3.2/SimpleR2Evaluator.cs
r1888 r2136 17 17 18 18 public override double Evaluate(double[,] values) { 19 return Calculate(values); 19 try { 20 return Calculate(values); 21 } 22 catch (ArgumentException) { 23 return double.NegativeInfinity; 24 } 20 25 } 21 26 … … 35 40 } 36 41 } 37 targetMean /= cnt;38 42 39 double targetDeviationTotalSumOfSquares = 0; 40 for (int i = 0; i < values.GetLength(0); i++) { 41 double target = values[i, 1]; 42 if (!double.IsNaN(target) && !double.IsInfinity(target)) { 43 target = target - targetMean; 44 target = target * target; 45 targetDeviationTotalSumOfSquares += target; 43 if (cnt > 0) { 44 targetMean /= cnt; 45 46 double targetDeviationTotalSumOfSquares = 0; 47 for (int i = 0; i < values.GetLength(0); i++) { 48 double target = values[i, 1]; 49 if (!double.IsNaN(target) && !double.IsInfinity(target)) { 50 target = target - targetMean; 51 target = target * target; 52 targetDeviationTotalSumOfSquares += target; 53 } 46 54 } 55 double quality = 1 - sse / targetDeviationTotalSumOfSquares; 56 if (quality > 1) 57 throw new InvalidProgramException(); 58 59 return quality; 60 } else { 61 throw new ArgumentException("Coefficient of determination is not defined for input vectors of NaN or Inf"); 47 62 } 48 double quality = 1 - sse / targetDeviationTotalSumOfSquares;49 if (quality > 1)50 throw new InvalidProgramException();51 52 return quality;53 63 } 54 64 } -
trunk/sources/HeuristicLab.Modeling/3.2/SimpleVarianceAccountedForEvaluator.cs
r1888 r2136 43 43 44 44 public override double Evaluate(double[,] values) { 45 return Calculate(values); 45 try { 46 return Calculate(values); 47 } 48 catch (ArgumentException) { 49 return double.NegativeInfinity; 50 } 46 51 } 47 52 … … 64 69 double errorsVariance = Statistics.Variance(errors); 65 70 double originalsVariance = Statistics.Variance(originalTargetVariableValues); 66 double quality = 1 - errorsVariance / originalsVariance; 67 68 return quality; 71 if (IsAlmost(originalsVariance, 0.0)) 72 if (IsAlmost(errorsVariance, 0.0)) { 73 return 1.0; 74 } else { 75 throw new ArgumentException("Variance of original values is zero"); 76 } else { 77 return 1.0 - errorsVariance / originalsVariance; 78 } 69 79 } 70 80 } -
trunk/sources/HeuristicLab.Modeling/3.2/VariableEvaluationImpactCalculator.cs
r2043 r2136 50 50 51 51 protected override double CalculateImpact(double[] referenceValue, double[] newValue) { 52 return SimpleMSEEvaluator.Calculate(CombineOutputs(referenceValue, newValue)); 52 try { 53 return SimpleMSEEvaluator.Calculate(CombineOutputs(referenceValue, newValue)); 54 } 55 catch (ArgumentException) { 56 return double.PositiveInfinity; 57 } 53 58 } 54 59 … … 59 64 protected override double[] PostProcessImpacts(double[] impacts) { 60 65 double mseSum = impacts.Sum(); 61 if ( mseSum == 0.0) mseSum = 1.0;66 if (IsAlmost(mseSum, 0.0)) mseSum = 1.0; 62 67 for (int i = 0; i < impacts.Length; i++) { 63 68 impacts[i] = impacts[i] / mseSum; … … 66 71 } 67 72 73 private bool IsAlmost(double x, double y) { 74 return Math.Abs(x - y) < 1.0E-12; 75 } 76 68 77 protected abstract double[] GetOutputs(IScope scope, Dataset dataset, int targetVariable, ItemList<IntData> allowedFeatures, int start, int end); 69 78 } -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/VariableQualityImpactCalculator.cs
r2043 r2136 45 45 for (int i = 0; i < end - start; i++) { 46 46 values[i, 0] = SVM.Prediction.Predict(model.Model, scaledProblem.X[i]); 47 values[i, 1] = dataset.GetValue(start + i, targetVariable);47 values[i, 1] = dataset.GetValue(start + i, targetVariable); 48 48 } 49 49 50 return HeuristicLab.Modeling.SimpleMSEEvaluator.Calculate(values); 50 try { return HeuristicLab.Modeling.SimpleMSEEvaluator.Calculate(values); } 51 catch (ArgumentException) { return double.PositiveInfinity; } 51 52 } 52 53 }
Note: See TracChangeset
for help on using the changeset viewer.