Free cookie consent management tool by TermsFeed Policy Generator

source: branches/M5Regression/HeuristicLab.Algorithms.DataAnalysis/3.4/M5Regression/LeafModels/DampenedLinearModel.cs @ 15830

Last change on this file since 15830 was 15830, checked in by bwerth, 6 years ago

#2847 adapted project to new rep structure; major changes to interfaces; restructures splitting and pruning

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.DataAnalysis;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  //mulitdimensional extension of http://www2.stat.duke.edu/~tjl13/s101/slides/unit6lec3H.pdf
31  [StorableClass]
32  public class DampenedLinearModel : RegressionModel, IConfidenceRegressionModel {
33    [Storable]
34    private IConfidenceRegressionModel Model;
35    [Storable]
36    private double Min;
37    [Storable]
38    private double Max;
39    [Storable]
40    private double Dampening;
41
42    [StorableConstructor]
43    private DampenedLinearModel(bool deserializing) : base(deserializing) { }
44    private DampenedLinearModel(DampenedLinearModel 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    public DampenedLinearModel(IConfidenceRegressionModel 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 DampenedLinearModel(this, cloner);
58    }
59    public override IEnumerable<string> VariablesUsedForPrediction {
60      get { return Model.VariablesUsedForPrediction; }
61    }
62    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
63      var slow = Sigmoid(-Dampening);
64      var shigh = Sigmoid(Dampening);
65      foreach (var x in Model.GetEstimatedValues(dataset, rows)) {
66        var y = Rescale(x, Min, Max, -Dampening, Dampening);
67        y = Sigmoid(y);
68        y = Rescale(y, slow, shigh, Min, Max);
69        yield return y;
70      }
71    }
72    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
73      return new ConfidenceRegressionSolution(this, problemData);
74    }
75    public IEnumerable<double> GetEstimatedVariances(IDataset dataset, IEnumerable<int> rows) {
76      return Model.GetEstimatedVariances(dataset, rows);
77    }
78
79    private static double Rescale(double x, double oMin, double oMax, double nMin, double nMax) {
80      var d = oMax - oMin;
81      var nd = nMax - nMin;
82      if (d.IsAlmost(0)) {
83        d = 1;
84        nMin += nd / 2;
85        nd = 0;
86      }
87      return ((x - oMin) / d) * nd + nMin;
88    }
89    private static double Sigmoid(double x) {
90      return 1 / (1 + Math.Exp(-x));
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.