Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.PopulationDiversityAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator.cs @ 12940

Last change on this file since 12940 was 4877, checked in by swinkler, 14 years ago

Created branch for population diversity analysis for symbolic regression. (#1278)

File size: 12.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis.Evaluators;
31using HeuristicLab.Problems.DataAnalysis.Symbolic;
32
33namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
34  [Item("SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator", "Calculates the mean and the variance of the squared errors of a linearly scaled symbolic regression solution.")]
35  [StorableClass]
36  public sealed class SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator : SymbolicRegressionMeanSquaredErrorEvaluator {
37    private const string QualityVarianceParameterName = "QualityVariance";
38    private const string QualitySamplesParameterName = "QualitySamples";
39    private const string DecompositionBiasParameterName = "QualityDecompositionBias";
40    private const string DecompositionVarianceParameterName = "QualityDecompositionVariance";
41    private const string DecompositionCovarianceParameterName = "QualityDecompositionCovariance";
42    private const string ApplyScalingParameterName = "ApplyScaling";
43
44    #region parameter properties
45    public IValueLookupParameter<BoolValue> ApplyScalingParameter {
46      get { return (IValueLookupParameter<BoolValue>)Parameters[ApplyScalingParameterName]; }
47    }
48    public ILookupParameter<DoubleValue> AlphaParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters["Alpha"]; }
50    }
51    public ILookupParameter<DoubleValue> BetaParameter {
52      get { return (ILookupParameter<DoubleValue>)Parameters["Beta"]; }
53    }
54    public ILookupParameter<DoubleValue> QualityVarianceParameter {
55      get { return (ILookupParameter<DoubleValue>)Parameters[QualityVarianceParameterName]; }
56    }
57    public ILookupParameter<IntValue> QualitySamplesParameter {
58      get { return (ILookupParameter<IntValue>)Parameters[QualitySamplesParameterName]; }
59    }
60    public ILookupParameter<DoubleValue> DecompositionBiasParameter {
61      get { return (ILookupParameter<DoubleValue>)Parameters[DecompositionBiasParameterName]; }
62    }
63    public ILookupParameter<DoubleValue> DecompositionVarianceParameter {
64      get { return (ILookupParameter<DoubleValue>)Parameters[DecompositionVarianceParameterName]; }
65    }
66    public ILookupParameter<DoubleValue> DecompositionCovarianceParameter {
67      get { return (ILookupParameter<DoubleValue>)Parameters[DecompositionCovarianceParameterName]; }
68    }
69
70    #endregion
71    #region properties
72    public BoolValue ApplyScaling {
73      get { return ApplyScalingParameter.ActualValue; }
74    }
75    public DoubleValue Alpha {
76      get { return AlphaParameter.ActualValue; }
77      set { AlphaParameter.ActualValue = value; }
78    }
79    public DoubleValue Beta {
80      get { return BetaParameter.ActualValue; }
81      set { BetaParameter.ActualValue = value; }
82    }
83    public DoubleValue QualityVariance {
84      get { return QualityVarianceParameter.ActualValue; }
85      set { QualityVarianceParameter.ActualValue = value; }
86    }
87    public IntValue QualitySamples {
88      get { return QualitySamplesParameter.ActualValue; }
89      set { QualitySamplesParameter.ActualValue = value; }
90    }
91    #endregion
92    [StorableConstructor]
93    private SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator(bool deserializing) : base(deserializing) { }
94    private SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator(SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator original, Cloner cloner) : base(original, cloner) { }
95    public SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator()
96      : base() {
97      Parameters.Add(new ValueLookupParameter<BoolValue>(ApplyScalingParameterName, "Determines if the estimated values should be scaled.", new BoolValue(true)));
98      Parameters.Add(new LookupParameter<DoubleValue>("Alpha", "Alpha parameter for linear scaling of the estimated values."));
99      Parameters.Add(new LookupParameter<DoubleValue>("Beta", "Beta parameter for linear scaling of the estimated values."));
100      Parameters.Add(new LookupParameter<DoubleValue>(QualityVarianceParameterName, "A parameter which stores the variance of the squared errors."));
101      Parameters.Add(new LookupParameter<IntValue>(QualitySamplesParameterName, " The number of evaluated samples."));
102      Parameters.Add(new LookupParameter<DoubleValue>(DecompositionBiasParameterName, "A parameter which stores the relativ bias of the MSE."));
103      Parameters.Add(new LookupParameter<DoubleValue>(DecompositionVarianceParameterName, "A parameter which stores the relativ bias of the MSE."));
104      Parameters.Add(new LookupParameter<DoubleValue>(DecompositionCovarianceParameterName, "A parameter which stores the relativ bias of the MSE."));
105    }
106
107    public override IDeepCloneable Clone(Cloner cloner) {
108      return new SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator(this, cloner);
109    }
110
111    public override double Evaluate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable<int> rows) {
112      double alpha, beta;
113      double meanSE, varianceSE;
114      int count;
115      double bias, variance, covariance;
116      double mse;
117      if (ApplyScaling.Value) {
118        mse = Calculate(interpreter, solution, LowerEstimationLimit.Value, UpperEstimationLimit.Value, dataset, targetVariable, rows, out beta, out alpha, out meanSE, out varianceSE, out count, out bias, out variance, out covariance);
119        Alpha = new DoubleValue(alpha);
120        Beta = new DoubleValue(beta);
121      } else {
122        mse = CalculateWithScaling(interpreter, solution, LowerEstimationLimit.Value, UpperEstimationLimit.Value, dataset, targetVariable, rows, 1, 0, out meanSE, out varianceSE, out count, out bias, out variance, out covariance);
123      }
124      QualityVariance = new DoubleValue(varianceSE);
125      QualitySamples = new IntValue(count);
126      DecompositionBiasParameter.ActualValue = new DoubleValue(bias / meanSE);
127      DecompositionVarianceParameter.ActualValue = new DoubleValue(variance / meanSE);
128      DecompositionCovarianceParameter.ActualValue = new DoubleValue(covariance / meanSE);
129      return mse;
130    }
131
132    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, out double bias, out double variance, out double covariance) {
133      IEnumerable<double> originalValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
134      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows);
135      CalculateScalingParameters(originalValues, estimatedValues, out beta, out alpha);
136
137      return CalculateWithScaling(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows, beta, alpha, out meanSE, out varianceSE, out count, out bias, out variance, out covariance);
138    }
139
140    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, out double bias, out double variance, out double covariance) {
141      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows);
142      IEnumerable<double> originalValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
143      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
144      IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
145      OnlineMeanAndVarianceCalculator seEvaluator = new OnlineMeanAndVarianceCalculator();
146      OnlineMeanAndVarianceCalculator originalMeanEvaluator = new OnlineMeanAndVarianceCalculator();
147      OnlineMeanAndVarianceCalculator estimatedMeanEvaluator = new OnlineMeanAndVarianceCalculator();
148      OnlinePearsonsRSquaredEvaluator r2Evaluator = new OnlinePearsonsRSquaredEvaluator();
149
150      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
151        double estimated = estimatedEnumerator.Current * beta + alpha;
152        double original = originalEnumerator.Current;
153        if (double.IsNaN(estimated))
154          estimated = upperEstimationLimit;
155        else
156          estimated = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, estimated));
157        double error = estimated - original;
158        error *= error;
159        seEvaluator.Add(error);
160        originalMeanEvaluator.Add(original);
161        estimatedMeanEvaluator.Add(estimated);
162        r2Evaluator.Add(original, estimated);
163      }
164
165      if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) {
166        throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match.");
167      } else {
168        meanSE = seEvaluator.Mean;
169        varianceSE = seEvaluator.Variance;
170        count = seEvaluator.Count;
171        bias = (originalMeanEvaluator.Mean - estimatedMeanEvaluator.Mean);
172        bias *= bias;
173
174        double sO = Math.Sqrt(originalMeanEvaluator.Variance);
175        double sE = Math.Sqrt(estimatedMeanEvaluator.Variance);
176        variance = sO - sE;
177        variance *= variance;
178        double r = Math.Sqrt(r2Evaluator.RSquared);
179        covariance = 2 * sO * sE * (1 - r);
180        return seEvaluator.Mean;
181      }
182    }
183
184    /// <summary>
185    /// Calculates linear scaling parameters in one pass.
186    /// The formulas to calculate the scaling parameters were taken from Scaled Symblic Regression by Maarten Keijzer.
187    /// http://www.springerlink.com/content/x035121165125175/
188    /// </summary>
189    public static void CalculateScalingParameters(IEnumerable<double> original, IEnumerable<double> estimated, out double beta, out double alpha) {
190      IEnumerator<double> originalEnumerator = original.GetEnumerator();
191      IEnumerator<double> estimatedEnumerator = estimated.GetEnumerator();
192      OnlineMeanAndVarianceCalculator yVarianceCalculator = new OnlineMeanAndVarianceCalculator();
193      OnlineMeanAndVarianceCalculator tMeanCalculator = new OnlineMeanAndVarianceCalculator();
194      OnlineCovarianceEvaluator ytCovarianceEvaluator = new OnlineCovarianceEvaluator();
195      int cnt = 0;
196
197      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
198        double y = estimatedEnumerator.Current;
199        double t = originalEnumerator.Current;
200        if (IsValidValue(t) && IsValidValue(y)) {
201          tMeanCalculator.Add(t);
202          yVarianceCalculator.Add(y);
203          ytCovarianceEvaluator.Add(y, t);
204
205          cnt++;
206        }
207      }
208
209      if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())
210        throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match.");
211      if (cnt < 2) {
212        alpha = 0;
213        beta = 1;
214      } else {
215        if (yVarianceCalculator.Variance.IsAlmost(0.0))
216          beta = 1;
217        else
218          beta = ytCovarianceEvaluator.Covariance / yVarianceCalculator.Variance;
219
220        alpha = tMeanCalculator.Mean - beta * yVarianceCalculator.Mean;
221      }
222    }
223
224    private static bool IsValidValue(double d) {
225      return !double.IsInfinity(d) && !double.IsNaN(d) && d > -1.0E07 && d < 1.0E07;  // don't consider very large or very small values for scaling
226    }
227  }
228}
Note: See TracBrowser for help on using the repository browser.