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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using System;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 |
|
---|
30 | namespace 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 | }
|
---|
52 | public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData)
|
---|
53 | : base(new RegressionEnsembleModel(models), problemData) {
|
---|
54 | trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
55 | testPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
56 | foreach (var model in models) {
|
---|
57 | trainingPartitions[model] = (IntRange)problemData.TrainingPartition.Clone();
|
---|
58 | testPartitions[model] = (IntRange)problemData.TestPartition.Clone();
|
---|
59 | }
|
---|
60 | RecalculateResults();
|
---|
61 | }
|
---|
62 |
|
---|
63 | public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
|
---|
64 | : base(new RegressionEnsembleModel(models), problemData) {
|
---|
65 | this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
66 | this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
67 | var modelEnumerator = models.GetEnumerator();
|
---|
68 | var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
|
---|
69 | var testPartitionEnumerator = testPartitions.GetEnumerator();
|
---|
70 | while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
|
---|
71 | this.trainingPartitions[modelEnumerator.Current] = (IntRange)trainingPartitionEnumerator.Current.Clone();
|
---|
72 | this.testPartitions[modelEnumerator.Current] = (IntRange)testPartitionEnumerator.Current.Clone();
|
---|
73 | }
|
---|
74 | if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
|
---|
75 | throw new ArgumentException();
|
---|
76 | }
|
---|
77 |
|
---|
78 | RecalculateResults();
|
---|
79 | }
|
---|
80 |
|
---|
81 | private void RecalculateResults() {
|
---|
82 | double[] estimatedTrainingValues = EstimatedTrainingValues.ToArray(); // cache values
|
---|
83 | var trainingIndizes = Enumerable.Range(ProblemData.TrainingPartition.Start,
|
---|
84 | ProblemData.TrainingPartition.End - ProblemData.TrainingPartition.Start);
|
---|
85 | IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, trainingIndizes);
|
---|
86 | double[] estimatedTestValues = EstimatedTestValues.ToArray(); // cache values
|
---|
87 | IEnumerable<double> originalTestValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TestIndizes);
|
---|
88 |
|
---|
89 | OnlineCalculatorError errorState;
|
---|
90 | double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
|
---|
91 | TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
|
---|
92 | double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
|
---|
93 | TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
|
---|
94 |
|
---|
95 | double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
|
---|
96 | TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;
|
---|
97 | double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
|
---|
98 | TestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;
|
---|
99 |
|
---|
100 | double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
|
---|
101 | TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
|
---|
102 | double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
|
---|
103 | TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
|
---|
104 |
|
---|
105 | double trainingNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
|
---|
106 | TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNMSE : double.NaN;
|
---|
107 | double testNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
|
---|
108 | TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNMSE : double.NaN;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
112 | return new RegressionEnsembleSolution(this, cloner);
|
---|
113 | }
|
---|
114 |
|
---|
115 | public override IEnumerable<double> EstimatedTrainingValues {
|
---|
116 | get {
|
---|
117 | var rows = Enumerable.Range(ProblemData.TrainingPartition.Start, ProblemData.TrainingPartition.End - ProblemData.TrainingPartition.Start);
|
---|
118 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
119 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
120 | .ToList();
|
---|
121 | var rowsEnumerator = rows.GetEnumerator();
|
---|
122 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
123 | int currentRow = rowsEnumerator.Current;
|
---|
124 |
|
---|
125 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
126 | where trainingPartitions == null || !trainingPartitions.ContainsKey(pair.Model) ||
|
---|
127 | (trainingPartitions[pair.Model].Start <= currentRow && currentRow < trainingPartitions[pair.Model].End)
|
---|
128 | select pair.EstimatedValuesEnumerator;
|
---|
129 | yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | public override IEnumerable<double> EstimatedTestValues {
|
---|
135 | get {
|
---|
136 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
137 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndizes).GetEnumerator() })
|
---|
138 | .ToList();
|
---|
139 | var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
|
---|
140 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
141 | int currentRow = rowsEnumerator.Current;
|
---|
142 |
|
---|
143 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
144 | where testPartitions == null || !testPartitions.ContainsKey(pair.Model) ||
|
---|
145 | (testPartitions[pair.Model].Start <= currentRow && currentRow < testPartitions[pair.Model].End)
|
---|
146 | select pair.EstimatedValuesEnumerator;
|
---|
147 |
|
---|
148 | yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
154 | return from xs in GetEstimatedValueVectors(ProblemData.Dataset, rows)
|
---|
155 | select AggregateEstimatedValues(xs);
|
---|
156 | }
|
---|
157 |
|
---|
158 | public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
|
---|
159 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
160 | select model.GetEstimatedValues(dataset, rows).GetEnumerator())
|
---|
161 | .ToList();
|
---|
162 |
|
---|
163 | while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
|
---|
164 | yield return from enumerator in estimatedValuesEnumerators
|
---|
165 | select enumerator.Current;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | private double AggregateEstimatedValues(IEnumerable<double> estimatedValues) {
|
---|
170 | return estimatedValues.Average();
|
---|
171 | }
|
---|
172 |
|
---|
173 | //[Storable]
|
---|
174 | //private string name;
|
---|
175 | //public string Name {
|
---|
176 | // get {
|
---|
177 | // return name;
|
---|
178 | // }
|
---|
179 | // set {
|
---|
180 | // if (value != null && value != name) {
|
---|
181 | // var cancelEventArgs = new CancelEventArgs<string>(value);
|
---|
182 | // OnNameChanging(cancelEventArgs);
|
---|
183 | // if (cancelEventArgs.Cancel == false) {
|
---|
184 | // name = value;
|
---|
185 | // OnNamedChanged(EventArgs.Empty);
|
---|
186 | // }
|
---|
187 | // }
|
---|
188 | // }
|
---|
189 | //}
|
---|
190 |
|
---|
191 | //public bool CanChangeName {
|
---|
192 | // get { return true; }
|
---|
193 | //}
|
---|
194 |
|
---|
195 | //[Storable]
|
---|
196 | //private string description;
|
---|
197 | //public string Description {
|
---|
198 | // get {
|
---|
199 | // return description;
|
---|
200 | // }
|
---|
201 | // set {
|
---|
202 | // if (value != null && value != description) {
|
---|
203 | // description = value;
|
---|
204 | // OnDescriptionChanged(EventArgs.Empty);
|
---|
205 | // }
|
---|
206 | // }
|
---|
207 | //}
|
---|
208 |
|
---|
209 | //public bool CanChangeDescription {
|
---|
210 | // get { return true; }
|
---|
211 | //}
|
---|
212 |
|
---|
213 | //#region events
|
---|
214 | //public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
215 | //private void OnNameChanging(CancelEventArgs<string> cancelEventArgs) {
|
---|
216 | // var listener = NameChanging;
|
---|
217 | // if (listener != null) listener(this, cancelEventArgs);
|
---|
218 | //}
|
---|
219 |
|
---|
220 | //public event EventHandler NameChanged;
|
---|
221 | //private void OnNamedChanged(EventArgs e) {
|
---|
222 | // var listener = NameChanged;
|
---|
223 | // if (listener != null) listener(this, e);
|
---|
224 | //}
|
---|
225 |
|
---|
226 | //public event EventHandler DescriptionChanged;
|
---|
227 | //private void OnDescriptionChanged(EventArgs e) {
|
---|
228 | // var listener = DescriptionChanged;
|
---|
229 | // if (listener != null) listener(this, e);
|
---|
230 | //}
|
---|
231 | // #endregion
|
---|
232 | }
|
---|
233 | }
|
---|