1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 HEAL.Attic;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
28 | /// <summary>
|
---|
29 | /// Represents a regression data analysis solution that supports confidence estimates
|
---|
30 | /// </summary>
|
---|
31 | [StorableType("C2D0DE07-E8F0-4850-AAF3-E2885EC1DDB6")]
|
---|
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(StorableConstructorFlag _) : base(_) {
|
---|
42 | varianceEvaluationCache = new Dictionary<int, double>();
|
---|
43 | }
|
---|
44 | protected ConfidenceRegressionSolution(ConfidenceRegressionSolution original, Cloner cloner)
|
---|
45 | : base(original, cloner) {
|
---|
46 | varianceEvaluationCache = new Dictionary<int, double>(original.varianceEvaluationCache);
|
---|
47 | }
|
---|
48 | public ConfidenceRegressionSolution(IConfidenceRegressionModel model, IRegressionProblemData problemData)
|
---|
49 | : base(model, problemData) {
|
---|
50 | varianceEvaluationCache = new Dictionary<int, double>(problemData.Dataset.Rows);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
54 | return new ConfidenceRegressionSolution(this, cloner);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public IEnumerable<double> EstimatedVariances {
|
---|
58 | get { return GetEstimatedVariances(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
|
---|
59 | }
|
---|
60 | public IEnumerable<double> EstimatedTrainingVariances {
|
---|
61 | get { return GetEstimatedVariances(ProblemData.TrainingIndices); }
|
---|
62 | }
|
---|
63 | public IEnumerable<double> EstimatedTestVariances {
|
---|
64 | get { return GetEstimatedVariances(ProblemData.TestIndices); }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public IEnumerable<double> GetEstimatedVariances(IEnumerable<int> rows) {
|
---|
68 | var rowsToEvaluate = rows.Except(varianceEvaluationCache.Keys);
|
---|
69 | var rowsEnumerator = rowsToEvaluate.GetEnumerator();
|
---|
70 | var valuesEnumerator = Model.GetEstimatedVariances(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
|
---|
71 |
|
---|
72 | while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
|
---|
73 | varianceEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
|
---|
74 | }
|
---|
75 |
|
---|
76 | return rows.Select(row => varianceEvaluationCache[row]);
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected override void OnProblemDataChanged() {
|
---|
80 | varianceEvaluationCache.Clear();
|
---|
81 | base.OnProblemDataChanged();
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected override void OnModelChanged() {
|
---|
85 | varianceEvaluationCache.Clear();
|
---|
86 | base.OnModelChanged();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | } |
---|