Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GeneticProgramming.BloodGlucosePrediction/Solution.cs @ 14313

Last change on this file since 14313 was 14313, checked in by gkronber, 8 years ago

added missing attribute for StorableConstructor

File size: 13.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Optimization;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HeuristicLab.Problems.DataAnalysis;
10
11namespace 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    [StorableConstructor]
44    public Solution(bool deserializing)
45      : base(deserializing) {
46    }
47
48    public Solution(Solution original, Cloner cloner)
49      : base(original, cloner) {
50    }
51
52    public Solution(IRegressionModel model, IRegressionProblemData problemData)
53      : base(model, problemData) {
54      Add(new Result(TrainingMeanSquaredErrorResultName, TrainingMeanSquaredErrorResultDescription, new DoubleValue()));
55      Add(new Result(TestMeanSquaredErrorResultName, TestMeanSquaredErrorResultDescription, new DoubleValue()));
56      Add(new Result(TrainingMeanAbsoluteErrorResultName, TrainingMeanAbsoluteErrorResultDescription, new DoubleValue()));
57      Add(new Result(TestMeanAbsoluteErrorResultName, TestMeanAbsoluteErrorResultDescription, new DoubleValue()));
58      Add(new Result(TrainingSquaredCorrelationResultName, TrainingSquaredCorrelationResultDescription, new DoubleValue()));
59      Add(new Result(TestSquaredCorrelationResultName, TestSquaredCorrelationResultDescription, new DoubleValue()));
60      Add(new Result(TrainingRelativeErrorResultName, TrainingRelativeErrorResultDescription, new PercentValue()));
61      Add(new Result(TestRelativeErrorResultName, TestRelativeErrorResultDescription, new PercentValue()));
62      Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, TrainingNormalizedMeanSquaredErrorResultDescription, new DoubleValue()));
63      Add(new Result(TestNormalizedMeanSquaredErrorResultName, TestNormalizedMeanSquaredErrorResultDescription, new DoubleValue()));
64      Add(new Result(TrainingRootMeanSquaredErrorResultName, TrainingRootMeanSquaredErrorResultDescription, new DoubleValue()));
65      Add(new Result(TestRootMeanSquaredErrorResultName, TestRootMeanSquaredErrorResultDescription, new DoubleValue()));
66      CalculateRegressionResults();
67    }
68
69    protected override void RecalculateResults() {
70      CalculateRegressionResults();
71    }
72
73    private void CalculateRegressionResults() {
74      IEnumerable<double> estimatedTrainingValues = EstimatedTrainingValues; // cache values
75      IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);
76      IEnumerable<double> estimatedTestValues = EstimatedTestValues; // cache values
77      IEnumerable<double> originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices);
78
79      // only take predictions for which the target is not NaN
80      var selectedTrainingTuples = originalTrainingValues.Zip(estimatedTrainingValues, Tuple.Create).Where(t => !double.IsNaN(t.Item1)).ToArray();
81      originalTrainingValues = selectedTrainingTuples.Select(t => t.Item1);
82      estimatedTrainingValues = selectedTrainingTuples.Select(t => t.Item2);
83
84      var selectedTestTuples = originalTestValues.Zip(estimatedTestValues, Tuple.Create).Where(t => !double.IsNaN(t.Item1)).ToArray();
85      originalTestValues = selectedTestTuples.Select(t => t.Item1);
86      estimatedTestValues = selectedTestTuples.Select(t => t.Item2);
87
88      OnlineCalculatorError errorState;
89      double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
90      TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
91      double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
92      TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
93
94      double trainingMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
95      TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMAE : double.NaN;
96      double testMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
97      TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMAE : double.NaN;
98
99      double trainingR = OnlinePearsonsRCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
100      TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR * trainingR : double.NaN;
101      double testR = OnlinePearsonsRCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
102      TestRSquared = errorState == OnlineCalculatorError.None ? testR * testR : double.NaN;
103
104      double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
105      TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
106      double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
107      TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
108
109      double trainingNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
110      TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNMSE : double.NaN;
111      double testNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
112      TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNMSE : double.NaN;
113
114      TrainingRootMeanSquaredError = Math.Sqrt(TrainingMeanSquaredError);
115      TestRootMeanSquaredError = Math.Sqrt(TestMeanSquaredError);
116    }
117
118    public new IRegressionModel Model {
119      get { return (IRegressionModel)base.Model; }
120      private set {
121        base.Model = value;
122      }
123    }
124    public new IRegressionProblemData ProblemData {
125      get { return (IRegressionProblemData)base.ProblemData; }
126      set {
127        base.ProblemData = value;
128      }
129    }
130
131    public IEnumerable<double> EstimatedValues {
132      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
133    }
134    public IEnumerable<double> EstimatedTrainingValues {
135      get {
136        var all = EstimatedValues.ToArray();
137        return ProblemData.TrainingIndices.Select(r => all[r]);
138      }
139    }
140    public IEnumerable<double> EstimatedTestValues {
141      get {
142        var all = EstimatedValues.ToArray();
143        return ProblemData.TestIndices.Select(r => all[r]);
144      }
145    }
146
147    public IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
148      var all = Model.GetEstimatedValues(ProblemData.Dataset, ProblemData.AllIndices).ToArray();
149      return rows.Select(r => all[r]);
150    }
151
152
153    #region Results
154    public double TrainingMeanSquaredError {
155      get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
156      private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
157    }
158    public double TestMeanSquaredError {
159      get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
160      private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
161    }
162    public double TrainingMeanAbsoluteError {
163      get { return ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value; }
164      private set { ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value = value; }
165    }
166    public double TestMeanAbsoluteError {
167      get { return ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value; }
168      private set { ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value = value; }
169    }
170    public double TrainingRSquared {
171      get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
172      private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
173    }
174    public double TestRSquared {
175      get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
176      private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
177    }
178    public double TrainingRelativeError {
179      get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
180      private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
181    }
182    public double TestRelativeError {
183      get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
184      private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
185    }
186    public double TrainingNormalizedMeanSquaredError {
187      get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }
188      private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }
189    }
190    public double TestNormalizedMeanSquaredError {
191      get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }
192      private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }
193    }
194    public double TrainingRootMeanSquaredError {
195      get { return ((DoubleValue)this[TrainingRootMeanSquaredErrorResultName].Value).Value; }
196      private set { ((DoubleValue)this[TrainingRootMeanSquaredErrorResultName].Value).Value = value; }
197    }
198    public double TestRootMeanSquaredError {
199      get { return ((DoubleValue)this[TestRootMeanSquaredErrorResultName].Value).Value; }
200      private set { ((DoubleValue)this[TestRootMeanSquaredErrorResultName].Value).Value = value; }
201    }
202
203
204    #endregion
205
206  }
207}
Note: See TracBrowser for help on using the repository browser.