Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolutionBase.cs @ 7317

Last change on this file since 7317 was 7270, checked in by spimming, 13 years ago

#1680:

  • merged changes from trunk into branch
File size: 11.3 KB
RevLine 
[6588]1#region License Information
2/* HeuristicLab
[7270]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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace 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)";
42
43    public new IRegressionModel Model {
44      get { return (IRegressionModel)base.Model; }
45      protected set { base.Model = value; }
46    }
47
48    public new IRegressionProblemData ProblemData {
49      get { return (IRegressionProblemData)base.ProblemData; }
[6653]50      set { base.ProblemData = value; }
[6588]51    }
52
53    public abstract IEnumerable<double> EstimatedValues { get; }
54    public abstract IEnumerable<double> EstimatedTrainingValues { get; }
55    public abstract IEnumerable<double> EstimatedTestValues { get; }
56    public abstract IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows);
57
58    #region Results
59    public double TrainingMeanSquaredError {
60      get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
61      private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
62    }
63    public double TestMeanSquaredError {
64      get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
65      private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
66    }
[6643]67    public double TrainingMeanAbsoluteError {
68      get { return ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value; }
69      private set { ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value = value; }
70    }
71    public double TestMeanAbsoluteError {
72      get { return ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value; }
73      private set { ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value = value; }
74    }
[6588]75    public double TrainingRSquared {
76      get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
77      private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
78    }
79    public double TestRSquared {
80      get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
81      private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
82    }
83    public double TrainingRelativeError {
84      get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
85      private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
86    }
87    public double TestRelativeError {
88      get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
89      private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
90    }
91    public double TrainingNormalizedMeanSquaredError {
92      get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }
93      private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }
94    }
95    public double TestNormalizedMeanSquaredError {
96      get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }
97      private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }
98    }
99    #endregion
100
101    [StorableConstructor]
102    protected RegressionSolutionBase(bool deserializing) : base(deserializing) { }
103    protected RegressionSolutionBase(RegressionSolutionBase original, Cloner cloner)
104      : base(original, cloner) {
105    }
106    protected RegressionSolutionBase(IRegressionModel model, IRegressionProblemData problemData)
107      : base(model, problemData) {
108      Add(new Result(TrainingMeanSquaredErrorResultName, "Mean of squared errors of the model on the training partition", new DoubleValue()));
109      Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new DoubleValue()));
[6643]110      Add(new Result(TrainingMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the training partition", new DoubleValue()));
111      Add(new Result(TestMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the test partition", new DoubleValue()));
[6588]112      Add(new Result(TrainingSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new DoubleValue()));
113      Add(new Result(TestSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleValue()));
114      Add(new Result(TrainingRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the training partition", new PercentValue()));
115      Add(new Result(TestRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the test partition", new PercentValue()));
116      Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the training partition", new DoubleValue()));
117      Add(new Result(TestNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the test partition", new DoubleValue()));
118    }
119
[6643]120    [StorableHook(HookType.AfterDeserialization)]
121    private void AfterDeserialization() {
122      // BackwardsCompatibility3.4
123
124      #region Backwards compatible code, remove with 3.5
125
126      if (!ContainsKey(TrainingMeanAbsoluteErrorResultName)) {
127        OnlineCalculatorError errorState;
128        Add(new Result(TrainingMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the training partition", new DoubleValue()));
[6740]129        double trainingMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(EstimatedTrainingValues, ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes), out errorState);
[6643]130        TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMAE : double.NaN;
131      }
132
133      if (!ContainsKey(TestMeanAbsoluteErrorResultName)) {
134        OnlineCalculatorError errorState;
135        Add(new Result(TestMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the test partition", new DoubleValue()));
[6740]136        double testMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(EstimatedTestValues, ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndizes), out errorState);
[6643]137        TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMAE : double.NaN;
138      }
139      #endregion
140    }
141
[6588]142    protected void CalculateResults() {
143      double[] estimatedTrainingValues = EstimatedTrainingValues.ToArray(); // cache values
[6740]144      double[] originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToArray();
[6588]145      double[] estimatedTestValues = EstimatedTestValues.ToArray(); // cache values
[6740]146      double[] originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndizes).ToArray();
[6588]147
148      OnlineCalculatorError errorState;
[6961]149      double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
[6588]150      TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
[6961]151      double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
[6588]152      TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
153
[6961]154      double trainingMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
[6643]155      TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMAE : double.NaN;
[6961]156      double testMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
[6643]157      TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMAE : double.NaN;
158
[6961]159      double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
[6588]160      TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;
[6961]161      double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
[6588]162      TestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;
163
[6961]164      double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
[6588]165      TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
[6961]166      double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
[6588]167      TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
168
[6961]169      double trainingNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
[6588]170      TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNMSE : double.NaN;
[6961]171      double testNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
[6588]172      TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNMSE : double.NaN;
173    }
174  }
175}
Note: See TracBrowser for help on using the repository browser.