#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Linq;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Common;
namespace HeuristicLab.Problems.DataAnalysis.Operators {
[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.")]
[StorableClass]
public class WeightedParentsQualityVarianceComparator : SingleSuccessorOperator, ISubScopesQualityComparator {
public IValueLookupParameter MaximizationParameter {
get { return (IValueLookupParameter)Parameters["Maximization"]; }
}
public ILookupParameter ResultParameter {
get { return (ILookupParameter)Parameters["Result"]; }
}
public IValueLookupParameter ConfidenceIntervalParameter {
get { return (IValueLookupParameter)Parameters["ConfidenceInterval"]; }
}
public ILookupParameter LeftSideParameter {
get { return (ILookupParameter)Parameters["LeftSide"]; }
}
public ILookupParameter LeftSideVarianceParameter {
get { return (ILookupParameter)Parameters["LeftSideVariance"]; }
}
public ILookupParameter LeftSideSamplesParameter {
get { return (ILookupParameter)Parameters["LeftSideSamples"]; }
}
public ILookupParameter> RightSideParameter {
get { return (ILookupParameter>)Parameters["RightSide"]; }
}
public ILookupParameter> RightSideVariancesParameters {
get { return (ILookupParameter>)Parameters["RightSideVariances"]; }
}
public ILookupParameter> RightSideSamplesParameters {
get { return (ILookupParameter>)Parameters["RightSideSamples"]; }
}
[StorableConstructor]
protected WeightedParentsQualityVarianceComparator(bool deserializing) : base(deserializing) { }
protected WeightedParentsQualityVarianceComparator(WeightedParentsQualityVarianceComparator original, Cloner cloner)
: base(original, cloner) {
}
public WeightedParentsQualityVarianceComparator()
: base() {
Parameters.Add(new ValueLookupParameter("Maximization", "True if the problem is a maximization problem, false otherwise"));
Parameters.Add(new LookupParameter("Result", "The result of the comparison: True means Quality is better, False means it is worse than parents."));
Parameters.Add(new ValueLookupParameter("ConfidenceInterval", "The confidence interval used for the test.", new DoubleValue(0.05)));
Parameters.Add(new LookupParameter("LeftSide", "The quality of the child."));
Parameters.Add(new LookupParameter("LeftSideVariance", "The variances of the quality of the new child."));
Parameters.Add(new LookupParameter("LeftSideSamples", "The number of samples used to calculate the quality of the new child."));
Parameters.Add(new ScopeTreeLookupParameter("RightSide", "The qualities of the parents."));
Parameters.Add(new ScopeTreeLookupParameter("RightSideVariances", "The variances of the parents."));
Parameters.Add(new ScopeTreeLookupParameter("RightSideSamples", "The number of samples used to calculate the quality of the parent."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new WeightedParentsQualityVarianceComparator(this, cloner);
}
public override IOperation Apply() {
double leftQuality = LeftSideParameter.ActualValue.Value;
double leftVariance = LeftSideVarianceParameter.ActualValue.Value;
int leftSamples = LeftSideSamplesParameter.ActualValue.Value;
ItemArray rightQualities = RightSideParameter.ActualValue;
ItemArray rightVariances = RightSideVariancesParameters.ActualValue;
ItemArray rightSamples = RightSideSamplesParameters.ActualValue;
if (rightQualities.Length < 1) throw new InvalidOperationException(Name + ": No subscopes found.");
bool maximization = MaximizationParameter.ActualValue.Value;
int bestParentIndex;
double bestParentQuality;
double bestParentVariance;
int bestParentSamples;
if (maximization)
bestParentQuality = rightQualities.Max(x => x.Value);
else
bestParentQuality = rightQualities.Min(x => x.Value);
bestParentIndex = rightQualities.FindIndex(x => x.Value == bestParentQuality);
bestParentVariance = rightVariances[bestParentIndex].Value;
bestParentSamples = rightSamples[bestParentIndex].Value;
double xmean = leftQuality;
double xvar = leftVariance;
int n = leftSamples;
double ymean = bestParentQuality;
double yvar = bestParentVariance;
double m = bestParentSamples;
//following code taken from ALGLIB studentttest line 351
// Two-sample unpooled test
double p = 0;
double stat = (xmean - ymean) / Math.Sqrt(xvar / n + yvar / m);
double c = xvar / n / (xvar / n + yvar / m);
double df = (n - 1) * (m - 1) / ((m - 1) * alglib.math.sqr(c) + (n - 1) * (1 - alglib.math.sqr(c)));
if ((double)(stat) > (double)(0))
p = 1 - 0.5 * alglib.ibetaf.incompletebeta(df / 2, 0.5, df / (df + alglib.math.sqr(stat)));
else
p = 0.5 * alglib.ibetaf.incompletebeta(df / 2, 0.5, df / (df + alglib.math.sqr(stat)));
double bothtails = 2 * Math.Min(p, 1 - p);
double lefttail = p;
double righttail = 1 - p;
bool result = false;
// reject only if the child is significantly worse
if (maximization) {
if (bothtails > ConfidenceIntervalParameter.ActualValue.Value) result = true;
else if (leftQuality > bestParentQuality) result = true;
else result = false;
} else {
if (bothtails > ConfidenceIntervalParameter.ActualValue.Value) result = true;
else if (leftQuality < bestParentQuality) result = true;
else result = false;
}
BoolValue resultValue = ResultParameter.ActualValue;
if (resultValue == null) {
ResultParameter.ActualValue = new BoolValue(result);
} else {
resultValue.Value = result;
}
return base.Apply();
}
}
}