Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleSolution.cs @ 6254

Last change on this file since 6254 was 6254, checked in by gkronber, 13 years ago

#1450: fixed bugs in calculation of estimated values in ensemble solutions for regression and classification.

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using System;
28using HeuristicLab.Data;
29
30namespace HeuristicLab.Problems.DataAnalysis {
31  /// <summary>
32  /// Represents regression solutions that contain an ensemble of multiple regression models
33  /// </summary>
34  [StorableClass]
35  [Item("Regression Ensemble Solution", "A regression solution that contains an ensemble of multiple regression models")]
36  // [Creatable("Data Analysis")]
37  public class RegressionEnsembleSolution : RegressionSolution, IRegressionEnsembleSolution {
38    public new IRegressionEnsembleModel Model {
39      get { return (IRegressionEnsembleModel)base.Model; }
40    }
41
42    [Storable]
43    private Dictionary<IRegressionModel, IntRange> trainingPartitions;
44    [Storable]
45    private Dictionary<IRegressionModel, IntRange> testPartitions;
46
47    [StorableConstructor]
48    protected RegressionEnsembleSolution(bool deserializing) : base(deserializing) { }
49    protected RegressionEnsembleSolution(RegressionEnsembleSolution original, Cloner cloner)
50      : base(original, cloner) {
51      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
52      testPartitions = new Dictionary<IRegressionModel, IntRange>();
53      foreach (var model in Model.Models) {
54        trainingPartitions[model] = (IntRange)ProblemData.TrainingPartition.Clone();
55        testPartitions[model] = (IntRange)ProblemData.TestPartition.Clone();
56      }
57    }
58
59    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData)
60      : base(new RegressionEnsembleModel(models), new RegressionEnsembleProblemData(problemData)) {
61      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
62      testPartitions = new Dictionary<IRegressionModel, IntRange>();
63      foreach (var model in models) {
64        trainingPartitions[model] = (IntRange)problemData.TrainingPartition.Clone();
65        testPartitions[model] = (IntRange)problemData.TestPartition.Clone();
66      }
67      RecalculateResults();
68    }
69
70    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
71      : base(new RegressionEnsembleModel(models), new RegressionEnsembleProblemData(problemData)) {
72      this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
73      this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
74      var modelEnumerator = models.GetEnumerator();
75      var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
76      var testPartitionEnumerator = testPartitions.GetEnumerator();
77      while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
78        this.trainingPartitions[modelEnumerator.Current] = (IntRange)trainingPartitionEnumerator.Current.Clone();
79        this.testPartitions[modelEnumerator.Current] = (IntRange)testPartitionEnumerator.Current.Clone();
80      }
81      if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
82        throw new ArgumentException();
83      }
84      RecalculateResults();
85    }
86
87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new RegressionEnsembleSolution(this, cloner);
89    }
90
91    public override IEnumerable<double> EstimatedTrainingValues {
92      get {
93        var rows = ProblemData.TrainingIndizes;
94        var estimatedValuesEnumerators = (from model in Model.Models
95                                          select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
96                                         .ToList();
97        var rowsEnumerator = rows.GetEnumerator();
98        // aggregate to make sure that MoveNext is called for all enumerators
99        while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
100          int currentRow = rowsEnumerator.Current;
101
102          var selectedEnumerators = from pair in estimatedValuesEnumerators
103                                    where RowIsTrainingForModel(currentRow, pair.Model) && !RowIsTestForModel(currentRow, pair.Model)
104                                    select pair.EstimatedValuesEnumerator;
105          yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
106        }
107      }
108    }
109
110    public override IEnumerable<double> EstimatedTestValues {
111      get {
112        var rows = ProblemData.TestIndizes;
113        var estimatedValuesEnumerators = (from model in Model.Models
114                                          select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
115                                         .ToList();
116        var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
117        // aggregate to make sure that MoveNext is called for all enumerators
118        while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
119          int currentRow = rowsEnumerator.Current;
120
121          var selectedEnumerators = from pair in estimatedValuesEnumerators
122                                    where RowIsTestForModel(currentRow, pair.Model)
123                                    select pair.EstimatedValuesEnumerator;
124
125          yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
126        }
127      }
128    }
129
130    private bool RowIsTrainingForModel(int currentRow, IRegressionModel model) {
131      return trainingPartitions == null || !trainingPartitions.ContainsKey(model) ||
132              (trainingPartitions[model].Start <= currentRow && currentRow < trainingPartitions[model].End);
133    }
134
135    private bool RowIsTestForModel(int currentRow, IRegressionModel model) {
136      return testPartitions == null || !testPartitions.ContainsKey(model) ||
137              (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
138    }
139
140    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
141      return from xs in GetEstimatedValueVectors(ProblemData.Dataset, rows)
142             select AggregateEstimatedValues(xs);
143    }
144
145    public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
146      var estimatedValuesEnumerators = (from model in Model.Models
147                                        select model.GetEstimatedValues(dataset, rows).GetEnumerator())
148                                       .ToList();
149
150      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
151        yield return from enumerator in estimatedValuesEnumerators
152                     select enumerator.Current;
153      }
154    }
155
156    private double AggregateEstimatedValues(IEnumerable<double> estimatedValues) {
157      return estimatedValues.DefaultIfEmpty(double.NaN).Average();
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.