Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/19/10 14:36:11 (12 years ago)
Author:
mkommend
Message:

added statistical comperator operator for SymReg OSGP (ticket #1082)

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator.cs

    r4038 r4044  
    3838
    3939namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
    40   [Item("SymbolicRegressionScaledMeanSquaredErrorEvaluator", "Calculates the mean squared error of 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.")]
    4141  [StorableClass]
    42   public class SymbolicRegressionScaledMeanSquaredErrorEvaluator : SymbolicRegressionMeanSquaredErrorEvaluator {
     42  public class SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator : SymbolicRegressionMeanSquaredErrorEvaluator {
     43    private const string QualityVarianceParameterName = "QualityVariance";
     44    private const string QualitySamplesParameterName = "QualitySamples";
    4345
    4446    #region parameter properties
     
    4951      get { return (ILookupParameter<DoubleValue>)Parameters["Beta"]; }
    5052    }
     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
    5160    #endregion
    5261    #region properties
     
    5968      set { BetaParameter.ActualValue = value; }
    6069    }
     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    }
    6178    #endregion
    62     public SymbolicRegressionScaledMeanSquaredErrorEvaluator()
     79    public SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator()
    6380      : base() {
    6481      Parameters.Add(new LookupParameter<DoubleValue>("Alpha", "Alpha parameter for linear scaling of the estimated values."));
    6582      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."));
    6685    }
    6786
    6887    protected override double Evaluate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, Dataset dataset, StringValue targetVariable, IEnumerable<int> rows) {
    6988      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);
    7396      return mse;
    7497    }
    7598
    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);
    78101      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows);
    79102      CalculateScalingParameters(originalValues, estimatedValues, out beta, out alpha);
    80103
    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);
    82105    }
    83106
    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) {
    85108      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows);
    86109      IEnumerable<double> originalValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
    87110      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
    88111      IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
    89       OnlineMeanSquaredErrorEvaluator mseEvaluator = new OnlineMeanSquaredErrorEvaluator();
     112      OnlineMeanAndVarianceCalculator seEvaluator = new OnlineMeanAndVarianceCalculator();
    90113
    91114      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
     
    96119        else
    97120          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);
    99124      }
    100125
     
    102127        throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match.");
    103128      } else {
    104         return mseEvaluator.MeanSquaredError;
     129        meanSE = seEvaluator.Mean;
     130        varianceSE = seEvaluator.Variance;
     131        count = seEvaluator.Count;
     132        return seEvaluator.Mean;
    105133      }
    106134    }
Note: See TracChangeset for help on using the changeset viewer.