Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolutionBase.cs @ 11303

Last change on this file since 11303 was 8206, checked in by gkronber, 12 years ago

#1847: merged r8084:8205 from trunk into GP move operators branch

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