#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 alglib;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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"]; }
}
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 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) * AP.Math.Sqr(c) + (n - 1) * (1 - AP.Math.Sqr(c)));
if ((double)(stat) > (double)(0))
p = 1 - 0.5 * ibetaf.incompletebeta(df / 2, 0.5, df / (df + AP.Math.Sqr(stat)));
else
p = 0.5 * ibetaf.incompletebeta(df / 2, 0.5, df / (df + AP.Math.Sqr(stat)));
double bothtails = 2 * Math.Min(p, 1 - p);
double lefttail = p;
double righttail = 1 - p;
bool result = false;
if (maximization)
result = righttail < ConfidenceIntervalParameter.ActualValue.Value;
else
result = lefttail < ConfidenceIntervalParameter.ActualValue.Value;
BoolValue resultValue = ResultParameter.ActualValue;
if (resultValue == null) {
ResultParameter.ActualValue = new BoolValue(result);
} else {
resultValue.Value = result;
}
return base.Apply();
}
}
}