[5620] | 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;
|
---|
[5620] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
[5777] | 27 | using HeuristicLab.Optimization;
|
---|
[5620] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 31 | /// <summary>
|
---|
[6184] | 32 | /// Represents a regression data analysis solution
|
---|
[5620] | 33 | /// </summary>
|
---|
| 34 | [StorableClass]
|
---|
[6184] | 35 | public class RegressionSolution : DataAnalysisSolution, IRegressionSolution {
|
---|
[5649] | 36 | private const string TrainingMeanSquaredErrorResultName = "Mean squared error (training)";
|
---|
| 37 | private const string TestMeanSquaredErrorResultName = "Mean squared error (test)";
|
---|
| 38 | private const string TrainingSquaredCorrelationResultName = "Pearson's R² (training)";
|
---|
| 39 | private const string TestSquaredCorrelationResultName = "Pearson's R² (test)";
|
---|
| 40 | private const string TrainingRelativeErrorResultName = "Average relative error (training)";
|
---|
| 41 | private const string TestRelativeErrorResultName = "Average relative error (test)";
|
---|
[5942] | 42 | private const string TrainingNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (training)";
|
---|
| 43 | private const string TestNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (test)";
|
---|
[5649] | 44 |
|
---|
[5717] | 45 | public new IRegressionModel Model {
|
---|
| 46 | get { return (IRegressionModel)base.Model; }
|
---|
| 47 | protected set { base.Model = value; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public new IRegressionProblemData ProblemData {
|
---|
| 51 | get { return (IRegressionProblemData)base.ProblemData; }
|
---|
| 52 | protected set { base.ProblemData = value; }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public double TrainingMeanSquaredError {
|
---|
| 56 | get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
|
---|
[6184] | 57 | protected set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
|
---|
[5717] | 58 | }
|
---|
| 59 |
|
---|
| 60 | public double TestMeanSquaredError {
|
---|
| 61 | get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
|
---|
[6184] | 62 | protected set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
|
---|
[5717] | 63 | }
|
---|
| 64 |
|
---|
| 65 | public double TrainingRSquared {
|
---|
| 66 | get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
|
---|
[6184] | 67 | protected set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
|
---|
[5717] | 68 | }
|
---|
| 69 |
|
---|
| 70 | public double TestRSquared {
|
---|
| 71 | get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
|
---|
[6184] | 72 | protected set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
|
---|
[5717] | 73 | }
|
---|
| 74 |
|
---|
| 75 | public double TrainingRelativeError {
|
---|
| 76 | get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
|
---|
[6184] | 77 | protected set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
|
---|
[5717] | 78 | }
|
---|
| 79 |
|
---|
| 80 | public double TestRelativeError {
|
---|
| 81 | get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
|
---|
[6184] | 82 | protected set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
|
---|
[5717] | 83 | }
|
---|
| 84 |
|
---|
[5942] | 85 | public double TrainingNormalizedMeanSquaredError {
|
---|
| 86 | get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }
|
---|
[6184] | 87 | protected set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }
|
---|
[5942] | 88 | }
|
---|
[5717] | 89 |
|
---|
[5942] | 90 | public double TestNormalizedMeanSquaredError {
|
---|
| 91 | get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }
|
---|
[6184] | 92 | protected set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }
|
---|
[5942] | 93 | }
|
---|
| 94 |
|
---|
| 95 |
|
---|
[5620] | 96 | [StorableConstructor]
|
---|
| 97 | protected RegressionSolution(bool deserializing) : base(deserializing) { }
|
---|
| 98 | protected RegressionSolution(RegressionSolution original, Cloner cloner)
|
---|
| 99 | : base(original, cloner) {
|
---|
| 100 | }
|
---|
[5624] | 101 | public RegressionSolution(IRegressionModel model, IRegressionProblemData problemData)
|
---|
| 102 | : base(model, problemData) {
|
---|
[5717] | 103 | Add(new Result(TrainingMeanSquaredErrorResultName, "Mean of squared errors of the model on the training partition", new DoubleValue()));
|
---|
| 104 | Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new DoubleValue()));
|
---|
| 105 | Add(new Result(TrainingSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new DoubleValue()));
|
---|
| 106 | Add(new Result(TestSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleValue()));
|
---|
| 107 | Add(new Result(TrainingRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the training partition", new PercentValue()));
|
---|
| 108 | Add(new Result(TestRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the test partition", new PercentValue()));
|
---|
[5962] | 109 | Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the training partition", new DoubleValue()));
|
---|
| 110 | Add(new Result(TestNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the test partition", new DoubleValue()));
|
---|
[5717] | 111 |
|
---|
| 112 | RecalculateResults();
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[6184] | 115 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 116 | return new RegressionSolution(this, cloner);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[5717] | 119 | protected override void OnProblemDataChanged(EventArgs e) {
|
---|
| 120 | base.OnProblemDataChanged(e);
|
---|
| 121 | RecalculateResults();
|
---|
| 122 | }
|
---|
| 123 | protected override void OnModelChanged(EventArgs e) {
|
---|
| 124 | base.OnModelChanged(e);
|
---|
| 125 | RecalculateResults();
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[6184] | 128 | private void RecalculateResults() {
|
---|
[5649] | 129 | double[] estimatedTrainingValues = EstimatedTrainingValues.ToArray(); // cache values
|
---|
| 130 | IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes);
|
---|
| 131 | double[] estimatedTestValues = EstimatedTestValues.ToArray(); // cache values
|
---|
| 132 | IEnumerable<double> originalTestValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TestIndizes);
|
---|
| 133 |
|
---|
[5942] | 134 | OnlineCalculatorError errorState;
|
---|
| 135 | double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
|
---|
| 136 | TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
|
---|
| 137 | double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
|
---|
| 138 | TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
|
---|
[5649] | 139 |
|
---|
[5942] | 140 | double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
|
---|
| 141 | TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;
|
---|
| 142 | double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
|
---|
| 143 | TestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;
|
---|
[5894] | 144 |
|
---|
[5942] | 145 | double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
|
---|
| 146 | TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
|
---|
| 147 | double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
|
---|
| 148 | TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
|
---|
| 149 |
|
---|
| 150 | double trainingNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
|
---|
| 151 | TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNMSE : double.NaN;
|
---|
| 152 | double testNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
|
---|
| 153 | TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNMSE : double.NaN;
|
---|
[5620] | 154 | }
|
---|
| 155 |
|
---|
| 156 | public virtual IEnumerable<double> EstimatedValues {
|
---|
| 157 | get {
|
---|
| 158 | return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows));
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | public virtual IEnumerable<double> EstimatedTrainingValues {
|
---|
| 163 | get {
|
---|
| 164 | return GetEstimatedValues(ProblemData.TrainingIndizes);
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | public virtual IEnumerable<double> EstimatedTestValues {
|
---|
| 169 | get {
|
---|
| 170 | return GetEstimatedValues(ProblemData.TestIndizes);
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | public virtual IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
[5649] | 175 | return Model.GetEstimatedValues(ProblemData.Dataset, rows);
|
---|
[5620] | 176 | }
|
---|
| 177 | }
|
---|
| 178 | }
|
---|