Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/DiscriminantFunctionClassificationSolution.cs @ 6415

Last change on this file since 6415 was 6415, checked in by mkommend, 13 years ago

#1479: Merged trunk changes, refactored grammar editor and added copy functionality.

File size: 8.7 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis {
32  /// <summary>
33  /// Represents a classification solution that uses a discriminant function and classification thresholds.
34  /// </summary>
35  [StorableClass]
36  [Item("DiscriminantFunctionClassificationSolution", "Represents a classification solution that uses a discriminant function and classification thresholds.")]
37  public class DiscriminantFunctionClassificationSolution : ClassificationSolution, IDiscriminantFunctionClassificationSolution {
38    private const string TrainingMeanSquaredErrorResultName = "Mean squared error (training)";
39    private const string TestMeanSquaredErrorResultName = "Mean squared error (test)";
40    private const string TrainingRSquaredResultName = "Pearson's R² (training)";
41    private const string TestRSquaredResultName = "Pearson's R² (test)";
42
43    public new IDiscriminantFunctionClassificationModel Model {
44      get { return (IDiscriminantFunctionClassificationModel)base.Model; }
45      protected set {
46        if (value != null && value != Model) {
47          if (Model != null) {
48            Model.ThresholdsChanged -= new EventHandler(Model_ThresholdsChanged);
49          }
50          value.ThresholdsChanged += new EventHandler(Model_ThresholdsChanged);
51          base.Model = value;
52        }
53      }
54    }
55
56    public double TrainingMeanSquaredError {
57      get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
58      private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
59    }
60
61    public double TestMeanSquaredError {
62      get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
63      private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
64    }
65
66    public double TrainingRSquared {
67      get { return ((DoubleValue)this[TrainingRSquaredResultName].Value).Value; }
68      private set { ((DoubleValue)this[TrainingRSquaredResultName].Value).Value = value; }
69    }
70
71    public double TestRSquared {
72      get { return ((DoubleValue)this[TestRSquaredResultName].Value).Value; }
73      private set { ((DoubleValue)this[TestRSquaredResultName].Value).Value = value; }
74    }
75
76    [StorableConstructor]
77    protected DiscriminantFunctionClassificationSolution(bool deserializing) : base(deserializing) { }
78    protected DiscriminantFunctionClassificationSolution(DiscriminantFunctionClassificationSolution original, Cloner cloner)
79      : base(original, cloner) {
80      RegisterEventHandler();
81    }
82    public DiscriminantFunctionClassificationSolution(IRegressionModel model, IClassificationProblemData problemData)
83      : this(new DiscriminantFunctionClassificationModel(model), problemData) {
84    }
85    public DiscriminantFunctionClassificationSolution(IDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData)
86      : base(model, problemData) {
87      Add(new Result(TrainingMeanSquaredErrorResultName, "Mean of squared errors of the model on the training partition", new DoubleValue()));
88      Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new DoubleValue()));
89      Add(new Result(TrainingRSquaredResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new DoubleValue()));
90      Add(new Result(TestRSquaredResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleValue()));
91      SetAccuracyMaximizingThresholds();
92
93      //mkommend: important to recalculate accuracy because during the calculation before no thresholds were present     
94      base.RecalculateResults();
95      CalculateResults();
96      RegisterEventHandler();
97    }
98
99    [StorableHook(HookType.AfterDeserialization)]
100    private void AfterDeserialization() {
101      RegisterEventHandler();
102    }
103
104    protected override void OnModelChanged(EventArgs e) {
105      DeregisterEventHandler();
106      SetAccuracyMaximizingThresholds();
107      RegisterEventHandler();
108      base.OnModelChanged(e);
109    }
110
111    protected override void RecalculateResults() {
112      base.RecalculateResults();
113      CalculateResults();
114    }
115
116    private void CalculateResults() {
117      double[] estimatedTrainingValues = EstimatedTrainingValues.ToArray(); // cache values
118      IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes);
119      double[] estimatedTestValues = EstimatedTestValues.ToArray(); // cache values
120      IEnumerable<double> originalTestValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TestIndizes);
121
122      OnlineCalculatorError errorState;
123      double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
124      TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
125      double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
126      TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
127
128      double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
129      TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;
130      double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
131      TestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;
132    }
133
134    private void RegisterEventHandler() {
135      Model.ThresholdsChanged += new EventHandler(Model_ThresholdsChanged);
136    }
137    private void DeregisterEventHandler() {
138      Model.ThresholdsChanged -= new EventHandler(Model_ThresholdsChanged);
139    }
140    private void Model_ThresholdsChanged(object sender, EventArgs e) {
141      OnModelThresholdsChanged(e);
142    }
143
144    public void SetAccuracyMaximizingThresholds() {
145      double[] classValues;
146      double[] thresholds;
147      var targetClassValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes);
148      AccuracyMaximizationThresholdCalculator.CalculateThresholds(ProblemData, EstimatedTrainingValues, targetClassValues, out classValues, out thresholds);
149
150      Model.SetThresholdsAndClassValues(thresholds, classValues);
151    }
152
153    public void SetClassDistibutionCutPointThresholds() {
154      double[] classValues;
155      double[] thresholds;
156      var targetClassValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes);
157      NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(ProblemData, EstimatedTrainingValues, targetClassValues, out classValues, out thresholds);
158
159      Model.SetThresholdsAndClassValues(thresholds, classValues);
160    }
161
162    protected virtual void OnModelThresholdsChanged(EventArgs e) {
163      RecalculateResults();
164    }
165
166    public IEnumerable<double> EstimatedValues {
167      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
168    }
169
170    public IEnumerable<double> EstimatedTrainingValues {
171      get { return GetEstimatedValues(ProblemData.TrainingIndizes); }
172    }
173
174    public IEnumerable<double> EstimatedTestValues {
175      get { return GetEstimatedValues(ProblemData.TestIndizes); }
176    }
177
178    public IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
179      return Model.GetEstimatedValues(ProblemData.Dataset, rows);
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.