Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis SolutionEnsembles/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolution.cs @ 5816

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

#1450 Added preliminary implementation for solution ensemble support.

File size: 7.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
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; }
50      protected set { base.ProblemData = value; }
51    }
52
53    public double TrainingMeanSquaredError {
54      get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
55      private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
56    }
57
58    public double TestMeanSquaredError {
59      get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
60      private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
61    }
62
63    public double TrainingRSquared {
64      get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
65      private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
66    }
67
68    public double TestRSquared {
69      get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
70      private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
71    }
72
73    public double TrainingRelativeError {
74      get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
75      private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
76    }
77
78    public double TestRelativeError {
79      get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
80      private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
81    }
82
83
84    [StorableConstructor]
85    protected RegressionSolution(bool deserializing) : base(deserializing) { }
86    protected RegressionSolution(RegressionSolution original, Cloner cloner)
87      : base(original, cloner) {
88    }
89    public RegressionSolution(IRegressionModel model, IRegressionProblemData problemData)
90      : base(model, problemData) {
91      Add(new Result(TrainingMeanSquaredErrorResultName, "Mean of squared errors of the model on the training partition", new DoubleValue()));
92      Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new DoubleValue()));
93      Add(new Result(TrainingSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new DoubleValue()));
94      Add(new Result(TestSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleValue()));
95      Add(new Result(TrainingRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the training partition", new PercentValue()));
96      Add(new Result(TestRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the test partition", new PercentValue()));
97
98      RecalculateResults();
99    }
100
101    public override IDeepCloneable Clone(Cloner cloner) {
102      return new RegressionSolution(this, cloner);
103    }
104
105    protected override void OnProblemDataChanged(EventArgs e) {
106      base.OnProblemDataChanged(e);
107      RecalculateResults();
108    }
109    protected override void OnModelChanged(EventArgs e) {
110      base.OnModelChanged(e);
111      RecalculateResults();
112    }
113
114    protected void RecalculateResults() {
115      double[] estimatedTrainingValues = EstimatedTrainingValues.ToArray(); // cache values
116      IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes);
117      double[] estimatedTestValues = EstimatedTestValues.ToArray(); // cache values
118      IEnumerable<double> originalTestValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TestIndizes);
119
120      double trainingMSE = OnlineMeanSquaredErrorEvaluator.Calculate(estimatedTrainingValues, originalTrainingValues);
121      double testMSE = OnlineMeanSquaredErrorEvaluator.Calculate(estimatedTestValues, originalTestValues);
122      double trainingR2 = OnlinePearsonsRSquaredEvaluator.Calculate(estimatedTrainingValues, originalTrainingValues);
123      double testR2 = OnlinePearsonsRSquaredEvaluator.Calculate(estimatedTestValues, originalTestValues);
124      double trainingRelError = OnlineMeanAbsolutePercentageErrorEvaluator.Calculate(estimatedTrainingValues, originalTrainingValues);
125      double testRelError = OnlineMeanAbsolutePercentageErrorEvaluator.Calculate(estimatedTestValues, originalTestValues);
126
127      TrainingMeanSquaredError = trainingMSE;
128      TestMeanSquaredError = testMSE;
129      TrainingRSquared = trainingR2;
130      TestRSquared = testR2;
131      TrainingRelativeError = trainingRelError;
132      TestRelativeError = testRelError;
133    }
134
135    public virtual IEnumerable<double> EstimatedValues {
136      get {
137        return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows));
138      }
139    }
140
141    public virtual IEnumerable<double> EstimatedTrainingValues {
142      get {
143        return GetEstimatedValues(ProblemData.TrainingIndizes);
144      }
145    }
146
147    public virtual IEnumerable<double> EstimatedTestValues {
148      get {
149        return GetEstimatedValues(ProblemData.TestIndizes);
150      }
151    }
152
153    public virtual IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
154      return Model.GetEstimatedValues(ProblemData.Dataset, rows);
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.