Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4195 was 4195, checked in by gkronber, 12 years ago

Created a feature/exploration branch for new data analysis features #1142

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