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 |
|
---|
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 | protected override void OnProblemDataChanged(EventArgs e) {
|
---|
102 | base.OnProblemDataChanged(e);
|
---|
103 | RecalculateResults();
|
---|
104 | }
|
---|
105 | protected override void OnModelChanged(EventArgs e) {
|
---|
106 | base.OnModelChanged(e);
|
---|
107 | RecalculateResults();
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected void RecalculateResults() {
|
---|
111 | double[] estimatedTrainingValues = EstimatedTrainingValues.ToArray(); // cache values
|
---|
112 | IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes);
|
---|
113 | double[] estimatedTestValues = EstimatedTestValues.ToArray(); // cache values
|
---|
114 | IEnumerable<double> originalTestValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TestIndizes);
|
---|
115 |
|
---|
116 | double trainingMSE = OnlineMeanSquaredErrorEvaluator.Calculate(estimatedTrainingValues, originalTrainingValues);
|
---|
117 | double testMSE = OnlineMeanSquaredErrorEvaluator.Calculate(estimatedTestValues, originalTestValues);
|
---|
118 | double trainingR2 = OnlinePearsonsRSquaredEvaluator.Calculate(estimatedTrainingValues, originalTrainingValues);
|
---|
119 | double testR2 = OnlinePearsonsRSquaredEvaluator.Calculate(estimatedTestValues, originalTestValues);
|
---|
120 | double trainingRelError = OnlineMeanAbsolutePercentageErrorEvaluator.Calculate(estimatedTrainingValues, originalTrainingValues);
|
---|
121 | double testRelError = OnlineMeanAbsolutePercentageErrorEvaluator.Calculate(estimatedTestValues, originalTestValues);
|
---|
122 |
|
---|
123 | TrainingMeanSquaredError = trainingMSE;
|
---|
124 | TestMeanSquaredError = testMSE;
|
---|
125 | TrainingRSquared = trainingR2;
|
---|
126 | TestRSquared = testR2;
|
---|
127 | TrainingRelativeError = trainingRelError;
|
---|
128 | TestRelativeError = testRelError;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public virtual IEnumerable<double> EstimatedValues {
|
---|
132 | get {
|
---|
133 | return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows));
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | public virtual IEnumerable<double> EstimatedTrainingValues {
|
---|
138 | get {
|
---|
139 | return GetEstimatedValues(ProblemData.TrainingIndizes);
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | public virtual IEnumerable<double> EstimatedTestValues {
|
---|
144 | get {
|
---|
145 | return GetEstimatedValues(ProblemData.TestIndizes);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public virtual IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
150 | return Model.GetEstimatedValues(ProblemData.Dataset, rows);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|