Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.3/Operators/WeightedParentsQualityVarianceComparator.cs @ 9271

Last change on this file since 9271 was 5275, checked in by gkronber, 14 years ago

Merged changes from trunk to data analysis exploration branch and added fractional distance metric evaluator. #1142

File size: 7.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
23using System.Linq;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Common;
31
32namespace HeuristicLab.Problems.DataAnalysis.Operators {
33  [Item("WeightedParentsQualityVarianceComparator", "Compares the quality and variance of the quality against that of its parents (assumes the parents are subscopes to the child scope). This operator works with any number of subscopes > 0.")]
34  [StorableClass]
35  public class WeightedParentsQualityVarianceComparator : SingleSuccessorOperator, ISubScopesQualityComparator {
36    public IValueLookupParameter<BoolValue> MaximizationParameter {
37      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
38    }
39    public ILookupParameter<BoolValue> ResultParameter {
40      get { return (ILookupParameter<BoolValue>)Parameters["Result"]; }
41    }
42    public IValueLookupParameter<DoubleValue> ConfidenceIntervalParameter {
43      get { return (IValueLookupParameter<DoubleValue>)Parameters["ConfidenceInterval"]; }
44    }
45    public ILookupParameter<DoubleValue> LeftSideParameter {
46      get { return (ILookupParameter<DoubleValue>)Parameters["LeftSide"]; }
47    }
48    public ILookupParameter<DoubleValue> LeftSideVarianceParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters["LeftSideVariance"]; }
50    }
51    public ILookupParameter<IntValue> LeftSideSamplesParameter {
52      get { return (ILookupParameter<IntValue>)Parameters["LeftSideSamples"]; }
53    }
54    public ILookupParameter<ItemArray<DoubleValue>> RightSideParameter {
55      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["RightSide"]; }
56    }
57    public ILookupParameter<ItemArray<DoubleValue>> RightSideVariancesParameters {
58      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["RightSideVariances"]; }
59    }
60    public ILookupParameter<ItemArray<IntValue>> RightSideSamplesParameters {
61      get { return (ILookupParameter<ItemArray<IntValue>>)Parameters["RightSideSamples"]; }
62    }
63
64    [StorableConstructor]
65    protected WeightedParentsQualityVarianceComparator(bool deserializing) : base(deserializing) { }
66    protected WeightedParentsQualityVarianceComparator(WeightedParentsQualityVarianceComparator original, Cloner cloner)
67      : base(original, cloner) {
68    }
69    public WeightedParentsQualityVarianceComparator()
70      : base() {
71      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
72      Parameters.Add(new LookupParameter<BoolValue>("Result", "The result of the comparison: True means Quality is better, False means it is worse than parents."));
73      Parameters.Add(new ValueLookupParameter<DoubleValue>("ConfidenceInterval", "The confidence interval used for the test.", new DoubleValue(0.05)));
74
75      Parameters.Add(new LookupParameter<DoubleValue>("LeftSide", "The quality of the child."));
76      Parameters.Add(new LookupParameter<DoubleValue>("LeftSideVariance", "The variances of the quality of the new child."));
77      Parameters.Add(new LookupParameter<IntValue>("LeftSideSamples", "The number of samples used to calculate the quality of the new child."));
78
79      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSide", "The qualities of the parents."));
80      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSideVariances", "The variances of the parents."));
81      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("RightSideSamples", "The number of samples used to calculate the quality of the parent."));
82    }
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new WeightedParentsQualityVarianceComparator(this, cloner);
85    }
86    public override IOperation Apply() {
87      double leftQuality = LeftSideParameter.ActualValue.Value;
88      double leftVariance = LeftSideVarianceParameter.ActualValue.Value;
89      int leftSamples = LeftSideSamplesParameter.ActualValue.Value;
90
91      ItemArray<DoubleValue> rightQualities = RightSideParameter.ActualValue;
92      ItemArray<DoubleValue> rightVariances = RightSideVariancesParameters.ActualValue;
93      ItemArray<IntValue> rightSamples = RightSideSamplesParameters.ActualValue;
94
95      if (rightQualities.Length < 1) throw new InvalidOperationException(Name + ": No subscopes found.");
96      bool maximization = MaximizationParameter.ActualValue.Value;
97
98      int bestParentIndex;
99      double bestParentQuality;
100      double bestParentVariance;
101      int bestParentSamples;
102
103      if (maximization)
104        bestParentQuality = rightQualities.Max(x => x.Value);
105      else
106        bestParentQuality = rightQualities.Min(x => x.Value);
107      bestParentIndex = rightQualities.FindIndex(x => x.Value == bestParentQuality);
108      bestParentVariance = rightVariances[bestParentIndex].Value;
109      bestParentSamples = rightSamples[bestParentIndex].Value;
110
111      double xmean = leftQuality;
112      double xvar = leftVariance;
113      int n = leftSamples;
114      double ymean = bestParentQuality;
115      double yvar = bestParentVariance;
116      double m = bestParentSamples;
117
118
119      //following code taken from ALGLIB studentttest line 351
120      // Two-sample unpooled test
121      double p = 0;
122      double stat = (xmean - ymean) / Math.Sqrt(xvar / n + yvar / m);
123      double c = xvar / n / (xvar / n + yvar / m);
124      double df = (n - 1) * (m - 1) / ((m - 1) * alglib.math.sqr(c) + (n - 1) * (1 - alglib.math.sqr(c)));
125      if ((double)(stat) > (double)(0))
126        p = 1 - 0.5 * alglib.ibetaf.incompletebeta(df / 2, 0.5, df / (df + alglib.math.sqr(stat)));
127      else
128        p = 0.5 * alglib.ibetaf.incompletebeta(df / 2, 0.5, df / (df + alglib.math.sqr(stat)));
129      double bothtails = 2 * Math.Min(p, 1 - p);
130      double lefttail = p;
131      double righttail = 1 - p;
132
133      bool result = false;
134      // reject only if the child is significantly worse
135      if (maximization) {
136        if (bothtails > ConfidenceIntervalParameter.ActualValue.Value) result = true;
137        else if (leftQuality > bestParentQuality) result = true;
138        else result = false;
139      } else {
140        if (bothtails > ConfidenceIntervalParameter.ActualValue.Value) result = true;
141        else if (leftQuality < bestParentQuality) result = true;
142        else result = false;
143      }
144
145      BoolValue resultValue = ResultParameter.ActualValue;
146      if (resultValue == null) {
147        ResultParameter.ActualValue = new BoolValue(result);
148      } else {
149        resultValue.Value = result;
150      }
151      return base.Apply();
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.