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