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