Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3044_variableScaling/HeuristicLab.Algorithms.DataAnalysis.DecisionTrees/3.4/LeafModels/DampenedModel.cs @ 17835

Last change on this file since 17835 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Problems.DataAnalysis;
27using HEAL.Attic;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  // multidimensional extension of http://www2.stat.duke.edu/~tjl13/s101/slides/unit6lec3H.pdf
31  [StorableType("42E9766F-207F-47B1-890C-D5DFCF469838")]
32  public class DampenedModel : RegressionModel {
33    [Storable]
34    protected IRegressionModel Model;
35    [Storable]
36    private double Min;
37    [Storable]
38    private double Max;
39    [Storable]
40    private double Dampening;
41
42    [StorableConstructor]
43    protected DampenedModel(StorableConstructorFlag _) : base(_) { }
44    protected DampenedModel(DampenedModel original, Cloner cloner) : base(original, cloner) {
45      Model = cloner.Clone(original.Model);
46      Min = original.Min;
47      Max = original.Max;
48      Dampening = original.Dampening;
49    }
50    protected DampenedModel(IRegressionModel model, IRegressionProblemData pd, double dampening) : base(model.TargetVariable) {
51      Model = model;
52      Min = pd.TargetVariableTrainingValues.Min();
53      Max = pd.TargetVariableTrainingValues.Max();
54      Dampening = dampening;
55    }
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new DampenedModel(this, cloner);
58    }
59
60    public static IConfidenceRegressionModel DampenModel(IConfidenceRegressionModel model, IRegressionProblemData pd, double dampening) {
61      return new ConfidenceDampenedModel(model, pd, dampening);
62    }
63    public static IRegressionModel DampenModel(IRegressionModel model, IRegressionProblemData pd, double dampening) {
64      var cmodel = model as IConfidenceRegressionModel;
65      return cmodel != null ? new ConfidenceDampenedModel(cmodel, pd, dampening) : new DampenedModel(model, pd, dampening);
66    }
67
68    public override IEnumerable<string> VariablesUsedForPrediction {
69      get { return Model.VariablesUsedForPrediction; }
70    }
71
72    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
73      var slow = Sigmoid(-Dampening);
74      var shigh = Sigmoid(Dampening);
75      foreach (var x in Model.GetEstimatedValues(dataset, rows)) {
76        var y = Rescale(x, Min, Max, -Dampening, Dampening);
77        y = Sigmoid(y);
78        y = Rescale(y, slow, shigh, Min, Max);
79        yield return y;
80      }
81    }
82
83    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
84      return new RegressionSolution(this, problemData);
85    }
86
87    private static double Rescale(double x, double oMin, double oMax, double nMin, double nMax) {
88      var d = oMax - oMin;
89      var nd = nMax - nMin;
90      if (d.IsAlmost(0)) {
91        d = 1;
92        nMin += nd / 2;
93        nd = 0;
94      }
95      return ((x - oMin) / d) * nd + nMin;
96    }
97
98    private static double Sigmoid(double x) {
99      return 1 / (1 + Math.Exp(-x));
100    }
101
102
103    [StorableType("CCC93BEC-8796-4D8E-AC58-DD175073A79B")]
104    private sealed class ConfidenceDampenedModel : DampenedModel, IConfidenceRegressionModel {
105      #region HLConstructors
106      [StorableConstructor]
107      private ConfidenceDampenedModel(StorableConstructorFlag _) : base(_) { }
108      private ConfidenceDampenedModel(ConfidenceDampenedModel original, Cloner cloner) : base(original, cloner) { }
109      public ConfidenceDampenedModel(IConfidenceRegressionModel model, IRegressionProblemData pd, double dampening) : base(model, pd, dampening) { }
110      public override IDeepCloneable Clone(Cloner cloner) {
111        return new ConfidenceDampenedModel(this, cloner);
112      }
113      #endregion
114
115      public IEnumerable<double> GetEstimatedVariances(IDataset dataset, IEnumerable<int> rows) {
116        return ((IConfidenceRegressionModel)Model).GetEstimatedVariances(dataset, rows);
117      }
118
119      public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
120        return new ConfidenceRegressionSolution(this, problemData);
121      }
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.