Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.PopulationDiversityAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SymbolicRegressionScaledMeanSquaredErrorEvaluator.cs @ 7461

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

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

File size: 7.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("SymbolicRegressionScaledMeanSquaredErrorEvaluator", "Calculates the mean squared error of a linearly scaled symbolic regression solution.")]
35  [StorableClass]
36  public sealed class SymbolicRegressionScaledMeanSquaredErrorEvaluator : SymbolicRegressionMeanSquaredErrorEvaluator {
37
38    #region parameter properties
39    public ILookupParameter<DoubleValue> AlphaParameter {
40      get { return (ILookupParameter<DoubleValue>)Parameters["Alpha"]; }
41    }
42    public ILookupParameter<DoubleValue> BetaParameter {
43      get { return (ILookupParameter<DoubleValue>)Parameters["Beta"]; }
44    }
45    #endregion
46    #region properties
47    public DoubleValue Alpha {
48      get { return AlphaParameter.ActualValue; }
49      set { AlphaParameter.ActualValue = value; }
50    }
51    public DoubleValue Beta {
52      get { return BetaParameter.ActualValue; }
53      set { BetaParameter.ActualValue = value; }
54    }
55    #endregion
56    [StorableConstructor]
57    private SymbolicRegressionScaledMeanSquaredErrorEvaluator(bool deserializing) : base(deserializing) { }
58    private SymbolicRegressionScaledMeanSquaredErrorEvaluator(SymbolicRegressionScaledMeanSquaredErrorEvaluator original, Cloner cloner) : base(original, cloner) { }
59    public SymbolicRegressionScaledMeanSquaredErrorEvaluator()
60      : base() {
61      Parameters.Add(new LookupParameter<DoubleValue>("Alpha", "Alpha parameter for linear scaling of the estimated values."));
62      Parameters.Add(new LookupParameter<DoubleValue>("Beta", "Beta parameter for linear scaling of the estimated values."));
63    }
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new SymbolicRegressionScaledMeanSquaredErrorEvaluator(this, cloner);
67    }
68
69    public override double Evaluate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable<int> rows) {
70      double alpha, beta;
71      double mse = Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows, out beta, out alpha);
72      AlphaParameter.ActualValue = new DoubleValue(alpha);
73      BetaParameter.ActualValue = new DoubleValue(beta);
74      return mse;
75    }
76
77    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) {
78      IEnumerable<double> originalValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
79      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows);
80      CalculateScalingParameters(originalValues, estimatedValues, out beta, out alpha);
81
82      return CalculateWithScaling(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows, beta, alpha);
83    }
84
85    public static double CalculateWithScaling(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable<int> rows, double beta, double alpha) {
86      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows);
87      IEnumerable<double> originalValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
88      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
89      IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
90      OnlineMeanSquaredErrorEvaluator mseEvaluator = new OnlineMeanSquaredErrorEvaluator();
91
92      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
93        double estimated = estimatedEnumerator.Current * beta + alpha;
94        double original = originalEnumerator.Current;
95        if (double.IsNaN(estimated))
96          estimated = upperEstimationLimit;
97        else
98          estimated = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, estimated));
99        mseEvaluator.Add(original, estimated);
100      }
101
102      if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) {
103        throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match.");
104      } else {
105        return mseEvaluator.MeanSquaredError;
106      }
107    }
108
109    /// <summary>
110    /// Calculates linear scaling parameters in one pass.
111    /// The formulas to calculate the scaling parameters were taken from Scaled Symblic Regression by Maarten Keijzer.
112    /// http://www.springerlink.com/content/x035121165125175/
113    /// </summary>
114    public static void CalculateScalingParameters(IEnumerable<double> original, IEnumerable<double> estimated, out double beta, out double alpha) {
115      IEnumerator<double> originalEnumerator = original.GetEnumerator();
116      IEnumerator<double> estimatedEnumerator = estimated.GetEnumerator();
117      OnlineMeanAndVarianceCalculator yVarianceCalculator = new OnlineMeanAndVarianceCalculator();
118      OnlineMeanAndVarianceCalculator tMeanCalculator = new OnlineMeanAndVarianceCalculator();
119      OnlineCovarianceEvaluator ytCovarianceEvaluator = new OnlineCovarianceEvaluator();
120      int cnt = 0;
121
122      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
123        double y = estimatedEnumerator.Current;
124        double t = originalEnumerator.Current;
125        if (IsValidValue(t) && IsValidValue(y)) {
126          tMeanCalculator.Add(t);
127          yVarianceCalculator.Add(y);
128          ytCovarianceEvaluator.Add(y, t);
129
130          cnt++;
131        }
132      }
133
134      if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())
135        throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match.");
136      if (cnt < 2) {
137        alpha = 0;
138        beta = 1;
139      } else {
140        if (yVarianceCalculator.PopulationVariance.IsAlmost(0.0))
141          beta = 1;
142        else
143          beta = ytCovarianceEvaluator.Covariance / yVarianceCalculator.PopulationVariance;
144
145        alpha = tMeanCalculator.Mean - beta * yVarianceCalculator.Mean;
146      }
147    }
148
149    private static bool IsValidValue(double d) {
150      return !double.IsInfinity(d) && !double.IsNaN(d) && d > -1.0E07 && d < 1.0E07;  // don't consider very large or very small values for scaling
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.