1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Optimization;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HeuristicLab.Problems.DataAnalysis;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Problems.GeneticProgramming.GlucosePrediction {
|
---|
12 | [StorableClass]
|
---|
13 | [Item("Solution", "")]
|
---|
14 | // almost a complete copy of RegressionSolutionBase and RegressionSolution
|
---|
15 | // only change: skipping missing values in the target
|
---|
16 | public sealed class Solution : DataAnalysisSolution, IRegressionSolution {
|
---|
17 | private const string TrainingMeanSquaredErrorResultName = "Mean squared error (training)";
|
---|
18 | private const string TestMeanSquaredErrorResultName = "Mean squared error (test)";
|
---|
19 | private const string TrainingMeanAbsoluteErrorResultName = "Mean absolute error (training)";
|
---|
20 | private const string TestMeanAbsoluteErrorResultName = "Mean absolute error (test)";
|
---|
21 | private const string TrainingSquaredCorrelationResultName = "Pearson's R² (training)";
|
---|
22 | private const string TestSquaredCorrelationResultName = "Pearson's R² (test)";
|
---|
23 | private const string TrainingRelativeErrorResultName = "Average relative error (training)";
|
---|
24 | private const string TestRelativeErrorResultName = "Average relative error (test)";
|
---|
25 | private const string TrainingNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (training)";
|
---|
26 | private const string TestNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (test)";
|
---|
27 | private const string TrainingRootMeanSquaredErrorResultName = "Root mean squared error (training)";
|
---|
28 | private const string TestRootMeanSquaredErrorResultName = "Root mean squared error (test)";
|
---|
29 |
|
---|
30 | private const string TrainingMeanSquaredErrorResultDescription = "Mean of squared errors of the model on the training partition";
|
---|
31 | private const string TestMeanSquaredErrorResultDescription = "Mean of squared errors of the model on the test partition";
|
---|
32 | private const string TrainingMeanAbsoluteErrorResultDescription = "Mean of absolute errors of the model on the training partition";
|
---|
33 | private const string TestMeanAbsoluteErrorResultDescription = "Mean of absolute errors of the model on the test partition";
|
---|
34 | private const string TrainingSquaredCorrelationResultDescription = "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition";
|
---|
35 | private const string TestSquaredCorrelationResultDescription = "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition";
|
---|
36 | private const string TrainingRelativeErrorResultDescription = "Average of the relative errors of the model output and the actual values on the training partition";
|
---|
37 | private const string TestRelativeErrorResultDescription = "Average of the relative errors of the model output and the actual values on the test partition";
|
---|
38 | private const string TrainingNormalizedMeanSquaredErrorResultDescription = "Normalized mean of squared errors of the model on the training partition";
|
---|
39 | private const string TestNormalizedMeanSquaredErrorResultDescription = "Normalized mean of squared errors of the model on the test partition";
|
---|
40 | private const string TrainingRootMeanSquaredErrorResultDescription = "Root mean of squared errors of the model on the training partition";
|
---|
41 | private const string TestRootMeanSquaredErrorResultDescription = "Root mean of squared errors of the model on the test partition";
|
---|
42 |
|
---|
43 | public Solution(bool deserializing)
|
---|
44 | : base(deserializing) {
|
---|
45 | }
|
---|
46 |
|
---|
47 | public Solution(Solution original, Cloner cloner)
|
---|
48 | : base(original, cloner) {
|
---|
49 | }
|
---|
50 |
|
---|
51 | public Solution(IRegressionModel model, IRegressionProblemData problemData)
|
---|
52 | : base(model, problemData) {
|
---|
53 | Add(new Result(TrainingMeanSquaredErrorResultName, TrainingMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
54 | Add(new Result(TestMeanSquaredErrorResultName, TestMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
55 | Add(new Result(TrainingMeanAbsoluteErrorResultName, TrainingMeanAbsoluteErrorResultDescription, new DoubleValue()));
|
---|
56 | Add(new Result(TestMeanAbsoluteErrorResultName, TestMeanAbsoluteErrorResultDescription, new DoubleValue()));
|
---|
57 | Add(new Result(TrainingSquaredCorrelationResultName, TrainingSquaredCorrelationResultDescription, new DoubleValue()));
|
---|
58 | Add(new Result(TestSquaredCorrelationResultName, TestSquaredCorrelationResultDescription, new DoubleValue()));
|
---|
59 | Add(new Result(TrainingRelativeErrorResultName, TrainingRelativeErrorResultDescription, new PercentValue()));
|
---|
60 | Add(new Result(TestRelativeErrorResultName, TestRelativeErrorResultDescription, new PercentValue()));
|
---|
61 | Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, TrainingNormalizedMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
62 | Add(new Result(TestNormalizedMeanSquaredErrorResultName, TestNormalizedMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
63 | Add(new Result(TrainingRootMeanSquaredErrorResultName, TrainingRootMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
64 | Add(new Result(TestRootMeanSquaredErrorResultName, TestRootMeanSquaredErrorResultDescription, new DoubleValue()));
|
---|
65 | CalculateRegressionResults();
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected override void RecalculateResults() {
|
---|
69 | CalculateRegressionResults();
|
---|
70 | }
|
---|
71 |
|
---|
72 | private void CalculateRegressionResults() {
|
---|
73 | IEnumerable<double> estimatedTrainingValues = EstimatedTrainingValues; // cache values
|
---|
74 | IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);
|
---|
75 | IEnumerable<double> estimatedTestValues = EstimatedTestValues; // cache values
|
---|
76 | IEnumerable<double> originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices);
|
---|
77 |
|
---|
78 | // only take predictions for which the target is not NaN
|
---|
79 | var selectedTrainingTuples = originalTrainingValues.Zip(estimatedTrainingValues, Tuple.Create).Where(t => !double.IsNaN(t.Item1)).ToArray();
|
---|
80 | originalTrainingValues = selectedTrainingTuples.Select(t => t.Item1);
|
---|
81 | estimatedTrainingValues = selectedTrainingTuples.Select(t => t.Item2);
|
---|
82 |
|
---|
83 | var selectedTestTuples = originalTestValues.Zip(estimatedTestValues, Tuple.Create).Where(t => !double.IsNaN(t.Item1)).ToArray();
|
---|
84 | originalTestValues = selectedTestTuples.Select(t => t.Item1);
|
---|
85 | estimatedTestValues = selectedTestTuples.Select(t => t.Item2);
|
---|
86 |
|
---|
87 | OnlineCalculatorError errorState;
|
---|
88 | double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
89 | TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
|
---|
90 | double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
91 | TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
|
---|
92 |
|
---|
93 | double trainingMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
94 | TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMAE : double.NaN;
|
---|
95 | double testMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
96 | TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMAE : double.NaN;
|
---|
97 |
|
---|
98 | double trainingR = OnlinePearsonsRCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
99 | TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR * trainingR : double.NaN;
|
---|
100 | double testR = OnlinePearsonsRCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
101 | TestRSquared = errorState == OnlineCalculatorError.None ? testR * testR : double.NaN;
|
---|
102 |
|
---|
103 | double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
104 | TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
|
---|
105 | double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
106 | TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
|
---|
107 |
|
---|
108 | double trainingNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
109 | TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNMSE : double.NaN;
|
---|
110 | double testNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
111 | TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNMSE : double.NaN;
|
---|
112 |
|
---|
113 | TrainingRootMeanSquaredError = Math.Sqrt(TrainingMeanSquaredError);
|
---|
114 | TestRootMeanSquaredError = Math.Sqrt(TestMeanSquaredError);
|
---|
115 | }
|
---|
116 |
|
---|
117 | public new IRegressionModel Model {
|
---|
118 | get { return (IRegressionModel)base.Model; }
|
---|
119 | private set {
|
---|
120 | base.Model = value;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | public new IRegressionProblemData ProblemData {
|
---|
124 | get { return (IRegressionProblemData)base.ProblemData; }
|
---|
125 | set {
|
---|
126 | base.ProblemData = value;
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | public IEnumerable<double> EstimatedValues {
|
---|
131 | get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
|
---|
132 | }
|
---|
133 | public IEnumerable<double> EstimatedTrainingValues {
|
---|
134 | get {
|
---|
135 | var all = EstimatedValues.ToArray();
|
---|
136 | return ProblemData.TrainingIndices.Select(r => all[r]);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | public IEnumerable<double> EstimatedTestValues {
|
---|
140 | get {
|
---|
141 | var all = EstimatedValues.ToArray();
|
---|
142 | return ProblemData.TestIndices.Select(r => all[r]);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | public IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
147 | var all = Model.GetEstimatedValues(ProblemData.Dataset, ProblemData.AllIndices).ToArray();
|
---|
148 | return rows.Select(r => all[r]);
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | #region Results
|
---|
153 | public double TrainingMeanSquaredError {
|
---|
154 | get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
|
---|
155 | private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
|
---|
156 | }
|
---|
157 | public double TestMeanSquaredError {
|
---|
158 | get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
|
---|
159 | private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
|
---|
160 | }
|
---|
161 | public double TrainingMeanAbsoluteError {
|
---|
162 | get { return ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value; }
|
---|
163 | private set { ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value = value; }
|
---|
164 | }
|
---|
165 | public double TestMeanAbsoluteError {
|
---|
166 | get { return ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value; }
|
---|
167 | private set { ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value = value; }
|
---|
168 | }
|
---|
169 | public double TrainingRSquared {
|
---|
170 | get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
|
---|
171 | private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
|
---|
172 | }
|
---|
173 | public double TestRSquared {
|
---|
174 | get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
|
---|
175 | private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
|
---|
176 | }
|
---|
177 | public double TrainingRelativeError {
|
---|
178 | get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
|
---|
179 | private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
|
---|
180 | }
|
---|
181 | public double TestRelativeError {
|
---|
182 | get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
|
---|
183 | private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
|
---|
184 | }
|
---|
185 | public double TrainingNormalizedMeanSquaredError {
|
---|
186 | get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }
|
---|
187 | private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }
|
---|
188 | }
|
---|
189 | public double TestNormalizedMeanSquaredError {
|
---|
190 | get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }
|
---|
191 | private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }
|
---|
192 | }
|
---|
193 | public double TrainingRootMeanSquaredError {
|
---|
194 | get { return ((DoubleValue)this[TrainingRootMeanSquaredErrorResultName].Value).Value; }
|
---|
195 | private set { ((DoubleValue)this[TrainingRootMeanSquaredErrorResultName].Value).Value = value; }
|
---|
196 | }
|
---|
197 | public double TestRootMeanSquaredError {
|
---|
198 | get { return ((DoubleValue)this[TestRootMeanSquaredErrorResultName].Value).Value; }
|
---|
199 | private set { ((DoubleValue)this[TestRootMeanSquaredErrorResultName].Value).Value = value; }
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | #endregion
|
---|
204 |
|
---|
205 | }
|
---|
206 | }
|
---|