1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Optimization;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
29 | [StorableClass]
|
---|
30 | public abstract class RegressionSolutionBase : DataAnalysisSolution, IRegressionSolution {
|
---|
31 | protected const string TrainingMeanSquaredErrorResultName = "Mean squared error (training)";
|
---|
32 | protected const string TestMeanSquaredErrorResultName = "Mean squared error (test)";
|
---|
33 | protected const string TrainingMeanAbsoluteErrorResultName = "Mean absolute error (training)";
|
---|
34 | protected const string TestMeanAbsoluteErrorResultName = "Mean absolute error (test)";
|
---|
35 | protected const string TrainingSquaredCorrelationResultName = "Pearson's R² (training)";
|
---|
36 | protected const string TestSquaredCorrelationResultName = "Pearson's R² (test)";
|
---|
37 | protected const string TrainingRelativeErrorResultName = "Average relative error (training)";
|
---|
38 | protected const string TestRelativeErrorResultName = "Average relative error (test)";
|
---|
39 | protected const string TrainingNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (training)";
|
---|
40 | protected const string TestNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (test)";
|
---|
41 | protected const string TrainingMeanErrorResultName = "Mean error (training)";
|
---|
42 | protected const string TestMeanErrorResultName = "Mean error (test)";
|
---|
43 |
|
---|
44 | protected const string TrainingMeanSquaredErrorResultDescription = "Mean of squared errors of the model on the training partition";
|
---|
45 | protected const string TestMeanSquaredErrorResultDescription = "Mean of squared errors of the model on the test partition";
|
---|
46 | protected const string TrainingMeanAbsoluteErrorResultDescription = "Mean of absolute errors of the model on the training partition";
|
---|
47 | protected const string TestMeanAbsoluteErrorResultDescription = "Mean of absolute errors of the model on the test partition";
|
---|
48 | protected const string TrainingSquaredCorrelationResultDescription = "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition";
|
---|
49 | protected const string TestSquaredCorrelationResultDescription = "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition";
|
---|
50 | protected const string TrainingRelativeErrorResultDescription = "Average of the relative errors of the model output and the actual values on the training partition";
|
---|
51 | protected const string TestRelativeErrorResultDescription = "Average of the relative errors of the model output and the actual values on the test partition";
|
---|
52 | protected const string TrainingNormalizedMeanSquaredErrorResultDescription = "Normalized mean of squared errors of the model on the training partition";
|
---|
53 | protected const string TestNormalizedMeanSquaredErrorResultDescription = "Normalized mean of squared errors of the model on the test partition";
|
---|
54 | protected const string TrainingMeanErrorResultDescription = "Mean of errors of the model on the training partition";
|
---|
55 | protected const string TestMeanErrorResultDescription = "Mean of errors of the model on the test partition";
|
---|
56 |
|
---|
57 | public new IRegressionModel Model {
|
---|
58 | get { return (IRegressionModel)base.Model; }
|
---|
59 | protected set { base.Model = value; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public new IRegressionProblemData ProblemData {
|
---|
63 | get { return (IRegressionProblemData)base.ProblemData; }
|
---|
64 | set { base.ProblemData = value; }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public abstract IEnumerable<double> EstimatedValues { get; }
|
---|
68 | public abstract IEnumerable<double> EstimatedTrainingValues { get; }
|
---|
69 | public abstract IEnumerable<double> EstimatedTestValues { get; }
|
---|
70 | public abstract IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows);
|
---|
71 |
|
---|
72 | #region Results
|
---|
73 | public double TrainingMeanSquaredError {
|
---|
74 | get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
|
---|
75 | private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
|
---|
76 | }
|
---|
77 | public double TestMeanSquaredError {
|
---|
78 | get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
|
---|
79 | private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
|
---|
80 | }
|
---|
81 | public double TrainingMeanAbsoluteError {
|
---|
82 | get { return ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value; }
|
---|
83 | private set { ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value = value; }
|
---|
84 | }
|
---|
85 | public double TestMeanAbsoluteError {
|
---|
86 | get { return ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value; }
|
---|
87 | private set { ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value = value; }
|
---|
88 | }
|
---|
89 | public double TrainingRSquared {
|
---|
90 | get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
|
---|
91 | private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
|
---|
92 | }
|
---|
93 | public double TestRSquared {
|
---|
94 | get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
|
---|
95 | private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
|
---|
96 | }
|
---|
97 | public double TrainingRelativeError {
|
---|
98 | get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
|
---|
99 | private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
|
---|
100 | }
|
---|
101 | public double TestRelativeError {
|
---|
102 | get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
|
---|
103 | private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
|
---|
104 | }
|
---|
105 | public double TrainingNormalizedMeanSquaredError {
|
---|
106 | get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }
|
---|
107 | private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }
|
---|
108 | }
|
---|
109 | public double TestNormalizedMeanSquaredError {
|
---|
110 | get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }
|
---|
111 | private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }
|
---|
112 | }
|
---|
113 | public double TrainingMeanError {
|
---|
114 | get { return ((DoubleValue)this[TrainingMeanErrorResultName].Value).Value; }
|
---|
115 | private set { ((DoubleValue)this[TrainingMeanErrorResultName].Value).Value = value; }
|
---|
116 | }
|
---|
117 | public double TestMeanError {
|
---|
118 | get { return ((DoubleValue)this[TestMeanErrorResultName].Value).Value; }
|
---|
119 | private set { ((DoubleValue)this[TestMeanErrorResultName].Value).Value = value; }
|
---|
120 | }
|
---|
121 | #endregion
|
---|
122 |
|
---|
123 | [StorableConstructor]
|
---|
124 | protected RegressionSolutionBase(bool deserializing) : base(deserializing) { }
|
---|
125 | protected RegressionSolutionBase(RegressionSolutionBase original, Cloner cloner)
|
---|
126 | : base(original, cloner) {
|
---|
127 | }
|
---|
128 | protected RegressionSolutionBase(IRegressionModel model, IRegressionProblemData problemData)
|
---|
129 | : base(model, problemData) {
|
---|
130 | Add(new Result(TrainingMeanSquaredErrorResultName, TrainingMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
131 | Add(new Result(TestMeanSquaredErrorResultName, TestMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
132 | Add(new Result(TrainingMeanAbsoluteErrorResultName, TrainingMeanAbsoluteErrorResultDescription, new DoubleValue()));
|
---|
133 | Add(new Result(TestMeanAbsoluteErrorResultName, TestMeanAbsoluteErrorResultDescription, new DoubleValue()));
|
---|
134 | Add(new Result(TrainingSquaredCorrelationResultName, TrainingSquaredCorrelationResultDescription, new DoubleValue()));
|
---|
135 | Add(new Result(TestSquaredCorrelationResultName, TestSquaredCorrelationResultDescription, new DoubleValue()));
|
---|
136 | Add(new Result(TrainingRelativeErrorResultName, TrainingRelativeErrorResultDescription, new PercentValue()));
|
---|
137 | Add(new Result(TestRelativeErrorResultName, TestRelativeErrorResultDescription, new PercentValue()));
|
---|
138 | Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, TrainingNormalizedMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
139 | Add(new Result(TestNormalizedMeanSquaredErrorResultName, TestNormalizedMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
140 | Add(new Result(TrainingMeanErrorResultName, TrainingMeanErrorResultDescription, new DoubleValue()));
|
---|
141 | Add(new Result(TestMeanErrorResultName, TestMeanErrorResultDescription, new DoubleValue()));
|
---|
142 | }
|
---|
143 |
|
---|
144 | [StorableHook(HookType.AfterDeserialization)]
|
---|
145 | private void AfterDeserialization() {
|
---|
146 | // BackwardsCompatibility3.4
|
---|
147 |
|
---|
148 | #region Backwards compatible code, remove with 3.5
|
---|
149 |
|
---|
150 | if (!ContainsKey(TrainingMeanAbsoluteErrorResultName)) {
|
---|
151 | OnlineCalculatorError errorState;
|
---|
152 | Add(new Result(TrainingMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the training partition", new DoubleValue()));
|
---|
153 | double trainingMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(EstimatedTrainingValues, ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices), out errorState);
|
---|
154 | TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMAE : double.NaN;
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (!ContainsKey(TestMeanAbsoluteErrorResultName)) {
|
---|
158 | OnlineCalculatorError errorState;
|
---|
159 | Add(new Result(TestMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the test partition", new DoubleValue()));
|
---|
160 | double testMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(EstimatedTestValues, ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices), out errorState);
|
---|
161 | TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMAE : double.NaN;
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (!ContainsKey(TrainingMeanErrorResultName)) {
|
---|
165 | OnlineCalculatorError errorState;
|
---|
166 | Add(new Result(TrainingMeanErrorResultName, "Mean of errors of the model on the training partition", new DoubleValue()));
|
---|
167 | double trainingME = OnlineMeanErrorCalculator.Calculate(EstimatedTrainingValues, ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices), out errorState);
|
---|
168 | TrainingMeanError = errorState == OnlineCalculatorError.None ? trainingME : double.NaN;
|
---|
169 | }
|
---|
170 | if (!ContainsKey(TestMeanErrorResultName)) {
|
---|
171 | OnlineCalculatorError errorState;
|
---|
172 | Add(new Result(TestMeanErrorResultName, "Mean of errors of the model on the test partition", new DoubleValue()));
|
---|
173 | double testME = OnlineMeanErrorCalculator.Calculate(EstimatedTestValues, ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices), out errorState);
|
---|
174 | TestMeanError = errorState == OnlineCalculatorError.None ? testME : double.NaN;
|
---|
175 | }
|
---|
176 | #endregion
|
---|
177 | }
|
---|
178 |
|
---|
179 | protected override void RecalculateResults() {
|
---|
180 | CalculateRegressionResults();
|
---|
181 | }
|
---|
182 |
|
---|
183 | protected void CalculateRegressionResults() {
|
---|
184 | IEnumerable<double> estimatedTrainingValues = EstimatedTrainingValues; // cache values
|
---|
185 | IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);
|
---|
186 | IEnumerable<double> estimatedTestValues = EstimatedTestValues; // cache values
|
---|
187 | IEnumerable<double> originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices);
|
---|
188 |
|
---|
189 | OnlineCalculatorError errorState;
|
---|
190 | double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
191 | TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
|
---|
192 | double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
193 | TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
|
---|
194 |
|
---|
195 | double trainingMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
196 | TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMAE : double.NaN;
|
---|
197 | double testMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
198 | TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMAE : double.NaN;
|
---|
199 |
|
---|
200 | double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
201 | TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;
|
---|
202 | double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
203 | TestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;
|
---|
204 |
|
---|
205 | double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
206 | TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
|
---|
207 | double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
208 | TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
|
---|
209 |
|
---|
210 | double trainingNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
211 | TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNMSE : double.NaN;
|
---|
212 | double testNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
213 | TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNMSE : double.NaN;
|
---|
214 |
|
---|
215 | double trainingME = OnlineMeanErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
216 | TrainingMeanError = errorState == OnlineCalculatorError.None ? trainingME : double.NaN;
|
---|
217 | double testME = OnlineMeanErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
218 | TestMeanError = errorState == OnlineCalculatorError.None ? testME : double.NaN;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|