- Timestamp:
- 07/19/10 14:36:11 (14 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator.cs
r4038 r4044 38 38 39 39 namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic { 40 [Item("SymbolicRegressionScaledMean SquaredErrorEvaluator", "Calculates the mean squared errorof a linearly scaled symbolic regression solution.")]40 [Item("SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator", "Calculates the mean and the variance of the squared errors of a linearly scaled symbolic regression solution.")] 41 41 [StorableClass] 42 public class SymbolicRegressionScaledMeanSquaredErrorEvaluator : SymbolicRegressionMeanSquaredErrorEvaluator { 42 public class SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator : SymbolicRegressionMeanSquaredErrorEvaluator { 43 private const string QualityVarianceParameterName = "QualityVariance"; 44 private const string QualitySamplesParameterName = "QualitySamples"; 43 45 44 46 #region parameter properties … … 49 51 get { return (ILookupParameter<DoubleValue>)Parameters["Beta"]; } 50 52 } 53 public ILookupParameter<DoubleValue> QualityVarianceParameter { 54 get { return (ILookupParameter<DoubleValue>)Parameters[QualityVarianceParameterName]; } 55 } 56 public ILookupParameter<IntValue> QualitySamplesParameter { 57 get { return (ILookupParameter<IntValue>)Parameters[QualitySamplesParameterName]; } 58 } 59 51 60 #endregion 52 61 #region properties … … 59 68 set { BetaParameter.ActualValue = value; } 60 69 } 70 public DoubleValue QualityVariance { 71 get { return QualityVarianceParameter.ActualValue; } 72 set { QualityVarianceParameter.ActualValue = value; } 73 } 74 public IntValue QualitySamples { 75 get { return QualitySamplesParameter.ActualValue; } 76 set { QualitySamplesParameter.ActualValue = value; } 77 } 61 78 #endregion 62 public SymbolicRegressionScaledMean SquaredErrorEvaluator()79 public SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator() 63 80 : base() { 64 81 Parameters.Add(new LookupParameter<DoubleValue>("Alpha", "Alpha parameter for linear scaling of the estimated values.")); 65 82 Parameters.Add(new LookupParameter<DoubleValue>("Beta", "Beta parameter for linear scaling of the estimated values.")); 83 Parameters.Add(new LookupParameter<DoubleValue>(QualityVarianceParameterName, "A parameter which stores the variance of the squared errors.")); 84 Parameters.Add(new LookupParameter<IntValue>(QualitySamplesParameterName, " The number of evaluated samples.")); 66 85 } 67 86 68 87 protected override double Evaluate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, Dataset dataset, StringValue targetVariable, IEnumerable<int> rows) { 69 88 double alpha, beta; 70 double mse = Calculate(interpreter, solution, LowerEstimationLimit.Value, UpperEstimationLimit.Value, dataset, targetVariable.Value, rows, out beta, out alpha); 71 AlphaParameter.ActualValue = new DoubleValue(alpha); 72 BetaParameter.ActualValue = new DoubleValue(beta); 89 double meanSE, varianceSE; 90 int count; 91 double mse = Calculate(interpreter, solution, LowerEstimationLimit.Value, UpperEstimationLimit.Value, dataset, targetVariable.Value, rows, out beta, out alpha, out meanSE, out varianceSE, out count); 92 Alpha = new DoubleValue(alpha); 93 Beta = new DoubleValue(beta); 94 QualityVariance = new DoubleValue(varianceSE); 95 QualitySamples = new IntValue(count); 73 96 return mse; 74 97 } 75 98 76 public static double Calculate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable<int> rows, out double beta, out double alpha ) {77 IEnumerable<double> originalValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);99 public static double Calculate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable<int> rows, out double beta, out double alpha, out double meanSE, out double varianceSE, out int count) { 100 IEnumerable<double> originalValues = dataset.GetEnumeratedVariableValues(targetVariable, rows); 78 101 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows); 79 102 CalculateScalingParameters(originalValues, estimatedValues, out beta, out alpha); 80 103 81 return CalculateWithScaling(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows, beta, alpha );104 return CalculateWithScaling(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows, beta, alpha, out meanSE, out varianceSE, out count); 82 105 } 83 106 84 public static double CalculateWithScaling(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable<int> rows, double beta, double alpha ) {107 public static double CalculateWithScaling(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable<int> rows, double beta, double alpha, out double meanSE, out double varianceSE, out int count) { 85 108 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows); 86 109 IEnumerable<double> originalValues = dataset.GetEnumeratedVariableValues(targetVariable, rows); 87 110 IEnumerator<double> originalEnumerator = originalValues.GetEnumerator(); 88 111 IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator(); 89 OnlineMean SquaredErrorEvaluator mseEvaluator = new OnlineMeanSquaredErrorEvaluator();112 OnlineMeanAndVarianceCalculator seEvaluator = new OnlineMeanAndVarianceCalculator(); 90 113 91 114 while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) { … … 96 119 else 97 120 estimated = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, estimated)); 98 mseEvaluator.Add(original, estimated); 121 double error = estimated - original; 122 error *= error; 123 seEvaluator.Add(error); 99 124 } 100 125 … … 102 127 throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match."); 103 128 } else { 104 return mseEvaluator.MeanSquaredError; 129 meanSE = seEvaluator.Mean; 130 varianceSE = seEvaluator.Variance; 131 count = seEvaluator.Count; 132 return seEvaluator.Mean; 105 133 } 106 134 }
Note: See TracChangeset
for help on using the changeset viewer.