Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolution.cs @ 6350

Last change on this file since 6350 was 6238, checked in by gkronber, 14 years ago

#1450 adapted views for regression solution to work for ensembles of regression solutions as well.

File size: 9.4 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.DataAnalysis {
31  /// <summary>
32  /// Represents a regression data analysis solution
33  /// </summary>
34  [StorableClass]
35  public class RegressionSolution : DataAnalysisSolution, IRegressionSolution {
36    private const string TrainingMeanSquaredErrorResultName = "Mean squared error (training)";
37    private const string TestMeanSquaredErrorResultName = "Mean squared error (test)";
38    private const string TrainingSquaredCorrelationResultName = "Pearson's R² (training)";
39    private const string TestSquaredCorrelationResultName = "Pearson's R² (test)";
40    private const string TrainingRelativeErrorResultName = "Average relative error (training)";
41    private const string TestRelativeErrorResultName = "Average relative error (test)";
42    private const string TrainingNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (training)";
43    private const string TestNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (test)";
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; }
52      protected set { base.ProblemData = value; }
53    }
54
55    public double TrainingMeanSquaredError {
56      get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
57      private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
58    }
59
60    public double TestMeanSquaredError {
61      get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
62      private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
63    }
64
65    public double TrainingRSquared {
66      get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
67      private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
68    }
69
70    public double TestRSquared {
71      get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
72      private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
73    }
74
75    public double TrainingRelativeError {
76      get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
77      private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
78    }
79
80    public double TestRelativeError {
81      get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
82      private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
83    }
84
85    public double TrainingNormalizedMeanSquaredError {
86      get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }
87      private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }
88    }
89
90    public double TestNormalizedMeanSquaredError {
91      get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }
92      private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }
93    }
94
95
96    [StorableConstructor]
97    protected RegressionSolution(bool deserializing) : base(deserializing) { }
98    protected RegressionSolution(RegressionSolution original, Cloner cloner)
99      : base(original, cloner) {
100    }
101    public RegressionSolution(IRegressionModel model, IRegressionProblemData problemData)
102      : base(model, problemData) {
103      Add(new Result(TrainingMeanSquaredErrorResultName, "Mean of squared errors of the model on the training partition", new DoubleValue()));
104      Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new DoubleValue()));
105      Add(new Result(TrainingSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new DoubleValue()));
106      Add(new Result(TestSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleValue()));
107      Add(new Result(TrainingRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the training partition", new PercentValue()));
108      Add(new Result(TestRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the test partition", new PercentValue()));
109      Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the training partition", new DoubleValue()));
110      Add(new Result(TestNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the test partition", new DoubleValue()));
111
112      RecalculateResults();
113    }
114
115    public override IDeepCloneable Clone(Cloner cloner) {
116      return new RegressionSolution(this, cloner);
117    }
118
119    protected override void OnProblemDataChanged(EventArgs e) {
120      base.OnProblemDataChanged(e);
121      RecalculateResults();
122    }
123    protected override void OnModelChanged(EventArgs e) {
124      base.OnModelChanged(e);
125      RecalculateResults();
126    }
127
128    protected void RecalculateResults() {
129      double[] estimatedTrainingValues = EstimatedTrainingValues.ToArray(); // cache values
130      IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes);
131      double[] estimatedTestValues = EstimatedTestValues.ToArray(); // cache values
132      IEnumerable<double> originalTestValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TestIndizes);
133
134      OnlineCalculatorError errorState;
135      double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
136      TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
137      double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
138      TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
139
140      double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
141      TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;
142      double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
143      TestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;
144
145      double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
146      TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
147      double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
148      TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
149
150      double trainingNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
151      TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNMSE : double.NaN;
152      double testNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
153      TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNMSE : double.NaN;
154    }
155
156    public virtual IEnumerable<double> EstimatedValues {
157      get {
158        return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows));
159      }
160    }
161
162    public virtual IEnumerable<double> EstimatedTrainingValues {
163      get {
164        return GetEstimatedValues(ProblemData.TrainingIndizes);
165      }
166    }
167
168    public virtual IEnumerable<double> EstimatedTestValues {
169      get {
170        return GetEstimatedValues(ProblemData.TestIndizes);
171      }
172    }
173
174    public virtual IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
175      return Model.GetEstimatedValues(ProblemData.Dataset, rows);
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.