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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using System;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
34 | /// <summary>
|
---|
35 | /// Abstract base class for regression data analysis solutions
|
---|
36 | /// </summary>
|
---|
37 | [StorableClass]
|
---|
38 | public abstract class RegressionSolution : DataAnalysisSolution, IRegressionSolution {
|
---|
39 | private const string TrainingMeanSquaredErrorResultName = "Mean squared error (training)";
|
---|
40 | private const string TestMeanSquaredErrorResultName = "Mean squared error (test)";
|
---|
41 | private const string TrainingSquaredCorrelationResultName = "Pearson's R² (training)";
|
---|
42 | private const string TestSquaredCorrelationResultName = "Pearson's R² (test)";
|
---|
43 | private const string TrainingRelativeErrorResultName = "Average relative error (training)";
|
---|
44 | private const string TestRelativeErrorResultName = "Average relative error (test)";
|
---|
45 |
|
---|
46 | public new IRegressionModel Model {
|
---|
47 | get { return (IRegressionModel)base.Model; }
|
---|
48 | protected set { base.Model = value; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public new IRegressionProblemData ProblemData {
|
---|
52 | get { return (IRegressionProblemData)base.ProblemData; }
|
---|
53 | protected set { base.ProblemData = value; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public double TrainingMeanSquaredError {
|
---|
57 | get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
|
---|
58 | private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public double TestMeanSquaredError {
|
---|
62 | get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
|
---|
63 | private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public double TrainingRSquared {
|
---|
67 | get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
|
---|
68 | private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public double TestRSquared {
|
---|
72 | get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
|
---|
73 | private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public double TrainingRelativeError {
|
---|
77 | get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
|
---|
78 | private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public double TestRelativeError {
|
---|
82 | get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
|
---|
83 | private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | [StorableConstructor]
|
---|
88 | protected RegressionSolution(bool deserializing) : base(deserializing) { }
|
---|
89 | protected RegressionSolution(RegressionSolution original, Cloner cloner)
|
---|
90 | : base(original, cloner) {
|
---|
91 | }
|
---|
92 | public RegressionSolution(IRegressionModel model, IRegressionProblemData problemData)
|
---|
93 | : base(model, problemData) {
|
---|
94 | Add(new Result(TrainingMeanSquaredErrorResultName, "Mean of squared errors of the model on the training partition", new DoubleValue()));
|
---|
95 | Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new DoubleValue()));
|
---|
96 | Add(new Result(TrainingSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new DoubleValue()));
|
---|
97 | Add(new Result(TestSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleValue()));
|
---|
98 | Add(new Result(TrainingRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the training partition", new PercentValue()));
|
---|
99 | Add(new Result(TestRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the test partition", new PercentValue()));
|
---|
100 |
|
---|
101 | RecalculateResults();
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected override void OnProblemDataChanged(EventArgs e) {
|
---|
105 | base.OnProblemDataChanged(e);
|
---|
106 | RecalculateResults();
|
---|
107 | }
|
---|
108 | protected override void OnModelChanged(EventArgs e) {
|
---|
109 | base.OnModelChanged(e);
|
---|
110 | RecalculateResults();
|
---|
111 | }
|
---|
112 |
|
---|
113 | private void RecalculateResults() {
|
---|
114 | double[] estimatedTrainingValues = EstimatedTrainingValues.ToArray(); // cache values
|
---|
115 | IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes);
|
---|
116 | double[] estimatedTestValues = EstimatedTestValues.ToArray(); // cache values
|
---|
117 | IEnumerable<double> originalTestValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TestIndizes);
|
---|
118 |
|
---|
119 | double trainingMSE = OnlineMeanSquaredErrorEvaluator.Calculate(estimatedTrainingValues, originalTrainingValues);
|
---|
120 | double testMSE = OnlineMeanSquaredErrorEvaluator.Calculate(estimatedTestValues, originalTestValues);
|
---|
121 | double trainingR2 = OnlinePearsonsRSquaredEvaluator.Calculate(estimatedTrainingValues, originalTrainingValues);
|
---|
122 | double testR2 = OnlinePearsonsRSquaredEvaluator.Calculate(estimatedTestValues, originalTestValues);
|
---|
123 | double trainingRelError = OnlineMeanAbsolutePercentageErrorEvaluator.Calculate(estimatedTrainingValues, originalTrainingValues);
|
---|
124 | double testRelError = OnlineMeanAbsolutePercentageErrorEvaluator.Calculate(estimatedTestValues, originalTestValues);
|
---|
125 |
|
---|
126 | TrainingMeanSquaredError = trainingMSE;
|
---|
127 | TestMeanSquaredError = testMSE;
|
---|
128 | TrainingRSquared = trainingR2;
|
---|
129 | TestRSquared = testR2;
|
---|
130 | TrainingRelativeError = trainingRelError;
|
---|
131 | TestRelativeError = testRelError;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public virtual IEnumerable<double> EstimatedValues {
|
---|
135 | get {
|
---|
136 | return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows));
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | public virtual IEnumerable<double> EstimatedTrainingValues {
|
---|
141 | get {
|
---|
142 | return GetEstimatedValues(ProblemData.TrainingIndizes);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | public virtual IEnumerable<double> EstimatedTestValues {
|
---|
147 | get {
|
---|
148 | return GetEstimatedValues(ProblemData.TestIndizes);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | public virtual IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
153 | return Model.GetEstimatedValues(ProblemData.Dataset, rows);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|