[5649] | 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 |
|
---|
[5777] | 22 | using System;
|
---|
[5649] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[5885] | 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
[6411] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5649] | 30 |
|
---|
| 31 | namespace 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 {
|
---|
[5885] | 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 |
|
---|
[5717] | 43 | public new IDiscriminantFunctionClassificationModel Model {
|
---|
| 44 | get { return (IDiscriminantFunctionClassificationModel)base.Model; }
|
---|
[5736] | 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 | }
|
---|
[5717] | 54 | }
|
---|
| 55 |
|
---|
[5885] | 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 |
|
---|
[5649] | 76 | [StorableConstructor]
|
---|
| 77 | protected DiscriminantFunctionClassificationSolution(bool deserializing) : base(deserializing) { }
|
---|
| 78 | protected DiscriminantFunctionClassificationSolution(DiscriminantFunctionClassificationSolution original, Cloner cloner)
|
---|
| 79 | : base(original, cloner) {
|
---|
[5736] | 80 | RegisterEventHandler();
|
---|
[5649] | 81 | }
|
---|
[5736] | 82 | public DiscriminantFunctionClassificationSolution(IRegressionModel model, IClassificationProblemData problemData)
|
---|
| 83 | : this(new DiscriminantFunctionClassificationModel(model), problemData) {
|
---|
[5649] | 84 | }
|
---|
| 85 | public DiscriminantFunctionClassificationSolution(IDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData)
|
---|
| 86 | : base(model, problemData) {
|
---|
[5885] | 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()));
|
---|
[6411] | 91 | SetAccuracyMaximizingThresholds();
|
---|
| 92 |
|
---|
| 93 | //mkommend: important to recalculate accuracy because during the calculation before no thresholds were present
|
---|
| 94 | base.RecalculateResults();
|
---|
| 95 | CalculateResults();
|
---|
[5736] | 96 | RegisterEventHandler();
|
---|
[5649] | 97 | }
|
---|
| 98 |
|
---|
[5736] | 99 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 100 | private void AfterDeserialization() {
|
---|
| 101 | RegisterEventHandler();
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[6411] | 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() {
|
---|
[5885] | 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 |
|
---|
[5942] | 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;
|
---|
[5885] | 127 |
|
---|
[5942] | 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;
|
---|
[5885] | 132 | }
|
---|
| 133 |
|
---|
[5736] | 134 | private void RegisterEventHandler() {
|
---|
| 135 | Model.ThresholdsChanged += new EventHandler(Model_ThresholdsChanged);
|
---|
| 136 | }
|
---|
[6411] | 137 | private void DeregisterEventHandler() {
|
---|
| 138 | Model.ThresholdsChanged -= new EventHandler(Model_ThresholdsChanged);
|
---|
| 139 | }
|
---|
[5736] | 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) {
|
---|
[5885] | 163 | RecalculateResults();
|
---|
[5736] | 164 | }
|
---|
| 165 |
|
---|
[5649] | 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 | }
|
---|