1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
32 | [Item("Variance Model Evaluator", "Can be used for modeling variance of a variable. Assumes that the variable values are sampled from a zero mean Gaussian. Use a model for the target variable to calculate the residuals. In a second step use the residuals as the target variable and use this evaluator to create the model for the conditional variance.")]
|
---|
33 | [StorableClass]
|
---|
34 | public class SymbolicRegressionVarianceModelEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
|
---|
35 | [StorableConstructor]
|
---|
36 | protected SymbolicRegressionVarianceModelEvaluator(bool deserializing) : base(deserializing) { }
|
---|
37 | protected SymbolicRegressionVarianceModelEvaluator(SymbolicRegressionVarianceModelEvaluator original, Cloner cloner)
|
---|
38 | : base(original, cloner) {
|
---|
39 | }
|
---|
40 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
41 | return new SymbolicRegressionVarianceModelEvaluator(this, cloner);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public SymbolicRegressionVarianceModelEvaluator() : base() { }
|
---|
45 |
|
---|
46 | public override bool Maximization { get { return true; } }
|
---|
47 |
|
---|
48 | public override IOperation InstrumentedApply() {
|
---|
49 | var solution = SymbolicExpressionTreeParameter.ActualValue;
|
---|
50 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
51 |
|
---|
52 | double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
|
---|
53 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
54 |
|
---|
55 | return base.InstrumentedApply();
|
---|
56 | }
|
---|
57 |
|
---|
58 | public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows) {
|
---|
59 | IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
|
---|
60 | IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
61 | IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
|
---|
62 |
|
---|
63 | // assumes residuals follow a zero-mean Gaussian distribution where the variance is a function of the inputs
|
---|
64 |
|
---|
65 | // boundedEstimatedValues is the estimator for std.dev.
|
---|
66 | // log likelihood for N(target_i | 0, boundesEstimatedValues)
|
---|
67 | var l2pi = Math.Log(2.0 * Math.PI);
|
---|
68 | var ll = -0.5 *
|
---|
69 | boundedEstimatedValues.Zip(targetValues, (s, t) =>
|
---|
70 | +l2pi
|
---|
71 | + Math.Log(s * s)
|
---|
72 | + (t * t) / (s * s)
|
---|
73 | ).Sum();
|
---|
74 |
|
---|
75 | return ll;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
|
---|
79 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
80 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
81 |
|
---|
82 | double ll = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
|
---|
83 |
|
---|
84 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
85 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
86 |
|
---|
87 | return ll;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|