Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolutionBase.cs @ 16628

Last change on this file since 16628 was 16628, checked in by gkronber, 5 years ago

#2971: made branch compile with current version of trunk

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