Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2608 worked on glucose prediction problem

File size: 12.8 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    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 { return GetEstimatedValues(ProblemData.TrainingIndices); }
135    }
136    public IEnumerable<double> EstimatedTestValues {
137      get { return GetEstimatedValues(ProblemData.TestIndices); }
138    }
139
140    public IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
141      return Model.GetEstimatedValues(ProblemData.Dataset, rows);
142    }
143
144
145    #region Results
146    public double TrainingMeanSquaredError {
147      get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
148      private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
149    }
150    public double TestMeanSquaredError {
151      get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
152      private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
153    }
154    public double TrainingMeanAbsoluteError {
155      get { return ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value; }
156      private set { ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value = value; }
157    }
158    public double TestMeanAbsoluteError {
159      get { return ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value; }
160      private set { ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value = value; }
161    }
162    public double TrainingRSquared {
163      get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
164      private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
165    }
166    public double TestRSquared {
167      get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
168      private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
169    }
170    public double TrainingRelativeError {
171      get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
172      private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
173    }
174    public double TestRelativeError {
175      get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
176      private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
177    }
178    public double TrainingNormalizedMeanSquaredError {
179      get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }
180      private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }
181    }
182    public double TestNormalizedMeanSquaredError {
183      get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }
184      private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }
185    }
186    public double TrainingRootMeanSquaredError {
187      get { return ((DoubleValue)this[TrainingRootMeanSquaredErrorResultName].Value).Value; }
188      private set { ((DoubleValue)this[TrainingRootMeanSquaredErrorResultName].Value).Value = value; }
189    }
190    public double TestRootMeanSquaredError {
191      get { return ((DoubleValue)this[TestRootMeanSquaredErrorResultName].Value).Value; }
192      private set { ((DoubleValue)this[TestRootMeanSquaredErrorResultName].Value).Value = value; }
193    }
194
195
196    #endregion
197
198  }
199}
Note: See TracBrowser for help on using the repository browser.