[3378] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
[4044] | 27 | using HeuristicLab.Optimization;
|
---|
[3378] | 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5275] | 30 | using HeuristicLab.Common;
|
---|
[3378] | 31 |
|
---|
[4044] | 32 | namespace 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.")]
|
---|
[3378] | 34 | [StorableClass]
|
---|
[4044] | 35 | public class WeightedParentsQualityVarianceComparator : SingleSuccessorOperator, ISubScopesQualityComparator {
|
---|
[3378] | 36 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 37 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 38 | }
|
---|
[4044] | 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 | }
|
---|
[3378] | 45 | public ILookupParameter<DoubleValue> LeftSideParameter {
|
---|
| 46 | get { return (ILookupParameter<DoubleValue>)Parameters["LeftSide"]; }
|
---|
| 47 | }
|
---|
[4044] | 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 | }
|
---|
[3378] | 54 | public ILookupParameter<ItemArray<DoubleValue>> RightSideParameter {
|
---|
| 55 | get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["RightSide"]; }
|
---|
| 56 | }
|
---|
[4044] | 57 | public ILookupParameter<ItemArray<DoubleValue>> RightSideVariancesParameters {
|
---|
| 58 | get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["RightSideVariances"]; }
|
---|
[3378] | 59 | }
|
---|
[4044] | 60 | public ILookupParameter<ItemArray<IntValue>> RightSideSamplesParameters {
|
---|
| 61 | get { return (ILookupParameter<ItemArray<IntValue>>)Parameters["RightSideSamples"]; }
|
---|
[3378] | 62 | }
|
---|
| 63 |
|
---|
[5275] | 64 | [StorableConstructor]
|
---|
| 65 | protected WeightedParentsQualityVarianceComparator(bool deserializing) : base(deserializing) { }
|
---|
| 66 | protected WeightedParentsQualityVarianceComparator(WeightedParentsQualityVarianceComparator original, Cloner cloner)
|
---|
| 67 | : base(original, cloner) {
|
---|
| 68 | }
|
---|
[4044] | 69 | public WeightedParentsQualityVarianceComparator()
|
---|
[3378] | 70 | : base() {
|
---|
| 71 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
|
---|
[4044] | 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 |
|
---|
[3378] | 75 | Parameters.Add(new LookupParameter<DoubleValue>("LeftSide", "The quality of the child."));
|
---|
[4044] | 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 |
|
---|
[3659] | 79 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSide", "The qualities of the parents."));
|
---|
[4044] | 80 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSideVariances", "The variances of the parents."));
|
---|
[4131] | 81 | Parameters.Add(new ScopeTreeLookupParameter<IntValue>("RightSideSamples", "The number of samples used to calculate the quality of the parent."));
|
---|
[3378] | 82 | }
|
---|
[5275] | 83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 84 | return new WeightedParentsQualityVarianceComparator(this, cloner);
|
---|
| 85 | }
|
---|
[3378] | 86 | public override IOperation Apply() {
|
---|
[4044] | 87 | double leftQuality = LeftSideParameter.ActualValue.Value;
|
---|
| 88 | double leftVariance = LeftSideVarianceParameter.ActualValue.Value;
|
---|
| 89 | int leftSamples = LeftSideSamplesParameter.ActualValue.Value;
|
---|
| 90 |
|
---|
[3378] | 91 | ItemArray<DoubleValue> rightQualities = RightSideParameter.ActualValue;
|
---|
[4044] | 92 | ItemArray<DoubleValue> rightVariances = RightSideVariancesParameters.ActualValue;
|
---|
| 93 | ItemArray<IntValue> rightSamples = RightSideSamplesParameters.ActualValue;
|
---|
| 94 |
|
---|
[3378] | 95 | if (rightQualities.Length < 1) throw new InvalidOperationException(Name + ": No subscopes found.");
|
---|
| 96 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
| 97 |
|
---|
[4044] | 98 | int bestParentIndex;
|
---|
| 99 | double bestParentQuality;
|
---|
| 100 | double bestParentVariance;
|
---|
| 101 | int bestParentSamples;
|
---|
[3378] | 102 |
|
---|
[4044] | 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;
|
---|
[3378] | 110 |
|
---|
[4044] | 111 | double xmean = leftQuality;
|
---|
| 112 | double xvar = leftVariance;
|
---|
| 113 | int n = leftSamples;
|
---|
| 114 | double ymean = bestParentQuality;
|
---|
| 115 | double yvar = bestParentVariance;
|
---|
| 116 | double m = bestParentSamples;
|
---|
[3378] | 117 |
|
---|
[4044] | 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);
|
---|
[5275] | 124 | double df = (n - 1) * (m - 1) / ((m - 1) * alglib.math.sqr(c) + (n - 1) * (1 - alglib.math.sqr(c)));
|
---|
[4044] | 125 | if ((double)(stat) > (double)(0))
|
---|
[5275] | 126 | p = 1 - 0.5 * alglib.ibetaf.incompletebeta(df / 2, 0.5, df / (df + alglib.math.sqr(stat)));
|
---|
[4044] | 127 | else
|
---|
[5275] | 128 | p = 0.5 * alglib.ibetaf.incompletebeta(df / 2, 0.5, df / (df + alglib.math.sqr(stat)));
|
---|
[4044] | 129 | double bothtails = 2 * Math.Min(p, 1 - p);
|
---|
| 130 | double lefttail = p;
|
---|
| 131 | double righttail = 1 - p;
|
---|
| 132 |
|
---|
| 133 | bool result = false;
|
---|
[4193] | 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 | }
|
---|
[4044] | 144 |
|
---|
[3378] | 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 | }
|
---|