[13824] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2016 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.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 28 | /// <summary>
|
---|
| 29 | /// Represents a regression data analysis solution that supports confidence estimates
|
---|
| 30 | /// </summary>
|
---|
| 31 | [StorableClass]
|
---|
[14166] | 32 | public class ConfidenceRegressionSolution : RegressionSolution, IConfidenceRegressionSolution {
|
---|
[13824] | 33 | protected readonly Dictionary<int, double> varianceEvaluationCache;
|
---|
| 34 |
|
---|
[14166] | 35 | public new IConfidenceRegressionModel Model {
|
---|
| 36 | get { return (IConfidenceRegressionModel)base.Model; }
|
---|
[13824] | 37 | set { base.Model = value; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | [StorableConstructor]
|
---|
[14166] | 41 | protected ConfidenceRegressionSolution(bool deserializing)
|
---|
[13824] | 42 | : base(deserializing) {
|
---|
| 43 | varianceEvaluationCache = new Dictionary<int, double>();
|
---|
| 44 | }
|
---|
[14166] | 45 | protected ConfidenceRegressionSolution(ConfidenceRegressionSolution original, Cloner cloner)
|
---|
[13824] | 46 | : base(original, cloner) {
|
---|
| 47 | varianceEvaluationCache = new Dictionary<int, double>(original.varianceEvaluationCache);
|
---|
| 48 | }
|
---|
[14166] | 49 | public ConfidenceRegressionSolution(IConfidenceRegressionModel model, IRegressionProblemData problemData)
|
---|
[13824] | 50 | : base(model, problemData) {
|
---|
| 51 | varianceEvaluationCache = new Dictionary<int, double>(problemData.Dataset.Rows);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[14166] | 55 | return new ConfidenceRegressionSolution(this, cloner);
|
---|
[13824] | 56 | }
|
---|
| 57 |
|
---|
| 58 | public IEnumerable<double> EstimatedVariances {
|
---|
| 59 | get { return GetEstimatedVariances(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
|
---|
| 60 | }
|
---|
| 61 | public IEnumerable<double> EstimatedTrainingVariances {
|
---|
| 62 | get { return GetEstimatedVariances(ProblemData.TrainingIndices); }
|
---|
| 63 | }
|
---|
| 64 | public IEnumerable<double> EstimatedTestVariances {
|
---|
| 65 | get { return GetEstimatedVariances(ProblemData.TestIndices); }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public IEnumerable<double> GetEstimatedVariances(IEnumerable<int> rows) {
|
---|
| 69 | var rowsToEvaluate = rows.Except(varianceEvaluationCache.Keys);
|
---|
| 70 | var rowsEnumerator = rowsToEvaluate.GetEnumerator();
|
---|
| 71 | var valuesEnumerator = Model.GetEstimatedVariances(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
|
---|
| 72 |
|
---|
| 73 | while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
|
---|
| 74 | varianceEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | return rows.Select(row => varianceEvaluationCache[row]);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | protected override void OnProblemDataChanged() {
|
---|
| 81 | varianceEvaluationCache.Clear();
|
---|
| 82 | base.OnProblemDataChanged();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | protected override void OnModelChanged() {
|
---|
| 86 | varianceEvaluationCache.Clear();
|
---|
| 87 | base.OnModelChanged();
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | } |
---|