[15967] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[15967] | 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.Problems.DataAnalysis;
|
---|
[16847] | 27 | using HEAL.Attic;
|
---|
[15967] | 28 |
|
---|
| 29 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[17083] | 30 | // multidimensional extension of http://www2.stat.duke.edu/~tjl13/s101/slides/unit6lec3H.pdf
|
---|
[16847] | 31 | [StorableType("42E9766F-207F-47B1-890C-D5DFCF469838")]
|
---|
[15967] | 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]
|
---|
[16847] | 43 | protected DampenedModel(StorableConstructorFlag _) : base(_) { }
|
---|
[15967] | 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 | }
|
---|
[16847] | 71 |
|
---|
[15967] | 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 | }
|
---|
[16847] | 82 |
|
---|
[15967] | 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 | }
|
---|
[16847] | 97 |
|
---|
[15967] | 98 | private static double Sigmoid(double x) {
|
---|
| 99 | return 1 / (1 + Math.Exp(-x));
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
[16847] | 103 | [StorableType("CCC93BEC-8796-4D8E-AC58-DD175073A79B")]
|
---|
[15967] | 104 | private sealed class ConfidenceDampenedModel : DampenedModel, IConfidenceRegressionModel {
|
---|
| 105 | #region HLConstructors
|
---|
| 106 | [StorableConstructor]
|
---|
[16847] | 107 | private ConfidenceDampenedModel(StorableConstructorFlag _) : base(_) { }
|
---|
[15967] | 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 | } |
---|