Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2645_ContextAlgorithms/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/ConfidenceBoundRegressionSolution.cs @ 16752

Last change on this file since 16752 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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]
32  public class ConfidenceRegressionSolution : RegressionSolution, IConfidenceRegressionSolution {
33    protected readonly Dictionary<int, double> varianceEvaluationCache;
34
35    public new IConfidenceRegressionModel Model {
36      get { return (IConfidenceRegressionModel)base.Model; }
37      set { base.Model = value; }
38    }
39
40    [StorableConstructor]
41    protected ConfidenceRegressionSolution(bool deserializing)
42      : base(deserializing) {
43      varianceEvaluationCache = new Dictionary<int, double>();
44    }
45    protected ConfidenceRegressionSolution(ConfidenceRegressionSolution original, Cloner cloner)
46      : base(original, cloner) {
47      varianceEvaluationCache = new Dictionary<int, double>(original.varianceEvaluationCache);
48    }
49    public ConfidenceRegressionSolution(IConfidenceRegressionModel model, IRegressionProblemData problemData)
50      : base(model, problemData) {
51      varianceEvaluationCache = new Dictionary<int, double>(problemData.Dataset.Rows);
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new ConfidenceRegressionSolution(this, cloner);
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.