[6588] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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;
|
---|
[16565] | 27 | using HEAL.Attic;
|
---|
[6588] | 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[16565] | 30 | [StorableType("03116F5E-ABBF-4966-9428-E3DC67D1D03D")]
|
---|
[6588] | 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; }
|
---|
[16244] | 72 | set {
|
---|
| 73 | if (value == null) throw new ArgumentNullException("The problemData must not be null.");
|
---|
| 74 | string errorMessage = string.Empty;
|
---|
| 75 | if (!Model.IsProblemDataCompatible(value, out errorMessage)) throw new ArgumentException(errorMessage);
|
---|
| 76 |
|
---|
| 77 | base.ProblemData = value;
|
---|
| 78 | }
|
---|
[6588] | 79 | }
|
---|
| 80 |
|
---|
| 81 | public abstract IEnumerable<double> EstimatedValues { get; }
|
---|
| 82 | public abstract IEnumerable<double> EstimatedTrainingValues { get; }
|
---|
| 83 | public abstract IEnumerable<double> EstimatedTestValues { get; }
|
---|
| 84 | public abstract IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows);
|
---|
| 85 |
|
---|
| 86 | #region Results
|
---|
| 87 | public double TrainingMeanSquaredError {
|
---|
| 88 | get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
|
---|
| 89 | private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
|
---|
| 90 | }
|
---|
| 91 | public double TestMeanSquaredError {
|
---|
| 92 | get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
|
---|
| 93 | private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
|
---|
| 94 | }
|
---|
[6643] | 95 | public double TrainingMeanAbsoluteError {
|
---|
| 96 | get { return ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value; }
|
---|
| 97 | private set { ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value = value; }
|
---|
| 98 | }
|
---|
| 99 | public double TestMeanAbsoluteError {
|
---|
| 100 | get { return ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value; }
|
---|
| 101 | private set { ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value = value; }
|
---|
| 102 | }
|
---|
[6588] | 103 | public double TrainingRSquared {
|
---|
| 104 | get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
|
---|
| 105 | private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
|
---|
| 106 | }
|
---|
| 107 | public double TestRSquared {
|
---|
| 108 | get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
|
---|
| 109 | private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
|
---|
| 110 | }
|
---|
| 111 | public double TrainingRelativeError {
|
---|
| 112 | get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
|
---|
| 113 | private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
|
---|
| 114 | }
|
---|
| 115 | public double TestRelativeError {
|
---|
| 116 | get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
|
---|
| 117 | private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
|
---|
| 118 | }
|
---|
| 119 | public double TrainingNormalizedMeanSquaredError {
|
---|
| 120 | get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }
|
---|
| 121 | private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }
|
---|
| 122 | }
|
---|
| 123 | public double TestNormalizedMeanSquaredError {
|
---|
| 124 | get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }
|
---|
| 125 | private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }
|
---|
| 126 | }
|
---|
[12581] | 127 | public double TrainingRootMeanSquaredError {
|
---|
| 128 | get { return ((DoubleValue)this[TrainingRootMeanSquaredErrorResultName].Value).Value; }
|
---|
| 129 | private set { ((DoubleValue)this[TrainingRootMeanSquaredErrorResultName].Value).Value = value; }
|
---|
[7272] | 130 | }
|
---|
[12581] | 131 | public double TestRootMeanSquaredError {
|
---|
| 132 | get { return ((DoubleValue)this[TestRootMeanSquaredErrorResultName].Value).Value; }
|
---|
| 133 | private set { ((DoubleValue)this[TestRootMeanSquaredErrorResultName].Value).Value = value; }
|
---|
[7272] | 134 | }
|
---|
[12581] | 135 |
|
---|
| 136 | // BackwardsCompatibility3.3
|
---|
| 137 | #region Backwards compatible code, remove with 3.5
|
---|
| 138 | private double TrainingMeanError {
|
---|
| 139 | get {
|
---|
| 140 | if (!ContainsKey(TrainingMeanErrorResultName)) return double.NaN;
|
---|
| 141 | return ((DoubleValue)this[TrainingMeanErrorResultName].Value).Value;
|
---|
| 142 | }
|
---|
| 143 | set {
|
---|
| 144 | if (ContainsKey(TrainingMeanErrorResultName))
|
---|
| 145 | ((DoubleValue)this[TrainingMeanErrorResultName].Value).Value = value;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | private double TestMeanError {
|
---|
| 149 | get {
|
---|
| 150 | if (!ContainsKey(TestMeanErrorResultName)) return double.NaN;
|
---|
| 151 | return ((DoubleValue)this[TestMeanErrorResultName].Value).Value;
|
---|
| 152 | }
|
---|
| 153 | set {
|
---|
| 154 | if (ContainsKey(TestMeanErrorResultName))
|
---|
| 155 | ((DoubleValue)this[TestMeanErrorResultName].Value).Value = value;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
[6588] | 158 | #endregion
|
---|
[12581] | 159 | #endregion
|
---|
[6588] | 160 |
|
---|
| 161 | [StorableConstructor]
|
---|
[16565] | 162 | protected RegressionSolutionBase(StorableConstructorFlag _) : base(_) { }
|
---|
[6588] | 163 | protected RegressionSolutionBase(RegressionSolutionBase original, Cloner cloner)
|
---|
| 164 | : base(original, cloner) {
|
---|
| 165 | }
|
---|
| 166 | protected RegressionSolutionBase(IRegressionModel model, IRegressionProblemData problemData)
|
---|
| 167 | : base(model, problemData) {
|
---|
[8798] | 168 | Add(new Result(TrainingMeanSquaredErrorResultName, TrainingMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
| 169 | Add(new Result(TestMeanSquaredErrorResultName, TestMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
| 170 | Add(new Result(TrainingMeanAbsoluteErrorResultName, TrainingMeanAbsoluteErrorResultDescription, new DoubleValue()));
|
---|
| 171 | Add(new Result(TestMeanAbsoluteErrorResultName, TestMeanAbsoluteErrorResultDescription, new DoubleValue()));
|
---|
| 172 | Add(new Result(TrainingSquaredCorrelationResultName, TrainingSquaredCorrelationResultDescription, new DoubleValue()));
|
---|
| 173 | Add(new Result(TestSquaredCorrelationResultName, TestSquaredCorrelationResultDescription, new DoubleValue()));
|
---|
| 174 | Add(new Result(TrainingRelativeErrorResultName, TrainingRelativeErrorResultDescription, new PercentValue()));
|
---|
| 175 | Add(new Result(TestRelativeErrorResultName, TestRelativeErrorResultDescription, new PercentValue()));
|
---|
| 176 | Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, TrainingNormalizedMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
| 177 | Add(new Result(TestNormalizedMeanSquaredErrorResultName, TestNormalizedMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
[12581] | 178 | Add(new Result(TrainingRootMeanSquaredErrorResultName, TrainingRootMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
| 179 | Add(new Result(TestRootMeanSquaredErrorResultName, TestRootMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
[6588] | 180 | }
|
---|
| 181 |
|
---|
[6643] | 182 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 183 | private void AfterDeserialization() {
|
---|
[14290] | 184 | if (string.IsNullOrEmpty(Model.TargetVariable))
|
---|
| 185 | Model.TargetVariable = this.ProblemData.TargetVariable;
|
---|
| 186 |
|
---|
[6643] | 187 | // BackwardsCompatibility3.4
|
---|
| 188 | #region Backwards compatible code, remove with 3.5
|
---|
| 189 | if (!ContainsKey(TrainingMeanAbsoluteErrorResultName)) {
|
---|
| 190 | OnlineCalculatorError errorState;
|
---|
| 191 | Add(new Result(TrainingMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the training partition", new DoubleValue()));
|
---|
[8139] | 192 | double trainingMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(EstimatedTrainingValues, ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices), out errorState);
|
---|
[6643] | 193 | TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMAE : double.NaN;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | if (!ContainsKey(TestMeanAbsoluteErrorResultName)) {
|
---|
| 197 | OnlineCalculatorError errorState;
|
---|
| 198 | Add(new Result(TestMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the test partition", new DoubleValue()));
|
---|
[8139] | 199 | double testMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(EstimatedTestValues, ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices), out errorState);
|
---|
[6643] | 200 | TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMAE : double.NaN;
|
---|
| 201 | }
|
---|
[7272] | 202 |
|
---|
[12581] | 203 | if (!ContainsKey(TrainingRootMeanSquaredErrorResultName)) {
|
---|
[7272] | 204 | OnlineCalculatorError errorState;
|
---|
[12581] | 205 | Add(new Result(TrainingRootMeanSquaredErrorResultName, TrainingRootMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
| 206 | double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(EstimatedTrainingValues, ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices), out errorState);
|
---|
| 207 | TrainingRootMeanSquaredError = errorState == OnlineCalculatorError.None ? Math.Sqrt(trainingMSE) : double.NaN;
|
---|
[7272] | 208 | }
|
---|
[12581] | 209 |
|
---|
| 210 | if (!ContainsKey(TestRootMeanSquaredErrorResultName)) {
|
---|
[7272] | 211 | OnlineCalculatorError errorState;
|
---|
[12581] | 212 | Add(new Result(TestRootMeanSquaredErrorResultName, TestRootMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
| 213 | double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(EstimatedTestValues, ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices), out errorState);
|
---|
| 214 | TestRootMeanSquaredError = errorState == OnlineCalculatorError.None ? Math.Sqrt(testMSE) : double.NaN;
|
---|
[7272] | 215 | }
|
---|
[6643] | 216 | #endregion
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[8723] | 219 | protected override void RecalculateResults() {
|
---|
| 220 | CalculateRegressionResults();
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | protected void CalculateRegressionResults() {
|
---|
[7735] | 224 | IEnumerable<double> estimatedTrainingValues = EstimatedTrainingValues; // cache values
|
---|
[8139] | 225 | IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);
|
---|
[7735] | 226 | IEnumerable<double> estimatedTestValues = EstimatedTestValues; // cache values
|
---|
[8139] | 227 | IEnumerable<double> originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices);
|
---|
[6588] | 228 |
|
---|
| 229 | OnlineCalculatorError errorState;
|
---|
[6961] | 230 | double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
[6588] | 231 | TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
|
---|
[6961] | 232 | double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
[6588] | 233 | TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
|
---|
| 234 |
|
---|
[6961] | 235 | double trainingMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
[6643] | 236 | TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMAE : double.NaN;
|
---|
[6961] | 237 | double testMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
[6643] | 238 | TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMAE : double.NaN;
|
---|
| 239 |
|
---|
[12641] | 240 | double trainingR = OnlinePearsonsRCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
[14290] | 241 | TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR * trainingR : double.NaN;
|
---|
[12641] | 242 | double testR = OnlinePearsonsRCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
[14290] | 243 | TestRSquared = errorState == OnlineCalculatorError.None ? testR * testR : double.NaN;
|
---|
[6588] | 244 |
|
---|
[6961] | 245 | double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
[6588] | 246 | TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
|
---|
[6961] | 247 | double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
[6588] | 248 | TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
|
---|
| 249 |
|
---|
[6961] | 250 | double trainingNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
[6588] | 251 | TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNMSE : double.NaN;
|
---|
[6961] | 252 | double testNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
[6588] | 253 | TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNMSE : double.NaN;
|
---|
[7272] | 254 |
|
---|
[12581] | 255 | TrainingRootMeanSquaredError = Math.Sqrt(TrainingMeanSquaredError);
|
---|
| 256 | TestRootMeanSquaredError = Math.Sqrt(TestMeanSquaredError);
|
---|
| 257 |
|
---|
| 258 | // BackwardsCompatibility3.3
|
---|
| 259 | #region Backwards compatible code, remove with 3.5
|
---|
| 260 | if (ContainsKey(TrainingMeanErrorResultName)) {
|
---|
| 261 | double trainingME = OnlineMeanErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
| 262 | TrainingMeanError = errorState == OnlineCalculatorError.None ? trainingME : double.NaN;
|
---|
| 263 | }
|
---|
| 264 | if (ContainsKey(TestMeanErrorResultName)) {
|
---|
| 265 | double testME = OnlineMeanErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
| 266 | TestMeanError = errorState == OnlineCalculatorError.None ? testME : double.NaN;
|
---|
| 267 | }
|
---|
| 268 | #endregion
|
---|
[6588] | 269 | }
|
---|
| 270 | }
|
---|
| 271 | }
|
---|