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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
31 | /// <summary>
|
---|
32 | /// Abstract base class for regression data analysis solutions
|
---|
33 | /// </summary>
|
---|
34 | [StorableClass]
|
---|
35 | public abstract 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, "", new DoubleValue()));
|
---|
110 | Add(new Result(TestNormalizedMeanSquaredErrorResultName, "", new DoubleValue()));
|
---|
111 |
|
---|
112 | RecalculateResults();
|
---|
113 | }
|
---|
114 |
|
---|
115 | protected override void OnProblemDataChanged(EventArgs e) {
|
---|
116 | base.OnProblemDataChanged(e);
|
---|
117 | RecalculateResults();
|
---|
118 | }
|
---|
119 | protected override void OnModelChanged(EventArgs e) {
|
---|
120 | base.OnModelChanged(e);
|
---|
121 | RecalculateResults();
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected void RecalculateResults() {
|
---|
125 | double[] estimatedTrainingValues = EstimatedTrainingValues.ToArray(); // cache values
|
---|
126 | IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes);
|
---|
127 | double[] estimatedTestValues = EstimatedTestValues.ToArray(); // cache values
|
---|
128 | IEnumerable<double> originalTestValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TestIndizes);
|
---|
129 |
|
---|
130 | OnlineCalculatorError errorState;
|
---|
131 | double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
|
---|
132 | TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
|
---|
133 | double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
|
---|
134 | TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
|
---|
135 |
|
---|
136 | double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
|
---|
137 | TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;
|
---|
138 | double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
|
---|
139 | TestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;
|
---|
140 |
|
---|
141 | double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
|
---|
142 | TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
|
---|
143 | double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
|
---|
144 | TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
|
---|
145 |
|
---|
146 | double trainingNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
|
---|
147 | TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNMSE : double.NaN;
|
---|
148 | double testNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
|
---|
149 | TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNMSE : double.NaN;
|
---|
150 | }
|
---|
151 |
|
---|
152 | public virtual IEnumerable<double> EstimatedValues {
|
---|
153 | get {
|
---|
154 | return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows));
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | public virtual IEnumerable<double> EstimatedTrainingValues {
|
---|
159 | get {
|
---|
160 | return GetEstimatedValues(ProblemData.TrainingIndizes);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | public virtual IEnumerable<double> EstimatedTestValues {
|
---|
165 | get {
|
---|
166 | return GetEstimatedValues(ProblemData.TestIndizes);
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | public virtual IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
171 | return Model.GetEstimatedValues(ProblemData.Dataset, rows);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|