Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/ConfidenceBoundRegressionSolution.cs @ 14573

Last change on this file since 14573 was 14099, checked in by pfleck, 8 years ago

#2597:

  • Renamed IConfidenceBoundRegressionModel/Solution+Views to IConfidenceRegressionModel/Solution+Views.
  • Moved IConfidenceRegressionModel and IConfidenceRegressionSolution into own files.
  • Moved some (helper) controls into separate sub folder.
  • Fixed typo.
File size: 3.5 KB
RevLine 
[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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Problems.DataAnalysis {
28  /// <summary>
29  /// Represents a regression data analysis solution that supports confidence estimates
30  /// </summary>
31  [StorableClass]
[14099]32  public class ConfidenceRegressionSolution : RegressionSolution, IConfidenceRegressionSolution {
[13824]33    protected readonly Dictionary<int, double> varianceEvaluationCache;
34
[14099]35    public new IConfidenceRegressionModel Model {
36      get { return (IConfidenceRegressionModel)base.Model; }
[13824]37      set { base.Model = value; }
38    }
39
40    [StorableConstructor]
[14099]41    protected ConfidenceRegressionSolution(bool deserializing)
[13824]42      : base(deserializing) {
43      varianceEvaluationCache = new Dictionary<int, double>();
44    }
[14099]45    protected ConfidenceRegressionSolution(ConfidenceRegressionSolution original, Cloner cloner)
[13824]46      : base(original, cloner) {
47      varianceEvaluationCache = new Dictionary<int, double>(original.varianceEvaluationCache);
48    }
[14099]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) {
[14099]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}
Note: See TracBrowser for help on using the repository browser.