Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5265 was 5265, checked in by gkronber, 13 years ago

changed project files and solution file to make the branch compatible with current trunk version. #1142

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