Free cookie consent management tool by TermsFeed Policy Generator

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

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

view for editing time lags for the real glucose value.
clone override

File size: 13.1 KB
RevLine 
[13865]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
[14313]43    [StorableConstructor]
[13865]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
[14317]69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new Solution(this, cloner);
71    }
72
[13865]73    protected override void RecalculateResults() {
74      CalculateRegressionResults();
75    }
76
77    private void CalculateRegressionResults() {
78      IEnumerable<double> estimatedTrainingValues = EstimatedTrainingValues; // cache values
79      IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);
80      IEnumerable<double> estimatedTestValues = EstimatedTestValues; // cache values
81      IEnumerable<double> originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices);
82
83      // only take predictions for which the target is not NaN
84      var selectedTrainingTuples = originalTrainingValues.Zip(estimatedTrainingValues, Tuple.Create).Where(t => !double.IsNaN(t.Item1)).ToArray();
85      originalTrainingValues = selectedTrainingTuples.Select(t => t.Item1);
86      estimatedTrainingValues = selectedTrainingTuples.Select(t => t.Item2);
87
88      var selectedTestTuples = originalTestValues.Zip(estimatedTestValues, Tuple.Create).Where(t => !double.IsNaN(t.Item1)).ToArray();
89      originalTestValues = selectedTestTuples.Select(t => t.Item1);
90      estimatedTestValues = selectedTestTuples.Select(t => t.Item2);
91
92      OnlineCalculatorError errorState;
93      double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
94      TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
95      double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
96      TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
97
98      double trainingMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
99      TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMAE : double.NaN;
100      double testMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
101      TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMAE : double.NaN;
102
103      double trainingR = OnlinePearsonsRCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
104      TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR * trainingR : double.NaN;
105      double testR = OnlinePearsonsRCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
106      TestRSquared = errorState == OnlineCalculatorError.None ? testR * testR : double.NaN;
107
108      double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
109      TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
110      double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
111      TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
112
113      double trainingNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
114      TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNMSE : double.NaN;
115      double testNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
116      TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNMSE : double.NaN;
117
118      TrainingRootMeanSquaredError = Math.Sqrt(TrainingMeanSquaredError);
119      TestRootMeanSquaredError = Math.Sqrt(TestMeanSquaredError);
120    }
121
122    public new IRegressionModel Model {
123      get { return (IRegressionModel)base.Model; }
124      private set {
125        base.Model = value;
126      }
127    }
128    public new IRegressionProblemData ProblemData {
129      get { return (IRegressionProblemData)base.ProblemData; }
130      set {
131        base.ProblemData = value;
132      }
133    }
134
135    public IEnumerable<double> EstimatedValues {
136      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
137    }
138    public IEnumerable<double> EstimatedTrainingValues {
[14311]139      get {
140        var all = EstimatedValues.ToArray();
141        return ProblemData.TrainingIndices.Select(r => all[r]);
142      }
[13865]143    }
144    public IEnumerable<double> EstimatedTestValues {
[14311]145      get {
146        var all = EstimatedValues.ToArray();
147        return ProblemData.TestIndices.Select(r => all[r]);
148      }
[13865]149    }
150
151    public IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
[14311]152      var all = Model.GetEstimatedValues(ProblemData.Dataset, ProblemData.AllIndices).ToArray();
153      return rows.Select(r => all[r]);
[13865]154    }
155
156
157    #region Results
158    public double TrainingMeanSquaredError {
159      get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
160      private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
161    }
162    public double TestMeanSquaredError {
163      get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
164      private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
165    }
166    public double TrainingMeanAbsoluteError {
167      get { return ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value; }
168      private set { ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value = value; }
169    }
170    public double TestMeanAbsoluteError {
171      get { return ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value; }
172      private set { ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value = value; }
173    }
174    public double TrainingRSquared {
175      get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
176      private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
177    }
178    public double TestRSquared {
179      get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
180      private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
181    }
182    public double TrainingRelativeError {
183      get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
184      private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
185    }
186    public double TestRelativeError {
187      get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
188      private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
189    }
190    public double TrainingNormalizedMeanSquaredError {
191      get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }
192      private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }
193    }
194    public double TestNormalizedMeanSquaredError {
195      get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }
196      private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }
197    }
198    public double TrainingRootMeanSquaredError {
199      get { return ((DoubleValue)this[TrainingRootMeanSquaredErrorResultName].Value).Value; }
200      private set { ((DoubleValue)this[TrainingRootMeanSquaredErrorResultName].Value).Value = value; }
201    }
202    public double TestRootMeanSquaredError {
203      get { return ((DoubleValue)this[TestRootMeanSquaredErrorResultName].Value).Value; }
204      private set { ((DoubleValue)this[TestRootMeanSquaredErrorResultName].Value).Value = value; }
205    }
206
207
208    #endregion
209
210  }
211}
Note: See TracBrowser for help on using the repository browser.