Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4044 was 4044, checked in by mkommend, 12 years ago

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