Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Operators/WeightedParentsQualityVarianceComparator.cs @ 4131

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

Fixed bug in WeightedParentsQualityVarianceComparator. #1082

File size: 6.8 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 alglib;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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    public WeightedParentsQualityVarianceComparator()
65      : base() {
66      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
67      Parameters.Add(new LookupParameter<BoolValue>("Result", "The result of the comparison: True means Quality is better, False means it is worse than parents."));
68      Parameters.Add(new ValueLookupParameter<DoubleValue>("ConfidenceInterval", "The confidence interval used for the test.", new DoubleValue(0.05)));
69
70      Parameters.Add(new LookupParameter<DoubleValue>("LeftSide", "The quality of the child."));
71      Parameters.Add(new LookupParameter<DoubleValue>("LeftSideVariance", "The variances of the quality of the new child."));
72      Parameters.Add(new LookupParameter<IntValue>("LeftSideSamples", "The number of samples used to calculate the quality of the new child."));
73
74      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSide", "The qualities of the parents."));
75      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSideVariances", "The variances of the parents."));
76      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("RightSideSamples", "The number of samples used to calculate the quality of the parent."));
77    }
78
79    public override IOperation Apply() {
80      double leftQuality = LeftSideParameter.ActualValue.Value;
81      double leftVariance = LeftSideVarianceParameter.ActualValue.Value;
82      int leftSamples = LeftSideSamplesParameter.ActualValue.Value;
83
84      ItemArray<DoubleValue> rightQualities = RightSideParameter.ActualValue;
85      ItemArray<DoubleValue> rightVariances = RightSideVariancesParameters.ActualValue;
86      ItemArray<IntValue> rightSamples = RightSideSamplesParameters.ActualValue;
87
88      if (rightQualities.Length < 1) throw new InvalidOperationException(Name + ": No subscopes found.");
89      bool maximization = MaximizationParameter.ActualValue.Value;
90
91      int bestParentIndex;
92      double bestParentQuality;
93      double bestParentVariance;
94      int bestParentSamples;
95
96      if (maximization)
97        bestParentQuality = rightQualities.Max(x => x.Value);
98      else
99        bestParentQuality = rightQualities.Min(x => x.Value);
100      bestParentIndex = rightQualities.FindIndex(x => x.Value == bestParentQuality);
101      bestParentVariance = rightVariances[bestParentIndex].Value;
102      bestParentSamples = rightSamples[bestParentIndex].Value;
103
104      double xmean = leftQuality;
105      double xvar = leftVariance;
106      int n = leftSamples;
107      double ymean = bestParentQuality;
108      double yvar = bestParentVariance;
109      double m = bestParentSamples;
110
111
112      //following code taken from ALGLIB studentttest line 351
113      // Two-sample unpooled test
114      double p = 0;
115      double stat = (xmean - ymean) / Math.Sqrt(xvar / n + yvar / m);
116      double c = xvar / n / (xvar / n + yvar / m);
117      double df = (n - 1) * (m - 1) / ((m - 1) * AP.Math.Sqr(c) + (n - 1) * (1 - AP.Math.Sqr(c)));
118      if ((double)(stat) > (double)(0))
119        p = 1 - 0.5 * ibetaf.incompletebeta(df / 2, 0.5, df / (df + AP.Math.Sqr(stat)));
120      else
121        p = 0.5 * ibetaf.incompletebeta(df / 2, 0.5, df / (df + AP.Math.Sqr(stat)));
122      double bothtails = 2 * Math.Min(p, 1 - p);
123      double lefttail = p;
124      double righttail = 1 - p;
125
126      bool result = false;
127      if (maximization)
128        result = righttail < ConfidenceIntervalParameter.ActualValue.Value;
129      else
130        result = lefttail < ConfidenceIntervalParameter.ActualValue.Value;
131
132      BoolValue resultValue = ResultParameter.ActualValue;
133      if (resultValue == null) {
134        ResultParameter.ActualValue = new BoolValue(result);
135      } else {
136        resultValue.Value = result;
137      }
138
139
140
141      return base.Apply();
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.