Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SimulationCore/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolutionBase.cs @ 6619

Last change on this file since 6619 was 6589, checked in by mkommend, 13 years ago

#1600: Adapted classification solutions to the same design as used by regression solutions.

File size: 8.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 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)";
34    private const string TrainingSquaredCorrelationResultName = "Pearson's R² (training)";
35    private const string TestSquaredCorrelationResultName = "Pearson's R² (test)";
36    private const string TrainingRelativeErrorResultName = "Average relative error (training)";
37    private const string TestRelativeErrorResultName = "Average relative error (test)";
38    private const string TrainingNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (training)";
39    private const string TestNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (test)";
40
41    public new IRegressionModel Model {
42      get { return (IRegressionModel)base.Model; }
43      protected set { base.Model = value; }
44    }
45
46    public new IRegressionProblemData ProblemData {
47      get { return (IRegressionProblemData)base.ProblemData; }
48      protected set { base.ProblemData = value; }
49    }
50
51    public abstract IEnumerable<double> EstimatedValues { get; }
52    public abstract IEnumerable<double> EstimatedTrainingValues { get; }
53    public abstract IEnumerable<double> EstimatedTestValues { get; }
54    public abstract IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows);
55
56    #region Results
57    public double TrainingMeanSquaredError {
58      get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
59      private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
60    }
61    public double TestMeanSquaredError {
62      get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
63      private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
64    }
65    public double TrainingRSquared {
66      get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
67      private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
68    }
69    public double TestRSquared {
70      get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
71      private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
72    }
73    public double TrainingRelativeError {
74      get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
75      private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
76    }
77    public double TestRelativeError {
78      get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
79      private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
80    }
81    public double TrainingNormalizedMeanSquaredError {
82      get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }
83      private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }
84    }
85    public double TestNormalizedMeanSquaredError {
86      get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }
87      private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }
88    }
89    #endregion
90
91    [StorableConstructor]
92    protected RegressionSolutionBase(bool deserializing) : base(deserializing) { }
93    protected RegressionSolutionBase(RegressionSolutionBase original, Cloner cloner)
94      : base(original, cloner) {
95    }
96    protected RegressionSolutionBase(IRegressionModel model, IRegressionProblemData problemData)
97      : base(model, problemData) {
98      Add(new Result(TrainingMeanSquaredErrorResultName, "Mean of squared errors of the model on the training partition", new DoubleValue()));
99      Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new DoubleValue()));
100      Add(new Result(TrainingSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new DoubleValue()));
101      Add(new Result(TestSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleValue()));
102      Add(new Result(TrainingRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the training partition", new PercentValue()));
103      Add(new Result(TestRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the test partition", new PercentValue()));
104      Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the training partition", new DoubleValue()));
105      Add(new Result(TestNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the test partition", new DoubleValue()));
106    }
107
108    protected void CalculateResults() {
109      double[] estimatedTrainingValues = EstimatedTrainingValues.ToArray(); // cache values
110      double[] originalTrainingValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToArray();
111      double[] estimatedTestValues = EstimatedTestValues.ToArray(); // cache values
112      double[] originalTestValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TestIndizes).ToArray();
113
114      OnlineCalculatorError errorState;
115      double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
116      TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
117      double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
118      TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
119
120      double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
121      TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;
122      double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
123      TestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;
124
125      double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
126      TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
127      double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
128      TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
129
130      double trainingNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
131      TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNMSE : double.NaN;
132      double testNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
133      TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNMSE : double.NaN;
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.