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