Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2904_CalculateImpacts/3.4/Implementation/Regression/RegressionEnsembleModel.cs @ 15898

Last change on this file since 15898 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 8.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis {
30  /// <summary>
31  /// Represents regression solutions that contain an ensemble of multiple regression models
32  /// </summary>
33  [StorableClass]
34  [Item("RegressionEnsembleModel", "A regression model that contains an ensemble of multiple regression models")]
35  public sealed class RegressionEnsembleModel : RegressionModel, IRegressionEnsembleModel {
36    public override IEnumerable<string> VariablesUsedForPrediction {
37      get { return models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); }
38    }
39
40    private List<IRegressionModel> models;
41    public IEnumerable<IRegressionModel> Models {
42      get { return new List<IRegressionModel>(models); }
43    }
44
45    [Storable(Name = "Models")]
46    private IEnumerable<IRegressionModel> StorableModels {
47      get { return models; }
48      set { models = value.ToList(); }
49    }
50
51    private List<double> modelWeights;
52    public IEnumerable<double> ModelWeights {
53      get { return modelWeights; }
54    }
55
56    [Storable(Name = "ModelWeights")]
57    private IEnumerable<double> StorableModelWeights {
58      get { return modelWeights; }
59      set { modelWeights = value.ToList(); }
60    }
61
62    [Storable]
63    private bool averageModelEstimates = true;
64    public bool AverageModelEstimates {
65      get { return averageModelEstimates; }
66      set {
67        if (averageModelEstimates != value) {
68          averageModelEstimates = value;
69          OnChanged();
70        }
71      }
72    }
73
74    #region backwards compatiblity 3.3.5
75    [Storable(Name = "models", AllowOneWay = true)]
76    private List<IRegressionModel> OldStorableModels {
77      set { models = value; }
78    }
79    #endregion
80
81    [StorableHook(HookType.AfterDeserialization)]
82    private void AfterDeserialization() {
83      // BackwardsCompatibility 3.3.14
84      #region Backwards compatible code, remove with 3.4
85      if (modelWeights == null || !modelWeights.Any())
86        modelWeights = new List<double>(models.Select(m => 1.0));
87      #endregion
88    }
89
90    [StorableConstructor]
91    private RegressionEnsembleModel(bool deserializing) : base(deserializing) { }
92    private RegressionEnsembleModel(RegressionEnsembleModel original, Cloner cloner)
93      : base(original, cloner) {
94      this.models = original.Models.Select(cloner.Clone).ToList();
95      this.modelWeights = new List<double>(original.ModelWeights);
96      this.averageModelEstimates = original.averageModelEstimates;
97    }
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new RegressionEnsembleModel(this, cloner);
100    }
101
102    public RegressionEnsembleModel() : this(Enumerable.Empty<IRegressionModel>()) { }
103    public RegressionEnsembleModel(IEnumerable<IRegressionModel> models) : this(models, models.Select(m => 1.0)) { }
104    public RegressionEnsembleModel(IEnumerable<IRegressionModel> models, IEnumerable<double> modelWeights)
105      : base(string.Empty) {
106      this.name = ItemName;
107      this.description = ItemDescription;
108
109      this.models = new List<IRegressionModel>(models);
110      this.modelWeights = new List<double>(modelWeights);
111
112      if (this.models.Any()) this.TargetVariable = this.models.First().TargetVariable;
113    }
114
115    public void Add(IRegressionModel model) {
116      if (string.IsNullOrEmpty(TargetVariable)) TargetVariable = model.TargetVariable;
117      Add(model, 1.0);
118    }
119    public void Add(IRegressionModel model, double weight) {
120      if (string.IsNullOrEmpty(TargetVariable)) TargetVariable = model.TargetVariable;
121
122      models.Add(model);
123      modelWeights.Add(weight);
124      OnChanged();
125    }
126
127    public void AddRange(IEnumerable<IRegressionModel> models) {
128      AddRange(models, models.Select(m => 1.0));
129    }
130    public void AddRange(IEnumerable<IRegressionModel> models, IEnumerable<double> weights) {
131      if (string.IsNullOrEmpty(TargetVariable)) TargetVariable = models.First().TargetVariable;
132
133      this.models.AddRange(models);
134      modelWeights.AddRange(weights);
135      OnChanged();
136    }
137
138    public void Remove(IRegressionModel model) {
139      var index = models.IndexOf(model);
140      models.RemoveAt(index);
141      modelWeights.RemoveAt(index);
142
143      if (!models.Any()) TargetVariable = string.Empty;
144      OnChanged();
145    }
146    public void RemoveRange(IEnumerable<IRegressionModel> models) {
147      foreach (var model in models) {
148        var index = this.models.IndexOf(model);
149        this.models.RemoveAt(index);
150        modelWeights.RemoveAt(index);
151      }
152
153      if (!models.Any()) TargetVariable = string.Empty;
154      OnChanged();
155    }
156
157    public double GetModelWeight(IRegressionModel model) {
158      var index = models.IndexOf(model);
159      return modelWeights[index];
160    }
161    public void SetModelWeight(IRegressionModel model, double weight) {
162      var index = models.IndexOf(model);
163      modelWeights[index] = weight;
164      OnChanged();
165    }
166
167    #region evaluation
168    public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(IDataset dataset, IEnumerable<int> rows) {
169      var estimatedValuesEnumerators = (from model in models
170                                        let weight = GetModelWeight(model)
171                                        select model.GetEstimatedValues(dataset, rows).Select(e => weight * e)
172                                        .GetEnumerator()).ToList();
173
174      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
175        yield return from enumerator in estimatedValuesEnumerators
176                     select enumerator.Current;
177      }
178    }
179
180    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
181      double weightsSum = modelWeights.Sum();
182      var summedEstimates = from estimatedValuesVector in GetEstimatedValueVectors(dataset, rows)
183                            select estimatedValuesVector.DefaultIfEmpty(double.NaN).Sum();
184
185      if (AverageModelEstimates)
186        return summedEstimates.Select(v => v / weightsSum);
187      else
188        return summedEstimates;
189
190    }
191
192    public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows, Func<int, IRegressionModel, bool> modelSelectionPredicate) {
193      var estimatedValuesEnumerators = GetEstimatedValueVectors(dataset, rows).GetEnumerator();
194      var rowsEnumerator = rows.GetEnumerator();
195
196      while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.MoveNext()) {
197        var estimatedValueEnumerator = estimatedValuesEnumerators.Current.GetEnumerator();
198        int currentRow = rowsEnumerator.Current;
199        double weightsSum = 0.0;
200        double filteredEstimatesSum = 0.0;
201
202        for (int m = 0; m < models.Count; m++) {
203          estimatedValueEnumerator.MoveNext();
204          var model = models[m];
205          if (!modelSelectionPredicate(currentRow, model)) continue;
206
207          filteredEstimatesSum += estimatedValueEnumerator.Current;
208          weightsSum += modelWeights[m];
209        }
210
211        if (AverageModelEstimates)
212          yield return filteredEstimatesSum / weightsSum;
213        else
214          yield return filteredEstimatesSum;
215      }
216    }
217
218    #endregion
219
220    public event EventHandler Changed;
221    private void OnChanged() {
222      var handler = Changed;
223      if (handler != null)
224        handler(this, EventArgs.Empty);
225    }
226
227
228    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
229      return new RegressionEnsembleSolution(this, new RegressionEnsembleProblemData(problemData));
230    }
231  }
232}
Note: See TracBrowser for help on using the repository browser.